-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AnalyserResultFinalizer - DRY of running CollectedDataNode rules
- Loading branch information
1 parent
4c0e939
commit 38e2c96
Showing
7 changed files
with
106 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Analyser; | ||
|
||
use PHPStan\AnalysedCodeException; | ||
use PHPStan\BetterReflection\NodeCompiler\Exception\UnableToCompileNode; | ||
use PHPStan\BetterReflection\Reflection\Exception\CircularReference; | ||
use PHPStan\BetterReflection\Reflector\Exception\IdentifierNotFound; | ||
use PHPStan\Node\CollectedDataNode; | ||
use PHPStan\Rules\Registry as RuleRegistry; | ||
use function count; | ||
use function sprintf; | ||
|
||
class AnalyserResultFinalizer | ||
{ | ||
|
||
public function __construct( | ||
private RuleRegistry $ruleRegistry, | ||
private RuleErrorTransformer $ruleErrorTransformer, | ||
private ScopeFactory $scopeFactory, | ||
) | ||
{ | ||
} | ||
|
||
public function finalize(AnalyserResult $analyserResult, bool $onlyFiles): AnalyserResult | ||
{ | ||
if (count($analyserResult->getCollectedData()) === 0) { | ||
return $analyserResult; | ||
} | ||
|
||
$hasInternalErrors = count($analyserResult->getInternalErrors()) > 0 || $analyserResult->hasReachedInternalErrorsCountLimit(); | ||
if ($hasInternalErrors) { | ||
return $analyserResult; | ||
} | ||
|
||
$nodeType = CollectedDataNode::class; | ||
$node = new CollectedDataNode($analyserResult->getCollectedData(), $onlyFiles); | ||
|
||
$file = 'N/A'; | ||
$scope = $this->scopeFactory->create(ScopeContext::create($file)); | ||
$errors = $analyserResult->getUnorderedErrors(); | ||
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'); | ||
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'); | ||
continue; | ||
} catch (UnableToCompileNode | CircularReference $e) { | ||
$errors[] = (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()); | ||
} | ||
} | ||
|
||
return new AnalyserResult( | ||
$errors, | ||
$analyserResult->getLocallyIgnoredErrors(), | ||
$analyserResult->getLinesToIgnore(), | ||
$analyserResult->getUnmatchedLineIgnores(), | ||
$analyserResult->getInternalErrors(), | ||
$analyserResult->getCollectedData(), | ||
$analyserResult->getDependencies(), | ||
$analyserResult->getExportedNodes(), | ||
$analyserResult->hasReachedInternalErrorsCountLimit(), | ||
$analyserResult->getPeakMemoryUsageBytes(), | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters