Skip to content

Commit fa70062

Browse files
committed
ignoreErrors entries for same message and path are summed together
1 parent 2456975 commit fa70062

File tree

2 files changed

+73
-5
lines changed

2 files changed

+73
-5
lines changed

src/Analyser/IgnoredErrorHelper.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Nette\Utils\Json;
66
use Nette\Utils\JsonException;
77
use PHPStan\File\FileHelper;
8+
use function array_key_exists;
9+
use function array_values;
810
use function is_array;
911
use function is_file;
1012
use function sprintf;
@@ -54,6 +56,33 @@ public function initialize(): IgnoredErrorHelperResult
5456
}
5557
}
5658

59+
$uniquedExpandedIgnoreErrors = [];
60+
foreach ($expandedIgnoreErrors as $ignoreError) {
61+
if (!isset($ignoreError['message'])) {
62+
$uniquedExpandedIgnoreErrors[] = $ignoreError;
63+
continue;
64+
}
65+
if (!isset($ignoreError['path'])) {
66+
$uniquedExpandedIgnoreErrors[] = $ignoreError;
67+
continue;
68+
}
69+
70+
$key = sprintf("%s\n%s", $ignoreError['message'], $ignoreError['path']);
71+
if (!array_key_exists($key, $uniquedExpandedIgnoreErrors)) {
72+
$uniquedExpandedIgnoreErrors[$key] = $ignoreError;
73+
continue;
74+
}
75+
76+
$uniquedExpandedIgnoreErrors[$key] = [
77+
'message' => $ignoreError['message'],
78+
'path' => $ignoreError['path'],
79+
'count' => ($uniquedExpandedIgnoreErrors[$key]['count'] ?? 1) + ($ignoreError['count'] ?? 1),
80+
'reportUnmatched' => ($uniquedExpandedIgnoreErrors[$key]['reportUnmatched'] ?? $this->reportUnmatchedIgnoredErrors) || ($ignoreError['reportUnmatched'] ?? $this->reportUnmatchedIgnoredErrors),
81+
];
82+
}
83+
84+
$expandedIgnoreErrors = array_values($uniquedExpandedIgnoreErrors);
85+
5786
foreach ($expandedIgnoreErrors as $i => $ignoreError) {
5887
$ignoreErrorEntry = [
5988
'index' => $i,

tests/PHPStan/Analyser/AnalyserTest.php

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,54 @@ public function testIgnoreErrorMultiByPath(): void
110110
$this->assertNoErrors($result);
111111
}
112112

113-
public function testIgnoreErrorByPathAndCount(): void
113+
public function dataIgnoreErrorByPathAndCount(): iterable
114114
{
115-
$ignoreErrors = [
115+
yield [
116116
[
117-
'message' => '#Fail\.#',
118-
'count' => 3,
119-
'path' => __DIR__ . '/data/two-fails.php',
117+
[
118+
'message' => '#Fail\.#',
119+
'count' => 3,
120+
'path' => __DIR__ . '/data/two-fails.php',
121+
],
122+
],
123+
];
124+
125+
yield [
126+
[
127+
[
128+
'message' => '#Fail\.#',
129+
'count' => 2,
130+
'path' => __DIR__ . '/data/two-fails.php',
131+
],
132+
[
133+
'message' => '#Fail\.#',
134+
'count' => 1,
135+
'path' => __DIR__ . '/data/two-fails.php',
136+
],
120137
],
121138
];
139+
140+
yield [
141+
[
142+
[
143+
'message' => '#Fail\.#',
144+
'count' => 2,
145+
'path' => __DIR__ . '/data/two-fails.php',
146+
],
147+
[
148+
'message' => '#Fail\.#',
149+
'path' => __DIR__ . '/data/two-fails.php',
150+
],
151+
],
152+
];
153+
}
154+
155+
/**
156+
* @dataProvider dataIgnoreErrorByPathAndCount
157+
* @param mixed[] $ignoreErrors
158+
*/
159+
public function testIgnoreErrorByPathAndCount(array $ignoreErrors): void
160+
{
122161
$result = $this->runAnalyser($ignoreErrors, true, __DIR__ . '/data/two-fails.php', false);
123162
$this->assertNoErrors($result);
124163
}

0 commit comments

Comments
 (0)