From fa70062df084110dfc3fc9ce9be867dc143eb482 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Fri, 20 Jan 2023 12:55:04 +0100 Subject: [PATCH] ignoreErrors entries for same message and path are summed together --- src/Analyser/IgnoredErrorHelper.php | 29 +++++++++++++++ tests/PHPStan/Analyser/AnalyserTest.php | 49 ++++++++++++++++++++++--- 2 files changed, 73 insertions(+), 5 deletions(-) diff --git a/src/Analyser/IgnoredErrorHelper.php b/src/Analyser/IgnoredErrorHelper.php index c25b0e495a..2f47f3fa14 100644 --- a/src/Analyser/IgnoredErrorHelper.php +++ b/src/Analyser/IgnoredErrorHelper.php @@ -5,6 +5,8 @@ use Nette\Utils\Json; use Nette\Utils\JsonException; use PHPStan\File\FileHelper; +use function array_key_exists; +use function array_values; use function is_array; use function is_file; use function sprintf; @@ -54,6 +56,33 @@ public function initialize(): IgnoredErrorHelperResult } } + $uniquedExpandedIgnoreErrors = []; + foreach ($expandedIgnoreErrors as $ignoreError) { + if (!isset($ignoreError['message'])) { + $uniquedExpandedIgnoreErrors[] = $ignoreError; + continue; + } + if (!isset($ignoreError['path'])) { + $uniquedExpandedIgnoreErrors[] = $ignoreError; + continue; + } + + $key = sprintf("%s\n%s", $ignoreError['message'], $ignoreError['path']); + if (!array_key_exists($key, $uniquedExpandedIgnoreErrors)) { + $uniquedExpandedIgnoreErrors[$key] = $ignoreError; + continue; + } + + $uniquedExpandedIgnoreErrors[$key] = [ + 'message' => $ignoreError['message'], + 'path' => $ignoreError['path'], + 'count' => ($uniquedExpandedIgnoreErrors[$key]['count'] ?? 1) + ($ignoreError['count'] ?? 1), + 'reportUnmatched' => ($uniquedExpandedIgnoreErrors[$key]['reportUnmatched'] ?? $this->reportUnmatchedIgnoredErrors) || ($ignoreError['reportUnmatched'] ?? $this->reportUnmatchedIgnoredErrors), + ]; + } + + $expandedIgnoreErrors = array_values($uniquedExpandedIgnoreErrors); + foreach ($expandedIgnoreErrors as $i => $ignoreError) { $ignoreErrorEntry = [ 'index' => $i, diff --git a/tests/PHPStan/Analyser/AnalyserTest.php b/tests/PHPStan/Analyser/AnalyserTest.php index 57d216fe21..0bb5e3da80 100644 --- a/tests/PHPStan/Analyser/AnalyserTest.php +++ b/tests/PHPStan/Analyser/AnalyserTest.php @@ -110,15 +110,54 @@ public function testIgnoreErrorMultiByPath(): void $this->assertNoErrors($result); } - public function testIgnoreErrorByPathAndCount(): void + public function dataIgnoreErrorByPathAndCount(): iterable { - $ignoreErrors = [ + yield [ [ - 'message' => '#Fail\.#', - 'count' => 3, - 'path' => __DIR__ . '/data/two-fails.php', + [ + 'message' => '#Fail\.#', + 'count' => 3, + 'path' => __DIR__ . '/data/two-fails.php', + ], + ], + ]; + + yield [ + [ + [ + 'message' => '#Fail\.#', + 'count' => 2, + 'path' => __DIR__ . '/data/two-fails.php', + ], + [ + 'message' => '#Fail\.#', + 'count' => 1, + 'path' => __DIR__ . '/data/two-fails.php', + ], ], ]; + + yield [ + [ + [ + 'message' => '#Fail\.#', + 'count' => 2, + 'path' => __DIR__ . '/data/two-fails.php', + ], + [ + 'message' => '#Fail\.#', + 'path' => __DIR__ . '/data/two-fails.php', + ], + ], + ]; + } + + /** + * @dataProvider dataIgnoreErrorByPathAndCount + * @param mixed[] $ignoreErrors + */ + public function testIgnoreErrorByPathAndCount(array $ignoreErrors): void + { $result = $this->runAnalyser($ignoreErrors, true, __DIR__ . '/data/two-fails.php', false); $this->assertNoErrors($result); }