rector/packages/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php

293 lines
14 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare (strict_types=1);
namespace Rector\NodeTypeResolver\PHPStan\Scope;
use RectorPrefix20211003\Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt;
2018-08-04 20:32:01 +00:00
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Trait_;
use PhpParser\NodeTraverser;
2020-04-01 01:55:44 +00:00
use PHPStan\AnalysedCodeException;
2019-12-08 21:00:38 +00:00
use PHPStan\Analyser\MutatingScope;
2020-06-16 19:43:45 +00:00
use PHPStan\Analyser\NodeScopeResolver;
2020-08-27 10:19:22 +00:00
use PHPStan\Analyser\Scope;
use PHPStan\BetterReflection\Reflection\Exception\NotAnInterfaceReflection;
use PHPStan\BetterReflection\Reflector\ClassReflector;
use PHPStan\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
use PHPStan\Node\UnreachableStatementNode;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
use Rector\Caching\Detector\ChangedFilesDetector;
2020-04-01 01:55:44 +00:00
use Rector\Caching\FileSystem\DependencyResolver;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\StaticReflection\SourceLocator\ParentAttributeSourceLocator;
use Rector\Core\StaticReflection\SourceLocator\RenamedClassesSourceLocator;
use Rector\NodeTypeResolver\Node\AttributeKey;
2019-08-05 16:52:55 +00:00
use Rector\NodeTypeResolver\PHPStan\Collector\TraitNodeScopeCollector;
use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\RemoveDeepChainMethodCallNodeVisitor;
use RectorPrefix20211003\Symplify\PackageBuilder\Reflection\PrivatesAccessor;
2020-04-19 17:56:05 +00:00
use Symplify\SmartFileSystem\SmartFileInfo;
use Throwable;
/**
* @inspired by https://github.com/silverstripe/silverstripe-upgrader/blob/532182b23e854d02e0b27e68ebc394f436de0682/src/UpgradeRule/PHP/Visitor/PHPStanScopeVisitor.php
* - https://github.com/silverstripe/silverstripe-upgrader/pull/57/commits/e5c7cfa166ad940d9d4ff69537d9f7608e992359#diff-5e0807bb3dc03d6a8d8b6ad049abd774
*/
2020-06-16 19:43:45 +00:00
final class PHPStanNodeScopeResolver
{
/**
* @var string
* @see https://regex101.com/r/aXsCkK/1
*/
private const ANONYMOUS_CLASS_START_REGEX = '#^AnonymousClass(\\w+)#';
/**
* @var string
* @see https://regex101.com/r/AIA24M/1
*/
private const NOT_AN_INTERFACE_EXCEPTION_REGEX = '#^Provided node ".*" is not interface, but "class"$#';
/**
* @var \Rector\Caching\Detector\ChangedFilesDetector
*/
private $changedFilesDetector;
/**
* @var \Rector\Caching\FileSystem\DependencyResolver
*/
private $dependencyResolver;
/**
* @var \PHPStan\Analyser\NodeScopeResolver
*/
private $nodeScopeResolver;
2019-05-12 08:19:38 +00:00
/**
* @var \PHPStan\Reflection\ReflectionProvider
2019-05-12 08:19:38 +00:00
*/
private $reflectionProvider;
2020-04-01 01:55:44 +00:00
/**
* @var \Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\RemoveDeepChainMethodCallNodeVisitor
2020-04-01 01:55:44 +00:00
*/
private $removeDeepChainMethodCallNodeVisitor;
2020-04-01 01:55:44 +00:00
/**
* @var \Rector\NodeTypeResolver\PHPStan\Scope\ScopeFactory
2020-04-01 01:55:44 +00:00
*/
private $scopeFactory;
2020-04-01 01:55:44 +00:00
/**
* @var \Rector\NodeTypeResolver\PHPStan\Collector\TraitNodeScopeCollector
2020-04-01 01:55:44 +00:00
*/
private $traitNodeScopeCollector;
/**
* @var \Symplify\PackageBuilder\Reflection\PrivatesAccessor
*/
private $privatesAccessor;
/**
* @var \Rector\Core\StaticReflection\SourceLocator\RenamedClassesSourceLocator
*/
private $renamedClassesSourceLocator;
/**
* @var \Rector\Core\StaticReflection\SourceLocator\ParentAttributeSourceLocator
*/
private $parentAttributeSourceLocator;
/**
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
*/
private $betterNodeFinder;
public function __construct(\Rector\Caching\Detector\ChangedFilesDetector $changedFilesDetector, \Rector\Caching\FileSystem\DependencyResolver $dependencyResolver, \PHPStan\Analyser\NodeScopeResolver $nodeScopeResolver, \PHPStan\Reflection\ReflectionProvider $reflectionProvider, \Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\RemoveDeepChainMethodCallNodeVisitor $removeDeepChainMethodCallNodeVisitor, \Rector\NodeTypeResolver\PHPStan\Scope\ScopeFactory $scopeFactory, \Rector\NodeTypeResolver\PHPStan\Collector\TraitNodeScopeCollector $traitNodeScopeCollector, \RectorPrefix20211003\Symplify\PackageBuilder\Reflection\PrivatesAccessor $privatesAccessor, \Rector\Core\StaticReflection\SourceLocator\RenamedClassesSourceLocator $renamedClassesSourceLocator, \Rector\Core\StaticReflection\SourceLocator\ParentAttributeSourceLocator $parentAttributeSourceLocator, \Rector\Core\PhpParser\Node\BetterNodeFinder $betterNodeFinder)
{
$this->changedFilesDetector = $changedFilesDetector;
$this->dependencyResolver = $dependencyResolver;
2020-06-16 19:43:45 +00:00
$this->nodeScopeResolver = $nodeScopeResolver;
$this->reflectionProvider = $reflectionProvider;
$this->removeDeepChainMethodCallNodeVisitor = $removeDeepChainMethodCallNodeVisitor;
$this->scopeFactory = $scopeFactory;
$this->traitNodeScopeCollector = $traitNodeScopeCollector;
$this->privatesAccessor = $privatesAccessor;
$this->renamedClassesSourceLocator = $renamedClassesSourceLocator;
$this->parentAttributeSourceLocator = $parentAttributeSourceLocator;
$this->betterNodeFinder = $betterNodeFinder;
}
/**
* @param Stmt[] $nodes
* @return Stmt[]
*/
public function processNodes(array $nodes, \Symplify\SmartFileSystem\SmartFileInfo $smartFileInfo) : array
{
$this->removeDeepChainMethodCallNodes($nodes);
2020-04-19 17:56:05 +00:00
$scope = $this->scopeFactory->createFromFile($smartFileInfo);
// skip chain method calls, performance issue: https://github.com/phpstan/phpstan/issues/254
$nodeCallback = function (\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope) : void {
2019-08-05 16:52:55 +00:00
// traversing trait inside class that is using it scope (from referenced) - the trait traversed by Rector is different (directly from parsed file)
if ($scope->isInTrait()) {
// has just entereted trait, to avoid adding it for ever ynode
$parentNode = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE);
if ($parentNode instanceof \PhpParser\Node\Stmt\Trait_) {
/** @var ClassReflection $classReflection */
$classReflection = $scope->getTraitReflection();
$traitName = $classReflection->getName();
$this->traitNodeScopeCollector->addForTrait($traitName, $scope);
}
2019-08-05 16:52:55 +00:00
return;
}
// the class reflection is resolved AFTER entering to class node
// so we need to get it from the first after this one
if ($node instanceof \PhpParser\Node\Stmt\Class_ || $node instanceof \PhpParser\Node\Stmt\Interface_) {
/** @var Scope $scope */
$scope = $this->resolveClassOrInterfaceScope($node, $scope);
}
// special case for unreachable nodes
if ($node instanceof \PHPStan\Node\UnreachableStatementNode) {
$originalNode = $node->getOriginalStatement();
$originalNode->setAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::IS_UNREACHABLE, \true);
$originalNode->setAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE, $scope);
} else {
$node->setAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE, $scope);
}
};
$this->decoratePHPStanNodeScopeResolverWithRenamedClassSourceLocator($this->nodeScopeResolver);
return $this->processNodesWithMixinHandling($smartFileInfo, $nodes, $scope, $nodeCallback);
}
/**
* @param Stmt[] $nodes
* @return Stmt[]
*/
private function processNodesWithMixinHandling(\Symplify\SmartFileSystem\SmartFileInfo $smartFileInfo, array $nodes, \PHPStan\Analyser\MutatingScope $mutatingScope, callable $nodeCallback) : array
{
if ($this->isMixinInSource($nodes)) {
return $nodes;
}
try {
$this->nodeScopeResolver->processNodes($nodes, $mutatingScope, $nodeCallback);
} catch (\Throwable $throwable) {
if (!$throwable instanceof \PHPStan\BetterReflection\Reflection\Exception\NotAnInterfaceReflection) {
throw $throwable;
}
if (!\RectorPrefix20211003\Nette\Utils\Strings::match($throwable->getMessage(), self::NOT_AN_INTERFACE_EXCEPTION_REGEX)) {
throw $throwable;
}
}
$this->resolveAndSaveDependentFiles($nodes, $mutatingScope, $smartFileInfo);
2019-08-05 16:52:55 +00:00
return $nodes;
}
/**
* @param Node[] $nodes
*/
private function isMixinInSource(array $nodes) : bool
{
return (bool) $this->betterNodeFinder->findFirst($nodes, function (\PhpParser\Node $node) : bool {
if (!$node instanceof \PhpParser\Node\Name\FullyQualified && !$node instanceof \PhpParser\Node\Stmt\Class_) {
return \false;
}
if ($node instanceof \PhpParser\Node\Stmt\Class_ && $node->isAnonymous()) {
return \false;
}
$className = $node instanceof \PhpParser\Node\Name\FullyQualified ? $node->toString() : $node->namespacedName->toString();
return $this->isCircularMixin($className);
});
}
private function isCircularMixin(string $className) : bool
{
// fix error in parallel test
// use function_exists on purpose as using reflectionProvider broke the test in parallel
if (\function_exists($className)) {
return \false;
}
$hasClass = $this->reflectionProvider->hasClass($className);
if (!$hasClass) {
return \false;
}
$classReflection = $this->reflectionProvider->getClass($className);
if ($classReflection->isBuiltIn()) {
return \false;
}
foreach ($classReflection->getMixinTags() as $mixinTag) {
$type = $mixinTag->getType();
if (!$type instanceof \PHPStan\Type\ObjectType) {
return \false;
}
if ($type->getClassName() === $className) {
return \true;
}
if ($this->isCircularMixin($type->getClassName())) {
return \true;
}
}
return \false;
}
2018-12-16 13:24:31 +00:00
/**
* @param Node[] $nodes
*/
private function removeDeepChainMethodCallNodes(array $nodes) : void
2018-12-16 13:24:31 +00:00
{
$nodeTraverser = new \PhpParser\NodeTraverser();
2018-12-16 13:24:31 +00:00
$nodeTraverser->addVisitor($this->removeDeepChainMethodCallNodeVisitor);
$nodeTraverser->traverse($nodes);
}
/**
* @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Interface_ $classLike
*/
private function resolveClassOrInterfaceScope($classLike, \PHPStan\Analyser\Scope $scope) : \PHPStan\Analyser\Scope
2020-08-27 10:19:22 +00:00
{
2020-07-26 09:00:23 +00:00
$className = $this->resolveClassName($classLike);
// is anonymous class? - not possible to enter it since PHPStan 0.12.33, see https://github.com/phpstan/phpstan-src/commit/e87fb0ec26f9c8552bbeef26a868b1e5d8185e91
if ($classLike instanceof \PhpParser\Node\Stmt\Class_ && \RectorPrefix20211003\Nette\Utils\Strings::match($className, self::ANONYMOUS_CLASS_START_REGEX)) {
2020-08-27 10:19:22 +00:00
$classReflection = $this->reflectionProvider->getAnonymousClassReflection($classLike, $scope);
} elseif (!$this->reflectionProvider->hasClass($className)) {
return $scope;
} else {
$classReflection = $this->reflectionProvider->getClass($className);
2020-07-26 09:00:23 +00:00
}
2020-08-27 10:19:22 +00:00
/** @var MutatingScope $scope */
return $scope->enterClass($classReflection);
2019-03-10 23:47:43 +00:00
}
/**
* @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Interface_ $classLike
2019-03-10 23:47:43 +00:00
*/
private function resolveClassName($classLike) : string
{
if (\property_exists($classLike, 'namespacedName')) {
return (string) $classLike->namespacedName;
}
if ($classLike->name === null) {
throw new \Rector\Core\Exception\ShouldNotHappenException();
}
return $classLike->name->toString();
}
/**
* @param Stmt[] $stmts
*/
private function resolveAndSaveDependentFiles(array $stmts, \PHPStan\Analyser\MutatingScope $mutatingScope, \Symplify\SmartFileSystem\SmartFileInfo $smartFileInfo) : void
{
$dependentFiles = [];
foreach ($stmts as $stmt) {
try {
$nodeDependentFiles = $this->dependencyResolver->resolveDependencies($stmt, $mutatingScope);
$dependentFiles = \array_merge($dependentFiles, $nodeDependentFiles);
} catch (\PHPStan\AnalysedCodeException $exception) {
// @ignoreException
}
}
$this->changedFilesDetector->addFileWithDependencies($smartFileInfo, $dependentFiles);
}
/**
* In case PHPStan tried to parse a file with missing class, it fails.
* But sometimes we want to rename old class that is missing with Rector..
*
* That's why we have to skip fatal errors of PHPStan caused by missing class,
* so Rector can fix it first. Then run Rector again to refactor code with new classes.
*/
private function decoratePHPStanNodeScopeResolverWithRenamedClassSourceLocator(\PHPStan\Analyser\NodeScopeResolver $nodeScopeResolver) : void
{
// 1. get PHPStan locator
/** @var ClassReflector $classReflector */
$classReflector = $this->privatesAccessor->getPrivateProperty($nodeScopeResolver, 'classReflector');
/** @var SourceLocator $sourceLocator */
$sourceLocator = $this->privatesAccessor->getPrivateProperty($classReflector, 'sourceLocator');
// 2. get Rector locator
$aggregateSourceLocator = new \PHPStan\BetterReflection\SourceLocator\Type\AggregateSourceLocator([$sourceLocator, $this->renamedClassesSourceLocator, $this->parentAttributeSourceLocator]);
$this->privatesAccessor->setPrivateProperty($classReflector, 'sourceLocator', $aggregateSourceLocator);
}
}