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

+1
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

+3-2
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

+6-3
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

+2-2
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

+4-4
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

+13-5
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) {
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
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
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
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types = 1);
2+
3+
$ignoreErrors = [];
4+
$ignoreErrors[] = [
5+
'message' => '#^Error to escape \'\\#$#',
6+
'count' => 1,
7+
'path' => __DIR__ . '/../app/config.php',
8+
];
9+
10+
return ['parameters' => ['ignoreErrors' => $ignoreErrors]];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php declare(strict_types = 1);
2+
3+
return ['includes' => [
4+
__DIR__ . '/another.identifier.php',
5+
__DIR__ . '/missing-identifier.php',
6+
__DIR__ . '/sample.identifier.php',
7+
]];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types = 1);
2+
3+
$ignoreErrors = [];
4+
$ignoreErrors[] = [
5+
'message' => '#^Error 3$#',
6+
'count' => 1,
7+
'path' => __DIR__ . '/../app/index.php',
8+
];
9+
10+
return ['parameters' => ['ignoreErrors' => $ignoreErrors]];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types = 1);
2+
3+
$ignoreErrors = [];
4+
$ignoreErrors[] = [
5+
'message' => '#^Error simple$#',
6+
'count' => 2,
7+
'path' => __DIR__ . '/../app/file.php',
8+
];
9+
10+
return ['parameters' => ['ignoreErrors' => $ignoreErrors]];

tests/SplitterTest.php

+31-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,36 @@ public function testBinaryWithPhp(): void
4242
self::assertFileEquals(__DIR__ . '/Rule/data/baselines-php/missing-identifier.php', $fakeRoot . '/baselines/missing-identifier.php');
4343
}
4444

45+
public function testBinaryWithNeonNoErrorCount(): void
46+
{
47+
$fakeRoot = $this->prepareSampleFolder();
48+
$squashed = $this->getSampleErrors();
49+
50+
file_put_contents($fakeRoot . '/baselines/loader.neon', Neon::encode($squashed));
51+
52+
$this->runCommand('php bin/split-phpstan-baseline ' . $fakeRoot . '/baselines/loader.neon --no-error-count', __DIR__ . '/..', 0);
53+
54+
self::assertFileEquals(__DIR__ . '/Rule/data/baselines-neon-no-error-count/loader.neon', $fakeRoot . '/baselines/loader.neon');
55+
self::assertFileEquals(__DIR__ . '/Rule/data/baselines-neon-no-error-count/sample.identifier.neon', $fakeRoot . '/baselines/sample.identifier.neon');
56+
self::assertFileEquals(__DIR__ . '/Rule/data/baselines-neon-no-error-count/another.identifier.neon', $fakeRoot . '/baselines/another.identifier.neon');
57+
self::assertFileEquals(__DIR__ . '/Rule/data/baselines-neon-no-error-count/missing-identifier.neon', $fakeRoot . '/baselines/missing-identifier.neon');
58+
}
59+
60+
public function testBinaryWithPhpNoErrorCount(): void
61+
{
62+
$fakeRoot = $this->prepareSampleFolder();
63+
$squashed = $this->getSampleErrors();
64+
65+
file_put_contents($fakeRoot . '/baselines/loader.php', '<?php return ' . var_export($squashed, true) . ';');
66+
67+
$this->runCommand('php bin/split-phpstan-baseline ' . $fakeRoot . '/baselines/loader.php --no-error-count', __DIR__ . '/..', 0);
68+
69+
self::assertFileEquals(__DIR__ . '/Rule/data/baselines-php-no-error-count/loader.php', $fakeRoot . '/baselines/loader.php');
70+
self::assertFileEquals(__DIR__ . '/Rule/data/baselines-php-no-error-count/sample.identifier.php', $fakeRoot . '/baselines/sample.identifier.php');
71+
self::assertFileEquals(__DIR__ . '/Rule/data/baselines-php-no-error-count/another.identifier.php', $fakeRoot . '/baselines/another.identifier.php');
72+
self::assertFileEquals(__DIR__ . '/Rule/data/baselines-php-no-error-count/missing-identifier.php', $fakeRoot . '/baselines/missing-identifier.php');
73+
}
74+
4575
public function testSplitter(): void
4676
{
4777
$folder = $this->prepareSampleFolder();
@@ -50,7 +80,7 @@ public function testSplitter(): void
5080
$loaderPath = $folder . '/baselines/loader.neon';
5181
file_put_contents($loaderPath, Neon::encode($squashed));
5282

53-
$splitter = new BaselineSplitter("\t");
83+
$splitter = new BaselineSplitter("\t", true);
5484
$written = $splitter->split($loaderPath);
5585

5686
self::assertSame([

0 commit comments

Comments
 (0)