Skip to content

Commit

Permalink
ignoreErrors entries for same message and path are summed together
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 20, 2023
1 parent 2456975 commit fa70062
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
29 changes: 29 additions & 0 deletions src/Analyser/IgnoredErrorHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
49 changes: 44 additions & 5 deletions tests/PHPStan/Analyser/AnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit fa70062

Please sign in to comment.