Skip to content

Commit d5b110c

Browse files
authored
Autofix: fix removal + multierror (#10)
* Autofix: fix removal + multierror * Fix plural
1 parent 0b6e0ec commit d5b110c

File tree

4 files changed

+73
-14
lines changed

4 files changed

+73
-14
lines changed

src/RuleTestCase.php

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use function array_filter;
1010
use function array_map;
1111
use function array_values;
12+
use function count;
1213
use function explode;
1314
use function file_get_contents;
1415
use function file_put_contents;
@@ -50,8 +51,10 @@ protected function analyzeFiles(
5051
$this->autofix($file, array_values($fileErrors));
5152
}
5253

54+
$plural = count($files) > 1 ? 's' : '';
55+
$were = count($files) > 1 ? 'were' : 'was';
5356
$filesStr = implode(', ', $files);
54-
self::fail("Files {$filesStr} were autofixed. This setup should never remain in the codebase.");
57+
self::fail("File{$plural} {$filesStr} {$were} autofixed. This setup should never remain in the codebase.");
5558
}
5659

5760
foreach ($files as $file) {
@@ -136,24 +139,30 @@ private function autofix(
136139
throw new LogicException('Error without line number: ' . $analyserError->getMessage());
137140
}
138141

139-
$errorsByLines[$line] = $analyserError;
142+
$errorsByLines[$line][] = $analyserError;
140143
}
141144

142145
$fileLines = $this->getFileLines($file);
143146

144147
foreach ($fileLines as $line => &$row) {
145-
if (!isset($errorsByLines[$line + 1])) {
146-
continue;
147-
}
148-
149-
$errorCommentPattern = '~ ?//.*$~';
150-
$errorMessage = $errorsByLines[$line + 1]->getMessage();
151-
$errorComment = ' // error: ' . $errorMessage;
152-
153-
if (preg_match($errorCommentPattern, $row) === 1) {
154-
$row = preg_replace($errorCommentPattern, $errorComment, $row);
155-
} else {
156-
$row .= $errorComment;
148+
$errorCommentPattern = '~ ?// error:.*$~';
149+
150+
if (isset($errorsByLines[$line + 1])) {
151+
// Line has errors - add or update error comments
152+
$errorComments = '';
153+
154+
foreach ($errorsByLines[$line + 1] as $error) {
155+
$errorComments .= ' // error: ' . $error->getMessage();
156+
}
157+
158+
if (preg_match($errorCommentPattern, $row) === 1) {
159+
$row = preg_replace($errorCommentPattern, $errorComments, $row);
160+
} else {
161+
$row .= $errorComments;
162+
}
163+
} elseif (preg_match($errorCommentPattern, $row) === 1) {
164+
// Line has no error but has an error comment - remove it
165+
$row = preg_replace($errorCommentPattern, '', $row);
157166
}
158167
}
159168

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace DisallowDivisionByLiteralZeroRule;
4+
5+
function testDivisionAutofix(): void
6+
{
7+
1 / 0; // error: Division by literal zero is not allowed
8+
1 / 0; // error: Division by literal zero is not allowed
9+
1 / 0; // error: Division by literal zero is not allowed
10+
1 / 1;
11+
1 / 1;
12+
13+
($a / 0) + (5 / 0); // error: Division by literal zero is not allowed // error: Division by literal zero is not allowed
14+
($a / 0) + (5 / 0); // error: Division by literal zero is not allowed // error: Division by literal zero is not allowed
15+
($a / 0) + (5 / 0); // error: Division by literal zero is not allowed // error: Division by literal zero is not allowed
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace DisallowDivisionByLiteralZeroRule;
4+
5+
function testDivisionAutofix(): void
6+
{
7+
1 / 0; // error: Division by literal zero is not allowed // error: Division by literal zero is not allowed
8+
1 / 0; // error: Division by literal zero is not allowed
9+
1 / 0;
10+
1 / 1; // error: Division by literal zero is not allowed
11+
1 / 1; // error: Division by literal zero is not allowed // error: Division by literal zero is not allowed
12+
13+
($a / 0) + (5 / 0); // error: Division by literal zero is not allowed // error: Division by literal zero is not allowed
14+
($a / 0) + (5 / 0); // error: Division by literal zero is not allowed
15+
($a / 0) + (5 / 0);
16+
}

tests/RuleTestCaseTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ShipMonkTests\PHPStanDev;
44

55
use PHPStan\Rules\Rule;
6+
use PHPUnit\Framework\AssertionFailedError;
67
use ShipMonk\PHPStanDev\RuleTestCase;
78
use ShipMonkTests\PHPStanDev\Rule\Data\DisallowDivisionByLiteralZeroRule\DisallowDivisionByLiteralZeroRule;
89

@@ -35,4 +36,21 @@ public function testMultipleErrorsOnSameLine(): void
3536
$this->analyzeFiles([$testFile]);
3637
}
3738

39+
public function testAutofix(): void
40+
{
41+
$expectedFile = __DIR__ . '/Rule/Data/DisallowDivisionByLiteralZeroRule/autofix.expected.php';
42+
$testFile = __DIR__ . '/Rule/Data/DisallowDivisionByLiteralZeroRule/autofix.php';
43+
$tmpFile = sys_get_temp_dir() . '/autofix.php';
44+
copy($testFile, $tmpFile);
45+
46+
try {
47+
$this->analyzeFiles([$tmpFile], true);
48+
self::fail('Autofix should have thrown an exception');
49+
} catch (AssertionFailedError $e) { // @phpstan-ignore catch.internalClass
50+
self::assertStringContainsString('autofixed', $e->getMessage());
51+
}
52+
53+
self::assertFileEquals($expectedFile, $tmpFile);
54+
}
55+
3856
}

0 commit comments

Comments
 (0)