Skip to content

Commit c9df2f4

Browse files
committed
Fix reporting unmatched ignored error from path that wasn't analysed
1 parent 7cba983 commit c9df2f4

3 files changed

Lines changed: 41 additions & 5 deletions

File tree

src/Analyser/IgnoredErrorHelperResult.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,15 @@ public function getWarnings(): array
7474

7575
/**
7676
* @param Error[] $errors
77+
* @param string[] $analysedFiles
7778
* @return string[]|Error[]
7879
*/
79-
public function process(array $errors, bool $onlyFiles, bool $hasInternalErrors): array
80+
public function process(
81+
array $errors,
82+
bool $onlyFiles,
83+
array $analysedFiles,
84+
bool $hasInternalErrors
85+
): array
8086
{
8187
$unmatchedIgnoredErrors = $this->ignoreErrors;
8288
$addErrors = [];
@@ -206,6 +212,8 @@ public function process(array $errors, bool $onlyFiles, bool $hasInternalErrors)
206212

207213
$errors = array_merge($errors, $addErrors);
208214

215+
$analysedFilesKeys = array_fill_keys($analysedFiles, true);
216+
209217
if ($this->reportUnmatchedIgnoredErrors && !$hasInternalErrors) {
210218
foreach ($unmatchedIgnoredErrors as $unmatchedIgnoredError) {
211219
if (
@@ -224,6 +232,10 @@ public function process(array $errors, bool $onlyFiles, bool $hasInternalErrors)
224232
), $unmatchedIgnoredError['file'], $unmatchedIgnoredError['line'], false);
225233
}
226234
} elseif (isset($unmatchedIgnoredError['realPath'])) {
235+
if (!array_key_exists($unmatchedIgnoredError['realPath'], $analysedFilesKeys)) {
236+
continue;
237+
}
238+
227239
$errors[] = new Error(
228240
sprintf(
229241
'Ignored error pattern %s was not matched in reported errors.',

src/Command/AnalyseApplication.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function analyse(
119119
$analyserResult = $this->resultCacheManager->process($intermediateAnalyserResult, $resultCache);
120120
$hasInferrablePropertyTypesFromConstructor = $analyserResult->hasInferrablePropertyTypesFromConstructor();
121121
$internalErrors = $analyserResult->getInternalErrors();
122-
$errors = $ignoredErrorHelperResult->process($analyserResult->getErrors(), $onlyFiles, count($internalErrors) > 0 || $analyserResult->hasReachedInternalErrorsCountLimit());
122+
$errors = $ignoredErrorHelperResult->process($analyserResult->getErrors(), $onlyFiles, $files, count($internalErrors) > 0 || $analyserResult->hasReachedInternalErrorsCountLimit());
123123
$warnings = $ignoredErrorHelperResult->getWarnings();
124124
if ($analyserResult->hasReachedInternalErrorsCountLimit()) {
125125
$errors[] = sprintf('Reached internal errors count limit of %d, exiting...', $this->internalErrorsCountLimit);

tests/PHPStan/Analyser/AnalyserTest.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,28 @@ public function testReportMultipleParserErrorsAtOnce(): void
326326
$this->assertSame(10, $errorTwo->getLine());
327327
}
328328

329+
/**
330+
* @dataProvider dataTrueAndFalse
331+
* @param bool $onlyFiles
332+
*/
333+
public function testDoNotReportUnmatchedIgnoredErrorsFromPathIfPathWasNotAnalysed(bool $onlyFiles): void
334+
{
335+
$ignoreErrors = [
336+
[
337+
'message' => '#Fail\.#',
338+
'path' => __DIR__ . '/data/bootstrap-error.php',
339+
],
340+
[
341+
'message' => '#Fail\.#',
342+
'path' => __DIR__ . '/data/two-fails.php',
343+
],
344+
];
345+
$result = $this->runAnalyser($ignoreErrors, true, [
346+
__DIR__ . '/data/two-fails.php',
347+
], $onlyFiles);
348+
$this->assertCount(0, $result);
349+
}
350+
329351
/**
330352
* @param mixed[] $ignoreErrors
331353
* @param bool $reportUnmatchedIgnoredErrors
@@ -357,11 +379,13 @@ private function runAnalyser(
357379
return $ignoredErrorHelperResult->getErrors();
358380
}
359381

360-
$analyserResult = $analyser->analyse(array_map(function (string $path): string {
382+
$normalizedFilePaths = array_map(function (string $path): string {
361383
return $this->getFileHelper()->normalizePath($path);
362-
}, $filePaths));
384+
}, $filePaths);
385+
386+
$analyserResult = $analyser->analyse($normalizedFilePaths);
363387

364-
$errors = $ignoredErrorHelperResult->process($analyserResult->getErrors(), $onlyFiles, $analyserResult->hasReachedInternalErrorsCountLimit());
388+
$errors = $ignoredErrorHelperResult->process($analyserResult->getErrors(), $onlyFiles, $normalizedFilePaths, $analyserResult->hasReachedInternalErrorsCountLimit());
365389
if ($analyserResult->hasReachedInternalErrorsCountLimit()) {
366390
$errors[] = sprintf('Reached internal errors count limit of %d, exiting...', 50);
367391
}

0 commit comments

Comments
 (0)