Skip to content
Merged
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
12 changes: 12 additions & 0 deletions src/Analyser/Ignore/IgnoredError.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@
final class IgnoredError
{

/**
* @param array{message?: string, rawMessage?: string, identifier?: string, identifiers?: list<string>, path?: string, paths?: list<string>}|string $ignoredError
*/
public static function getIgnoredErrorLabel(array|string $ignoredError): string
{
if (is_array($ignoredError) && !isset($ignoredError['message']) && isset($ignoredError['rawMessage'])) {
return 'Ignored error';
}

return 'Ignored error pattern';
}

/**
* @param mixed[]|string $ignoredError
* @return string Representation of the ignored error
Expand Down
12 changes: 8 additions & 4 deletions src/Analyser/Ignore/IgnoredErrorHelperResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ public function process(
}

$errors[] = (new Error(sprintf(
'Ignored error pattern %s is expected to occur %d %s, but occurred %d %s.',
'%s %s is expected to occur %d %s, but occurred %d %s.',
IgnoredError::getIgnoredErrorLabel($unmatchedIgnoredError),
IgnoredError::stringifyPattern($unmatchedIgnoredError),
$unmatchedIgnoredError['count'],
$unmatchedIgnoredError['count'] === 1 ? 'time' : 'times',
Expand All @@ -205,7 +206,8 @@ public function process(
) {
if ($unmatchedIgnoredError['realCount'] < $unmatchedIgnoredError['count']) {
$errors[] = (new Error(sprintf(
'Ignored error pattern %s is expected to occur %d %s, but occurred only %d %s.',
'%s %s is expected to occur %d %s, but occurred only %d %s.',
IgnoredError::getIgnoredErrorLabel($unmatchedIgnoredError),
IgnoredError::stringifyPattern($unmatchedIgnoredError),
$unmatchedIgnoredError['count'],
$unmatchedIgnoredError['count'] === 1 ? 'time' : 'times',
Expand All @@ -224,15 +226,17 @@ public function process(

$errors[] = (new Error(
sprintf(
'Ignored error pattern %s was not matched in reported errors.',
'%s %s was not matched in reported errors.',
IgnoredError::getIgnoredErrorLabel($unmatchedIgnoredError),
IgnoredError::stringifyPattern($unmatchedIgnoredError),
),
$unmatchedIgnoredError['realPath'],
canBeIgnored: false,
))->withIdentifier('ignore.unmatched');
} elseif (!$onlyFiles) {
$stringErrors[] = sprintf(
'Ignored error pattern %s was not matched in reported errors.',
'%s %s was not matched in reported errors.',
IgnoredError::getIgnoredErrorLabel($unmatchedIgnoredError),
IgnoredError::stringifyPattern($unmatchedIgnoredError),
);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/AnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function testFileWithAnIgnoredErrorRawMessageAndWrongIdentifier(): void
assert($result[0] instanceof Error);
$this->assertSame('Fail.', $result[0]->getMessage());
assert(is_string($result[1]));
$this->assertSame('Ignored error pattern "Fail." (wrong.identifier) was not matched in reported errors.', $result[1]);
$this->assertSame('Ignored error "Fail." (wrong.identifier) was not matched in reported errors.', $result[1]);
}

public function testFileWithAnIgnoredWrongIdentifier(): void
Expand Down Expand Up @@ -721,7 +721,7 @@ public function testIgnoreErrorExplicitReportUnmatchedEnableRaw(): void
];
$result = $this->runAnalyser($ignoreErrors, false, __DIR__ . '/data/bootstrap.php', false);
$this->assertCount(1, $result);
$this->assertSame('Ignored error pattern "Fail." was not matched in reported errors.', $result[0]);
$this->assertSame('Ignored error "Fail." was not matched in reported errors.', $result[0]);
}

public function testIgnoreErrorExplicitReportUnmatchedEnableMulti(): void
Expand Down
Loading