|
2 | 2 |
|
3 | 3 | namespace ShipMonk\PHPStan; |
4 | 4 |
|
5 | | -use LogicException; |
6 | | -use PHPStan\Analyser\Error; |
7 | 5 | use PHPStan\Rules\Rule; |
8 | | -use PHPStan\Testing\RuleTestCase as OriginalRuleTestCase; |
9 | | -use function array_values; |
10 | | -use function explode; |
11 | | -use function file_get_contents; |
12 | | -use function file_put_contents; |
13 | | -use function implode; |
14 | | -use function ksort; |
15 | | -use function preg_match; |
16 | | -use function preg_match_all; |
17 | | -use function preg_replace; |
18 | | -use function sprintf; |
19 | | -use function trim; |
20 | | -use function uniqid; |
| 6 | +use ShipMonk\PHPStanDev\RuleTestCase as ShipMonkDevRuleTestCase; |
21 | 7 |
|
22 | 8 | /** |
23 | 9 | * @template TRule of Rule |
24 | | - * @extends OriginalRuleTestCase<TRule> |
| 10 | + * @extends ShipMonkDevRuleTestCase<TRule> |
25 | 11 | */ |
26 | | -abstract class RuleTestCase extends OriginalRuleTestCase |
| 12 | +abstract class RuleTestCase extends ShipMonkDevRuleTestCase |
27 | 13 | { |
28 | 14 |
|
29 | | - protected function analyseFile( |
30 | | - string $file, |
31 | | - bool $autofix = false |
32 | | - ): void |
33 | | - { |
34 | | - $analyserErrors = $this->gatherAnalyserErrors([$file]); |
35 | | - |
36 | | - if ($autofix === true) { |
37 | | - $this->autofix($file, $analyserErrors); |
38 | | - self::fail("File $file was autofixed. This setup should never remain in the codebase."); |
39 | | - } |
40 | | - |
41 | | - $actualErrors = $this->processActualErrors($analyserErrors); |
42 | | - $expectedErrors = $this->parseExpectedErrors($file); |
43 | | - |
44 | | - self::assertSame( |
45 | | - implode("\n", $expectedErrors) . "\n", |
46 | | - implode("\n", $actualErrors) . "\n", |
47 | | - ); |
48 | | - } |
49 | | - |
50 | | - /** |
51 | | - * @param list<Error> $actualErrors |
52 | | - * @return list<string> |
53 | | - */ |
54 | | - protected function processActualErrors(array $actualErrors): array |
55 | | - { |
56 | | - $resultToAssert = []; |
57 | | - |
58 | | - foreach ($actualErrors as $error) { |
59 | | - $usedLine = $error->getLine() ?? -1; |
60 | | - $key = sprintf('%04d', $usedLine) . '-' . uniqid(); |
61 | | - $resultToAssert[$key] = $this->formatErrorForAssert($error->getMessage(), $usedLine); |
62 | | - |
63 | | - self::assertNotNull($error->getIdentifier(), "Missing error identifier for error: {$error->getMessage()}"); |
64 | | - self::assertStringStartsWith('shipmonk.', $error->getIdentifier(), "Unexpected error identifier for: {$error->getMessage()}"); |
65 | | - } |
66 | | - |
67 | | - ksort($resultToAssert); |
68 | | - |
69 | | - return array_values($resultToAssert); |
70 | | - } |
71 | | - |
72 | | - /** |
73 | | - * @return list<string> |
74 | | - */ |
75 | | - private function parseExpectedErrors(string $file): array |
76 | | - { |
77 | | - $fileLines = $this->getFileLines($file); |
78 | | - $expectedErrors = []; |
79 | | - |
80 | | - foreach ($fileLines as $line => $row) { |
81 | | - /** @var array{0: list<string>, 1: list<non-empty-string>} $matches */ |
82 | | - $matched = preg_match_all('#// error:(.+)#', $row, $matches); |
83 | | - |
84 | | - if ($matched === false) { |
85 | | - throw new LogicException('Error while matching errors'); |
86 | | - } |
87 | | - |
88 | | - if ($matched === 0) { |
89 | | - continue; |
90 | | - } |
91 | | - |
92 | | - foreach ($matches[1] as $error) { |
93 | | - $actualLine = $line + 1; |
94 | | - $key = sprintf('%04d', $actualLine) . '-' . uniqid(); |
95 | | - $expectedErrors[$key] = $this->formatErrorForAssert(trim($error), $actualLine); |
96 | | - } |
97 | | - } |
98 | | - |
99 | | - ksort($expectedErrors); |
100 | | - |
101 | | - return array_values($expectedErrors); |
102 | | - } |
103 | | - |
104 | | - private function formatErrorForAssert( |
105 | | - string $message, |
106 | | - int $line |
107 | | - ): string |
108 | | - { |
109 | | - return sprintf('%02d: %s', $line, $message); |
110 | | - } |
111 | | - |
112 | | - /** |
113 | | - * @param list<Error> $analyserErrors |
114 | | - */ |
115 | | - private function autofix( |
116 | | - string $file, |
117 | | - array $analyserErrors |
118 | | - ): void |
119 | | - { |
120 | | - $errorsByLines = []; |
121 | | - |
122 | | - foreach ($analyserErrors as $analyserError) { |
123 | | - $line = $analyserError->getLine(); |
124 | | - |
125 | | - if ($line === null) { |
126 | | - throw new LogicException('Error without line number: ' . $analyserError->getMessage()); |
127 | | - } |
128 | | - |
129 | | - $errorsByLines[$line] = $analyserError; |
130 | | - } |
131 | | - |
132 | | - $fileLines = $this->getFileLines($file); |
133 | | - |
134 | | - foreach ($fileLines as $line => &$row) { |
135 | | - if (!isset($errorsByLines[$line + 1])) { |
136 | | - continue; |
137 | | - } |
138 | | - |
139 | | - $errorCommentPattern = '~ ?//.*$~'; |
140 | | - $errorMessage = $errorsByLines[$line + 1]->getMessage(); |
141 | | - $errorComment = ' // error: ' . $errorMessage; |
142 | | - |
143 | | - if (preg_match($errorCommentPattern, $row) === 1) { |
144 | | - $row = preg_replace($errorCommentPattern, $errorComment, $row); |
145 | | - } else { |
146 | | - $row .= $errorComment; |
147 | | - } |
148 | | - } |
149 | | - |
150 | | - file_put_contents($file, implode("\n", $fileLines)); |
151 | | - } |
152 | | - |
153 | | - /** |
154 | | - * @return list<string> |
155 | | - */ |
156 | | - private function getFileLines(string $file): array |
157 | | - { |
158 | | - $fileData = file_get_contents($file); |
159 | | - |
160 | | - if ($fileData === false) { |
161 | | - throw new LogicException('Error while reading data from ' . $file); |
162 | | - } |
163 | | - |
164 | | - return explode("\n", $fileData); |
| 15 | + public function analyseFile(string $file, bool $autofix = false): void { |
| 16 | + $this->analyzeFiles([$file], $autofix); |
165 | 17 | } |
166 | 18 |
|
167 | 19 | } |
0 commit comments