Skip to content

Commit 84ee6e3

Browse files
authored
Add CLI option to remove error counts from baselines (#39)
* feat: add option to remove error counts from baselines * test: Add tests for new option
1 parent 2b3d016 commit 84ee6e3

15 files changed

+119
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ Prepare composer script to simplify generation:
102102

103103
## Cli options
104104
- ``--tabs`` to use tabs as indents in generated neon files
105+
- ``--no-error-count`` to remove errors count in generated files
105106

106107
## PHP Baseline
107108
- If the loader file extension is php, the generated files will be php files as well

bin/split-phpstan-baseline

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ function error(string $message): void
2727
}
2828

2929
/** @var int $restIndex */
30-
$providedOptions = getopt('', ['tabs'], $restIndex);
30+
$providedOptions = getopt('', ['tabs', 'no-error-count'], $restIndex);
3131
$args = array_slice($argv, $restIndex);
3232

3333
$loaderFile = $args[0] ?? null;
3434
$indent = isset($providedOptions['tabs']) || in_array('--tabs', $args, true)
3535
? "\t"
3636
: ' ';
37+
$includeCount = !(isset($providedOptions['no-error-count']) || in_array('--no-error-count', $args, true));
3738

3839
if ($loaderFile === null) {
3940
error(
@@ -47,7 +48,7 @@ if (!is_file($loaderFile)) {
4748
}
4849

4950
try {
50-
$splitter = new BaselineSplitter($indent);
51+
$splitter = new BaselineSplitter($indent, $includeCount);
5152
$writtenBaselines = $splitter->split($loaderFile);
5253

5354
foreach ($writtenBaselines as $writtenBaseline => $errorsCount) {

src/BaselineSplitter.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ class BaselineSplitter
2020

2121
private string $indent;
2222

23-
public function __construct(string $indent)
23+
private bool $includeCount;
24+
25+
public function __construct(string $indent, bool $includeCount)
2426
{
2527
$this->indent = $indent;
28+
$this->includeCount = $includeCount;
2629
}
2730

2831
/**
@@ -70,14 +73,14 @@ public function split(string $loaderFilePath): array
7073
$baselineFiles[] = $fileName;
7174

7275
$plural = $errorsCount === 1 ? '' : 's';
73-
$prefix = "total $errorsCount error$plural";
76+
$prefix = $this->includeCount ? "total $errorsCount error$plural" : null;
7477

7578
$encodedData = $handler->encodeBaseline($prefix, $errors, $this->indent);
7679
$this->writeFile($filePath, $encodedData);
7780
}
7881

7982
$plural = $totalErrorCount === 1 ? '' : 's';
80-
$prefix = "total $totalErrorCount error$plural";
83+
$prefix = $this->includeCount ? "total $totalErrorCount error$plural" : null;
8184
$baselineLoaderData = $handler->encodeBaselineLoader($prefix, $baselineFiles, $this->indent);
8285
$this->writeFile($realPath, $baselineLoaderData);
8386

src/Handler/BaselineHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ public function decodeBaseline(string $filepath): array;
1616
/**
1717
* @param list<array{message: string, count: int, path: string}> $errors
1818
*/
19-
public function encodeBaseline(string $comment, array $errors, string $indent): string;
19+
public function encodeBaseline(?string $comment, array $errors, string $indent): string;
2020

2121
/**
2222
* @param list<string> $filePaths
2323
*/
24-
public function encodeBaselineLoader(string $comment, array $filePaths, string $indent): string;
24+
public function encodeBaselineLoader(?string $comment, array $filePaths, string $indent): string;
2525

2626
}

src/Handler/NeonBaselineHandler.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ public function decodeBaseline(string $filepath): array
2929
}
3030
}
3131

32-
public function encodeBaseline(string $comment, array $errors, string $indent): string
32+
public function encodeBaseline(?string $comment, array $errors, string $indent): string
3333
{
34-
$prefix = "# $comment\n\n";
34+
$prefix = $comment !== null ? "# $comment\n\n" : '';
3535
return $prefix . NeonHelper::encode(['parameters' => ['ignoreErrors' => $errors]], $indent);
3636
}
3737

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

src/Handler/PhpBaselineHandler.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,15 @@ public function decodeBaseline(string $filepath): array
2828
}
2929
}
3030

31-
public function encodeBaseline(string $comment, array $errors, string $indent): string
31+
public function encodeBaseline(?string $comment, array $errors, string $indent): string
3232
{
3333
$php = '<?php declare(strict_types = 1);';
34-
$php .= "\n\n";
35-
$php .= "// $comment";
34+
35+
if ($comment !== null) {
36+
$php .= "\n\n";
37+
$php .= "// $comment";
38+
}
39+
3640
$php .= "\n\n";
3741
$php .= "\$ignoreErrors = [];\n";
3842

@@ -52,10 +56,14 @@ public function encodeBaseline(string $comment, array $errors, string $indent):
5256
return $php;
5357
}
5458

55-
public function encodeBaselineLoader(string $comment, array $filePaths, string $indent): string
59+
public function encodeBaselineLoader(?string $comment, array $filePaths, string $indent): string
5660
{
5761
$php = "<?php declare(strict_types = 1);\n\n";
58-
$php .= "// $comment\n";
62+
63+
if ($comment !== null) {
64+
$php .= "// $comment\n";
65+
}
66+
5967
$php .= "return ['includes' => [\n";
6068

6169
foreach ($filePaths as $filePath) {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
parameters:
2+
ignoreErrors:
3+
-
4+
message: '#^Error to escape ''\#$#'
5+
count: 1
6+
path: ../app/config.php
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
includes:
2+
- another.identifier.neon
3+
- missing-identifier.neon
4+
- sample.identifier.neon
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
parameters:
2+
ignoreErrors:
3+
-
4+
message: '#^Error 3$#'
5+
count: 1
6+
path: ../app/index.php
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
parameters:
2+
ignoreErrors:
3+
-
4+
message: '#^Error simple$#'
5+
count: 2
6+
path: ../app/file.php

0 commit comments

Comments
 (0)