Skip to content

Commit 2b3d016

Browse files
authored
Add the total error count in the loader (#38)
1 parent f40ea03 commit 2b3d016

File tree

7 files changed

+18
-6
lines changed

7 files changed

+18
-6
lines changed

src/BaselinePerIdentifierFormatter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public function formatErrors(
7070
ksort($fileErrors, SORT_STRING);
7171

7272
$includes = [];
73+
$totalErrorsCount = 0;
7374

7475
foreach ($fileErrors as $identifier => $errors) {
7576
$errorsToOutput = [];
@@ -102,6 +103,7 @@ public function formatErrors(
102103
$includes[] = $identifier . '.neon';
103104
$baselineFilePath = $this->baselinesDir . '/' . $identifier . '.neon';
104105

106+
$totalErrorsCount += $errorsCount;
105107
$output->writeLineFormatted(sprintf('Writing baseline file %s with %d errors', $baselineFilePath, $errorsCount));
106108

107109
$plurality = $errorsCount === 1 ? '' : 's';
@@ -114,7 +116,9 @@ public function formatErrors(
114116
}
115117
}
116118

117-
$writtenLoader = file_put_contents($this->baselinesDir . '/loader.neon', NeonHelper::encode(['includes' => $includes], $this->indent));
119+
$plurality = $totalErrorsCount === 1 ? '' : 's';
120+
$prefix = "# total $totalErrorsCount error$plurality\n";
121+
$writtenLoader = file_put_contents($this->baselinesDir . '/loader.neon', $prefix . NeonHelper::encode(['includes' => $includes], $this->indent));
118122

119123
if ($writtenLoader === false) {
120124
throw new LogicException('Error while writing to ' . $this->baselinesDir . '/loader.neon');

src/BaselineSplitter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,13 @@ public function split(string $loaderFilePath): array
5858

5959
$outputInfo = [];
6060
$baselineFiles = [];
61+
$totalErrorCount = 0;
6162

6263
foreach ($groupedErrors as $identifier => $errors) {
6364
$fileName = $identifier . '.' . $extension;
6465
$filePath = $folder . '/' . $fileName;
6566
$errorsCount = array_reduce($errors, static fn(int $carry, array $item): int => $carry + $item['count'], 0);
67+
$totalErrorCount += $errorsCount;
6668

6769
$outputInfo[$filePath] = $errorsCount;
6870
$baselineFiles[] = $fileName;
@@ -74,7 +76,9 @@ public function split(string $loaderFilePath): array
7476
$this->writeFile($filePath, $encodedData);
7577
}
7678

77-
$baselineLoaderData = $handler->encodeBaselineLoader($baselineFiles, $this->indent);
79+
$plural = $totalErrorCount === 1 ? '' : 's';
80+
$prefix = "total $totalErrorCount error$plural";
81+
$baselineLoaderData = $handler->encodeBaselineLoader($prefix, $baselineFiles, $this->indent);
7882
$this->writeFile($realPath, $baselineLoaderData);
7983

8084
$outputInfo[$realPath] = null;

src/Handler/BaselineHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ public function encodeBaseline(string $comment, array $errors, string $indent):
2121
/**
2222
* @param list<string> $filePaths
2323
*/
24-
public function encodeBaselineLoader(array $filePaths, string $indent): string;
24+
public function encodeBaselineLoader(string $comment, array $filePaths, string $indent): string;
2525

2626
}

src/Handler/NeonBaselineHandler.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ public function encodeBaseline(string $comment, array $errors, string $indent):
3535
return $prefix . NeonHelper::encode(['parameters' => ['ignoreErrors' => $errors]], $indent);
3636
}
3737

38-
public function encodeBaselineLoader(array $filePaths, string $indent): string
38+
public function encodeBaselineLoader(string $comment, array $filePaths, string $indent): string
3939
{
40-
return NeonHelper::encode(['includes' => $filePaths], $indent);
40+
$prefix = "# $comment\n";
41+
return $prefix . NeonHelper::encode(['includes' => $filePaths], $indent);
4142
}
4243

4344
}

src/Handler/PhpBaselineHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ public function encodeBaseline(string $comment, array $errors, string $indent):
5252
return $php;
5353
}
5454

55-
public function encodeBaselineLoader(array $filePaths, string $indent): string
55+
public function encodeBaselineLoader(string $comment, array $filePaths, string $indent): string
5656
{
5757
$php = "<?php declare(strict_types = 1);\n\n";
58+
$php .= "// $comment\n";
5859
$php .= "return ['includes' => [\n";
5960

6061
foreach ($filePaths as $filePath) {

tests/Rule/data/baselines-neon/loader.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# total 4 errors
12
includes:
23
- another.identifier.neon
34
- missing-identifier.neon

tests/Rule/data/baselines-php/loader.php

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

3+
// total 4 errors
34
return ['includes' => [
45
__DIR__ . '/another.identifier.php',
56
__DIR__ . '/missing-identifier.php',

0 commit comments

Comments
 (0)