diff --git a/src/Command/ErrorFormatter/GithubErrorFormatter.php b/src/Command/ErrorFormatter/GithubErrorFormatter.php index a81fd0bd01e..e1808f89d2b 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 .= "\n🔧 Can be fixed automatically. Run PHPStan again with --fix."; + } + + $identifier = $error->getIdentifier(); + if ($identifier !== null) { + $message .= sprintf("\n🪪 %s: https://phpstan.org/error-identifiers/%s", $identifier, $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..4a615f85ecc 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🔧 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. +', + $this->getOutputContent(), + ); + } + }