Skip to content

Commit a096ae6

Browse files
authored
Support rawMessage (#47)
1 parent 1a5f802 commit a096ae6

File tree

19 files changed

+113
-28
lines changed

19 files changed

+113
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ _(optional)_ You can simplify generation with e.g. composer script:
6969
<details>
7070
<summary><h3>Legacy usage</h3></summary>
7171

72-
> _This usage is deprecated since 2.0, but it works in all versions. Downside is that it cannot utilize result cache_
72+
> _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`._
7373
7474
Setup where your baseline files should be stored and include its loader:
7575
```neon

src/BaselineSplitter.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function split(string $loaderFilePath): array
9595

9696
/**
9797
* @param array<mixed> $errors
98-
* @return array<string, list<array{message: string, count: int, path: string}>>
98+
* @return array<string, list<array{message: string, count: int, path: string}|array{rawMessage: string, count: int, path: string}>>
9999
*
100100
* @throws ErrorException
101101
*/
@@ -113,12 +113,16 @@ private function groupErrorsByIdentifier(
113113

114114
$identifier = $error['identifier'] ?? 'missing-identifier';
115115

116-
if (!isset($error['message'])) {
117-
throw new ErrorException("Ignored error #$index is missing 'message'");
116+
if (isset($error['rawMessage'])) {
117+
$message = $error['rawMessage'];
118+
$rawMessage = true;
119+
} elseif (isset($error['message'])) {
120+
$message = $error['message'];
121+
$rawMessage = false;
122+
} else {
123+
throw new ErrorException("Ignored error #$index is missing 'message' or 'rawMessage'");
118124
}
119125

120-
$message = $error['message'];
121-
122126
if (!isset($error['count'])) {
123127
throw new ErrorException("Ignored error #$index is missing 'count'");
124128
}
@@ -140,7 +144,11 @@ private function groupErrorsByIdentifier(
140144

141145
unset($error['identifier']);
142146

143-
$groupedErrors[$identifier][] = [
147+
$groupedErrors[$identifier][] = $rawMessage ? [
148+
'rawMessage' => $message,
149+
'count' => $count,
150+
'path' => $normalizedPath,
151+
] : [
144152
'message' => $message,
145153
'count' => $count,
146154
'path' => $normalizedPath,

src/Handler/BaselineHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interface BaselineHandler
1515
public function decodeBaseline(string $filepath): array;
1616

1717
/**
18-
* @param list<array{message: string, count: int, path: string}> $errors
18+
* @param list<array{message: string, count: int, path: string}|array{rawMessage: string, count: int, path: string}> $errors
1919
*/
2020
public function encodeBaseline(
2121
?string $comment,

src/Handler/PhpBaselineHandler.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,18 @@ public function encodeBaseline(
4545
$php .= "\$ignoreErrors = [];\n";
4646

4747
foreach ($errors as $error) {
48+
if (isset($error['rawMessage'])) {
49+
$message = $error['rawMessage'];
50+
$messageKey = 'rawMessage';
51+
} else {
52+
$message = $error['message']; // @phpstan-ignore offsetAccess.notFound
53+
$messageKey = 'message';
54+
}
55+
4856
$php .= sprintf(
49-
"\$ignoreErrors[] = [\n{$indent}'message' => %s,\n{$indent}'count' => %d,\n{$indent}'path' => __DIR__ . %s,\n];\n",
50-
var_export($error['message'], true),
57+
"\$ignoreErrors[] = [\n{$indent}%s => %s,\n{$indent}'count' => %d,\n{$indent}'path' => __DIR__ . %s,\n];\n",
58+
var_export($messageKey, true),
59+
var_export($message, true),
5160
var_export($error['count'], true),
5261
var_export('/' . $error['path'], true),
5362
);

tests/Integration/IntegrationTest.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ final class IntegrationTest extends BinTestCase
1515
/**
1616
* @dataProvider provideExtension
1717
*/
18-
public function testResultCache(string $extension): void
18+
public function testResultCache(
19+
string $extension,
20+
bool $bleedingEdge
21+
): void
1922
{
2023
$emptyConfig = $extension === 'php' ? '<?php return [];' : '';
2124
$baselinesDir = 'cache/integration-test/baselines';
@@ -33,25 +36,28 @@ public function testResultCache(string $extension): void
3336
$cwd = __DIR__;
3437
$phpstan = '../../vendor/bin/phpstan';
3538
$split = '../../bin/split-phpstan-baseline';
39+
$config = $bleedingEdge ? "$extension.bleedingEdge.neon" : "$extension.neon";
3640

37-
$this->runCommand("$phpstan clear-result-cache -c $extension.neon", $cwd, 0);
38-
$this->runCommand("$phpstan analyse -vv -c $extension.neon --generate-baseline=../../$baselinesDir/_loader.$extension", $cwd, 0, null, 'Result cache is saved.');
41+
$this->runCommand("$phpstan clear-result-cache -c $config", $cwd, 0);
42+
$this->runCommand("$phpstan analyse -vv -c $config --generate-baseline=../../$baselinesDir/_loader.$extension", $cwd, 0, null, 'Result cache is saved.');
3943
$this->runCommand("php $split ../../$baselinesDir/_loader.$extension", $cwd, 0, 'Writing baseline file');
40-
$this->runCommand("$phpstan analyse -vv -c $extension.neon", $cwd, 0, null, 'Result cache restored. 0 files will be reanalysed.');
44+
$this->runCommand("$phpstan analyse -vv -c $config", $cwd, 0, null, 'Result cache restored. 0 files will be reanalysed.');
4145

4246
// cache should invalidate by editing the baseline
4347
file_put_contents($baselinesDirAbs . "/method.notFound.$extension", $emptyConfig);
4448

45-
$this->runCommand("$phpstan analyse -vv -c $extension.neon", $cwd, 1, 'Call to an undefined method DateTime::invalid()');
49+
$this->runCommand("$phpstan analyse -vv -c $config", $cwd, 1, 'Call to an undefined method DateTime::invalid()');
4650
}
4751

4852
/**
49-
* @return iterable<array{string}>
53+
* @return iterable<array{string, bool}>
5054
*/
5155
public function provideExtension(): iterable
5256
{
53-
yield 'Neon' => ['neon'];
54-
yield 'PHP' => ['php'];
57+
yield 'Neon' => ['neon', false];
58+
yield 'Neon (bleeding edge)' => ['neon', true];
59+
yield 'PHP' => ['php', false];
60+
yield 'PHP (bleeding edge)' => ['php', true];
5561
}
5662

5763
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
includes:
2+
- phar://phpstan.phar/conf/bleedingEdge.neon
3+
- neon.neon
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
includes:
2+
- phar://phpstan.phar/conf/bleedingEdge.neon
3+
- php.neon

tests/Rule/BaselinePerIdentifierFormatterTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ public function testFormat(): void
3636
$this->createOutput(),
3737
);
3838

39-
self::assertFileEquals(__DIR__ . '/data/baselines-neon/loader.neon', $fakeRoot . '/baselines/loader.neon');
40-
self::assertFileEquals(__DIR__ . '/data/baselines-neon/sample.identifier.neon', $fakeRoot . '/baselines/sample.identifier.neon');
41-
self::assertFileEquals(__DIR__ . '/data/baselines-neon/another.identifier.neon', $fakeRoot . '/baselines/another.identifier.neon');
42-
self::assertFileEquals(__DIR__ . '/data/baselines-neon/missing-identifier.neon', $fakeRoot . '/baselines/missing-identifier.neon');
39+
self::assertFileEquals(__DIR__ . '/data/baselines-neon-deprecated/loader.neon', $fakeRoot . '/baselines/loader.neon');
40+
self::assertFileEquals(__DIR__ . '/data/baselines-neon-deprecated/sample.identifier.neon', $fakeRoot . '/baselines/sample.identifier.neon');
41+
self::assertFileEquals(__DIR__ . '/data/baselines-neon-deprecated/another.identifier.neon', $fakeRoot . '/baselines/another.identifier.neon');
42+
self::assertFileEquals(__DIR__ . '/data/baselines-neon-deprecated/missing-identifier.neon', $fakeRoot . '/baselines/missing-identifier.neon');
4343
}
4444

4545
/**
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# total 1 error
2+
3+
parameters:
4+
ignoreErrors:
5+
-
6+
message: '#^Error to escape ''\#$#'
7+
count: 1
8+
path: ../app/config.php
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# total 4 errors
2+
3+
includes:
4+
- another.identifier.neon
5+
- missing-identifier.neon
6+
- sample.identifier.neon

0 commit comments

Comments
 (0)