Skip to content

Commit

Permalink
Identifiers in the PHP baseline as real array keys
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 1, 2024
1 parent 9e7f39e commit f38addd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
29 changes: 15 additions & 14 deletions src/Command/ErrorFormatter/BaselinePhpErrorFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PHPStan\File\RelativePathHelper;
use function array_keys;
use function count;
use function implode;
use function ksort;
use function preg_quote;
use function sort;
Expand Down Expand Up @@ -74,22 +73,24 @@ public function formatErrors(
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 ($identifiers as $identifier) {
$php .= sprintf(
"\$ignoreErrors[] = [\n\t'message' => %s,\n\t'identifier' => %s,\n\t'count' => %d,\n\t'path' => __DIR__ . %s,\n];\n",
var_export(Helpers::escape('#^' . preg_quote($message, '#') . '$#'), true),
var_export(Helpers::escape($identifier), true),
var_export($count, true),
var_export(Helpers::escape($file), true),
);
}
} else {
$php .= sprintf(
"\$ignoreErrors[] = [\n\t'message' => %s,\n\t'count' => %d,\n\t'path' => __DIR__ . %s,\n];\n",
var_export(Helpers::escape('#^' . preg_quote($message, '#') . '$#'), true),
var_export($count, true),
var_export(Helpers::escape($file), true),
);
}

$php .= sprintf(
"\$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 @@ -80,8 +80,8 @@ public function dataFormatErrors(): iterable
'path' => __DIR__ . '/Foo.php',
];
\$ignoreErrors[] = [
// identifier: argument.type
'message' => '#^Foo with identifier$#',
'identifier' => 'argument.type',
'count' => 2,
'path' => __DIR__ . '/Foo.php',
];
Expand Down Expand Up @@ -127,14 +127,20 @@ public function dataFormatErrors(): iterable
'path' => __DIR__ . '/Foo.php',
];
\$ignoreErrors[] = [
// identifier: argument.type
'message' => '#^Foo with another message$#',
'identifier' => 'argument.type',
'count' => 1,
'path' => __DIR__ . '/Foo.php',
];
\$ignoreErrors[] = [
// identifiers: argument.byRef, argument.type
'message' => '#^Foo with same message, different identifier$#',
'identifier' => 'argument.byRef',
'count' => 2,
'path' => __DIR__ . '/Foo.php',
];
\$ignoreErrors[] = [
'message' => '#^Foo with same message, different identifier$#',
'identifier' => 'argument.type',
'count' => 2,
'path' => __DIR__ . '/Foo.php',
];
Expand Down

3 comments on commit f38addd

@ondrejmirtes
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/cc @ruudk This one's for you 😂

@ruudk
Copy link
Contributor

@ruudk ruudk commented on f38addd Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ondrejmirtes Thanks. This makes me happy 🎉

@Wirone
Copy link
Contributor

@Wirone Wirone commented on f38addd Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

YEAH 🥳!! This will simplify our area-scoped baselines generator, as we now convert comments to array items on-the-fly.

Image

Please sign in to comment.