Skip to content

Add support for multiple identifiers in ignoreErrors configuration #4080

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions conf/parametersSchema.neon
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,14 @@ parametersSchema:
structure([
?messages: listOf(string())
?identifier: string()
?identifiers: listOf(string())
?path: string()
?reportUnmatched: bool()
]),
structure([
?message: string()
?identifier: string()
?identifiers: listOf(string())
?path: string()
?reportUnmatched: bool()
]),
Expand All @@ -123,18 +125,21 @@ parametersSchema:
count: int()
path: string()
?identifier: string()
?identifiers: listOf(string())
?reportUnmatched: bool()
]),
structure([
?message: string()
paths: listOf(string())
?identifier: string()
?identifiers: listOf(string())
?reportUnmatched: bool()
]),
structure([
?messages: listOf(string())
paths: listOf(string())
?identifier: string()
?identifiers: listOf(string())
?reportUnmatched: bool()
])
)
Expand Down
7 changes: 7 additions & 0 deletions src/Analyser/Ignore/IgnoredError.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ public static function stringifyPattern($ignoredError): string
} else {
$message = sprintf('%s (%s)', $message, $ignoredError['identifier']);
}
} elseif (isset($ignoredError['identifiers'])) {
$identifierList = implode(', ', $ignoredError['identifiers']);
if ($message === '') {
$message = $identifierList;
} else {
$message = sprintf('%s (%s)', $message, $identifierList);
}
}

if ($message === '') {
Expand Down
9 changes: 8 additions & 1 deletion src/Analyser/Ignore/IgnoredErrorHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function initialize(): IgnoredErrorHelperResult
$expandedIgnoreErrors = [];
foreach ($this->ignoreErrors as $ignoreError) {
if (is_array($ignoreError)) {
if (!isset($ignoreError['message']) && !isset($ignoreError['messages']) && !isset($ignoreError['identifier'])) {
if (!isset($ignoreError['message']) && !isset($ignoreError['messages']) && !isset($ignoreError['identifier']) && !isset($ignoreError['identifiers'])) {
$errors[] = sprintf(
'Ignored error %s is missing a message or an identifier.',
Json::encode($ignoreError),
Expand All @@ -54,6 +54,13 @@ public function initialize(): IgnoredErrorHelperResult
$expandedIgnoreError['message'] = $message;
$expandedIgnoreErrors[] = $expandedIgnoreError;
}
} elseif (isset($ignoreError['identifiers'])) {
foreach ($ignoreError['identifiers'] as $identifier) {
$expandedIgnoreError = $ignoreError;
unset($expandedIgnoreError['identifiers']);
$expandedIgnoreError['identifier'] = $identifier;
$expandedIgnoreErrors[] = $expandedIgnoreError;
}
} else {
$expandedIgnoreErrors[] = $ignoreError;
}
Expand Down
22 changes: 22 additions & 0 deletions tests/PHPStan/Analyser/AnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,28 @@ public function testFileWithAnIgnoredErrorMessages(): void
$this->assertEquals([], $result);
}

public function testFileWithAnIgnoredErrorIdentifiers(): void
{
$result = $this->runAnalyser([['identifiers' => ['tests.alwaysFail']]], true, __DIR__ . '/data/bootstrap-error.php', false);
$this->assertEmpty($result);
}

public function testFileWithAnIgnoredErrorIdentifiersWithPath(): void
{
$result = $this->runAnalyser([['identifiers' => ['tests.alwaysFail'], 'path' => __DIR__ . '/data/bootstrap-error.php']], true, __DIR__ . '/data/bootstrap-error.php', false);
$this->assertEmpty($result);
}

public function testFileWithAnIgnoredErrorIdentifiersWithWrongIdentifier(): void
{
$result = $this->runAnalyser([['identifiers' => ['wrong.identifier']]], true, __DIR__ . '/data/bootstrap-error.php', false);
$this->assertCount(2, $result);
assert($result[0] instanceof Error);
$this->assertSame('Fail.', $result[0]->getMessage());
assert(is_string($result[1]));
$this->assertSame('Ignored error pattern wrong.identifier was not matched in reported errors.', $result[1]);
}

public function testIgnoringBrokenConfigurationDoesNotWork(): void
{
$this->markTestIncomplete();
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/DependencyInjection/IgnoreErrorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class IgnoreErrorsTest extends PHPStanTestCase

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

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/PHPStan/DependencyInjection/ignoreErrors.neon
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,21 @@ parameters:
paths:
- '/dir/*'
reportUnmatched: false
-
identifiers:
- 'error.identifier'
-
identifiers:
- 'error.identifier'
path: '/dir/*'
-
identifiers:
- 'error.identifier'
paths:
- '/dir/*'
-
identifiers:
- 'error.identifier'
- 'another.identifier'
path: '/dir/*'
reportUnmatched: false
Loading