Skip to content

Commit f38addd

Browse files
committed
Identifiers in the PHP baseline as real array keys
1 parent 9e7f39e commit f38addd

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

src/Command/ErrorFormatter/BaselinePhpErrorFormatter.php

+15-14
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use PHPStan\File\RelativePathHelper;
99
use function array_keys;
1010
use function count;
11-
use function implode;
1211
use function ksort;
1312
use function preg_quote;
1413
use function sort;
@@ -74,22 +73,24 @@ public function formatErrors(
7473
foreach ($fileErrorsByMessage as $message => [$count, $identifiersInKeys]) {
7574
$identifiers = array_keys($identifiersInKeys);
7675
sort($identifiers);
77-
$identifiersComment = '';
7876
if (count($identifiers) > 0) {
79-
if (count($identifiers) === 1) {
80-
$identifiersComment = "\n\t// identifier: " . $identifiers[0];
81-
} else {
82-
$identifiersComment = "\n\t// identifiers: " . implode(', ', $identifiers);
77+
foreach ($identifiers as $identifier) {
78+
$php .= sprintf(
79+
"\$ignoreErrors[] = [\n\t'message' => %s,\n\t'identifier' => %s,\n\t'count' => %d,\n\t'path' => __DIR__ . %s,\n];\n",
80+
var_export(Helpers::escape('#^' . preg_quote($message, '#') . '$#'), true),
81+
var_export(Helpers::escape($identifier), true),
82+
var_export($count, true),
83+
var_export(Helpers::escape($file), true),
84+
);
8385
}
86+
} else {
87+
$php .= sprintf(
88+
"\$ignoreErrors[] = [\n\t'message' => %s,\n\t'count' => %d,\n\t'path' => __DIR__ . %s,\n];\n",
89+
var_export(Helpers::escape('#^' . preg_quote($message, '#') . '$#'), true),
90+
var_export($count, true),
91+
var_export(Helpers::escape($file), true),
92+
);
8493
}
85-
86-
$php .= sprintf(
87-
"\$ignoreErrors[] = [%s\n\t'message' => %s,\n\t'count' => %d,\n\t'path' => __DIR__ . %s,\n];\n",
88-
$identifiersComment,
89-
var_export(Helpers::escape('#^' . preg_quote($message, '#') . '$#'), true),
90-
var_export($count, true),
91-
var_export(Helpers::escape($file), true),
92-
);
9394
}
9495
}
9596

tests/PHPStan/Command/ErrorFormatter/BaselinePhpErrorFormatterTest.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ public function dataFormatErrors(): iterable
8080
'path' => __DIR__ . '/Foo.php',
8181
];
8282
\$ignoreErrors[] = [
83-
// identifier: argument.type
8483
'message' => '#^Foo with identifier$#',
84+
'identifier' => 'argument.type',
8585
'count' => 2,
8686
'path' => __DIR__ . '/Foo.php',
8787
];
@@ -127,14 +127,20 @@ public function dataFormatErrors(): iterable
127127
'path' => __DIR__ . '/Foo.php',
128128
];
129129
\$ignoreErrors[] = [
130-
// identifier: argument.type
131130
'message' => '#^Foo with another message$#',
131+
'identifier' => 'argument.type',
132132
'count' => 1,
133133
'path' => __DIR__ . '/Foo.php',
134134
];
135135
\$ignoreErrors[] = [
136-
// identifiers: argument.byRef, argument.type
137136
'message' => '#^Foo with same message, different identifier$#',
137+
'identifier' => 'argument.byRef',
138+
'count' => 2,
139+
'path' => __DIR__ . '/Foo.php',
140+
];
141+
\$ignoreErrors[] = [
142+
'message' => '#^Foo with same message, different identifier$#',
143+
'identifier' => 'argument.type',
138144
'count' => 2,
139145
'path' => __DIR__ . '/Foo.php',
140146
];

0 commit comments

Comments
 (0)