Skip to content

Commit 3276bae

Browse files
JanTvrdikclaude
andcommitted
Refactor analyseFile to analyzeFiles accepting multiple files
- Rename analyseFile() to analyzeFiles() - Change signature to accept array of files only - Process errors per file with proper filtering - Update all references in tests and documentation - Add better error messages showing which file has mismatches 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent e644310 commit 3276bae

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class YourRuleTest extends RuleTestCase
3434

3535
public function testRule(): void
3636
{
37-
$this->analyseFile(__DIR__ . '/Data/YourRule/code.php');
37+
$this->analyzeFiles([__DIR__ . '/Data/YourRule/code.php']);
3838
}
3939
}
4040
```
@@ -82,7 +82,7 @@ During development, automatically generate error comments:
8282
public function testRule(): void
8383
{
8484
// Set to true temporarily to generate error comments
85-
$this->analyseFile(__DIR__ . '/Data/code.php', autofix: true);
85+
$this->analyzeFiles([__DIR__ . '/Data/code.php'], autofix: true);
8686
}
8787
```
8888

@@ -110,13 +110,13 @@ class ComplexRuleTest extends RuleTestCase
110110

111111
public function testDefault(): void
112112
{
113-
$this->analyseFile(__DIR__ . '/Data/ComplexRule/default.php');
113+
$this->analyzeFiles([__DIR__ . '/Data/ComplexRule/default.php']);
114114
}
115115

116116
public function testStrict(): void
117117
{
118118
$this->strictMode = true;
119-
$this->analyseFile(__DIR__ . '/Data/ComplexRule/strict.php');
119+
$this->analyzeFiles([__DIR__ . '/Data/ComplexRule/strict.php']);
120120
}
121121
}
122122
```
@@ -127,7 +127,7 @@ class ComplexRuleTest extends RuleTestCase
127127
public function testPhp82Features(): void
128128
{
129129
$this->phpVersion = $this->createPhpVersion(80_200);
130-
$this->analyseFile(__DIR__ . '/Data/Rule/php82-features.php');
130+
$this->analyzeFiles([__DIR__ . '/Data/Rule/php82-features.php']);
131131
}
132132
```
133133

src/RuleTestCase.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPStan\Analyser\Error;
77
use PHPStan\Rules\Rule;
88
use PHPStan\Testing\RuleTestCase as OriginalRuleTestCase;
9+
use function array_filter;
910
use function array_values;
1011
use function explode;
1112
use function file_get_contents;
@@ -15,6 +16,7 @@
1516
use function preg_match;
1617
use function preg_match_all;
1718
use function preg_replace;
19+
use function sort;
1820
use function sprintf;
1921
use function trim;
2022
use function uniqid;
@@ -26,22 +28,36 @@
2628
abstract class RuleTestCase extends OriginalRuleTestCase
2729
{
2830

29-
protected function analyseFile(string $file, bool $autofix = false): void
31+
/**
32+
* @param list<string> $files
33+
*/
34+
protected function analyzeFiles(array $files, bool $autofix = false): void
3035
{
31-
$analyserErrors = $this->gatherAnalyserErrors([$file]);
36+
sort($files);
37+
38+
$analyserErrors = $this->gatherAnalyserErrors($files);
3239

3340
if ($autofix) {
34-
$this->autofix($file, $analyserErrors);
35-
self::fail("File {$file} was autofixed. This setup should never remain in the codebase.");
41+
foreach ($files as $file) {
42+
$fileErrors = array_filter($analyserErrors, static fn(Error $error): bool => $error->getFile() === $file);
43+
$this->autofix($file, array_values($fileErrors));
44+
}
45+
46+
$filesStr = implode(', ', $files);
47+
self::fail("Files {$filesStr} were autofixed. This setup should never remain in the codebase.");
3648
}
3749

38-
$actualErrors = $this->processActualErrors($analyserErrors);
39-
$expectedErrors = $this->parseExpectedErrors($file);
50+
foreach ($files as $file) {
51+
$fileErrors = array_filter($analyserErrors, static fn(Error $error): bool => $error->getFile() === $file);
52+
$actualErrors = $this->processActualErrors(array_values($fileErrors));
53+
$expectedErrors = $this->parseExpectedErrors($file);
4054

41-
self::assertSame(
42-
implode("\n", $expectedErrors) . "\n",
43-
implode("\n", $actualErrors) . "\n",
44-
);
55+
self::assertSame(
56+
implode("\n", $expectedErrors) . "\n",
57+
implode("\n", $actualErrors) . "\n",
58+
"Errors in file {$file} do not match",
59+
);
60+
}
4561
}
4662

4763
/**

tests/RuleTestCaseTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ protected function getRule(): Rule
1919

2020
public function testRule(): void
2121
{
22-
$this->analyseFile(__DIR__ . '/Rule/Data/DisallowDivisionByLiteralZeroRule/code.php');
22+
$this->analyzeFiles([__DIR__ . '/Rule/Data/DisallowDivisionByLiteralZeroRule/code.php']);
2323
}
2424

2525
public function testMultipleErrorsOnSameLine(): void
2626
{
2727
// Create a dedicated test file for multiple errors demonstration
2828
$testFile = __DIR__ . '/Rule/Data/DisallowDivisionByLiteralZeroRule/multiple-errors.php';
2929

30-
$this->analyseFile($testFile);
30+
$this->analyzeFiles([$testFile]);
3131
}
3232

3333
}

0 commit comments

Comments
 (0)