Skip to content

Commit 2247f0c

Browse files
authored
Add support for multiple identifiers in ignoreErrors configuration
1 parent 13898d4 commit 2247f0c

File tree

6 files changed

+61
-2
lines changed

6 files changed

+61
-2
lines changed

conf/parametersSchema.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,14 @@ parametersSchema:
110110
structure([
111111
?messages: listOf(string())
112112
?identifier: string()
113+
?identifiers: listOf(string())
113114
?path: string()
114115
?reportUnmatched: bool()
115116
]),
116117
structure([
117118
?message: string()
118119
?identifier: string()
120+
?identifiers: listOf(string())
119121
?path: string()
120122
?reportUnmatched: bool()
121123
]),
@@ -124,18 +126,21 @@ parametersSchema:
124126
count: int()
125127
path: string()
126128
?identifier: string()
129+
?identifiers: listOf(string())
127130
?reportUnmatched: bool()
128131
]),
129132
structure([
130133
?message: string()
131134
paths: listOf(string())
132135
?identifier: string()
136+
?identifiers: listOf(string())
133137
?reportUnmatched: bool()
134138
]),
135139
structure([
136140
?messages: listOf(string())
137141
paths: listOf(string())
138142
?identifier: string()
143+
?identifiers: listOf(string())
139144
?reportUnmatched: bool()
140145
])
141146
)

src/Analyser/Ignore/IgnoredError.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ public static function stringifyPattern($ignoredError): string
3737
} else {
3838
$message = sprintf('%s (%s)', $message, $ignoredError['identifier']);
3939
}
40+
} elseif (isset($ignoredError['identifiers'])) {
41+
$identifierList = implode(', ', $ignoredError['identifiers']);
42+
if ($message === '') {
43+
$message = $identifierList;
44+
} else {
45+
$message = sprintf('%s (%s)', $message, $identifierList);
46+
}
4047
}
4148

4249
if ($message === '') {

src/Analyser/Ignore/IgnoredErrorHelper.php

Lines changed: 8 additions & 1 deletion
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['identifier']) && !isset($ignoreError['identifiers'])) {
4444
$errors[] = sprintf(
4545
'Ignored error %s is missing a message or an identifier.',
4646
Json::encode($ignoreError),
@@ -54,6 +54,13 @@ public function initialize(): IgnoredErrorHelperResult
5454
$expandedIgnoreError['message'] = $message;
5555
$expandedIgnoreErrors[] = $expandedIgnoreError;
5656
}
57+
} elseif (isset($ignoreError['identifiers'])) {
58+
foreach ($ignoreError['identifiers'] as $identifier) {
59+
$expandedIgnoreError = $ignoreError;
60+
unset($expandedIgnoreError['identifiers']);
61+
$expandedIgnoreError['identifier'] = $identifier;
62+
$expandedIgnoreErrors[] = $expandedIgnoreError;
63+
}
5764
} else {
5865
$expandedIgnoreErrors[] = $ignoreError;
5966
}

tests/PHPStan/Analyser/AnalyserTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,28 @@ public function testFileWithAnIgnoredErrorMessages(): void
116116
$this->assertEquals([], $result);
117117
}
118118

119+
public function testFileWithAnIgnoredErrorIdentifiers(): void
120+
{
121+
$result = $this->runAnalyser([['identifiers' => ['tests.alwaysFail']]], true, __DIR__ . '/data/bootstrap-error.php', false);
122+
$this->assertNoErrors($result);
123+
}
124+
125+
public function testFileWithAnIgnoredErrorIdentifiersWithPath(): void
126+
{
127+
$result = $this->runAnalyser([['identifiers' => ['tests.alwaysFail'], 'path' => __DIR__ . '/data/bootstrap-error.php']], true, __DIR__ . '/data/bootstrap-error.php', false);
128+
$this->assertNoErrors($result);
129+
}
130+
131+
public function testFileWithAnIgnoredErrorIdentifiersWithWrongIdentifier(): void
132+
{
133+
$result = $this->runAnalyser([['identifiers' => ['wrong.identifier']]], true, __DIR__ . '/data/bootstrap-error.php', false);
134+
$this->assertCount(2, $result);
135+
$this->assertInstanceOf(Error::class, $result[0]);
136+
$this->assertSame('Fail.', $result[0]->getMessage());
137+
assert(is_string($result[1]));
138+
$this->assertSame('Ignored error pattern wrong.identifier was not matched in reported errors.', $result[1]);
139+
}
140+
119141
public function testIgnoringBrokenConfigurationDoesNotWork(): void
120142
{
121143
$this->markTestIncomplete();

tests/PHPStan/DependencyInjection/IgnoreErrorsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class IgnoreErrorsTest extends PHPStanTestCase
99

1010
public function testIgnoreErrors(): void
1111
{
12-
$this->assertCount(12, self::getContainer()->getParameter('ignoreErrors'));
12+
$this->assertCount(16, self::getContainer()->getParameter('ignoreErrors'));
1313
}
1414

1515
/**

tests/PHPStan/DependencyInjection/ignoreErrors.neon

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,21 @@ parameters:
4545
paths:
4646
- '/dir/*'
4747
reportUnmatched: false
48+
-
49+
identifiers:
50+
- 'error.identifier'
51+
-
52+
identifiers:
53+
- 'error.identifier'
54+
path: '/dir/*'
55+
-
56+
identifiers:
57+
- 'error.identifier'
58+
paths:
59+
- '/dir/*'
60+
-
61+
identifiers:
62+
- 'error.identifier'
63+
- 'another.identifier'
64+
path: '/dir/*'
65+
reportUnmatched: false

0 commit comments

Comments
 (0)