diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000000..3fd3624ba9 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +tests/PHPStan/Command/ErrorFormatter/data/WindowsNewlines.php eol=crlf diff --git a/src/Analyser/IgnoredError.php b/src/Analyser/IgnoredError.php index 347c75339c..acf1bfb7f5 100644 --- a/src/Analyser/IgnoredError.php +++ b/src/Analyser/IgnoredError.php @@ -48,10 +48,15 @@ public static function shouldIgnore( ?string $path ): bool { + // normalize newlines to allow working with ignore-patterns independent of used OS newline-format + $errorMessage = $error->getMessage(); + $errorMessage = str_replace(['\r\n', '\r'], '\n', $errorMessage); + $ignoredErrorPattern = str_replace(['\r\n', '\r'], '\n', $ignoredErrorPattern); + if ($path !== null) { $fileExcluder = new FileExcluder($fileHelper, [$path]); - if (\Nette\Utils\Strings::match($error->getMessage(), $ignoredErrorPattern) === null) { + if (\Nette\Utils\Strings::match($errorMessage, $ignoredErrorPattern) === null) { return false; } @@ -63,7 +68,7 @@ public static function shouldIgnore( return $isExcluded; } - return \Nette\Utils\Strings::match($error->getMessage(), $ignoredErrorPattern) !== null; + return \Nette\Utils\Strings::match($errorMessage, $ignoredErrorPattern) !== null; } } diff --git a/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php b/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php index 11caca3cac..d12a2109cf 100644 --- a/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php +++ b/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php @@ -55,6 +55,9 @@ public function formatErrors( } foreach ($fileErrorsCounts as $message => $count) { + // normalize newlines to allow working with ignore-patterns independent of used OS newline-format + $message = str_replace(['\r\n', '\r'], '\n', $message); + $errorsToOutput[] = [ 'message' => '#^' . preg_quote($message, '#') . '$#', 'count' => $count, diff --git a/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterIntegrationTest.php b/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterIntegrationTest.php index f7e92f816f..dacb651584 100644 --- a/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterIntegrationTest.php +++ b/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterIntegrationTest.php @@ -15,8 +15,8 @@ public function testErrorWithTrait(): void { $output = $this->runPhpStan(null); $errors = Json::decode($output, Json::FORCE_ARRAY); - $this->assertSame(4, array_sum($errors['totals'])); - $this->assertCount(4, $errors['files']); + $this->assertSame(7, array_sum($errors['totals'])); + $this->assertCount(5, $errors['files']); } public function testGenerateBaselineAndRunAgainWithIt(): void diff --git a/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php b/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php index 00e17eaf6a..aacaa72d95 100644 --- a/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php +++ b/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php @@ -149,4 +149,32 @@ public function testFormatErrorMessagesRegexEscape(): void ); } + public function testFormatErrorMessagesNormalizedNewlines(): void + { + $formatter = new BaselineNeonErrorFormatter(new SimpleRelativePathHelper(self::DIRECTORY_PATH)); + + $result = new AnalysisResult( + [new Error('Error message\nwith\r\ndifferent\rnewlines', 'Testfile')], + ['Error message\nwith\r\ndifferent\rnewlines'], + false, + false, + null + ); + $formatter->formatErrors( + $result, + $this->getErrorConsoleStyle() + ); + + self::assertSame( + trim(Neon::encode(['parameters' => ['ignoreErrors' => [ + [ + 'message' => '#^Error message\\\\nwith\\\\ndifferent\\\\nnewlines$#', + 'count' => 1, + 'path' => 'Testfile', + ], + ]]], Neon::BLOCK)), + trim($this->getOutputContent()) + ); + } + } diff --git a/tests/PHPStan/Command/ErrorFormatter/data/WindowsNewlines.php b/tests/PHPStan/Command/ErrorFormatter/data/WindowsNewlines.php new file mode 100644 index 0000000000..adea9777bb --- /dev/null +++ b/tests/PHPStan/Command/ErrorFormatter/data/WindowsNewlines.php @@ -0,0 +1,16 @@ +