Skip to content

Commit 7defb25

Browse files
committed
Introduce rawMessage key in ignoreErrors
1 parent 89d0610 commit 7defb25

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
@@ -143,6 +143,25 @@ parametersSchema:
143143
?identifiers: listOf(string())
144144
?reportUnmatched: bool()
145145
])
146+
structure([
147+
rawMessage: string()
148+
?identifier: string()
149+
?path: string()
150+
?reportUnmatched: bool()
151+
]),
152+
structure([
153+
rawMessage: string()
154+
count: int()
155+
path: string()
156+
?identifier: string()
157+
?reportUnmatched: bool()
158+
]),
159+
structure([
160+
rawMessage: string()
161+
paths: listOf(string())
162+
?identifier: string()
163+
?reportUnmatched: bool()
164+
]),
146165
)
147166
)
148167
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 === '') {
@@ -71,6 +73,7 @@ public static function shouldIgnore(
7173
FileHelper $fileHelper,
7274
Error $error,
7375
?string $ignoredErrorPattern,
76+
?string $ignoredErrorMessage,
7477
?string $identifier,
7578
?string $path,
7679
): bool
@@ -91,6 +94,12 @@ public static function shouldIgnore(
9194
}
9295
}
9396

97+
if ($ignoredErrorMessage !== null) {
98+
if ($error->getMessage() !== $ignoredErrorMessage) {
99+
return false;
100+
}
101+
}
102+
94103
if ($path !== null) {
95104
$fileExcluder = new FileExcluder($fileHelper, [$path]);
96105
$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']) && !isset($ignoreError['identifiers'])) {
43+
if (!isset($ignoreError['message']) && !isset($ignoreError['messages']) && !isset($ignoreError['rawMessage']) && !isset($ignoreError['identifier']) && !isset($ignoreError['identifiers'])) {
4444
$errors[] = sprintf(
4545
'Ignored error %s is missing a message or an identifier.',
4646
Json::encode($ignoreError),
@@ -71,7 +71,7 @@ public function initialize(): IgnoredErrorHelperResult
7171

7272
$uniquedExpandedIgnoreErrors = [];
7373
foreach ($expandedIgnoreErrors as $ignoreError) {
74-
if (!isset($ignoreError['message']) && !isset($ignoreError['identifier'])) {
74+
if (!isset($ignoreError['message']) && !isset($ignoreError['rawMessage']) && !isset($ignoreError['identifier'])) {
7575
$uniquedExpandedIgnoreErrors[] = $ignoreError;
7676
continue;
7777
}
@@ -84,6 +84,9 @@ public function initialize(): IgnoredErrorHelperResult
8484
if (isset($ignoreError['message'])) {
8585
$key = sprintf("%s\n%s", $key, $ignoreError['message']);
8686
}
87+
if (isset($ignoreError['rawMessage'])) {
88+
$key = sprintf("%s\n%s", $key, $ignoreError['rawMessage']);
89+
}
8790
if (isset($ignoreError['identifier'])) {
8891
$key = sprintf("%s\n%s", $key, $ignoreError['identifier']);
8992
}
@@ -98,6 +101,7 @@ public function initialize(): IgnoredErrorHelperResult
98101

99102
$uniquedExpandedIgnoreErrors[$key] = [
100103
'message' => $ignoreError['message'] ?? null,
104+
'rawMessage' => $ignoreError['rawMessage'] ?? null,
101105
'path' => $ignoreError['path'],
102106
'identifier' => $ignoreError['identifier'] ?? null,
103107
'count' => ($uniquedExpandedIgnoreErrors[$key]['count'] ?? 1) + ($ignoreError['count'] ?? 1),
@@ -114,7 +118,7 @@ public function initialize(): IgnoredErrorHelperResult
114118
];
115119
try {
116120
if (is_array($ignoreError)) {
117-
if (!isset($ignoreError['message']) && !isset($ignoreError['identifier'])) {
121+
if (!isset($ignoreError['message']) && !isset($ignoreError['rawMessage']) && !isset($ignoreError['identifier'])) {
118122
$errors[] = sprintf(
119123
'Ignored error %s is missing a message or an identifier.',
120124
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);
@@ -215,6 +237,31 @@ public static function dataIgnoreErrorByPathAndCount(): iterable
215237
],
216238
],
217239
];
240+
241+
yield [
242+
[
243+
[
244+
'rawMessage' => 'Fail.',
245+
'count' => 3,
246+
'path' => __DIR__ . '/data/two-fails.php',
247+
],
248+
],
249+
];
250+
251+
yield [
252+
[
253+
[
254+
'rawMessage' => 'Fail.',
255+
'count' => 2,
256+
'path' => __DIR__ . '/data/two-fails.php',
257+
],
258+
[
259+
'rawMessage' => 'Fail.',
260+
'count' => 1,
261+
'path' => __DIR__ . '/data/two-fails.php',
262+
],
263+
],
264+
];
218265
}
219266

220267
/**
@@ -351,6 +398,18 @@ public function testIgnoreErrorByPaths(): void
351398
$this->assertNoErrors($result);
352399
}
353400

401+
public function testIgnoreErrorRawByPaths(): void
402+
{
403+
$ignoreErrors = [
404+
[
405+
'rawMessage' => 'Fail.',
406+
'paths' => [__DIR__ . '/data/bootstrap-error.php'],
407+
],
408+
];
409+
$result = $this->runAnalyser($ignoreErrors, true, __DIR__ . '/data/bootstrap-error.php', false);
410+
$this->assertNoErrors($result);
411+
}
412+
354413
public function testIgnoreErrorMultiByPaths(): void
355414
{
356415
$ignoreErrors = [
@@ -606,6 +665,18 @@ public function testIgnoreErrorExplicitReportUnmatchedDisable(): void
606665
$this->assertNoErrors($result);
607666
}
608667

668+
public function testIgnoreErrorExplicitReportUnmatchedDisableRaw(): void
669+
{
670+
$ignoreErrors = [
671+
[
672+
'rawMessage' => 'Fail.',
673+
'reportUnmatched' => false,
674+
],
675+
];
676+
$result = $this->runAnalyser($ignoreErrors, true, __DIR__ . '/data/bootstrap.php', false);
677+
$this->assertNoErrors($result);
678+
}
679+
609680
public function testIgnoreErrorExplicitReportUnmatchedDisableMulti(): void
610681
{
611682
$ignoreErrors = [
@@ -631,6 +702,19 @@ public function testIgnoreErrorExplicitReportUnmatchedEnable(): void
631702
$this->assertSame('Ignored error pattern #Fail# was not matched in reported errors.', $result[0]);
632703
}
633704

705+
public function testIgnoreErrorExplicitReportUnmatchedEnableRaw(): void
706+
{
707+
$ignoreErrors = [
708+
[
709+
'rawMessage' => 'Fail.',
710+
'reportUnmatched' => true,
711+
],
712+
];
713+
$result = $this->runAnalyser($ignoreErrors, false, __DIR__ . '/data/bootstrap.php', false);
714+
$this->assertCount(1, $result);
715+
$this->assertSame('Ignored error pattern "Fail." was not matched in reported errors.', $result[0]);
716+
}
717+
634718
public function testIgnoreErrorExplicitReportUnmatchedEnableMulti(): void
635719
{
636720
$ignoreErrors = [

0 commit comments

Comments
 (0)