Skip to content

Commit 8f920df

Browse files
authored
Fix error count by type (#33)
* Fix error count by type * Add tests for error count
1 parent 3635758 commit 8f920df

File tree

6 files changed

+11
-9
lines changed

6 files changed

+11
-9
lines changed

src/BaselinePerIdentifierFormatter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public function formatErrors(
7373

7474
foreach ($fileErrors as $identifier => $errors) {
7575
$errorsToOutput = [];
76+
$errorsCount = 0;
7677

7778
foreach ($errors as $file => $errorMessages) {
7879
$fileErrorsCounts = [];
@@ -94,12 +95,12 @@ public function formatErrors(
9495
'count' => $count,
9596
'path' => NeonHelper::escape($file),
9697
];
98+
$errorsCount += $count;
9799
}
98100
}
99101

100102
$includes[] = $identifier . '.neon';
101103
$baselineFilePath = $this->baselinesDir . '/' . $identifier . '.neon';
102-
$errorsCount = count($errorsToOutput);
103104

104105
$output->writeLineFormatted(sprintf('Writing baseline file %s with %d errors', $baselineFilePath, $errorsCount));
105106

src/BaselineSplitter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
use ShipMonk\PHPStan\Baseline\Exception\ErrorException;
66
use ShipMonk\PHPStan\Baseline\Handler\HandlerFactory;
77
use SplFileInfo;
8+
use function array_reduce;
89
use function assert;
9-
use function count;
1010
use function dirname;
1111
use function file_put_contents;
1212
use function is_array;
@@ -62,7 +62,7 @@ public function split(string $loaderFilePath): array
6262
foreach ($groupedErrors as $identifier => $errors) {
6363
$fileName = $identifier . '.' . $extension;
6464
$filePath = $folder . '/' . $fileName;
65-
$errorsCount = count($errors);
65+
$errorsCount = array_reduce($errors, static fn(int $carry, array $item): int => $carry + $item['count'], 0);
6666

6767
$outputInfo[$filePath] = $errorsCount;
6868
$baselineFiles[] = $fileName;

tests/Rule/BaselinePerIdentifierFormatterTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function testFormat(): void
2424
$this->createAnalysisResult(
2525
[
2626
(new Error('Error simple', $fakeRoot . '/app/file.php', 1))->withIdentifier('sample.identifier'), // @phpstan-ignore phpstanApi.constructor
27+
(new Error('Error simple', $fakeRoot . '/app/file.php', 5))->withIdentifier('sample.identifier'), // @phpstan-ignore phpstanApi.constructor
2728
(new Error('Error to escape \'#', $fakeRoot . '/app/config.php', 2))->withIdentifier('another.identifier'), // @phpstan-ignore phpstanApi.constructor
2829
(new Error('Error 3', $fakeRoot . '/app/index.php', 3)), // @phpstan-ignore phpstanApi.constructor
2930
],
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# total 1 error
1+
# total 2 errors
22

33
parameters:
44
ignoreErrors:
55
-
66
message: '#^Error simple$#'
7-
count: 1
7+
count: 2
88
path: ../app/file.php

tests/Rule/data/baselines-php/sample.identifier.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php declare(strict_types = 1);
22

3-
// total 1 error
3+
// total 2 errors
44

55
$ignoreErrors = [];
66
$ignoreErrors[] = [
77
'message' => '#^Error simple$#',
8-
'count' => 1,
8+
'count' => 2,
99
'path' => __DIR__ . '/../app/file.php',
1010
];
1111

tests/SplitterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function testSplitter(): void
6161
self::assertSame([
6262
$folder . '/baselines/another.identifier.neon' => 1,
6363
$folder . '/baselines/missing-identifier.neon' => 1,
64-
$folder . '/baselines/sample.identifier.neon' => 1,
64+
$folder . '/baselines/sample.identifier.neon' => 2,
6565
$folder . '/baselines/loader.neon' => null,
6666
], $written);
6767
}
@@ -84,7 +84,7 @@ private function getSampleErrors(): array
8484
'ignoreErrors' => [
8585
[
8686
'message' => '#^Error simple$#',
87-
'count' => 1,
87+
'count' => 2,
8888
'path' => '../app/file.php',
8989
'identifier' => 'sample.identifier',
9090
],

0 commit comments

Comments
 (0)