Skip to content

Commit

Permalink
Move handling of unmatched ignored errors from FileAnalyser to Analys…
Browse files Browse the repository at this point in the history
…erResultFinalizer
  • Loading branch information
ondrejmirtes committed Apr 20, 2024
1 parent 52036a5 commit 38a2734
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 39 deletions.
3 changes: 2 additions & 1 deletion conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -478,12 +478,13 @@ services:

-
class: PHPStan\Analyser\AnalyserResultFinalizer
arguments:
reportUnmatchedIgnoredErrors: %reportUnmatchedIgnoredErrors%

-
class: PHPStan\Analyser\FileAnalyser
arguments:
parser: @defaultAnalysisParser
reportUnmatchedIgnoredErrors: %reportUnmatchedIgnoredErrors%

-
class: PHPStan\Analyser\LocalIgnoresProcessor
Expand Down
57 changes: 55 additions & 2 deletions src/Analyser/AnalyserResultFinalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@ public function __construct(
private RuleRegistry $ruleRegistry,
private RuleErrorTransformer $ruleErrorTransformer,
private ScopeFactory $scopeFactory,
private bool $reportUnmatchedIgnoredErrors,
)
{
}

public function finalize(AnalyserResult $analyserResult, bool $onlyFiles): AnalyserResult
{
if (count($analyserResult->getCollectedData()) === 0) {
return $analyserResult;
return $this->addUnmatchedIgnoredErrors($analyserResult);
}

$hasInternalErrors = count($analyserResult->getInternalErrors()) > 0 || $analyserResult->hasReachedInternalErrorsCountLimit();
if ($hasInternalErrors) {
return $analyserResult;
return $this->addUnmatchedIgnoredErrors($analyserResult);
}

$nodeType = CollectedDataNode::class;
Expand Down Expand Up @@ -58,6 +59,58 @@ public function finalize(AnalyserResult $analyserResult, bool $onlyFiles): Analy
}
}

return $this->addUnmatchedIgnoredErrors(new AnalyserResult(
$errors,
$analyserResult->getLocallyIgnoredErrors(),
$analyserResult->getLinesToIgnore(),
$analyserResult->getUnmatchedLineIgnores(),
$analyserResult->getInternalErrors(),
$analyserResult->getCollectedData(),
$analyserResult->getDependencies(),
$analyserResult->getExportedNodes(),
$analyserResult->hasReachedInternalErrorsCountLimit(),
$analyserResult->getPeakMemoryUsageBytes(),
));
}

private function addUnmatchedIgnoredErrors(AnalyserResult $analyserResult): AnalyserResult
{
if (!$this->reportUnmatchedIgnoredErrors) {
return $analyserResult;
}

$errors = $analyserResult->getUnorderedErrors();
foreach ($analyserResult->getUnmatchedLineIgnores() as $file => $data) {
foreach ($data as $ignoredFile => $lines) {
if ($ignoredFile !== $file) {
continue;
}

foreach ($lines as $line => $identifiers) {
if ($identifiers === null) {
$errors[] = (new Error(
sprintf('No error to ignore is reported on line %d.', $line),
$file,
$line,
false,
$file,
))->withIdentifier('ignore.unmatchedLine');
continue;
}

foreach ($identifiers as $identifier) {
$errors[] = (new Error(
sprintf('No error with identifier %s is reported on line %d.', $identifier, $line),
$file,
$line,
false,
$file,
))->withIdentifier('ignore.unmatchedIdentifier');
}
}
}
}

return new AnalyserResult(
$errors,
$analyserResult->getLocallyIgnoredErrors(),
Expand Down
32 changes: 0 additions & 32 deletions src/Analyser/FileAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public function __construct(
private DependencyResolver $dependencyResolver,
private RuleErrorTransformer $ruleErrorTransformer,
private LocalIgnoresProcessor $localIgnoresProcessor,
private bool $reportUnmatchedIgnoredErrors,
)
{
}
Expand Down Expand Up @@ -189,37 +188,6 @@ public function analyseFile(
}
$linesToIgnore = $localIgnoresProcessorResult->getLinesToIgnore();
$unmatchedLineIgnores = $localIgnoresProcessorResult->getUnmatchedLineIgnores();

if ($this->reportUnmatchedIgnoredErrors) {
foreach ($unmatchedLineIgnores as $ignoredFile => $lines) {
if ($ignoredFile !== $file) {
continue;
}

foreach ($lines as $line => $identifiers) {
if ($identifiers === null) {
$fileErrors[] = (new Error(
sprintf('No error to ignore is reported on line %d.', $line),
$file,
$line,
false,
$file,
))->withIdentifier('ignore.unmatchedLine');
continue;
}

foreach ($identifiers as $identifier) {
$fileErrors[] = (new Error(
sprintf('No error with identifier %s is reported on line %d.', $identifier, $line),
$file,
$line,
false,
$file,
))->withIdentifier('ignore.unmatchedIdentifier');
}
}
}
}
} catch (\PhpParser\Error $e) {
$fileErrors[] = (new Error($e->getMessage(), $file, $e->getStartLine() !== -1 ? $e->getStartLine() : null, $e))->withIdentifier('phpstan.parse');
} catch (ParserErrorsException $e) {
Expand Down
1 change: 1 addition & 0 deletions src/Analyser/ResultCache/ResultCacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,7 @@ private function getMeta(array $allAnalysedFiles, ?array $projectConfigArray): a
unset($projectConfigArray['parameters']['editorUrlTitle']);
unset($projectConfigArray['parameters']['errorFormat']);
unset($projectConfigArray['parameters']['ignoreErrors']);
unset($projectConfigArray['parameters']['reportUnmatchedIgnoredErrors']);
unset($projectConfigArray['parameters']['tipsOfTheDay']);
unset($projectConfigArray['parameters']['parallel']);
unset($projectConfigArray['parameters']['internalErrorsCountLimit']);
Expand Down
2 changes: 1 addition & 1 deletion src/Testing/RuleTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ private function getAnalyser(): Analyser
self::getContainer()->getByType(DependencyResolver::class),
new RuleErrorTransformer(),
new LocalIgnoresProcessor(),
true,
);
$this->analyser = new Analyser(
$fileAnalyser,
Expand Down Expand Up @@ -184,6 +183,7 @@ public function gatherAnalyserErrors(array $files): array
]),
new RuleErrorTransformer(),
$this->createScopeFactory($this->createReflectionProvider(), $this->getTypeSpecifier()),
true,
);

return $finalizer->finalize($analyserResult, false)->getUnorderedErrors();
Expand Down
16 changes: 13 additions & 3 deletions tests/PHPStan/Analyser/AnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ private function runAnalyser(
bool $enableIgnoreErrorsWithinPhpDocs = true,
): array
{
$analyser = $this->createAnalyser($reportUnmatchedIgnoredErrors, $enableIgnoreErrorsWithinPhpDocs);
$analyser = $this->createAnalyser($enableIgnoreErrorsWithinPhpDocs);

if (is_string($filePaths)) {
$filePaths = [$filePaths];
Expand All @@ -648,6 +648,17 @@ private function runAnalyser(

$analyserResult = $analyser->analyse($normalizedFilePaths);

$finalizer = new AnalyserResultFinalizer(
new DirectRuleRegistry([]),
new RuleErrorTransformer(),
$this->createScopeFactory(
$this->createReflectionProvider(),
self::getContainer()->getService('typeSpecifier'),
),
$reportUnmatchedIgnoredErrors,
);
$analyserResult = $finalizer->finalize($analyserResult, $onlyFiles);

$ignoredErrorHelperProcessedResult = $ignoredErrorHelperResult->process($analyserResult->getErrors(), $onlyFiles, $normalizedFilePaths, $analyserResult->hasReachedInternalErrorsCountLimit());
$errors = $ignoredErrorHelperProcessedResult->getNotIgnoredErrors();
$errors = array_merge($errors, $ignoredErrorHelperProcessedResult->getOtherIgnoreMessages());
Expand All @@ -661,7 +672,7 @@ private function runAnalyser(
);
}

private function createAnalyser(bool $reportUnmatchedIgnoredErrors, bool $enableIgnoreErrorsWithinPhpDocs): Analyser
private function createAnalyser(bool $enableIgnoreErrorsWithinPhpDocs): Analyser
{
$ruleRegistry = new DirectRuleRegistry([
new AlwaysFailRule(),
Expand Down Expand Up @@ -716,7 +727,6 @@ private function createAnalyser(bool $reportUnmatchedIgnoredErrors, bool $enable
new DependencyResolver($fileHelper, $reflectionProvider, new ExportedNodeResolver($fileTypeMapper, new ExprPrinter(new Printer())), $fileTypeMapper),
new RuleErrorTransformer(),
new LocalIgnoresProcessor(),
$reportUnmatchedIgnoredErrors,
);

return new Analyser(
Expand Down

0 comments on commit 38a2734

Please sign in to comment.