Skip to content

Commit dc3b75a

Browse files
committed
Allow ignoring errors from CollectedDataNode with local comments
1 parent 38a2734 commit dc3b75a

File tree

4 files changed

+49
-8
lines changed

4 files changed

+49
-8
lines changed

src/Analyser/AnalyserResultFinalizer.php

+32-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function __construct(
1818
private RuleRegistry $ruleRegistry,
1919
private RuleErrorTransformer $ruleErrorTransformer,
2020
private ScopeFactory $scopeFactory,
21+
private LocalIgnoresProcessor $localIgnoresProcessor,
2122
private bool $reportUnmatchedIgnoredErrors,
2223
)
2324
{
@@ -39,31 +40,54 @@ public function finalize(AnalyserResult $analyserResult, bool $onlyFiles): Analy
3940

4041
$file = 'N/A';
4142
$scope = $this->scopeFactory->create(ScopeContext::create($file));
42-
$errors = $analyserResult->getUnorderedErrors();
43+
$collectorErrors = [];
4344
foreach ($this->ruleRegistry->getRules($nodeType) as $rule) {
4445
try {
4546
$ruleErrors = $rule->processNode($node, $scope);
4647
} catch (AnalysedCodeException $e) {
47-
$errors[] = (new Error($e->getMessage(), $file, $node->getStartLine(), $e, null, null, $e->getTip()))->withIdentifier('phpstan.internal');
48+
$collectorErrors[] = (new Error($e->getMessage(), $file, $node->getStartLine(), $e, null, null, $e->getTip()))->withIdentifier('phpstan.internal');
4849
continue;
4950
} catch (IdentifierNotFound $e) {
50-
$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');
51+
$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');
5152
continue;
5253
} catch (UnableToCompileNode | CircularReference $e) {
53-
$errors[] = (new Error(sprintf('Reflection error: %s', $e->getMessage()), $file, $node->getStartLine(), $e))->withIdentifier('phpstan.reflection');
54+
$collectorErrors[] = (new Error(sprintf('Reflection error: %s', $e->getMessage()), $file, $node->getStartLine(), $e))->withIdentifier('phpstan.reflection');
5455
continue;
5556
}
5657

5758
foreach ($ruleErrors as $ruleError) {
58-
$errors[] = $this->ruleErrorTransformer->transform($ruleError, $scope, $nodeType, $node->getStartLine());
59+
$collectorErrors[] = $this->ruleErrorTransformer->transform($ruleError, $scope, $nodeType, $node->getStartLine());
5960
}
6061
}
6162

63+
$errors = $analyserResult->getUnorderedErrors();
64+
$locallyIgnoredErrors = $analyserResult->getLocallyIgnoredErrors();
65+
$allLinesToIgnore = $analyserResult->getLinesToIgnore();
66+
$allUnmatchedLineIgnores = $analyserResult->getUnmatchedLineIgnores();
67+
foreach ($collectorErrors as $collectorError) {
68+
$file = $collectorError->getFilePath();
69+
$linesToIgnore = $allLinesToIgnore[$file] ?? [];
70+
$unmatchedLineIgnores = $allUnmatchedLineIgnores[$file] ?? [];
71+
$localIgnoresProcessorResult = $this->localIgnoresProcessor->process(
72+
[$collectorError],
73+
$linesToIgnore,
74+
$unmatchedLineIgnores,
75+
);
76+
foreach ($localIgnoresProcessorResult->getFileErrors() as $error) {
77+
$errors[] = $error;
78+
}
79+
foreach ($localIgnoresProcessorResult->getLocallyIgnoredErrors() as $locallyIgnoredError) {
80+
$locallyIgnoredErrors[] = $locallyIgnoredError;
81+
}
82+
$allLinesToIgnore[$file] = $localIgnoresProcessorResult->getLinesToIgnore();
83+
$allUnmatchedLineIgnores[$file] = $localIgnoresProcessorResult->getUnmatchedLineIgnores();
84+
}
85+
6286
return $this->addUnmatchedIgnoredErrors(new AnalyserResult(
6387
$errors,
64-
$analyserResult->getLocallyIgnoredErrors(),
65-
$analyserResult->getLinesToIgnore(),
66-
$analyserResult->getUnmatchedLineIgnores(),
88+
$locallyIgnoredErrors,
89+
$allLinesToIgnore,
90+
$allUnmatchedLineIgnores,
6791
$analyserResult->getInternalErrors(),
6892
$analyserResult->getCollectedData(),
6993
$analyserResult->getDependencies(),

src/Testing/RuleTestCase.php

+1
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ public function gatherAnalyserErrors(array $files): array
183183
]),
184184
new RuleErrorTransformer(),
185185
$this->createScopeFactory($this->createReflectionProvider(), $this->getTypeSpecifier()),
186+
new LocalIgnoresProcessor(),
186187
true,
187188
);
188189

tests/PHPStan/Analyser/AnalyserTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,7 @@ private function runAnalyser(
655655
$this->createReflectionProvider(),
656656
self::getContainer()->getService('typeSpecifier'),
657657
),
658+
new LocalIgnoresProcessor(),
658659
$reportUnmatchedIgnoredErrors,
659660
);
660661
$analyserResult = $finalizer->finalize($analyserResult, $onlyFiles);

tests/PHPStan/Rules/DeadCode/data/call-to-method-without-impure-points.php

+15
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,18 @@ private function doBar(): int
133133
}
134134

135135
}
136+
137+
class TestIgnoring
138+
{
139+
140+
public function doFoo(): void
141+
{
142+
$this->doBar(); // @phpstan-ignore method.resultUnused
143+
}
144+
145+
private function doBar(): int
146+
{
147+
return 1;
148+
}
149+
150+
}

0 commit comments

Comments
 (0)