Skip to content

Commit

Permalink
Allow multiple paths per ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 18, 2019
1 parent 4dc595c commit 105e83e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 12 deletions.
6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -332,7 +332,7 @@ parameters:
- '#Call to an undefined method PHPUnit_Framework_MockObject_MockObject::[a-zA-Z0-9_]+\(\)#'
```

To exclude an error in a specific directory or file, specify a `path` along with the `message`:
To exclude an error in a specific directory or file, specify a `path` or `paths` along with the `message`:

```
parameters:
Expand All @@ -342,7 +342,9 @@ parameters:
path: %currentWorkingDirectory%/some/dir/SomeFile.php
-
message: '#Call to an undefined method [a-zA-Z0-9\\_]+::method\(\)#'
path: %currentWorkingDirectory%/other/dir/*
paths:
- %currentWorkingDirectory%/some/dir/*
- %currentWorkingDirectory%/other/dir/*
- '#Other error to catch anywhere#'
```

Expand Down
4 changes: 4 additions & 0 deletions conf/config.neon
Expand Up @@ -120,6 +120,10 @@ parametersSchema:
structure([
message: string()
path: string()
]),
structure([
message: string()
paths: listOf(string())
])
)
)
Expand Down
14 changes: 12 additions & 2 deletions src/Analyser/Analyser.php
Expand Up @@ -104,7 +104,7 @@ public function analyse(
);
continue;
}
if (!isset($ignoreError['path'])) {
if (!isset($ignoreError['path']) && !isset($ignoreError['paths'])) {
$errors[] = sprintf(
'Ignored error %s is missing a path.',
Json::encode($ignoreError)
Expand Down Expand Up @@ -240,7 +240,11 @@ function (\PhpParser\Node $node, Scope $scope) use (&$fileErrors, $file, &$scope
$errors = array_values(array_filter($errors, function (Error $error) use (&$unmatchedIgnoredErrors, &$addErrors): bool {
foreach ($this->ignoreErrors as $i => $ignore) {
if (IgnoredError::shouldIgnore($this->fileHelper, $error, $ignore)) {
unset($unmatchedIgnoredErrors[$i]);
if (is_string($ignore)) {
unset($unmatchedIgnoredErrors[$i]);
} elseif (!isset($ignore['paths'])) {
unset($unmatchedIgnoredErrors[$i]);
}
if (!$error->canBeIgnored()) {
$addErrors[] = sprintf(
'Error message "%s" cannot be ignored, use excludes_analyse instead.',
Expand All @@ -259,6 +263,12 @@ function (\PhpParser\Node $node, Scope $scope) use (&$fileErrors, $file, &$scope

if (!$onlyFiles && $this->reportUnmatchedIgnoredErrors && !$reachedInternalErrorsCountLimit) {
foreach ($unmatchedIgnoredErrors as $unmatchedIgnoredError) {
if (
is_array($unmatchedIgnoredError)
&& isset($unmatchedIgnoredError['paths'])
) {
continue;
}
$errors[] = sprintf(
'Ignored error pattern %s was not matched in reported errors.',
IgnoredError::stringifyPattern($unmatchedIgnoredError)
Expand Down
17 changes: 10 additions & 7 deletions src/Analyser/IgnoredError.php
Expand Up @@ -31,7 +31,7 @@ public static function stringifyPattern($ignoredError): string
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
* @param FileHelper $fileHelper
* @param Error $error
* @param array<string, string>|string $ignoredError
* @param mixed[]|string $ignoredError
* @return bool To ignore or not to ignore?
*/
public static function shouldIgnore(
Expand All @@ -41,15 +41,18 @@ public static function shouldIgnore(
): bool
{
if (is_array($ignoredError)) {
// ignore by path
if (isset($ignoredError['path'])) {
$fileExcluder = new FileExcluder($fileHelper, [$ignoredError['path']]);

return \Nette\Utils\Strings::match($error->getMessage(), $ignoredError['message']) !== null
&& $fileExcluder->isExcludedFromAnalysing($error->getFile());
$ignoredPaths = [$ignoredError['path']];
} elseif (isset($ignoredError['paths'])) {
$ignoredPaths = $ignoredError['paths'];
} else {
throw new \PHPStan\ShouldNotHappenException();
}

throw new \PHPStan\ShouldNotHappenException();
$fileExcluder = new FileExcluder($fileHelper, $ignoredPaths);

return \Nette\Utils\Strings::match($error->getMessage(), $ignoredError['message']) !== null
&& $fileExcluder->isExcludedFromAnalysing($error->getFile());
}

return \Nette\Utils\Strings::match($error->getMessage(), $ignoredError) !== null;
Expand Down
14 changes: 13 additions & 1 deletion tests/PHPStan/Analyser/AnalyserTest.php
Expand Up @@ -70,6 +70,18 @@ public function testIgnoreErrorByPath(): void
$this->assertCount(0, $result);
}

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

public function testIgnoreErrorNotFoundInPath(): void
{
$ignoreErrors = [
Expand Down Expand Up @@ -144,7 +156,7 @@ public function testReportMultipleParserErrorsAtOnce(): void
}

/**
* @param string[]|array<array<string, string>> $ignoreErrors
* @param mixed[] $ignoreErrors
* @param bool $reportUnmatchedIgnoredErrors
* @param string $filePath
* @param bool $onlyFiles
Expand Down

0 comments on commit 105e83e

Please sign in to comment.