diff --git a/README.md b/README.md index 73e014b..639211e 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ _(optional)_ You can simplify generation with e.g. composer script:

Legacy usage

-> _This usage is deprecated since 2.0, but it works in all versions. Downside is that it cannot utilize result cache_ +> _This usage is deprecated since 2.0, but it works in all versions. Downside is that it cannot utilize result cache and does not support `rawMessage`._ Setup where your baseline files should be stored and include its loader: ```neon diff --git a/src/BaselineSplitter.php b/src/BaselineSplitter.php index 0fc6dd3..7a66674 100644 --- a/src/BaselineSplitter.php +++ b/src/BaselineSplitter.php @@ -95,7 +95,7 @@ public function split(string $loaderFilePath): array /** * @param array $errors - * @return array> + * @return array> * * @throws ErrorException */ @@ -113,12 +113,16 @@ private function groupErrorsByIdentifier( $identifier = $error['identifier'] ?? 'missing-identifier'; - if (!isset($error['message'])) { - throw new ErrorException("Ignored error #$index is missing 'message'"); + if (isset($error['rawMessage'])) { + $message = $error['rawMessage']; + $rawMessage = true; + } elseif (isset($error['message'])) { + $message = $error['message']; + $rawMessage = false; + } else { + throw new ErrorException("Ignored error #$index is missing 'message' or 'rawMessage'"); } - $message = $error['message']; - if (!isset($error['count'])) { throw new ErrorException("Ignored error #$index is missing 'count'"); } @@ -140,7 +144,11 @@ private function groupErrorsByIdentifier( unset($error['identifier']); - $groupedErrors[$identifier][] = [ + $groupedErrors[$identifier][] = $rawMessage ? [ + 'rawMessage' => $message, + 'count' => $count, + 'path' => $normalizedPath, + ] : [ 'message' => $message, 'count' => $count, 'path' => $normalizedPath, diff --git a/src/Handler/BaselineHandler.php b/src/Handler/BaselineHandler.php index a4c49da..5903e9f 100644 --- a/src/Handler/BaselineHandler.php +++ b/src/Handler/BaselineHandler.php @@ -15,7 +15,7 @@ interface BaselineHandler public function decodeBaseline(string $filepath): array; /** - * @param list $errors + * @param list $errors */ public function encodeBaseline( ?string $comment, diff --git a/src/Handler/PhpBaselineHandler.php b/src/Handler/PhpBaselineHandler.php index 0e1ca8a..60dfece 100644 --- a/src/Handler/PhpBaselineHandler.php +++ b/src/Handler/PhpBaselineHandler.php @@ -45,9 +45,18 @@ public function encodeBaseline( $php .= "\$ignoreErrors = [];\n"; foreach ($errors as $error) { + if (isset($error['rawMessage'])) { + $message = $error['rawMessage']; + $messageKey = 'rawMessage'; + } else { + $message = $error['message']; // @phpstan-ignore offsetAccess.notFound + $messageKey = 'message'; + } + $php .= sprintf( - "\$ignoreErrors[] = [\n{$indent}'message' => %s,\n{$indent}'count' => %d,\n{$indent}'path' => __DIR__ . %s,\n];\n", - var_export($error['message'], true), + "\$ignoreErrors[] = [\n{$indent}%s => %s,\n{$indent}'count' => %d,\n{$indent}'path' => __DIR__ . %s,\n];\n", + var_export($messageKey, true), + var_export($message, true), var_export($error['count'], true), var_export('/' . $error['path'], true), ); diff --git a/tests/Integration/IntegrationTest.php b/tests/Integration/IntegrationTest.php index d75ee05..7f64844 100644 --- a/tests/Integration/IntegrationTest.php +++ b/tests/Integration/IntegrationTest.php @@ -15,7 +15,10 @@ final class IntegrationTest extends BinTestCase /** * @dataProvider provideExtension */ - public function testResultCache(string $extension): void + public function testResultCache( + string $extension, + bool $bleedingEdge + ): void { $emptyConfig = $extension === 'php' ? 'runCommand("$phpstan clear-result-cache -c $extension.neon", $cwd, 0); - $this->runCommand("$phpstan analyse -vv -c $extension.neon --generate-baseline=../../$baselinesDir/_loader.$extension", $cwd, 0, null, 'Result cache is saved.'); + $this->runCommand("$phpstan clear-result-cache -c $config", $cwd, 0); + $this->runCommand("$phpstan analyse -vv -c $config --generate-baseline=../../$baselinesDir/_loader.$extension", $cwd, 0, null, 'Result cache is saved.'); $this->runCommand("php $split ../../$baselinesDir/_loader.$extension", $cwd, 0, 'Writing baseline file'); - $this->runCommand("$phpstan analyse -vv -c $extension.neon", $cwd, 0, null, 'Result cache restored. 0 files will be reanalysed.'); + $this->runCommand("$phpstan analyse -vv -c $config", $cwd, 0, null, 'Result cache restored. 0 files will be reanalysed.'); // cache should invalidate by editing the baseline file_put_contents($baselinesDirAbs . "/method.notFound.$extension", $emptyConfig); - $this->runCommand("$phpstan analyse -vv -c $extension.neon", $cwd, 1, 'Call to an undefined method DateTime::invalid()'); + $this->runCommand("$phpstan analyse -vv -c $config", $cwd, 1, 'Call to an undefined method DateTime::invalid()'); } /** - * @return iterable + * @return iterable */ public function provideExtension(): iterable { - yield 'Neon' => ['neon']; - yield 'PHP' => ['php']; + yield 'Neon' => ['neon', false]; + yield 'Neon (bleeding edge)' => ['neon', true]; + yield 'PHP' => ['php', false]; + yield 'PHP (bleeding edge)' => ['php', true]; } } diff --git a/tests/Integration/neon.bleedingEdge.neon b/tests/Integration/neon.bleedingEdge.neon new file mode 100644 index 0000000..0336ff9 --- /dev/null +++ b/tests/Integration/neon.bleedingEdge.neon @@ -0,0 +1,3 @@ +includes: + - phar://phpstan.phar/conf/bleedingEdge.neon + - neon.neon diff --git a/tests/Integration/php.bleedingEdge.neon b/tests/Integration/php.bleedingEdge.neon new file mode 100644 index 0000000..d3f2735 --- /dev/null +++ b/tests/Integration/php.bleedingEdge.neon @@ -0,0 +1,3 @@ +includes: + - phar://phpstan.phar/conf/bleedingEdge.neon + - php.neon diff --git a/tests/Rule/BaselinePerIdentifierFormatterTest.php b/tests/Rule/BaselinePerIdentifierFormatterTest.php index 49c6d0a..fb839a1 100644 --- a/tests/Rule/BaselinePerIdentifierFormatterTest.php +++ b/tests/Rule/BaselinePerIdentifierFormatterTest.php @@ -36,10 +36,10 @@ public function testFormat(): void $this->createOutput(), ); - self::assertFileEquals(__DIR__ . '/data/baselines-neon/loader.neon', $fakeRoot . '/baselines/loader.neon'); - self::assertFileEquals(__DIR__ . '/data/baselines-neon/sample.identifier.neon', $fakeRoot . '/baselines/sample.identifier.neon'); - self::assertFileEquals(__DIR__ . '/data/baselines-neon/another.identifier.neon', $fakeRoot . '/baselines/another.identifier.neon'); - self::assertFileEquals(__DIR__ . '/data/baselines-neon/missing-identifier.neon', $fakeRoot . '/baselines/missing-identifier.neon'); + self::assertFileEquals(__DIR__ . '/data/baselines-neon-deprecated/loader.neon', $fakeRoot . '/baselines/loader.neon'); + self::assertFileEquals(__DIR__ . '/data/baselines-neon-deprecated/sample.identifier.neon', $fakeRoot . '/baselines/sample.identifier.neon'); + self::assertFileEquals(__DIR__ . '/data/baselines-neon-deprecated/another.identifier.neon', $fakeRoot . '/baselines/another.identifier.neon'); + self::assertFileEquals(__DIR__ . '/data/baselines-neon-deprecated/missing-identifier.neon', $fakeRoot . '/baselines/missing-identifier.neon'); } /** diff --git a/tests/Rule/data/baselines-neon-deprecated/another.identifier.neon b/tests/Rule/data/baselines-neon-deprecated/another.identifier.neon new file mode 100644 index 0000000..8ebec17 --- /dev/null +++ b/tests/Rule/data/baselines-neon-deprecated/another.identifier.neon @@ -0,0 +1,8 @@ +# total 1 error + +parameters: + ignoreErrors: + - + message: '#^Error to escape ''\#$#' + count: 1 + path: ../app/config.php diff --git a/tests/Rule/data/baselines-neon-deprecated/loader.neon b/tests/Rule/data/baselines-neon-deprecated/loader.neon new file mode 100644 index 0000000..c1b9ee8 --- /dev/null +++ b/tests/Rule/data/baselines-neon-deprecated/loader.neon @@ -0,0 +1,6 @@ +# total 4 errors + +includes: + - another.identifier.neon + - missing-identifier.neon + - sample.identifier.neon diff --git a/tests/Rule/data/baselines-neon-deprecated/missing-identifier.neon b/tests/Rule/data/baselines-neon-deprecated/missing-identifier.neon new file mode 100644 index 0000000..5f0fe31 --- /dev/null +++ b/tests/Rule/data/baselines-neon-deprecated/missing-identifier.neon @@ -0,0 +1,8 @@ +# total 1 error + +parameters: + ignoreErrors: + - + message: '#^Error 3$#' + count: 1 + path: ../app/index.php diff --git a/tests/Rule/data/baselines-neon-deprecated/sample.identifier.neon b/tests/Rule/data/baselines-neon-deprecated/sample.identifier.neon new file mode 100644 index 0000000..09db972 --- /dev/null +++ b/tests/Rule/data/baselines-neon-deprecated/sample.identifier.neon @@ -0,0 +1,8 @@ +# total 2 errors + +parameters: + ignoreErrors: + - + message: '#^Error simple$#' + count: 2 + path: ../app/file.php diff --git a/tests/Rule/data/baselines-neon-no-error-count/sample.identifier.neon b/tests/Rule/data/baselines-neon-no-error-count/sample.identifier.neon index a7b6912..d7d2526 100644 --- a/tests/Rule/data/baselines-neon-no-error-count/sample.identifier.neon +++ b/tests/Rule/data/baselines-neon-no-error-count/sample.identifier.neon @@ -4,3 +4,8 @@ parameters: message: '#^Error simple$#' count: 2 path: ../app/file.php + + - + rawMessage: Error raw message list|null + count: 1 + path: ../app/index.php diff --git a/tests/Rule/data/baselines-neon/loader.neon b/tests/Rule/data/baselines-neon/loader.neon index c1b9ee8..3958eb9 100644 --- a/tests/Rule/data/baselines-neon/loader.neon +++ b/tests/Rule/data/baselines-neon/loader.neon @@ -1,4 +1,4 @@ -# total 4 errors +# total 5 errors includes: - another.identifier.neon diff --git a/tests/Rule/data/baselines-neon/sample.identifier.neon b/tests/Rule/data/baselines-neon/sample.identifier.neon index 09db972..6526810 100644 --- a/tests/Rule/data/baselines-neon/sample.identifier.neon +++ b/tests/Rule/data/baselines-neon/sample.identifier.neon @@ -1,4 +1,4 @@ -# total 2 errors +# total 3 errors parameters: ignoreErrors: @@ -6,3 +6,8 @@ parameters: message: '#^Error simple$#' count: 2 path: ../app/file.php + + - + rawMessage: Error raw message list|null + count: 1 + path: ../app/index.php diff --git a/tests/Rule/data/baselines-php-no-error-count/sample.identifier.php b/tests/Rule/data/baselines-php-no-error-count/sample.identifier.php index e701f14..bc18607 100644 --- a/tests/Rule/data/baselines-php-no-error-count/sample.identifier.php +++ b/tests/Rule/data/baselines-php-no-error-count/sample.identifier.php @@ -6,5 +6,10 @@ 'count' => 2, 'path' => __DIR__ . '/../app/file.php', ]; +$ignoreErrors[] = [ + 'rawMessage' => 'Error raw message list|null', + 'count' => 1, + 'path' => __DIR__ . '/../app/index.php', +]; return ['parameters' => ['ignoreErrors' => $ignoreErrors]]; diff --git a/tests/Rule/data/baselines-php/loader.php b/tests/Rule/data/baselines-php/loader.php index 17f7938..3e6c829 100644 --- a/tests/Rule/data/baselines-php/loader.php +++ b/tests/Rule/data/baselines-php/loader.php @@ -1,6 +1,6 @@ [ __DIR__ . '/another.identifier.php', diff --git a/tests/Rule/data/baselines-php/sample.identifier.php b/tests/Rule/data/baselines-php/sample.identifier.php index 878469e..4485730 100644 --- a/tests/Rule/data/baselines-php/sample.identifier.php +++ b/tests/Rule/data/baselines-php/sample.identifier.php @@ -1,6 +1,6 @@ 2, 'path' => __DIR__ . '/../app/file.php', ]; +$ignoreErrors[] = [ + 'rawMessage' => 'Error raw message list|null', + 'count' => 1, + 'path' => __DIR__ . '/../app/index.php', +]; return ['parameters' => ['ignoreErrors' => $ignoreErrors]]; diff --git a/tests/SplitterTest.php b/tests/SplitterTest.php index 43f1e6c..a65b4b6 100644 --- a/tests/SplitterTest.php +++ b/tests/SplitterTest.php @@ -87,7 +87,7 @@ public function testSplitter(): void self::assertSame([ $folder . '/baselines/another.identifier.neon' => 1, $folder . '/baselines/missing-identifier.neon' => 1, - $folder . '/baselines/sample.identifier.neon' => 2, + $folder . '/baselines/sample.identifier.neon' => 3, $folder . '/baselines/loader.neon' => null, ], $written); } @@ -104,7 +104,7 @@ private function prepareSampleFolder(): string } /** - * @return array{parameters: array{ignoreErrors: array{0: array{message: string, count: int, path: string, identifier?: string}}}} + * @return array{parameters: array{ignoreErrors: array{0: array{message?: string, rawMessage?: string, count: int, path: string, identifier?: string}}}} */ private function getSampleErrors(): array { @@ -128,6 +128,12 @@ private function getSampleErrors(): array 'count' => 1, 'path' => '../app/index.php', ], + [ + 'rawMessage' => 'Error raw message list|null', + 'count' => 1, + 'path' => '../app/index.php', + 'identifier' => 'sample.identifier', + ], ], ], ];