Skip to content

Commit

Permalink
ignoreErrors fix reportUnmatched without path/paths
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinMystikJonas committed Sep 28, 2022
1 parent d2b627e commit e04715d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Analyser/IgnoredErrorHelperResult.php
Expand Up @@ -104,7 +104,10 @@ public function process(
break;
}
} else {
throw new ShouldNotHappenException();
$shouldBeIgnored = IgnoredError::shouldIgnore($this->fileHelper, $error, $ignore['message'], null);
if ($shouldBeIgnored) {
unset($unmatchedIgnoredErrors[$i]);
}
}
}

Expand Down
55 changes: 55 additions & 0 deletions tests/PHPStan/Analyser/AnalyserTest.php
Expand Up @@ -260,6 +260,22 @@ public function testIgnoreErrorByPathsUnmatched(): void
$this->assertStringContainsString('was not matched in reported errors', $result[0]);
}

public function testIgnoreErrorByPathsUnmatchedExplicitReportUnmatched(): void
{
$ignoreErrors = [
[
'message' => '#Fail\.#',
'paths' => [__DIR__ . '/data/bootstrap-error.php', __DIR__ . '/data/another-path.php'],
'reportUnmatched' => true,
],
];
$result = $this->runAnalyser($ignoreErrors, false, __DIR__ . '/data/bootstrap-error.php', false);
$this->assertCount(1, $result);
$this->assertIsString($result[0]);
$this->assertStringContainsString('Ignored error pattern #Fail\.# in path ', $result[0]);
$this->assertStringContainsString('was not matched in reported errors', $result[0]);
}

public function testIgnoreErrorNotFoundInPath(): void
{
$ignoreErrors = [
Expand All @@ -273,6 +289,20 @@ public function testIgnoreErrorNotFoundInPath(): void
$this->assertSame('Ignored error pattern #Fail\.# in path ' . __DIR__ . '/data/not-existent-path.php was not matched in reported errors.', $result[0]);
}

public function testIgnoreErrorNotFoundInPathExplicitReportUnmatched(): void
{
$ignoreErrors = [
[
'message' => '#Fail\.#',
'path' => __DIR__ . '/data/not-existent-path.php',
'reportUnmatched' => true,
],
];
$result = $this->runAnalyser($ignoreErrors, false, __DIR__ . '/data/empty/empty.php', false);
$this->assertCount(1, $result);
$this->assertSame('Ignored error pattern #Fail\.# in path ' . __DIR__ . '/data/not-existent-path.php was not matched in reported errors.', $result[0]);
}

public function dataIgnoreErrorInTraitUsingClassFilePath(): array
{
return [
Expand Down Expand Up @@ -458,6 +488,18 @@ public function testIgnoreErrorExplicitReportUnmatchedDisable(): void
$this->assertNoErrors($result);
}

public function testIgnoreErrorExplicitReportUnmatchedDisableMulti(): void
{
$ignoreErrors = [
[
'message' => ['#Fail#'],
'reportUnmatched' => false,
],
];
$result = $this->runAnalyser($ignoreErrors, true, __DIR__ . '/data/bootstrap.php', false);
$this->assertNoErrors($result);
}

public function testIgnoreErrorExplicitReportUnmatchedEnable(): void
{
$ignoreErrors = [
Expand All @@ -471,6 +513,19 @@ public function testIgnoreErrorExplicitReportUnmatchedEnable(): void
$this->assertSame('Ignored error pattern #Fail# was not matched in reported errors.', $result[0]);
}

public function testIgnoreErrorExplicitReportUnmatchedEnableMulti(): void
{
$ignoreErrors = [
[
'messages' => ['#Fail#'],
'reportUnmatched' => true,
],
];
$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]);
}

/**
* @param mixed[] $ignoreErrors
* @param string|string[] $filePaths
Expand Down

0 comments on commit e04715d

Please sign in to comment.