Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion src/Command/ErrorFormatter/GithubErrorFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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()),
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down
34 changes: 32 additions & 2 deletions tests/PHPStan/Command/ErrorFormatter/GithubErrorFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Bar>
::error file=foo.php,line=5,col=0::Bar%0ABar2
::error file=foo.php,line=5,col=0::Bar%0ABar2%0A💡 a tip
',
];

Expand All @@ -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<Bar>
::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<error>
',
Expand Down Expand Up @@ -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 <fg=cyan>line</> in %configurationFile%"),
new Error('Tip with type', self::DIRECTORY_PATH . '/foo.php', 4, tip: 'Use array<int, string> 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<int, string> instead.
',
$this->getOutputContent(),
);
}

}
Loading