Skip to content

Commit

Permalink
Allow ignoring errors from CollectedDataNode with local comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 20, 2024
1 parent 38a2734 commit dc3b75a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
40 changes: 32 additions & 8 deletions src/Analyser/AnalyserResultFinalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function __construct(
private RuleRegistry $ruleRegistry,
private RuleErrorTransformer $ruleErrorTransformer,
private ScopeFactory $scopeFactory,
private LocalIgnoresProcessor $localIgnoresProcessor,
private bool $reportUnmatchedIgnoredErrors,
)
{
Expand All @@ -39,31 +40,54 @@ public function finalize(AnalyserResult $analyserResult, bool $onlyFiles): Analy

$file = 'N/A';
$scope = $this->scopeFactory->create(ScopeContext::create($file));
$errors = $analyserResult->getUnorderedErrors();
$collectorErrors = [];
foreach ($this->ruleRegistry->getRules($nodeType) as $rule) {
try {
$ruleErrors = $rule->processNode($node, $scope);
} catch (AnalysedCodeException $e) {
$errors[] = (new Error($e->getMessage(), $file, $node->getStartLine(), $e, null, null, $e->getTip()))->withIdentifier('phpstan.internal');
$collectorErrors[] = (new Error($e->getMessage(), $file, $node->getStartLine(), $e, null, null, $e->getTip()))->withIdentifier('phpstan.internal');
continue;
} catch (IdentifierNotFound $e) {
$errors[] = (new Error(sprintf('Reflection error: %s not found.', $e->getIdentifier()->getName()), $file, $node->getStartLine(), $e, null, null, 'Learn more at https://phpstan.org/user-guide/discovering-symbols'))->withIdentifier('phpstan.reflection');
$collectorErrors[] = (new Error(sprintf('Reflection error: %s not found.', $e->getIdentifier()->getName()), $file, $node->getStartLine(), $e, null, null, 'Learn more at https://phpstan.org/user-guide/discovering-symbols'))->withIdentifier('phpstan.reflection');
continue;
} catch (UnableToCompileNode | CircularReference $e) {
$errors[] = (new Error(sprintf('Reflection error: %s', $e->getMessage()), $file, $node->getStartLine(), $e))->withIdentifier('phpstan.reflection');
$collectorErrors[] = (new Error(sprintf('Reflection error: %s', $e->getMessage()), $file, $node->getStartLine(), $e))->withIdentifier('phpstan.reflection');
continue;
}

foreach ($ruleErrors as $ruleError) {
$errors[] = $this->ruleErrorTransformer->transform($ruleError, $scope, $nodeType, $node->getStartLine());
$collectorErrors[] = $this->ruleErrorTransformer->transform($ruleError, $scope, $nodeType, $node->getStartLine());
}
}

$errors = $analyserResult->getUnorderedErrors();
$locallyIgnoredErrors = $analyserResult->getLocallyIgnoredErrors();
$allLinesToIgnore = $analyserResult->getLinesToIgnore();
$allUnmatchedLineIgnores = $analyserResult->getUnmatchedLineIgnores();
foreach ($collectorErrors as $collectorError) {
$file = $collectorError->getFilePath();
$linesToIgnore = $allLinesToIgnore[$file] ?? [];
$unmatchedLineIgnores = $allUnmatchedLineIgnores[$file] ?? [];
$localIgnoresProcessorResult = $this->localIgnoresProcessor->process(
[$collectorError],
$linesToIgnore,
$unmatchedLineIgnores,
);
foreach ($localIgnoresProcessorResult->getFileErrors() as $error) {
$errors[] = $error;
}
foreach ($localIgnoresProcessorResult->getLocallyIgnoredErrors() as $locallyIgnoredError) {
$locallyIgnoredErrors[] = $locallyIgnoredError;
}
$allLinesToIgnore[$file] = $localIgnoresProcessorResult->getLinesToIgnore();
$allUnmatchedLineIgnores[$file] = $localIgnoresProcessorResult->getUnmatchedLineIgnores();
}

return $this->addUnmatchedIgnoredErrors(new AnalyserResult(
$errors,
$analyserResult->getLocallyIgnoredErrors(),
$analyserResult->getLinesToIgnore(),
$analyserResult->getUnmatchedLineIgnores(),
$locallyIgnoredErrors,
$allLinesToIgnore,
$allUnmatchedLineIgnores,
$analyserResult->getInternalErrors(),
$analyserResult->getCollectedData(),
$analyserResult->getDependencies(),
Expand Down
1 change: 1 addition & 0 deletions src/Testing/RuleTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public function gatherAnalyserErrors(array $files): array
]),
new RuleErrorTransformer(),
$this->createScopeFactory($this->createReflectionProvider(), $this->getTypeSpecifier()),
new LocalIgnoresProcessor(),
true,
);

Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/AnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ private function runAnalyser(
$this->createReflectionProvider(),
self::getContainer()->getService('typeSpecifier'),
),
new LocalIgnoresProcessor(),
$reportUnmatchedIgnoredErrors,
);
$analyserResult = $finalizer->finalize($analyserResult, $onlyFiles);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,18 @@ private function doBar(): int
}

}

class TestIgnoring
{

public function doFoo(): void
{
$this->doBar(); // @phpstan-ignore method.resultUnused
}

private function doBar(): int
{
return 1;
}

}

0 comments on commit dc3b75a

Please sign in to comment.