From 1b3d2aac786fa591ddafa09f9a62da157bd4baae Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 2 Dec 2026 17:56:05 +0100 Subject: [PATCH 1/4] GithubErrorFormatter supports identifiers and tips --- .../ErrorFormatter/GithubErrorFormatter.php | 49 ++++++++++++++++++- .../GithubErrorFormatterTest.php | 34 ++++++++++++- 2 files changed, 80 insertions(+), 3 deletions(-) diff --git a/src/Command/ErrorFormatter/GithubErrorFormatter.php b/src/Command/ErrorFormatter/GithubErrorFormatter.php index a81fd0bd01e..399c3476934 100644 --- a/src/Command/ErrorFormatter/GithubErrorFormatter.php +++ b/src/Command/ErrorFormatter/GithubErrorFormatter.php @@ -2,15 +2,20 @@ namespace PHPStan\Command\ErrorFormatter; +use PHPStan\Analyser\Error; use PHPStan\Command\AnalysisResult; use PHPStan\Command\Output; use PHPStan\DependencyInjection\AutowiredParameter; use PHPStan\DependencyInjection\AutowiredService; use PHPStan\File\RelativePathHelper; +use Symfony\Component\Console\Formatter\OutputFormatter; use function array_walk; +use function explode; use function implode; +use function ltrim; use function preg_replace; use function sprintf; +use function str_contains; use function str_replace; /** @@ -30,6 +35,13 @@ public function __construct( public function formatErrors(AnalysisResult $analysisResult, Output $output): int { + $projectConfigFile = 'phpstan.neon'; + if ($analysisResult->getProjectConfigFile() !== null) { + $projectConfigFile = $this->relativePathHelper->getRelativePath($analysisResult->getProjectConfigFile()); + } + + $tipFormatter = new OutputFormatter(false); + foreach ($analysisResult->getFileSpecificErrors() as $fileSpecificError) { $metas = [ 'file' => $this->relativePathHelper->getRelativePath($fileSpecificError->getFile()), @@ -40,7 +52,7 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in $value = sprintf('%s=%s', $key, (string) $value); }); - $message = $fileSpecificError->getMessage(); + $message = $this->buildFileSpecificMessage($fileSpecificError, $tipFormatter, $projectConfigFile); $message = $this->formatMessage($message); $line = sprintf('::error %s::%s', implode(',', $metas), $message); @@ -71,6 +83,41 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in return $analysisResult->hasErrors() ? 1 : 0; } + /** + * Mimics the TableErrorFormatter's layout: the message is followed by + * the fixable marker, the identifier and the tip on separate lines. + */ + private function buildFileSpecificMessage(Error $error, OutputFormatter $tipFormatter, string $projectConfigFile): string + { + $message = $error->getMessage(); + if ($error->getFixedErrorDiffs() !== []) { + $message .= ' 🔧'; + } + + $identifier = $error->getIdentifier(); + if ($identifier !== null) { + $message .= "\n🪪 " . $identifier; + if (!$error->canBeIgnored()) { + $message .= ' (non-ignorable)'; + } + } + + $tip = $error->getTip(); + if ($tip !== null) { + $tip = str_replace('%configurationFile%', $projectConfigFile, $tip); + $tip = $tipFormatter->format(ConsoleTagEscaper::escape($tip)) ?? $tip; + if (str_contains($tip, "\n")) { + foreach (explode("\n", $tip) as $tipLine) { + $message .= "\n💡 " . ltrim($tipLine, ' •'); + } + } else { + $message .= "\n💡 " . $tip; + } + } + + return $message; + } + private function formatMessage(string $message): string { // newlines need to be encoded diff --git a/tests/PHPStan/Command/ErrorFormatter/GithubErrorFormatterTest.php b/tests/PHPStan/Command/ErrorFormatter/GithubErrorFormatterTest.php index b349e962d4f..2ffc27475e9 100644 --- a/tests/PHPStan/Command/ErrorFormatter/GithubErrorFormatterTest.php +++ b/tests/PHPStan/Command/ErrorFormatter/GithubErrorFormatterTest.php @@ -2,6 +2,9 @@ namespace PHPStan\Command\ErrorFormatter; +use PHPStan\Analyser\Error; +use PHPStan\Analyser\FixedErrorDiff; +use PHPStan\Command\AnalysisResult; use PHPStan\File\FuzzyRelativePathHelper; use PHPStan\File\NullRelativePathHelper; use PHPStan\Testing\ErrorFormatterTestCase; @@ -47,7 +50,7 @@ public static function dataFormatterOutputProvider(): iterable '::error file=folder with unicode 😃/file name with "spaces" and unicode 😃.php,line=2,col=0::Bar%0ABar2 ::error file=folder with unicode 😃/file name with "spaces" and unicode 😃.php,line=4,col=0::Foo ::error file=foo.php,line=1,col=0::Foo -::error file=foo.php,line=5,col=0::Bar%0ABar2 +::error file=foo.php,line=5,col=0::Bar%0ABar2%0A💡 a tip ', ]; @@ -69,7 +72,7 @@ public static function dataFormatterOutputProvider(): iterable '::error file=folder with unicode 😃/file name with "spaces" and unicode 😃.php,line=2,col=0::Bar%0ABar2 ::error file=folder with unicode 😃/file name with "spaces" and unicode 😃.php,line=4,col=0::Foo ::error file=foo.php,line=1,col=0::Foo -::error file=foo.php,line=5,col=0::Bar%0ABar2 +::error file=foo.php,line=5,col=0::Bar%0ABar2%0A💡 a tip ::error ::first generic error ::error ::second generic ', @@ -110,4 +113,31 @@ public function testFormatErrors( $this->assertSame($expected, $this->getOutputContent(), sprintf('%s: output do not match', $message)); } + public function testFormatErrorsLikeTable(): void + { + $relativePathHelper = new FuzzyRelativePathHelper(new NullRelativePathHelper(), self::DIRECTORY_PATH, [], '/'); + $formatter = new GithubErrorFormatter($relativePathHelper); + + $errors = [ + new Error('Fixable error', self::DIRECTORY_PATH . '/foo.php', 1, identifier: 'foo.bar', fixedErrorDiffs: [new FixedErrorDiff('foo.php', 'hash', 'diff')]), + new Error('Non-ignorable error', self::DIRECTORY_PATH . '/foo.php', 2, canBeIgnored: false, identifier: 'foo.baz'), + new Error('Multi-line tip', self::DIRECTORY_PATH . '/foo.php', 3, tip: "• First line\n• Second line in %configurationFile%"), + new Error('Tip with type', self::DIRECTORY_PATH . '/foo.php', 4, tip: 'Use array instead.'), + ]; + + $this->assertSame(1, $formatter->formatErrors( + new AnalysisResult($errors, [], [], [], [], false, self::DIRECTORY_PATH . '/phpstan.neon', true, 0, false, []), + $this->getOutput(), + )); + + $this->assertSame( + '::error file=foo.php,line=1,col=0::Fixable error 🔧%0A🪪 foo.bar +::error file=foo.php,line=2,col=0::Non-ignorable error%0A🪪 foo.baz (non-ignorable) +::error file=foo.php,line=3,col=0::Multi-line tip%0A💡 First line%0A💡 Second line in phpstan.neon +::error file=foo.php,line=4,col=0::Tip with type%0A💡 Use array instead. +', + $this->getOutputContent(), + ); + } + } From 1de5e3276d3f1da561b09d1c27ef0b1a8e8f1659 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 2 Dec 2026 18:01:46 +0100 Subject: [PATCH 2/4] example errors --- .../ErrorFormatter/CheckstyleErrorFormatter.php | 17 +++++++++++++++++ .../ErrorFormatter/RawErrorFormatter.php | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/Command/ErrorFormatter/CheckstyleErrorFormatter.php b/src/Command/ErrorFormatter/CheckstyleErrorFormatter.php index 6a8c27b122d..f947cc73d9d 100644 --- a/src/Command/ErrorFormatter/CheckstyleErrorFormatter.php +++ b/src/Command/ErrorFormatter/CheckstyleErrorFormatter.php @@ -10,6 +10,7 @@ use PHPStan\File\RelativePathHelper; use function count; use function htmlspecialchars; +use function PHPStan\debugScope; use function sprintf; use const ENT_COMPAT; use const ENT_XML1; @@ -124,4 +125,20 @@ private function groupByFile(AnalysisResult $analysisResult): array return $files; } + /** @param 1 $i */ + public function demoErrorAnnotations(int $i): array + { + if ($i === 1) { + echo 1; // @phpstan-ignore variable.undefined + } + + debugScope(); + return [$this->demoWithoutTip()]; + } + + private function demoWithoutTip(): int + { + return 'not an int'; + } + } diff --git a/src/Command/ErrorFormatter/RawErrorFormatter.php b/src/Command/ErrorFormatter/RawErrorFormatter.php index c16d060406f..871f2c95ff4 100644 --- a/src/Command/ErrorFormatter/RawErrorFormatter.php +++ b/src/Command/ErrorFormatter/RawErrorFormatter.php @@ -2,6 +2,7 @@ namespace PHPStan\Command\ErrorFormatter; +use Exception; use PHPStan\Command\AnalysisResult; use PHPStan\Command\Output; use PHPStan\DependencyInjection\AutowiredService; @@ -49,4 +50,20 @@ public function formatErrors( return $analysisResult->hasErrors() ? 1 : 0; } + public function demoErrorAnnotations(): void + { + $this->demoObjectShape(new Exception()); + $rocket = '🚀 launch 😃'; + if ($rocket === '🐢 slow') { + return; + } + } + + /** + * @param object{foo: int, bar: string} $o + */ + private function demoObjectShape(object $o): void + { + } + } From 5d2cc9ad3fd560875f576ba0a1e90527de6de1d0 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 3 Dec 2026 07:53:50 +0100 Subject: [PATCH 3/4] Improve GitHub error annotations Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/Command/ErrorFormatter/GithubErrorFormatter.php | 4 ++-- .../Command/ErrorFormatter/GithubErrorFormatterTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Command/ErrorFormatter/GithubErrorFormatter.php b/src/Command/ErrorFormatter/GithubErrorFormatter.php index 399c3476934..054eb41cfcb 100644 --- a/src/Command/ErrorFormatter/GithubErrorFormatter.php +++ b/src/Command/ErrorFormatter/GithubErrorFormatter.php @@ -91,12 +91,12 @@ private function buildFileSpecificMessage(Error $error, OutputFormatter $tipForm { $message = $error->getMessage(); if ($error->getFixedErrorDiffs() !== []) { - $message .= ' 🔧'; + $message .= "\n🔧 Can be fixed automatically. Run PHPStan again with --fix."; } $identifier = $error->getIdentifier(); if ($identifier !== null) { - $message .= "\n🪪 " . $identifier; + $message .= sprintf("\n🪪 [%s](https://phpstan.org/error-identifiers/%s)", $identifier, $identifier); if (!$error->canBeIgnored()) { $message .= ' (non-ignorable)'; } diff --git a/tests/PHPStan/Command/ErrorFormatter/GithubErrorFormatterTest.php b/tests/PHPStan/Command/ErrorFormatter/GithubErrorFormatterTest.php index 2ffc27475e9..047b9bc873e 100644 --- a/tests/PHPStan/Command/ErrorFormatter/GithubErrorFormatterTest.php +++ b/tests/PHPStan/Command/ErrorFormatter/GithubErrorFormatterTest.php @@ -131,8 +131,8 @@ public function testFormatErrorsLikeTable(): void )); $this->assertSame( - '::error file=foo.php,line=1,col=0::Fixable error 🔧%0A🪪 foo.bar -::error file=foo.php,line=2,col=0::Non-ignorable error%0A🪪 foo.baz (non-ignorable) + '::error file=foo.php,line=1,col=0::Fixable error%0A🔧 Can be fixed automatically. Run PHPStan again with --fix.%0A🪪 [foo.bar](https://phpstan.org/error-identifiers/foo.bar) +::error file=foo.php,line=2,col=0::Non-ignorable error%0A🪪 [foo.baz](https://phpstan.org/error-identifiers/foo.baz) (non-ignorable) ::error file=foo.php,line=3,col=0::Multi-line tip%0A💡 First line%0A💡 Second line in phpstan.neon ::error file=foo.php,line=4,col=0::Tip with type%0A💡 Use array instead. ', From 5a89b396ab371954608a34bbcc05335e5de5f4e2 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 3 Dec 2026 08:01:16 +0100 Subject: [PATCH 4/4] Use plain URLs in GitHub annotations Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/Command/ErrorFormatter/GithubErrorFormatter.php | 2 +- .../Command/ErrorFormatter/GithubErrorFormatterTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Command/ErrorFormatter/GithubErrorFormatter.php b/src/Command/ErrorFormatter/GithubErrorFormatter.php index 054eb41cfcb..e1808f89d2b 100644 --- a/src/Command/ErrorFormatter/GithubErrorFormatter.php +++ b/src/Command/ErrorFormatter/GithubErrorFormatter.php @@ -96,7 +96,7 @@ private function buildFileSpecificMessage(Error $error, OutputFormatter $tipForm $identifier = $error->getIdentifier(); if ($identifier !== null) { - $message .= sprintf("\n🪪 [%s](https://phpstan.org/error-identifiers/%s)", $identifier, $identifier); + $message .= sprintf("\n🪪 %s: https://phpstan.org/error-identifiers/%s", $identifier, $identifier); if (!$error->canBeIgnored()) { $message .= ' (non-ignorable)'; } diff --git a/tests/PHPStan/Command/ErrorFormatter/GithubErrorFormatterTest.php b/tests/PHPStan/Command/ErrorFormatter/GithubErrorFormatterTest.php index 047b9bc873e..4a615f85ecc 100644 --- a/tests/PHPStan/Command/ErrorFormatter/GithubErrorFormatterTest.php +++ b/tests/PHPStan/Command/ErrorFormatter/GithubErrorFormatterTest.php @@ -131,8 +131,8 @@ public function testFormatErrorsLikeTable(): void )); $this->assertSame( - '::error file=foo.php,line=1,col=0::Fixable error%0A🔧 Can be fixed automatically. Run PHPStan again with --fix.%0A🪪 [foo.bar](https://phpstan.org/error-identifiers/foo.bar) -::error file=foo.php,line=2,col=0::Non-ignorable error%0A🪪 [foo.baz](https://phpstan.org/error-identifiers/foo.baz) (non-ignorable) + '::error file=foo.php,line=1,col=0::Fixable error%0A🔧 Can be fixed automatically. Run PHPStan again with --fix.%0A🪪 foo.bar: https://phpstan.org/error-identifiers/foo.bar +::error file=foo.php,line=2,col=0::Non-ignorable error%0A🪪 foo.baz: https://phpstan.org/error-identifiers/foo.baz (non-ignorable) ::error file=foo.php,line=3,col=0::Multi-line tip%0A💡 First line%0A💡 Second line in phpstan.neon ::error file=foo.php,line=4,col=0::Tip with type%0A💡 Use array instead. ',