Skip to content

Add Grouped Error Formatter #4002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 2.1.x
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2262,6 +2262,13 @@ services:
editorUrl: %editorUrl%
editorUrlTitle: %editorUrlTitle%

errorFormatter.grouped:
class: PHPStan\Command\ErrorFormatter\GroupedErrorFormatter
arguments:
relativePathHelper: @simpleRelativePathHelper
editorUrl: %editorUrl%
editorUrlTitle: %editorUrlTitle%

errorFormatter.checkstyle:
class: PHPStan\Command\ErrorFormatter\CheckstyleErrorFormatter
arguments:
Expand Down
112 changes: 112 additions & 0 deletions src/Command/ErrorFormatter/GroupedErrorFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php declare(strict_types = 1);

namespace PHPStan\Command\ErrorFormatter;

use PHPStan\Analyser\Error;
use PHPStan\Command\AnalysisResult;
use PHPStan\Command\Output;
use PHPStan\File\RelativePathHelper;
use Symfony\Component\Console\Formatter\OutputFormatter;
use function count;
use function is_string;
use function sprintf;
use function str_replace;
use function uasort;

final class GroupedErrorFormatter implements ErrorFormatter
{

private const NO_IDENTIFIER = 'without identifier';

public function __construct(private RelativePathHelper $relativePathHelper, private ?string $editorUrl, private ?string $editorUrlTitle)
{
}

public function formatErrors(AnalysisResult $analysisResult, Output $output): int
{
$style = $output->getStyle();

if (!$analysisResult->hasErrors() && !$analysisResult->hasWarnings()) {
$style->success('No errors');

return 0;
}

/** @var array<string, Error[]> $groupedErrors */
$groupedErrors = [];

foreach ($analysisResult->getFileSpecificErrors() as $fileSpecificError) {
$identifier = $fileSpecificError->getIdentifier() ?? self::NO_IDENTIFIER;

$groupedErrors[$identifier][] = $fileSpecificError;
}

uasort($groupedErrors, static fn ($errorsA, $errorsB) => count($errorsB) <=> count($errorsA));

foreach ($groupedErrors as $identifier => $errors) {
$count = count($errors);
$output->writeRaw(sprintf('[%s] (%dx):', $identifier, $count));
$output->writeLineFormatted('');

foreach ($errors as $error) {
$file = $error->getTraitFilePath() ?? $error->getFilePath();
$relFile = $this->relativePathHelper->getRelativePath($file);
$line = (string) $error->getLine();
$message = $error->getMessage();

if (is_string($this->editorUrl)) {
$url = str_replace(
['%file%', '%relFile%', '%line%'],
[$file, $relFile, $line],
$this->editorUrl,
);

if (is_string($this->editorUrlTitle)) {
$title = str_replace(
['%file%', '%relFile%', '%line%'],
[$file, $relFile, $line],
$this->editorUrlTitle,
);
} else {
$title = sprintf('%s:%s', $file, $line);
}

$fileStr = '<href=' . OutputFormatter::escape($url) . '>' . $title . '</>';
} else {
$fileStr = sprintf('%s:%s', $file, $line);
}

$output->writeLineFormatted(sprintf("\t- %s: %s", $fileStr, $message));
}
$output->writeLineFormatted('');
}

foreach ($analysisResult->getNotFileSpecificErrors() as $notFileSpecificError) {
$output->writeRaw(sprintf('?:?:%s', $notFileSpecificError));
$output->writeLineFormatted('');
}

foreach ($analysisResult->getWarnings() as $warning) {
$output->writeRaw(sprintf('?:?:%s', $warning));
$output->writeLineFormatted('');
}

$totalErrorsCount = $analysisResult->getTotalErrorsCount();
$warningsCount = count($analysisResult->getWarnings());

$finalMessage = sprintf($totalErrorsCount === 1 ? 'Found %d error' : 'Found %d errors', $totalErrorsCount);

if ($analysisResult->hasWarnings()) {
$finalMessage .= sprintf($warningsCount === 1 ? ' and %d warning' : ' and %d warnings', $warningsCount);
}

if ($analysisResult->hasErrors()) {
$style->error($finalMessage);
} else {
$style->warning($finalMessage);
}

return $analysisResult->hasErrors() ? 1 : 0;
}

}
153 changes: 153 additions & 0 deletions tests/PHPStan/Command/ErrorFormatter/GroupedErrorFormatterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php declare(strict_types = 1);

namespace PHPStan\Command\ErrorFormatter;

use PHPStan\File\FuzzyRelativePathHelper;
use PHPStan\File\NullRelativePathHelper;
use PHPStan\Testing\ErrorFormatterTestCase;
use function sprintf;

class GroupedErrorFormatterTest extends ErrorFormatterTestCase
{

public function dataFormatterOutputProvider(): iterable
{
yield [
'message' => 'No errors',
'exitCode' => 0,
'numFileErrors' => 0,
'numGenericErrors' => 0,
'expected' => '
[OK] No errors

',
];

yield [
'message' => 'One file error',
'exitCode' => 1,
'numFileErrors' => 1,
'numGenericErrors' => 0,
'expected' => '[without identifier] (1x):
- /data/folder/with space/and unicode 😃/project/folder with unicode 😃/file name with "spaces" and unicode 😃.php:4: Foo


[ERROR] Found 1 error

',
];

yield [
'message' => 'One generic error',
'exitCode' => 1,
'numFileErrors' => 0,
'numGenericErrors' => 1,
'expected' => '?:?:first generic error

[ERROR] Found 1 error

',
];

yield [
'message' => 'Multiple file errors',
'exitCode' => 1,
'numFileErrors' => 4,
'numGenericErrors' => 0,
'expected' => '[without identifier] (4x):
- /data/folder/with space/and unicode 😃/project/folder with unicode 😃/file name with "spaces" and unicode 😃.php:2: Bar
Bar2
- /data/folder/with space/and unicode 😃/project/folder with unicode 😃/file name with "spaces" and unicode 😃.php:4: Foo
- /data/folder/with space/and unicode 😃/project/foo.php:1: Foo<Bar>
- /data/folder/with space/and unicode 😃/project/foo.php:5: Bar
Bar2


[ERROR] Found 4 errors

',
];

yield [
'message' => 'Multiple generic errors',
'exitCode' => 1,
'numFileErrors' => 0,
'numGenericErrors' => 2,
'expected' => '?:?:first generic error
?:?:second generic<error>

[ERROR] Found 2 errors

',
];

yield [
'message' => 'Multiple file, multiple generic errors',
'exitCode' => 1,
'numFileErrors' => 4,
'numGenericErrors' => 2,
'expected' => '[without identifier] (4x):
- /data/folder/with space/and unicode 😃/project/folder with unicode 😃/file name with "spaces" and unicode 😃.php:2: Bar
Bar2
- /data/folder/with space/and unicode 😃/project/folder with unicode 😃/file name with "spaces" and unicode 😃.php:4: Foo
- /data/folder/with space/and unicode 😃/project/foo.php:1: Foo<Bar>
- /data/folder/with space/and unicode 😃/project/foo.php:5: Bar
Bar2

?:?:first generic error
?:?:second generic<error>

[ERROR] Found 6 errors

',
];

yield [
'message' => 'One file error with identifier',
'exitCode' => 1,
'numFileErrors' => [5, 6],
'numGenericErrors' => 0,
'expected' => '[foobar.buz] (1x):
- /data/folder/with space/and unicode 😃/project/foo.php:5: Foobar\Buz


[ERROR] Found 1 error

',
];
}

/**
* @dataProvider dataFormatterOutputProvider
* @param array{int, int}|int $numFileErrors
*/
public function testFormatErrors(
string $message,
int $exitCode,
array|int $numFileErrors,
int $numGenericErrors,
string $expected,
): void
{
$formatter = $this->createErrorFormatter(null);

$this->assertSame($exitCode, $formatter->formatErrors(
$this->getAnalysisResult($numFileErrors, $numGenericErrors),
$this->getOutput(),
), sprintf('%s: response code do not match', $message));

$this->assertEquals($expected, $this->getOutputContent(), sprintf('%s: output do not match', $message));
}

private function createErrorFormatter(?string $editorUrl, ?string $editorUrlTitle = null): GroupedErrorFormatter
{
$relativePathHelper = new FuzzyRelativePathHelper(new NullRelativePathHelper(), self::DIRECTORY_PATH, [], '/');

return new GroupedErrorFormatter(
$relativePathHelper,
$editorUrl,
$editorUrlTitle,
);
}

}
Loading