From e3f29a9110387193dc6a434c75069dd69c1bb095 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Thu, 8 Jun 2023 19:14:58 +0000 Subject: [PATCH 01/13] Add more information to JunitErrorFormatter output --- .../ErrorFormatter/JunitErrorFormatter.php | 40 ++++++++++++++++--- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/src/Command/ErrorFormatter/JunitErrorFormatter.php b/src/Command/ErrorFormatter/JunitErrorFormatter.php index b3d681ec62c..8576ac636a2 100644 --- a/src/Command/ErrorFormatter/JunitErrorFormatter.php +++ b/src/Command/ErrorFormatter/JunitErrorFormatter.php @@ -7,10 +7,14 @@ use PHPStan\DependencyInjection\AutowiredParameter; use PHPStan\DependencyInjection\AutowiredService; use PHPStan\File\RelativePathHelper; +use SplFileObject; +use function array_filter; use function htmlspecialchars; +use function implode; use function sprintf; use const ENT_COMPAT; use const ENT_XML1; +use const PHP_EOL; #[AutowiredService(name: 'errorFormatter.junit')] final class JunitErrorFormatter implements ErrorFormatter @@ -33,7 +37,7 @@ public function formatErrors( $result = ''; $result .= sprintf( - '', + '', $totalFailuresCount, $totalTestsCount, ); @@ -41,9 +45,11 @@ public function formatErrors( foreach ($analysisResult->getFileSpecificErrors() as $fileSpecificError) { $fileName = $this->relativePathHelper->getRelativePath($fileSpecificError->getFile()); $result .= $this->createTestCase( - sprintf('%s:%s', $fileName, (string) $fileSpecificError->getLine()), + $fileName, 'ERROR', - $fileSpecificError->getMessage(), + implode(PHP_EOL, array_filter([ $fileSpecificError->getMessage(), $fileSpecificError->getTip() ], static fn (?string $part): bool => $part !== null && $part !== '')), + $fileSpecificError->getLine(), + $fileSpecificError->getIdentifier(), ); } @@ -71,12 +77,24 @@ public function formatErrors( * * */ - private function createTestCase(string $reference, string $type, ?string $message = null): string + private function createTestCase(string $reference, string $type, ?string $message = null, ?int $line = null, ?string $identifier = null): string { - $result = sprintf('', $this->escape($reference)); + $result = sprintf( + '', // class="%s" file="%s" line="%d", + $this->escape($identifier), + //$this->escape($reference), + //$this->escape($reference), + $this->escape(sprintf('%s:%d', $reference, $line ?? 0)), + //$line, + ); if ($message !== null) { - $result .= sprintf('', $this->escape($type), $this->escape($message)); + $result .= sprintf( + '', + $this->escape($type), + $this->escape($message), + $this->fileGetLine($reference, $line), + ); } $result .= ''; @@ -84,6 +102,16 @@ private function createTestCase(string $reference, string $type, ?string $messag return $result; } + private function fileGetLine(string $fileName, int $lineNumber): ?string + { + $file = new SplFileObject($fileName); + if (!$file->eof()) { + $file->seek($lineNumber); + return $file->current(); // $contents would hold the data from line x + } + return null; + } + /** * Escapes values for using in XML * From 027b60c1f397516bdc5eb11ba5589bfa8121fcf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Thu, 8 Jun 2023 19:34:21 +0000 Subject: [PATCH 02/13] Add check to have required values defined and ignore line where phpstan misses that the data type can only be one of the three possible --- .../ErrorFormatter/JunitErrorFormatter.php | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Command/ErrorFormatter/JunitErrorFormatter.php b/src/Command/ErrorFormatter/JunitErrorFormatter.php index 8576ac636a2..76473d1e0fa 100644 --- a/src/Command/ErrorFormatter/JunitErrorFormatter.php +++ b/src/Command/ErrorFormatter/JunitErrorFormatter.php @@ -89,12 +89,20 @@ private function createTestCase(string $reference, string $type, ?string $messag ); if ($message !== null) { - $result .= sprintf( - '', - $this->escape($type), - $this->escape($message), - $this->fileGetLine($reference, $line), - ); + if ($line === null) { + $result .= sprintf( + '', + $this->escape($type), + $this->escape($message), + ); + } else { + $result .= sprintf( + '', + $this->escape($type), + $this->escape($message), + $this->fileGetLine($reference, $line), + ); + } } $result .= ''; @@ -107,6 +115,7 @@ private function fileGetLine(string $fileName, int $lineNumber): ?string $file = new SplFileObject($fileName); if (!$file->eof()) { $file->seek($lineNumber); + // @phpstan-ignore-next-line return $file->current(); // $contents would hold the data from line x } return null; From d8a1685435c61df6a2baf4aa6af40a473416600a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Tue, 11 Jul 2023 09:20:36 +0200 Subject: [PATCH 03/13] Make sure, string is passed Suppress test warning --- src/Command/ErrorFormatter/JunitErrorFormatter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Command/ErrorFormatter/JunitErrorFormatter.php b/src/Command/ErrorFormatter/JunitErrorFormatter.php index 76473d1e0fa..7d0265c1e8a 100644 --- a/src/Command/ErrorFormatter/JunitErrorFormatter.php +++ b/src/Command/ErrorFormatter/JunitErrorFormatter.php @@ -81,7 +81,7 @@ private function createTestCase(string $reference, string $type, ?string $messag { $result = sprintf( '', // class="%s" file="%s" line="%d", - $this->escape($identifier), + $this->escape(is_null($identifier) ? '' : $identifier), //$this->escape($reference), //$this->escape($reference), $this->escape(sprintf('%s:%d', $reference, $line ?? 0)), From 75fd7776900c145a8b446b8e089e1971e4aded82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Tue, 11 Jul 2023 09:30:41 +0200 Subject: [PATCH 04/13] Update schema version in tests accordingly from 5.5.1 to 5.9.3 --- .../ErrorFormatter/JunitErrorFormatterTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/PHPStan/Command/ErrorFormatter/JunitErrorFormatterTest.php b/tests/PHPStan/Command/ErrorFormatter/JunitErrorFormatterTest.php index 885c62fadb3..e145286d3c4 100644 --- a/tests/PHPStan/Command/ErrorFormatter/JunitErrorFormatterTest.php +++ b/tests/PHPStan/Command/ErrorFormatter/JunitErrorFormatterTest.php @@ -32,7 +32,7 @@ public static function dataFormatterOutputProvider(): Generator 0, 0, ' - + ', @@ -43,7 +43,7 @@ public static function dataFormatterOutputProvider(): Generator 1, 0, ' - + @@ -56,7 +56,7 @@ public static function dataFormatterOutputProvider(): Generator 0, 1, ' - + @@ -69,7 +69,7 @@ public static function dataFormatterOutputProvider(): Generator 4, 0, ' - + @@ -91,7 +91,7 @@ public static function dataFormatterOutputProvider(): Generator 0, 2, ' - + @@ -107,7 +107,7 @@ public static function dataFormatterOutputProvider(): Generator 4, 2, ' - + From 5a253898c275e932c3155bcd1d3ddbd5cc47e43c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Tue, 11 Jul 2023 15:10:21 +0200 Subject: [PATCH 05/13] Adjust tag attribute expectations to changes --- .../PHPStan/Command/ErrorFormatter/JunitErrorFormatterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/PHPStan/Command/ErrorFormatter/JunitErrorFormatterTest.php b/tests/PHPStan/Command/ErrorFormatter/JunitErrorFormatterTest.php index e145286d3c4..4109815f977 100644 --- a/tests/PHPStan/Command/ErrorFormatter/JunitErrorFormatterTest.php +++ b/tests/PHPStan/Command/ErrorFormatter/JunitErrorFormatterTest.php @@ -33,7 +33,7 @@ public static function dataFormatterOutputProvider(): Generator 0, ' - + ', ]; From fca4577a3835713d69ac526a4e87f36c89f66c19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Tue, 11 Jul 2023 15:21:22 +0200 Subject: [PATCH 06/13] use function is_null --- src/Command/ErrorFormatter/JunitErrorFormatter.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Command/ErrorFormatter/JunitErrorFormatter.php b/src/Command/ErrorFormatter/JunitErrorFormatter.php index 7d0265c1e8a..d00f11855fe 100644 --- a/src/Command/ErrorFormatter/JunitErrorFormatter.php +++ b/src/Command/ErrorFormatter/JunitErrorFormatter.php @@ -11,6 +11,7 @@ use function array_filter; use function htmlspecialchars; use function implode; +use function is_null; use function sprintf; use const ENT_COMPAT; use const ENT_XML1; From 89187b3e8ae03a6aba0ac97027287347973174ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Tue, 11 Jul 2023 15:30:54 +0200 Subject: [PATCH 07/13] Refactor SlevomatCodingStandard.Variables.UselessVariable.UselessVariable --- src/Command/ErrorFormatter/JunitErrorFormatter.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Command/ErrorFormatter/JunitErrorFormatter.php b/src/Command/ErrorFormatter/JunitErrorFormatter.php index d00f11855fe..260748ae44c 100644 --- a/src/Command/ErrorFormatter/JunitErrorFormatter.php +++ b/src/Command/ErrorFormatter/JunitErrorFormatter.php @@ -106,9 +106,7 @@ private function createTestCase(string $reference, string $type, ?string $messag } } - $result .= ''; - - return $result; + return $result . ''; } private function fileGetLine(string $fileName, int $lineNumber): ?string From 4a0dfa7267c23a09874d32d1eea6110d63ff2a5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Tue, 11 Jul 2023 15:41:37 +0200 Subject: [PATCH 08/13] Add check if file exists --- src/Command/ErrorFormatter/JunitErrorFormatter.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Command/ErrorFormatter/JunitErrorFormatter.php b/src/Command/ErrorFormatter/JunitErrorFormatter.php index 260748ae44c..4eb1f0aec34 100644 --- a/src/Command/ErrorFormatter/JunitErrorFormatter.php +++ b/src/Command/ErrorFormatter/JunitErrorFormatter.php @@ -111,6 +111,10 @@ private function createTestCase(string $reference, string $type, ?string $messag private function fileGetLine(string $fileName, int $lineNumber): ?string { + if (!file_exists()) { + return null; + } + $file = new SplFileObject($fileName); if (!$file->eof()) { $file->seek($lineNumber); From da15a7e020f713d6db0a89cec8aed7b93c89a3d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Tue, 11 Jul 2023 15:48:36 +0200 Subject: [PATCH 09/13] use function file_exists; --- src/Command/ErrorFormatter/JunitErrorFormatter.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Command/ErrorFormatter/JunitErrorFormatter.php b/src/Command/ErrorFormatter/JunitErrorFormatter.php index 4eb1f0aec34..253a1b7f046 100644 --- a/src/Command/ErrorFormatter/JunitErrorFormatter.php +++ b/src/Command/ErrorFormatter/JunitErrorFormatter.php @@ -9,6 +9,7 @@ use PHPStan\File\RelativePathHelper; use SplFileObject; use function array_filter; +use function file_exists; use function htmlspecialchars; use function implode; use function is_null; From afd954f83de8b004108fba17869b60859370f641 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Tue, 11 Jul 2023 15:48:59 +0200 Subject: [PATCH 10/13] Pass parameter to function --- src/Command/ErrorFormatter/JunitErrorFormatter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Command/ErrorFormatter/JunitErrorFormatter.php b/src/Command/ErrorFormatter/JunitErrorFormatter.php index 253a1b7f046..b4f7957c25b 100644 --- a/src/Command/ErrorFormatter/JunitErrorFormatter.php +++ b/src/Command/ErrorFormatter/JunitErrorFormatter.php @@ -112,7 +112,7 @@ private function createTestCase(string $reference, string $type, ?string $messag private function fileGetLine(string $fileName, int $lineNumber): ?string { - if (!file_exists()) { + if (!file_exists($fileName)) { return null; } From b57546a88dee7b06bc5528380ca67d8c776d94ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Tue, 11 Jul 2023 15:49:37 +0200 Subject: [PATCH 11/13] Use early return --- src/Command/ErrorFormatter/JunitErrorFormatter.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Command/ErrorFormatter/JunitErrorFormatter.php b/src/Command/ErrorFormatter/JunitErrorFormatter.php index b4f7957c25b..ade93d0fa69 100644 --- a/src/Command/ErrorFormatter/JunitErrorFormatter.php +++ b/src/Command/ErrorFormatter/JunitErrorFormatter.php @@ -117,12 +117,13 @@ private function fileGetLine(string $fileName, int $lineNumber): ?string } $file = new SplFileObject($fileName); - if (!$file->eof()) { - $file->seek($lineNumber); - // @phpstan-ignore-next-line - return $file->current(); // $contents would hold the data from line x + if ($file->eof()) { + return null; } - return null; + + $file->seek($lineNumber); + // @phpstan-ignore-next-line + return $file->current(); // $contents would hold the data from line x } /** From 25e49f1445e89f6825767044c7a7f414ce0e2cf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Tue, 11 Jul 2023 15:57:21 +0200 Subject: [PATCH 12/13] Adjust expected results to changes --- .../Command/ErrorFormatter/JunitErrorFormatterTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/PHPStan/Command/ErrorFormatter/JunitErrorFormatterTest.php b/tests/PHPStan/Command/ErrorFormatter/JunitErrorFormatterTest.php index 4109815f977..1bdd7c98190 100644 --- a/tests/PHPStan/Command/ErrorFormatter/JunitErrorFormatterTest.php +++ b/tests/PHPStan/Command/ErrorFormatter/JunitErrorFormatterTest.php @@ -44,7 +44,7 @@ public static function dataFormatterOutputProvider(): Generator 0, ' - + @@ -70,16 +70,16 @@ public static function dataFormatterOutputProvider(): Generator 0, ' - + - + - + - + From 2f4554b70297ca473d026cd9cf98241c2abd37fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Tue, 11 Jul 2023 16:11:22 +0200 Subject: [PATCH 13/13] Adjust another test case Amp-Thread-ID: https://ampcode.com/threads/T-01a19219-8e6c-730d-9b2f-3ac8a4a98fcc --- .../JunitErrorFormatterTest.php | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/PHPStan/Command/ErrorFormatter/JunitErrorFormatterTest.php b/tests/PHPStan/Command/ErrorFormatter/JunitErrorFormatterTest.php index 1bdd7c98190..e0d7f035a40 100644 --- a/tests/PHPStan/Command/ErrorFormatter/JunitErrorFormatterTest.php +++ b/tests/PHPStan/Command/ErrorFormatter/JunitErrorFormatterTest.php @@ -57,7 +57,7 @@ public static function dataFormatterOutputProvider(): Generator 1, ' - + @@ -80,7 +80,7 @@ public static function dataFormatterOutputProvider(): Generator - + ', @@ -92,10 +92,10 @@ public static function dataFormatterOutputProvider(): Generator 2, ' - + - + @@ -108,22 +108,22 @@ public static function dataFormatterOutputProvider(): Generator 2, ' - + - + - + - - + + - + - +