Skip to content

Commit

Permalink
BaselinePhpErrorFormatter - error identifiers in comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jun 16, 2023
1 parent 991322c commit 4b32cac
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 10 deletions.
45 changes: 35 additions & 10 deletions src/Command/ErrorFormatter/BaselinePhpErrorFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
use PHPStan\Command\AnalysisResult;
use PHPStan\Command\Output;
use PHPStan\File\RelativePathHelper;
use function array_keys;
use function count;
use function implode;
use function ksort;
use function preg_quote;
use function sort;
use function sprintf;
use function var_export;
use const SORT_STRING;
Expand Down Expand Up @@ -38,29 +42,50 @@ public function formatErrors(
if (!$fileSpecificError->canBeIgnored()) {
continue;
}
$fileErrors['/' . $this->relativePathHelper->getRelativePath($fileSpecificError->getFilePath())][] = $fileSpecificError->getMessage();
$fileErrors['/' . $this->relativePathHelper->getRelativePath($fileSpecificError->getFilePath())][] = $fileSpecificError;
}
ksort($fileErrors, SORT_STRING);

$php = '<?php declare(strict_types = 1);';
$php .= "\n\n";
$php .= '$ignoreErrors = [];';
$php .= "\n";
foreach ($fileErrors as $file => $errorMessages) {
$fileErrorsCounts = [];
foreach ($errorMessages as $errorMessage) {
if (!isset($fileErrorsCounts[$errorMessage])) {
$fileErrorsCounts[$errorMessage] = 1;
foreach ($fileErrors as $file => $errors) {
$fileErrorsByMessage = [];
foreach ($errors as $error) {
$errorMessage = $error->getMessage();
if (!isset($fileErrorsByMessage[$errorMessage])) {
$fileErrorsByMessage[$errorMessage] = [
1,
$error->getIdentifier() !== null ? [$error->getIdentifier() => true] : [],
];
continue;
}

$fileErrorsCounts[$errorMessage]++;
$fileErrorsByMessage[$errorMessage][0]++;

if ($error->getIdentifier() === null) {
continue;
}
$fileErrorsByMessage[$errorMessage][1][$error->getIdentifier()] = true;
}
ksort($fileErrorsCounts, SORT_STRING);
ksort($fileErrorsByMessage, SORT_STRING);

foreach ($fileErrorsByMessage as $message => [$count, $identifiersInKeys]) {
$identifiers = array_keys($identifiersInKeys);
sort($identifiers);
$identifiersComment = '';
if (count($identifiers) > 0) {
if (count($identifiers) === 1) {
$identifiersComment = "\n\t// identifier: " . $identifiers[0];
} else {
$identifiersComment = "\n\t// identifiers: " . implode(', ', $identifiers);
}
}

foreach ($fileErrorsCounts as $message => $count) {
$php .= sprintf(
"\$ignoreErrors[] = [\n\t'message' => %s,\n\t'count' => %d,\n\t'path' => __DIR__ . %s,\n];\n",
"\$ignoreErrors[] = [%s\n\t'message' => %s,\n\t'count' => %d,\n\t'path' => __DIR__ . %s,\n];\n",
$identifiersComment,
var_export(Helpers::escape('#^' . preg_quote($message, '#') . '$#'), true),
var_export($count, true),
var_export(Helpers::escape($file), true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,105 @@ public function dataFormatErrors(): iterable
return ['parameters' => ['ignoreErrors' => $ignoreErrors]];
PHP,
];

yield [
[
new Error(
'Foo',
__DIR__ . '/Foo.php',
5,
),
new Error(
'Foo',
__DIR__ . '/Foo.php',
5,
),
(new Error(
'Foo with identifier',
__DIR__ . '/Foo.php',
5,
))->withIdentifier('argument.type'),
(new Error(
'Foo with identifier',
__DIR__ . '/Foo.php',
6,
))->withIdentifier('argument.type'),
],
<<<'PHP'
<?php declare(strict_types = 1);
$ignoreErrors = [];
$ignoreErrors[] = [
'message' => '#^Foo$#',
'count' => 2,
'path' => __DIR__ . '/Foo.php',
];
$ignoreErrors[] = [
// identifier: argument.type
'message' => '#^Foo with identifier$#',
'count' => 2,
'path' => __DIR__ . '/Foo.php',
];
return ['parameters' => ['ignoreErrors' => $ignoreErrors]];
PHP,
];

yield [
[
new Error(
'Foo',
__DIR__ . '/Foo.php',
5,
),
new Error(
'Foo',
__DIR__ . '/Foo.php',
5,
),
(new Error(
'Foo with same message, different identifier',
__DIR__ . '/Foo.php',
5,
))->withIdentifier('argument.type'),
(new Error(
'Foo with same message, different identifier',
__DIR__ . '/Foo.php',
6,
))->withIdentifier('argument.byRef'),

Check failure on line 118 in tests/PHPStan/Command/ErrorFormatter/BaselinePhpErrorFormatterTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, ubuntu-latest)

Syntax error, unexpected EOF, expecting T_VARIABLE or T_DOLLAR_OPEN_CURLY_BRACES or T_CURLY_OPEN on line 118

Check failure on line 118 in tests/PHPStan/Command/ErrorFormatter/BaselinePhpErrorFormatterTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, windows-latest)

Syntax error, unexpected EOF, expecting T_VARIABLE or T_DOLLAR_OPEN_CURLY_BRACES or T_CURLY_OPEN on line 118
(new Error(
'Foo with another message',
__DIR__ . '/Foo.php',
5,
))->withIdentifier('argument.type'),
],
<<<'PHP'
<?php declare(strict_types = 1);
$ignoreErrors = [];
$ignoreErrors[] = [
'message' => '#^Foo$#',
'count' => 2,
'path' => __DIR__ . '/Foo.php',
];
$ignoreErrors[] = [
// identifier: argument.type
'message' => '#^Foo with another message$#',
'count' => 1,
'path' => __DIR__ . '/Foo.php',
];
$ignoreErrors[] = [
// identifiers: argument.byRef, argument.type
'message' => '#^Foo with same message, different identifier$#',
'count' => 2,
'path' => __DIR__ . '/Foo.php',
];
return ['parameters' => ['ignoreErrors' => $ignoreErrors]];
PHP,
];
}
Expand Down

0 comments on commit 4b32cac

Please sign in to comment.