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 29, 2022
1 parent d2b627e commit 4c3119b
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 21 deletions.
7 changes: 0 additions & 7 deletions src/Analyser/IgnoredErrorHelper.php
Expand Up @@ -69,13 +69,6 @@ public function initialize(): IgnoredErrorHelperResult
continue;
}
if (!isset($ignoreError['path'])) {
if (!isset($ignoreError['paths']) && !isset($ignoreError['reportUnmatched'])) {
$errors[] = sprintf(
'Ignored error %s is missing a path, paths or reportUnmatched.',
Json::encode($ignoreError),
);
}

$otherIgnoreErrors[] = $ignoreErrorEntry;
} elseif (@is_file($ignoreError['path'])) {
$normalizedPath = $this->fileHelper->normalizePath($ignoreError['path']);
Expand Down
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
79 changes: 67 additions & 12 deletions tests/PHPStan/Analyser/AnalyserTest.php
Expand Up @@ -61,6 +61,18 @@ public function testFileWithAnIgnoredError(): void
$this->assertEmpty($result);
}

public function testFileWithAnIgnoredErrorMessage(): void
{
$result = $this->runAnalyser([['message' => '#Fail\.#']], true, __DIR__ . '/data/bootstrap-error.php', false);
$this->assertEmpty($result);
}

public function testFileWithAnIgnoredErrorMessages(): void
{
$result = $this->runAnalyser([['messages' => ['#Fail\.#']]], true, __DIR__ . '/data/bootstrap-error.php', false);
$this->assertEquals([], $result);
}

public function testIgnoringBrokenConfigurationDoesNotWork(): void
{
$this->markTestIncomplete();
Expand Down Expand Up @@ -260,6 +272,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 +301,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 @@ -322,18 +364,6 @@ public function testIgnoredErrorMissingMessage(): void
$this->assertSame('Ignored error {"path":"' . $expectedPath . '/data/empty/empty.php"} is missing a message.', $result[0]);
}

public function testIgnoredErrorMissingPath(): void
{
$ignoreErrors = [
[
'message' => '#Fail\.#',
],
];
$result = $this->runAnalyser($ignoreErrors, true, __DIR__ . '/data/empty/empty.php', false);
$this->assertCount(1, $result);
$this->assertSame('Ignored error {"message":"#Fail\\\\.#"} is missing a path, paths or reportUnmatched.', $result[0]);
}

public function testReportMultipleParserErrorsAtOnce(): void
{
$result = $this->runAnalyser([], false, __DIR__ . '/data/multipleParseErrors.php', false);
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
2 changes: 1 addition & 1 deletion tests/PHPStan/DependencyInjection/IgnoreErrorsTest.php
Expand Up @@ -9,7 +9,7 @@ class IgnoreErrorsTest extends PHPStanTestCase

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

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/DependencyInjection/ignoreErrors.neon
@@ -1,6 +1,8 @@
parameters:
ignoreErrors:
- "#error#"
-
message: '#error#'
-
message: '#error#'
reportUnmatched: false
Expand All @@ -11,6 +13,9 @@ parameters:
message: '#error#'
path: '/dir/*'
reportUnmatched: false
-
messages:
- '#error#'
-
messages:
- '#error#'
Expand Down

0 comments on commit 4c3119b

Please sign in to comment.