Skip to content

Commit 5181c1f

Browse files
committed
Introduce rawMessage key in ignoreErrors
1 parent 5878035 commit 5181c1f

File tree

5 files changed

+123
-7
lines changed

5 files changed

+123
-7
lines changed

conf/parametersSchema.neon

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,25 @@ parametersSchema:
138138
?identifier: string()
139139
?reportUnmatched: bool()
140140
])
141+
structure([
142+
rawMessage: string()
143+
?identifier: string()
144+
?path: string()
145+
?reportUnmatched: bool()
146+
]),
147+
structure([
148+
rawMessage: string()
149+
count: int()
150+
path: string()
151+
?identifier: string()
152+
?reportUnmatched: bool()
153+
]),
154+
structure([
155+
rawMessage: string()
156+
paths: listOf(string())
157+
?identifier: string()
158+
?reportUnmatched: bool()
159+
]),
141160
)
142161
)
143162
internalErrorsCountLimit: int()

src/Analyser/Ignore/IgnoredError.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public static function stringifyPattern($ignoredError): string
3030
$message = '';
3131
if (isset($ignoredError['message'])) {
3232
$message = $ignoredError['message'];
33+
} elseif (isset($ignoredError['rawMessage'])) {
34+
$message = '"' . $ignoredError['rawMessage'] . '"';
3335
}
3436
if (isset($ignoredError['identifier'])) {
3537
if ($message === '') {
@@ -64,6 +66,7 @@ public static function shouldIgnore(
6466
FileHelper $fileHelper,
6567
Error $error,
6668
?string $ignoredErrorPattern,
69+
?string $ignoredErrorMessage,
6770
?string $identifier,
6871
?string $path,
6972
): bool
@@ -84,6 +87,12 @@ public static function shouldIgnore(
8487
}
8588
}
8689

90+
if ($ignoredErrorMessage !== null) {
91+
if ($error->getMessage() !== $ignoredErrorMessage) {
92+
return false;
93+
}
94+
}
95+
8796
if ($path !== null) {
8897
$fileExcluder = new FileExcluder($fileHelper, [$path]);
8998
$isExcluded = $fileExcluder->isExcludedFromAnalysing($error->getFilePath());

src/Analyser/Ignore/IgnoredErrorHelper.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function initialize(): IgnoredErrorHelperResult
4040
$expandedIgnoreErrors = [];
4141
foreach ($this->ignoreErrors as $ignoreError) {
4242
if (is_array($ignoreError)) {
43-
if (!isset($ignoreError['message']) && !isset($ignoreError['messages']) && !isset($ignoreError['identifier'])) {
43+
if (!isset($ignoreError['message']) && !isset($ignoreError['messages']) && !isset($ignoreError['rawMessage']) && !isset($ignoreError['identifier'])) {
4444
$errors[] = sprintf(
4545
'Ignored error %s is missing a message or an identifier.',
4646
Json::encode($ignoreError),
@@ -64,7 +64,7 @@ public function initialize(): IgnoredErrorHelperResult
6464

6565
$uniquedExpandedIgnoreErrors = [];
6666
foreach ($expandedIgnoreErrors as $ignoreError) {
67-
if (!isset($ignoreError['message']) && !isset($ignoreError['identifier'])) {
67+
if (!isset($ignoreError['message']) && !isset($ignoreError['rawMessage']) && !isset($ignoreError['identifier'])) {
6868
$uniquedExpandedIgnoreErrors[] = $ignoreError;
6969
continue;
7070
}
@@ -77,6 +77,9 @@ public function initialize(): IgnoredErrorHelperResult
7777
if (isset($ignoreError['message'])) {
7878
$key = sprintf("%s\n%s", $key, $ignoreError['message']);
7979
}
80+
if (isset($ignoreError['rawMessage'])) {
81+
$key = sprintf("%s\n%s", $key, $ignoreError['rawMessage']);
82+
}
8083
if (isset($ignoreError['identifier'])) {
8184
$key = sprintf("%s\n%s", $key, $ignoreError['identifier']);
8285
}
@@ -91,6 +94,7 @@ public function initialize(): IgnoredErrorHelperResult
9194

9295
$uniquedExpandedIgnoreErrors[$key] = [
9396
'message' => $ignoreError['message'] ?? null,
97+
'rawMessage' => $ignoreError['rawMessage'] ?? null,
9498
'path' => $ignoreError['path'],
9599
'identifier' => $ignoreError['identifier'] ?? null,
96100
'count' => ($uniquedExpandedIgnoreErrors[$key]['count'] ?? 1) + ($ignoreError['count'] ?? 1),
@@ -107,7 +111,7 @@ public function initialize(): IgnoredErrorHelperResult
107111
];
108112
try {
109113
if (is_array($ignoreError)) {
110-
if (!isset($ignoreError['message']) && !isset($ignoreError['identifier'])) {
114+
if (!isset($ignoreError['message']) && !isset($ignoreError['rawMessage']) && !isset($ignoreError['identifier'])) {
111115
$errors[] = sprintf(
112116
'Ignored error %s is missing a message or an identifier.',
113117
Json::encode($ignoreError),

src/Analyser/Ignore/IgnoredErrorHelperResult.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ public function process(
5858
$processIgnoreError = function (Error $error, int $i, $ignore) use (&$unmatchedIgnoredErrors, &$stringErrors): bool {
5959
$shouldBeIgnored = false;
6060
if (is_string($ignore)) {
61-
$shouldBeIgnored = IgnoredError::shouldIgnore($this->fileHelper, $error, $ignore, null, null);
61+
$shouldBeIgnored = IgnoredError::shouldIgnore($this->fileHelper, $error, $ignore, null, null, null);
6262
if ($shouldBeIgnored) {
6363
unset($unmatchedIgnoredErrors[$i]);
6464
}
6565
} else {
6666
if (isset($ignore['path'])) {
67-
$shouldBeIgnored = IgnoredError::shouldIgnore($this->fileHelper, $error, $ignore['message'] ?? null, $ignore['identifier'] ?? null, $ignore['path']);
67+
$shouldBeIgnored = IgnoredError::shouldIgnore($this->fileHelper, $error, $ignore['message'] ?? null, $ignore['rawMessage'] ?? null, $ignore['identifier'] ?? null, $ignore['path']);
6868
if ($shouldBeIgnored) {
6969
if (isset($ignore['count'])) {
7070
$realCount = $unmatchedIgnoredErrors[$i]['realCount'] ?? 0;
@@ -85,7 +85,7 @@ public function process(
8585
}
8686
} elseif (isset($ignore['paths'])) {
8787
foreach ($ignore['paths'] as $j => $ignorePath) {
88-
$shouldBeIgnored = IgnoredError::shouldIgnore($this->fileHelper, $error, $ignore['message'] ?? null, $ignore['identifier'] ?? null, $ignorePath);
88+
$shouldBeIgnored = IgnoredError::shouldIgnore($this->fileHelper, $error, $ignore['message'] ?? null, $ignore['rawMessage'] ?? null, $ignore['identifier'] ?? null, $ignorePath);
8989
if (!$shouldBeIgnored) {
9090
continue;
9191
}
@@ -102,7 +102,7 @@ public function process(
102102
break;
103103
}
104104
} else {
105-
$shouldBeIgnored = IgnoredError::shouldIgnore($this->fileHelper, $error, $ignore['message'] ?? null, $ignore['identifier'] ?? null, null);
105+
$shouldBeIgnored = IgnoredError::shouldIgnore($this->fileHelper, $error, $ignore['message'] ?? null, $ignore['rawMessage'] ?? null, $ignore['identifier'] ?? null, null);
106106
if ($shouldBeIgnored) {
107107
unset($unmatchedIgnoredErrors[$i]);
108108
}

tests/PHPStan/Analyser/AnalyserTest.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ public function testFileWithAnIgnoredErrorMessage(): void
7878
$this->assertEmpty($result);
7979
}
8080

81+
public function testFileWithAnIgnoredErrorRawMessage(): void
82+
{
83+
$result = $this->runAnalyser([['rawMessage' => 'Fail.']], true, __DIR__ . '/data/bootstrap-error.php', false);
84+
$this->assertEmpty($result);
85+
}
86+
8187
public function testFileWithAnIgnoredErrorMessageAndWrongIdentifier(): void
8288
{
8389
$result = $this->runAnalyser([['message' => '#Fail\.#', 'identifier' => 'wrong.identifier']], true, __DIR__ . '/data/bootstrap-error.php', false);
@@ -88,6 +94,16 @@ public function testFileWithAnIgnoredErrorMessageAndWrongIdentifier(): void
8894
$this->assertSame('Ignored error pattern #Fail\.# (wrong.identifier) was not matched in reported errors.', $result[1]);
8995
}
9096

97+
public function testFileWithAnIgnoredErrorRawMessageAndWrongIdentifier(): void
98+
{
99+
$result = $this->runAnalyser([['rawMessage' => 'Fail.', 'identifier' => 'wrong.identifier']], true, __DIR__ . '/data/bootstrap-error.php', false);
100+
$this->assertCount(2, $result);
101+
assert($result[0] instanceof Error);
102+
$this->assertSame('Fail.', $result[0]->getMessage());
103+
assert(is_string($result[1]));
104+
$this->assertSame('Ignored error pattern "Fail." (wrong.identifier) was not matched in reported errors.', $result[1]);
105+
}
106+
91107
public function testFileWithAnIgnoredWrongIdentifier(): void
92108
{
93109
$result = $this->runAnalyser([['identifier' => 'wrong.identifier']], true, __DIR__ . '/data/bootstrap-error.php', false);
@@ -104,6 +120,12 @@ public function testFileWithAnIgnoredErrorMessageAndCorrectIdentifier(): void
104120
$this->assertEmpty($result);
105121
}
106122

123+
public function testFileWithAnIgnoredErrorRawMessageAndCorrectIdentifier(): void
124+
{
125+
$result = $this->runAnalyser([['rawMessage' => 'Fail.', 'identifier' => 'tests.alwaysFail']], true, __DIR__ . '/data/bootstrap-error.php', false);
126+
$this->assertEmpty($result);
127+
}
128+
107129
public function testFileWithAnIgnoredErrorIdentifier(): void
108130
{
109131
$result = $this->runAnalyser([['identifier' => 'tests.alwaysFail']], true, __DIR__ . '/data/bootstrap-error.php', false);
@@ -193,6 +215,31 @@ public static function dataIgnoreErrorByPathAndCount(): iterable
193215
],
194216
],
195217
];
218+
219+
yield [
220+
[
221+
[
222+
'rawMessage' => 'Fail.',
223+
'count' => 3,
224+
'path' => __DIR__ . '/data/two-fails.php',
225+
],
226+
],
227+
];
228+
229+
yield [
230+
[
231+
[
232+
'rawMessage' => 'Fail.',
233+
'count' => 2,
234+
'path' => __DIR__ . '/data/two-fails.php',
235+
],
236+
[
237+
'rawMessage' => 'Fail.',
238+
'count' => 1,
239+
'path' => __DIR__ . '/data/two-fails.php',
240+
],
241+
],
242+
];
196243
}
197244

198245
/**
@@ -329,6 +376,18 @@ public function testIgnoreErrorByPaths(): void
329376
$this->assertNoErrors($result);
330377
}
331378

379+
public function testIgnoreErrorRawByPaths(): void
380+
{
381+
$ignoreErrors = [
382+
[
383+
'rawMessage' => 'Fail.',
384+
'paths' => [__DIR__ . '/data/bootstrap-error.php'],
385+
],
386+
];
387+
$result = $this->runAnalyser($ignoreErrors, true, __DIR__ . '/data/bootstrap-error.php', false);
388+
$this->assertNoErrors($result);
389+
}
390+
332391
public function testIgnoreErrorMultiByPaths(): void
333392
{
334393
$ignoreErrors = [
@@ -584,6 +643,18 @@ public function testIgnoreErrorExplicitReportUnmatchedDisable(): void
584643
$this->assertNoErrors($result);
585644
}
586645

646+
public function testIgnoreErrorExplicitReportUnmatchedDisableRaw(): void
647+
{
648+
$ignoreErrors = [
649+
[
650+
'rawMessage' => 'Fail.',
651+
'reportUnmatched' => false,
652+
],
653+
];
654+
$result = $this->runAnalyser($ignoreErrors, true, __DIR__ . '/data/bootstrap.php', false);
655+
$this->assertNoErrors($result);
656+
}
657+
587658
public function testIgnoreErrorExplicitReportUnmatchedDisableMulti(): void
588659
{
589660
$ignoreErrors = [
@@ -609,6 +680,19 @@ public function testIgnoreErrorExplicitReportUnmatchedEnable(): void
609680
$this->assertSame('Ignored error pattern #Fail# was not matched in reported errors.', $result[0]);
610681
}
611682

683+
public function testIgnoreErrorExplicitReportUnmatchedEnableRaw(): void
684+
{
685+
$ignoreErrors = [
686+
[
687+
'rawMessage' => 'Fail.',
688+
'reportUnmatched' => true,
689+
],
690+
];
691+
$result = $this->runAnalyser($ignoreErrors, false, __DIR__ . '/data/bootstrap.php', false);
692+
$this->assertCount(1, $result);
693+
$this->assertSame('Ignored error pattern "Fail." was not matched in reported errors.', $result[0]);
694+
}
695+
612696
public function testIgnoreErrorExplicitReportUnmatchedEnableMulti(): void
613697
{
614698
$ignoreErrors = [

0 commit comments

Comments
 (0)