From 8f225e9dd997726e1b96ce424fdfe8d0bd97f438 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 6 Nov 2019 20:55:55 +0100 Subject: [PATCH 01/26] baseline: normalize newlines in error messages --- src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php b/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php index 11caca3cac..66efa61cf8 100644 --- a/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php +++ b/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php @@ -55,6 +55,7 @@ public function formatErrors( } foreach ($fileErrorsCounts as $message => $count) { + $message = preg_replace('/\r\n|\r|\n/', "\r\n", $message); $errorsToOutput[] = [ 'message' => '#^' . preg_quote($message, '#') . '$#', 'count' => $count, From e9099bb09384ca61e75d1123fa4118b0230c2b26 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 6 Nov 2019 20:57:18 +0100 Subject: [PATCH 02/26] Update BaselineNeonErrorFormatter.php --- src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php b/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php index 66efa61cf8..060ba76a1a 100644 --- a/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php +++ b/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php @@ -55,7 +55,7 @@ public function formatErrors( } foreach ($fileErrorsCounts as $message => $count) { - $message = preg_replace('/\r\n|\r|\n/', "\r\n", $message); + $message = preg_replace('/\r\n|\r|\n/', "\n", $message); $errorsToOutput[] = [ 'message' => '#^' . preg_quote($message, '#') . '$#', 'count' => $count, From 95c267e433f9fe903981c549df851edd70cb181f Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 9 Nov 2019 10:35:43 +0100 Subject: [PATCH 03/26] Added testFormatErrorMessagesNormalizedNewlines --- .../BaselineNeonErrorFormatterTest.php | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php b/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php index 00e17eaf6a..445b86d9f8 100644 --- a/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php +++ b/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php @@ -149,4 +149,31 @@ 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()) + ); + } } From decac4f6a2939df8233ef31b0638186ad0bb05e7 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 9 Nov 2019 10:41:45 +0100 Subject: [PATCH 04/26] fix CS --- .../Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php b/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php index 445b86d9f8..5160b4cf0f 100644 --- a/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php +++ b/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php @@ -176,4 +176,5 @@ public function testFormatErrorMessagesNormalizedNewlines(): void trim($this->getOutputContent()) ); } + } From b78c241ae482f1c87e69edbdbe2608693f3a7fe3 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 9 Nov 2019 10:53:34 +0100 Subject: [PATCH 05/26] normalize newlines to allow working with ignore-patterns indepent of use newline-format --- src/Analyser/IgnoredError.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Analyser/IgnoredError.php b/src/Analyser/IgnoredError.php index 347c75339c..0851fc9a4c 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 indepent of use newline-format + $errorMessage = $error->getMessage(); + $errorMessage = preg_replace('/\r\n|\r|\n/', "\n", $errorMessage); + $ignoredErrorPattern = preg_replace('/\r\n|\r|\n/', "\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; } } From 4d6204c4f27356a868135178e8c581310b887c3f Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 9 Nov 2019 11:00:41 +0100 Subject: [PATCH 06/26] added testcase with newlines in phpdoc --- tests/PHPStan/Command/ErrorFormatter/data/Bar.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/PHPStan/Command/ErrorFormatter/data/Bar.php b/tests/PHPStan/Command/ErrorFormatter/data/Bar.php index 330aff4cec..7f096ed2a6 100644 --- a/tests/PHPStan/Command/ErrorFormatter/data/Bar.php +++ b/tests/PHPStan/Command/ErrorFormatter/data/Bar.php @@ -15,4 +15,10 @@ public function doFoo(): array return [['foo']]; } + /** + * @param + * $object + */ + public function phpdocWithNewlines($object) { + } } From accec36d5eedf115be394eb80377e24f1d0b6524 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 9 Nov 2019 11:01:42 +0100 Subject: [PATCH 07/26] added docs for the docs --- tests/PHPStan/Command/ErrorFormatter/data/Bar.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/PHPStan/Command/ErrorFormatter/data/Bar.php b/tests/PHPStan/Command/ErrorFormatter/data/Bar.php index 7f096ed2a6..ea1f1ae1f3 100644 --- a/tests/PHPStan/Command/ErrorFormatter/data/Bar.php +++ b/tests/PHPStan/Command/ErrorFormatter/data/Bar.php @@ -16,6 +16,8 @@ public function doFoo(): array } /** + * The following phpdoc is invalid and should trigger a error message containing newlines. + * * @param * $object */ From fd471108c5bdf6dfaba2627d75c1c1eea78084e4 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 9 Nov 2019 11:09:14 +0100 Subject: [PATCH 08/26] fix typo --- src/Analyser/IgnoredError.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Analyser/IgnoredError.php b/src/Analyser/IgnoredError.php index 0851fc9a4c..3a7de97ec9 100644 --- a/src/Analyser/IgnoredError.php +++ b/src/Analyser/IgnoredError.php @@ -48,7 +48,7 @@ public static function shouldIgnore( ?string $path ): bool { - // normalize newlines to allow working with ignore-patterns indepent of use newline-format + // normalize newlines to allow working with ignore-patterns independent of used OS newline-format $errorMessage = $error->getMessage(); $errorMessage = preg_replace('/\r\n|\r|\n/', "\n", $errorMessage); $ignoredErrorPattern = preg_replace('/\r\n|\r|\n/', "\n", $ignoredErrorPattern); From ee8fcbb2c4d01662027d38e6b856fa4d280d1009 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 10 Nov 2019 11:05:43 +0100 Subject: [PATCH 09/26] added test-file with windows line-endings --- tests/PHPStan/Command/ErrorFormatter/data/Bar.php | 9 --------- .../ErrorFormatter/data/WindowsNewlines.php | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 9 deletions(-) create mode 100644 tests/PHPStan/Command/ErrorFormatter/data/WindowsNewlines.php diff --git a/tests/PHPStan/Command/ErrorFormatter/data/Bar.php b/tests/PHPStan/Command/ErrorFormatter/data/Bar.php index ea1f1ae1f3..1694f262c0 100644 --- a/tests/PHPStan/Command/ErrorFormatter/data/Bar.php +++ b/tests/PHPStan/Command/ErrorFormatter/data/Bar.php @@ -14,13 +14,4 @@ public function doFoo(): array { return [['foo']]; } - - /** - * The following phpdoc is invalid and should trigger a error message containing newlines. - * - * @param - * $object - */ - public function phpdocWithNewlines($object) { - } } diff --git a/tests/PHPStan/Command/ErrorFormatter/data/WindowsNewlines.php b/tests/PHPStan/Command/ErrorFormatter/data/WindowsNewlines.php new file mode 100644 index 0000000000..acc8ad4139 --- /dev/null +++ b/tests/PHPStan/Command/ErrorFormatter/data/WindowsNewlines.php @@ -0,0 +1,15 @@ + Date: Sun, 10 Nov 2019 11:07:00 +0100 Subject: [PATCH 10/26] enforce windows line-endings via .gitattributes --- .gitattributes | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitattributes 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 From af2705b24f54eaef23068f940efeeee582afb734 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 10 Nov 2019 11:09:11 +0100 Subject: [PATCH 11/26] fix CS --- .../Command/ErrorFormatter/data/Bar.php | 1 + .../ErrorFormatter/data/WindowsNewlines.php | 31 ++++++++++--------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/tests/PHPStan/Command/ErrorFormatter/data/Bar.php b/tests/PHPStan/Command/ErrorFormatter/data/Bar.php index 1694f262c0..330aff4cec 100644 --- a/tests/PHPStan/Command/ErrorFormatter/data/Bar.php +++ b/tests/PHPStan/Command/ErrorFormatter/data/Bar.php @@ -14,4 +14,5 @@ public function doFoo(): array { return [['foo']]; } + } diff --git a/tests/PHPStan/Command/ErrorFormatter/data/WindowsNewlines.php b/tests/PHPStan/Command/ErrorFormatter/data/WindowsNewlines.php index acc8ad4139..adea9777bb 100644 --- a/tests/PHPStan/Command/ErrorFormatter/data/WindowsNewlines.php +++ b/tests/PHPStan/Command/ErrorFormatter/data/WindowsNewlines.php @@ -1,15 +1,16 @@ - Date: Sun, 10 Nov 2019 11:31:10 +0100 Subject: [PATCH 12/26] fixed assertion --- .../BaselineNeonErrorFormatterIntegrationTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterIntegrationTest.php b/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterIntegrationTest.php index f7e92f816f..0abeebaf69 100644 --- a/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterIntegrationTest.php +++ b/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterIntegrationTest.php @@ -15,7 +15,7 @@ public function testErrorWithTrait(): void { $output = $this->runPhpStan(null); $errors = Json::decode($output, Json::FORCE_ARRAY); - $this->assertSame(4, array_sum($errors['totals'])); + $this->assertSame(7, array_sum($errors['totals'])); $this->assertCount(4, $errors['files']); } From bc5f53e24432166c0d652c74ac86fe8446facd77 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 10 Nov 2019 11:37:34 +0100 Subject: [PATCH 13/26] fixed another assertion --- .../BaselineNeonErrorFormatterIntegrationTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterIntegrationTest.php b/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterIntegrationTest.php index 0abeebaf69..dacb651584 100644 --- a/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterIntegrationTest.php +++ b/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterIntegrationTest.php @@ -16,7 +16,7 @@ public function testErrorWithTrait(): void $output = $this->runPhpStan(null); $errors = Json::decode($output, Json::FORCE_ARRAY); $this->assertSame(7, array_sum($errors['totals'])); - $this->assertCount(4, $errors['files']); + $this->assertCount(5, $errors['files']); } public function testGenerateBaselineAndRunAgainWithIt(): void From 6d5f4869053315a402cd7c84635852d66f5f1ee2 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 10 Nov 2019 14:04:13 +0100 Subject: [PATCH 14/26] Throw Exception on preg errors --- src/Analyser/IgnoredError.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Analyser/IgnoredError.php b/src/Analyser/IgnoredError.php index 3a7de97ec9..197b635560 100644 --- a/src/Analyser/IgnoredError.php +++ b/src/Analyser/IgnoredError.php @@ -53,6 +53,10 @@ public static function shouldIgnore( $errorMessage = preg_replace('/\r\n|\r|\n/', "\n", $errorMessage); $ignoredErrorPattern = preg_replace('/\r\n|\r|\n/', "\n", $ignoredErrorPattern); + if ($errorMessage === null || $ignoredErrorPattern === null) { + throw new \Exception(preg_last_error()); + } + if ($path !== null) { $fileExcluder = new FileExcluder($fileHelper, [$path]); From dd48780c60829850884932b60f59ce8fc130f068 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 10 Nov 2019 14:05:41 +0100 Subject: [PATCH 15/26] Throw Exception on preg errors --- src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php b/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php index 060ba76a1a..7a8fcb61c9 100644 --- a/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php +++ b/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php @@ -56,6 +56,11 @@ public function formatErrors( foreach ($fileErrorsCounts as $message => $count) { $message = preg_replace('/\r\n|\r|\n/', "\n", $message); + + if ($errorMessage === null || $ignoredErrorPattern === null) { + throw new \Exception(preg_last_error()); + } + $errorsToOutput[] = [ 'message' => '#^' . preg_quote($message, '#') . '$#', 'count' => $count, From 547d3b5433b3cb73cc1b14b96ad6d2e647104e22 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 10 Nov 2019 14:09:13 +0100 Subject: [PATCH 16/26] Fix CS --- src/Analyser/IgnoredError.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Analyser/IgnoredError.php b/src/Analyser/IgnoredError.php index 197b635560..e4db84a69d 100644 --- a/src/Analyser/IgnoredError.php +++ b/src/Analyser/IgnoredError.php @@ -52,10 +52,9 @@ public static function shouldIgnore( $errorMessage = $error->getMessage(); $errorMessage = preg_replace('/\r\n|\r|\n/', "\n", $errorMessage); $ignoredErrorPattern = preg_replace('/\r\n|\r|\n/', "\n", $ignoredErrorPattern); - - if ($errorMessage === null || $ignoredErrorPattern === null) { - throw new \Exception(preg_last_error()); - } + if ($errorMessage === null || $ignoredErrorPattern === null) { + throw new \Exception(preg_last_error()); + } if ($path !== null) { $fileExcluder = new FileExcluder($fileHelper, [$path]); From 33c60ef5d2f11fa0861725de51827cb662fe3cd6 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 10 Nov 2019 14:11:34 +0100 Subject: [PATCH 17/26] Update BaselineNeonErrorFormatter.php --- src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php b/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php index 7a8fcb61c9..7fb6bac9fa 100644 --- a/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php +++ b/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php @@ -56,10 +56,9 @@ public function formatErrors( foreach ($fileErrorsCounts as $message => $count) { $message = preg_replace('/\r\n|\r|\n/', "\n", $message); - - if ($errorMessage === null || $ignoredErrorPattern === null) { - throw new \Exception(preg_last_error()); - } + if ($message === null) { + throw new \Exception(preg_last_error()); + } $errorsToOutput[] = [ 'message' => '#^' . preg_quote($message, '#') . '$#', From 952e2b57bf1bb4c8271cd1530fcab526dcd8d0f6 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 10 Nov 2019 20:06:09 +0100 Subject: [PATCH 18/26] fix CS --- src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php b/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php index 7fb6bac9fa..b9031efd9e 100644 --- a/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php +++ b/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php @@ -56,8 +56,8 @@ public function formatErrors( foreach ($fileErrorsCounts as $message => $count) { $message = preg_replace('/\r\n|\r|\n/', "\n", $message); - if ($message === null) { - throw new \Exception(preg_last_error()); + if ($message === null) { + throw new \Exception(preg_last_error()); } $errorsToOutput[] = [ From d7c2924cee926072c3188595aa1d351e5de64c73 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 10 Nov 2019 20:25:45 +0100 Subject: [PATCH 19/26] fix exception message --- src/Analyser/IgnoredError.php | 2 +- src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Analyser/IgnoredError.php b/src/Analyser/IgnoredError.php index e4db84a69d..a7a593aa0f 100644 --- a/src/Analyser/IgnoredError.php +++ b/src/Analyser/IgnoredError.php @@ -53,7 +53,7 @@ public static function shouldIgnore( $errorMessage = preg_replace('/\r\n|\r|\n/', "\n", $errorMessage); $ignoredErrorPattern = preg_replace('/\r\n|\r|\n/', "\n", $ignoredErrorPattern); if ($errorMessage === null || $ignoredErrorPattern === null) { - throw new \Exception(preg_last_error()); + throw new \Exception('Error while executing regex: '. preg_last_error()); } if ($path !== null) { diff --git a/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php b/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php index b9031efd9e..6d0a718975 100644 --- a/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php +++ b/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php @@ -57,7 +57,7 @@ public function formatErrors( foreach ($fileErrorsCounts as $message => $count) { $message = preg_replace('/\r\n|\r|\n/', "\n", $message); if ($message === null) { - throw new \Exception(preg_last_error()); + throw new \Exception('Error while executing regex: '. preg_last_error()); } $errorsToOutput[] = [ From cf9bde298cd0384bd234a161a00b07a73def29c1 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 10 Nov 2019 20:29:30 +0100 Subject: [PATCH 20/26] fix CS --- src/Analyser/IgnoredError.php | 2 +- src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Analyser/IgnoredError.php b/src/Analyser/IgnoredError.php index a7a593aa0f..000c328955 100644 --- a/src/Analyser/IgnoredError.php +++ b/src/Analyser/IgnoredError.php @@ -53,7 +53,7 @@ public static function shouldIgnore( $errorMessage = preg_replace('/\r\n|\r|\n/', "\n", $errorMessage); $ignoredErrorPattern = preg_replace('/\r\n|\r|\n/', "\n", $ignoredErrorPattern); if ($errorMessage === null || $ignoredErrorPattern === null) { - throw new \Exception('Error while executing regex: '. preg_last_error()); + throw new \Exception('Error while executing regex: ' . preg_last_error()); } if ($path !== null) { diff --git a/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php b/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php index 6d0a718975..f4cb88ad3a 100644 --- a/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php +++ b/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php @@ -57,7 +57,7 @@ public function formatErrors( foreach ($fileErrorsCounts as $message => $count) { $message = preg_replace('/\r\n|\r|\n/', "\n", $message); if ($message === null) { - throw new \Exception('Error while executing regex: '. preg_last_error()); + throw new \Exception('Error while executing regex: ' . preg_last_error()); } $errorsToOutput[] = [ From f8a5bec809a8c5254574125c223047906925bdd9 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 12 Nov 2019 20:49:52 +0100 Subject: [PATCH 21/26] fix newline replacement --- src/Analyser/IgnoredError.php | 4 ++-- src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php | 5 +---- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Analyser/IgnoredError.php b/src/Analyser/IgnoredError.php index 000c328955..c9fbfb083f 100644 --- a/src/Analyser/IgnoredError.php +++ b/src/Analyser/IgnoredError.php @@ -50,8 +50,8 @@ public static function shouldIgnore( { // normalize newlines to allow working with ignore-patterns independent of used OS newline-format $errorMessage = $error->getMessage(); - $errorMessage = preg_replace('/\r\n|\r|\n/', "\n", $errorMessage); - $ignoredErrorPattern = preg_replace('/\r\n|\r|\n/', "\n", $ignoredErrorPattern); + $errorMessage = str_replace(['\r\n', '\r'], '\n', $errorMessage); + $ignoredErrorPattern = str_replace(['\r\n', '\r'], '\n', $ignoredErrorPattern); if ($errorMessage === null || $ignoredErrorPattern === null) { throw new \Exception('Error while executing regex: ' . preg_last_error()); } diff --git a/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php b/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php index f4cb88ad3a..614a6abced 100644 --- a/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php +++ b/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php @@ -55,10 +55,7 @@ public function formatErrors( } foreach ($fileErrorsCounts as $message => $count) { - $message = preg_replace('/\r\n|\r|\n/', "\n", $message); - if ($message === null) { - throw new \Exception('Error while executing regex: ' . preg_last_error()); - } + $message = str_replace(['\r\n', '\r'], '\n', $message); $errorsToOutput[] = [ 'message' => '#^' . preg_quote($message, '#') . '$#', From 582862ed0eea274c35ee79f548f504af053720e2 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 12 Nov 2019 20:57:03 +0100 Subject: [PATCH 22/26] fix test --- .../Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php b/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php index 5160b4cf0f..0f2d105a3f 100644 --- a/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php +++ b/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php @@ -154,8 +154,8 @@ 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"], + [new Error('Error message\nwith\r\ndifferent\rnewlines', 'Testfile')], + ['Error message\nwith\r\ndifferent\rnewlines'], false, false, null From fe854e03196706d60aa7e33fa9a18f4dd02c79d0 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 12 Nov 2019 20:58:18 +0100 Subject: [PATCH 23/26] added docs --- src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php b/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php index 614a6abced..d12a2109cf 100644 --- a/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php +++ b/src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php @@ -55,6 +55,7 @@ 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[] = [ From 5a444da757eff223e581eb2d4552671eff902fa1 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 12 Nov 2019 21:05:49 +0100 Subject: [PATCH 24/26] fix test --- .../Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php b/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php index 0f2d105a3f..1ff6004811 100644 --- a/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php +++ b/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php @@ -168,7 +168,7 @@ public function testFormatErrorMessagesNormalizedNewlines(): void self::assertSame( trim(Neon::encode(['parameters' => ['ignoreErrors' => [ [ - 'message' => "#^Error message\nwith\ndifferent\nnewlines$#", + 'message' => '#^Error message\\\\nwith\\\\ndifferent\\\\nnewlines$', 'count' => 1, 'path' => 'Testfile', ], From 7e4a5b2daae0de9326c7a1a9e054886fadd63d0c Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 12 Nov 2019 21:05:01 +0100 Subject: [PATCH 25/26] fix test expectations --- .../Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php b/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php index 1ff6004811..aacaa72d95 100644 --- a/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php +++ b/tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php @@ -168,7 +168,7 @@ public function testFormatErrorMessagesNormalizedNewlines(): void self::assertSame( trim(Neon::encode(['parameters' => ['ignoreErrors' => [ [ - 'message' => '#^Error message\\\\nwith\\\\ndifferent\\\\nnewlines$', + 'message' => '#^Error message\\\\nwith\\\\ndifferent\\\\nnewlines$#', 'count' => 1, 'path' => 'Testfile', ], From b0d008fce68d8ade90a0fd7857301ca155aa5ab1 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 12 Nov 2019 21:10:43 +0100 Subject: [PATCH 26/26] removed dead code --- src/Analyser/IgnoredError.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Analyser/IgnoredError.php b/src/Analyser/IgnoredError.php index c9fbfb083f..acf1bfb7f5 100644 --- a/src/Analyser/IgnoredError.php +++ b/src/Analyser/IgnoredError.php @@ -52,9 +52,6 @@ public static function shouldIgnore( $errorMessage = $error->getMessage(); $errorMessage = str_replace(['\r\n', '\r'], '\n', $errorMessage); $ignoredErrorPattern = str_replace(['\r\n', '\r'], '\n', $ignoredErrorPattern); - if ($errorMessage === null || $ignoredErrorPattern === null) { - throw new \Exception('Error while executing regex: ' . preg_last_error()); - } if ($path !== null) { $fileExcluder = new FileExcluder($fileHelper, [$path]);