From dd16f830c3dda668dc773b80dfdb65b5cbe394e1 Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Wed, 3 Dec 2025 17:26:44 +0100 Subject: [PATCH 1/2] Autofix: fix removal + multierror --- src/RuleTestCase.php | 32 +++++++++++-------- .../autofix.expected.php | 16 ++++++++++ .../autofix.php | 16 ++++++++++ tests/RuleTestCaseTest.php | 18 +++++++++++ 4 files changed, 69 insertions(+), 13 deletions(-) create mode 100644 tests/Rule/Data/DisallowDivisionByLiteralZeroRule/autofix.expected.php create mode 100644 tests/Rule/Data/DisallowDivisionByLiteralZeroRule/autofix.php diff --git a/src/RuleTestCase.php b/src/RuleTestCase.php index 5d9ba69..85d11e4 100644 --- a/src/RuleTestCase.php +++ b/src/RuleTestCase.php @@ -136,24 +136,30 @@ private function autofix( throw new LogicException('Error without line number: ' . $analyserError->getMessage()); } - $errorsByLines[$line] = $analyserError; + $errorsByLines[$line][] = $analyserError; } $fileLines = $this->getFileLines($file); foreach ($fileLines as $line => &$row) { - if (!isset($errorsByLines[$line + 1])) { - continue; - } - - $errorCommentPattern = '~ ?//.*$~'; - $errorMessage = $errorsByLines[$line + 1]->getMessage(); - $errorComment = ' // error: ' . $errorMessage; - - if (preg_match($errorCommentPattern, $row) === 1) { - $row = preg_replace($errorCommentPattern, $errorComment, $row); - } else { - $row .= $errorComment; + $errorCommentPattern = '~ ?// error:.*$~'; + + if (isset($errorsByLines[$line + 1])) { + // Line has errors - add or update error comments + $errorComments = ''; + + foreach ($errorsByLines[$line + 1] as $error) { + $errorComments .= ' // error: ' . $error->getMessage(); + } + + if (preg_match($errorCommentPattern, $row) === 1) { + $row = preg_replace($errorCommentPattern, $errorComments, $row); + } else { + $row .= $errorComments; + } + } elseif (preg_match($errorCommentPattern, $row) === 1) { + // Line has no error but has an error comment - remove it + $row = preg_replace($errorCommentPattern, '', $row); } } diff --git a/tests/Rule/Data/DisallowDivisionByLiteralZeroRule/autofix.expected.php b/tests/Rule/Data/DisallowDivisionByLiteralZeroRule/autofix.expected.php new file mode 100644 index 0000000..7490aee --- /dev/null +++ b/tests/Rule/Data/DisallowDivisionByLiteralZeroRule/autofix.expected.php @@ -0,0 +1,16 @@ +analyzeFiles([$testFile]); } + public function testAutofix(): void + { + $expectedFile = __DIR__ . '/Rule/Data/DisallowDivisionByLiteralZeroRule/autofix.expected.php'; + $testFile = __DIR__ . '/Rule/Data/DisallowDivisionByLiteralZeroRule/autofix.php'; + $tmpFile = sys_get_temp_dir() . '/autofix.php'; + copy($testFile, $tmpFile); + + try { + $this->analyzeFiles([$tmpFile], true); + self::fail('Autofix should have thrown an exception'); + } catch (AssertionFailedError $e) { // @phpstan-ignore catch.internalClass + self::assertStringContainsString('autofixed', $e->getMessage()); + } + + self::assertFileEquals($expectedFile, $tmpFile); + } + } From 04429d0f0972c960304f014c81737a67c1b8621d Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Wed, 3 Dec 2025 18:54:57 +0100 Subject: [PATCH 2/2] Fix plural --- src/RuleTestCase.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/RuleTestCase.php b/src/RuleTestCase.php index 85d11e4..14b93e6 100644 --- a/src/RuleTestCase.php +++ b/src/RuleTestCase.php @@ -9,6 +9,7 @@ use function array_filter; use function array_map; use function array_values; +use function count; use function explode; use function file_get_contents; use function file_put_contents; @@ -50,8 +51,10 @@ protected function analyzeFiles( $this->autofix($file, array_values($fileErrors)); } + $plural = count($files) > 1 ? 's' : ''; + $were = count($files) > 1 ? 'were' : 'was'; $filesStr = implode(', ', $files); - self::fail("Files {$filesStr} were autofixed. This setup should never remain in the codebase."); + self::fail("File{$plural} {$filesStr} {$were} autofixed. This setup should never remain in the codebase."); } foreach ($files as $file) {