Skip to content

Commit 8676d4c

Browse files
committed
AnalyseApplication - call CollectedDataNode rules
1 parent b10fb30 commit 8676d4c

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

Diff for: src/Command/AnalyseApplication.php

+53-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@
22

33
namespace PHPStan\Command;
44

5+
use PHPStan\AnalysedCodeException;
56
use PHPStan\Analyser\AnalyserResult;
7+
use PHPStan\Analyser\Error;
68
use PHPStan\Analyser\IgnoredErrorHelper;
79
use PHPStan\Analyser\ResultCache\ResultCacheManagerFactory;
10+
use PHPStan\Analyser\RuleErrorTransformer;
11+
use PHPStan\Analyser\ScopeContext;
12+
use PHPStan\Analyser\ScopeFactory;
13+
use PHPStan\BetterReflection\NodeCompiler\Exception\UnableToCompileNode;
14+
use PHPStan\BetterReflection\Reflection\Exception\NotAClassReflection;
15+
use PHPStan\BetterReflection\Reflection\Exception\NotAnInterfaceReflection;
16+
use PHPStan\BetterReflection\Reflector\Exception\IdentifierNotFound;
17+
use PHPStan\Collectors\CollectedData;
818
use PHPStan\Internal\BytesHelper;
19+
use PHPStan\Node\CollectedDataNode;
920
use PHPStan\PhpDoc\StubFilesProvider;
1021
use PHPStan\PhpDoc\StubValidator;
22+
use PHPStan\Rules\Registry as RuleRegistry;
1123
use PHPStan\ShouldNotHappenException;
1224
use Symfony\Component\Console\Input\InputInterface;
1325
use function array_merge;
@@ -27,6 +39,9 @@ public function __construct(
2739
private IgnoredErrorHelper $ignoredErrorHelper,
2840
private int $internalErrorsCountLimit,
2941
private StubFilesProvider $stubFilesProvider,
42+
private RuleRegistry $ruleRegistry,
43+
private ScopeFactory $scopeFactory,
44+
private RuleErrorTransformer $ruleErrorTransformer,
3045
)
3146
{
3247
}
@@ -87,7 +102,11 @@ public function analyse(
87102
$resultCacheResult = $resultCacheManager->process($intermediateAnalyserResult, $resultCache, $errorOutput, $onlyFiles, true);
88103
$analyserResult = $resultCacheResult->getAnalyserResult();
89104
$internalErrors = $analyserResult->getInternalErrors();
90-
$errors = $ignoredErrorHelperResult->process($analyserResult->getErrors(), $onlyFiles, $files, count($internalErrors) > 0 || $analyserResult->hasReachedInternalErrorsCountLimit());
105+
$errors = $analyserResult->getErrors();
106+
foreach ($this->getCollectedDataErrors($analyserResult->getCollectedData()) as $error) {
107+
$errors[] = $error;
108+
}
109+
$errors = $ignoredErrorHelperResult->process($errors, $onlyFiles, $files, count($internalErrors) > 0 || $analyserResult->hasReachedInternalErrorsCountLimit());
91110
$collectedData = $analyserResult->getCollectedData();
92111
$savedResultCache = $resultCacheResult->isSaved();
93112
if ($analyserResult->hasReachedInternalErrorsCountLimit()) {
@@ -119,6 +138,39 @@ public function analyse(
119138
);
120139
}
121140

141+
/**
142+
* @param CollectedData[] $collectedData
143+
* @return Error[]
144+
*/
145+
private function getCollectedDataErrors(array $collectedData): array
146+
{
147+
$nodeType = CollectedDataNode::class;
148+
$node = new CollectedDataNode($collectedData);
149+
$file = 'N/A';
150+
$scope = $this->scopeFactory->create(ScopeContext::create($file));
151+
$errors = [];
152+
foreach ($this->ruleRegistry->getRules($nodeType) as $rule) {
153+
try {
154+
$ruleErrors = $rule->processNode($node, $scope);
155+
} catch (AnalysedCodeException $e) {
156+
$errors[] = new Error($e->getMessage(), $file, $node->getLine(), $e, null, null, $e->getTip());
157+
continue;
158+
} catch (IdentifierNotFound $e) {
159+
$errors[] = new Error(sprintf('Reflection error: %s not found.', $e->getIdentifier()->getName()), $file, $node->getLine(), $e, null, null, 'Learn more at https://phpstan.org/user-guide/discovering-symbols');
160+
continue;
161+
} catch (UnableToCompileNode | NotAClassReflection | NotAnInterfaceReflection $e) {
162+
$errors[] = new Error(sprintf('Reflection error: %s', $e->getMessage()), $file, $node->getLine(), $e);
163+
continue;
164+
}
165+
166+
foreach ($ruleErrors as $ruleError) {
167+
$errors[] = $this->ruleErrorTransformer->transform($ruleError, $scope, $nodeType, $node->getLine());
168+
}
169+
}
170+
171+
return $errors;
172+
}
173+
122174
/**
123175
* @param string[] $files
124176
* @param string[] $allAnalysedFiles

0 commit comments

Comments
 (0)