Skip to content

Commit dd16f83

Browse files
committed
Autofix: fix removal + multierror
1 parent 0b6e0ec commit dd16f83

File tree

4 files changed

+69
-13
lines changed

4 files changed

+69
-13
lines changed

src/RuleTestCase.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -136,24 +136,30 @@ private function autofix(
136136
throw new LogicException('Error without line number: ' . $analyserError->getMessage());
137137
}
138138

139-
$errorsByLines[$line] = $analyserError;
139+
$errorsByLines[$line][] = $analyserError;
140140
}
141141

142142
$fileLines = $this->getFileLines($file);
143143

144144
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;
145+
$errorCommentPattern = '~ ?// error:.*$~';
146+
147+
if (isset($errorsByLines[$line + 1])) {
148+
// Line has errors - add or update error comments
149+
$errorComments = '';
150+
151+
foreach ($errorsByLines[$line + 1] as $error) {
152+
$errorComments .= ' // error: ' . $error->getMessage();
153+
}
154+
155+
if (preg_match($errorCommentPattern, $row) === 1) {
156+
$row = preg_replace($errorCommentPattern, $errorComments, $row);
157+
} else {
158+
$row .= $errorComments;
159+
}
160+
} elseif (preg_match($errorCommentPattern, $row) === 1) {
161+
// Line has no error but has an error comment - remove it
162+
$row = preg_replace($errorCommentPattern, '', $row);
157163
}
158164
}
159165

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)