*/ private $shortNamesByFilePath = []; /** * @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory */ private $phpDocInfoFactory; /** * @readonly * @var \Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser */ private $simpleCallableNodeTraverser; /** * @readonly * @var \Rector\NodeNameResolver\NodeNameResolver */ private $nodeNameResolver; /** * @readonly * @var \PHPStan\Reflection\ReflectionProvider */ private $reflectionProvider; /** * @readonly * @var \Rector\Core\PhpParser\Node\BetterNodeFinder */ private $betterNodeFinder; /** * @readonly * @var \Rector\CodingStyle\NodeAnalyzer\UseImportNameMatcher */ private $useImportNameMatcher; public function __construct(\RectorPrefix20220522\Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser $simpleCallableNodeTraverser, \Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver, \PHPStan\Reflection\ReflectionProvider $reflectionProvider, \Rector\Core\PhpParser\Node\BetterNodeFinder $betterNodeFinder, \Rector\CodingStyle\NodeAnalyzer\UseImportNameMatcher $useImportNameMatcher) { $this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser; $this->nodeNameResolver = $nodeNameResolver; $this->reflectionProvider = $reflectionProvider; $this->betterNodeFinder = $betterNodeFinder; $this->useImportNameMatcher = $useImportNameMatcher; } // Avoids circular reference /** * @required */ public function autowire(\Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory $phpDocInfoFactory) : void { $this->phpDocInfoFactory = $phpDocInfoFactory; } /** * @return array */ public function resolveFromFile(\Rector\Core\ValueObject\Application\File $file) : array { $filePath = $file->getFilePath(); if (isset($this->shortNamesByFilePath[$filePath])) { return $this->shortNamesByFilePath[$filePath]; } $shortNamesToFullyQualifiedNames = $this->resolveForStmts($file->getNewStmts()); $this->shortNamesByFilePath[$filePath] = $shortNamesToFullyQualifiedNames; return $shortNamesToFullyQualifiedNames; } /** * Collects all "class ", "trait " and "interface " * @return string[] */ public function resolveShortClassLikeNamesForNode(\PhpParser\Node $node) : array { $namespace = $this->betterNodeFinder->findParentType($node, \PhpParser\Node\Stmt\Namespace_::class); if (!$namespace instanceof \PhpParser\Node\Stmt\Namespace_) { // only handle namespace nodes return []; } /** @var ClassLike[] $classLikes */ $classLikes = $this->betterNodeFinder->findInstanceOf($namespace, \PhpParser\Node\Stmt\ClassLike::class); $shortClassLikeNames = []; foreach ($classLikes as $classLike) { $shortClassLikeNames[] = $this->nodeNameResolver->getShortName($classLike); } return \array_unique($shortClassLikeNames); } /** * @param Stmt[] $stmts * @return array */ private function resolveForStmts(array $stmts) : array { $shortNamesToFullyQualifiedNames = []; $this->simpleCallableNodeTraverser->traverseNodesWithCallable($stmts, function (\PhpParser\Node $node) use(&$shortNamesToFullyQualifiedNames) { // class name is used! if ($node instanceof \PhpParser\Node\Stmt\ClassLike && $node->name instanceof \PhpParser\Node\Identifier) { $fullyQualifiedName = $this->nodeNameResolver->getName($node); if ($fullyQualifiedName === null) { return null; } $shortNamesToFullyQualifiedNames[$node->name->toString()] = $fullyQualifiedName; return null; } if (!$node instanceof \PhpParser\Node\Name) { return null; } $originalName = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::ORIGINAL_NAME); if (!$originalName instanceof \PhpParser\Node\Name) { return null; } // already short if (\strpos($originalName->toString(), '\\') !== \false) { return null; } $fullyQualifiedName = $this->nodeNameResolver->getName($node); $shortNamesToFullyQualifiedNames[$originalName->toString()] = $fullyQualifiedName; return null; }); $docBlockShortNamesToFullyQualifiedNames = $this->resolveFromStmtsDocBlocks($stmts); /** @var array $result */ $result = \array_merge($shortNamesToFullyQualifiedNames, $docBlockShortNamesToFullyQualifiedNames); return $result; } /** * @param Stmt[] $stmts * @return array */ private function resolveFromStmtsDocBlocks(array $stmts) : array { $reflectionClass = $this->resolveNativeClassReflection($stmts); $shortNames = []; $this->simpleCallableNodeTraverser->traverseNodesWithCallable($stmts, function (\PhpParser\Node $node) use(&$shortNames) { // speed up for nodes that are $phpDocInfo = $this->phpDocInfoFactory->createFromNode($node); if (!$phpDocInfo instanceof \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo) { return null; } $phpDocNodeTraverser = new \RectorPrefix20220522\Symplify\Astral\PhpDocParser\PhpDocNodeTraverser(); $phpDocNodeTraverser->traverseWithCallable($phpDocInfo->getPhpDocNode(), '', function ($node) use(&$shortNames) { if ($node instanceof \PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode) { $shortName = \trim($node->name, '@'); if (\Rector\Core\Util\StringUtils::isMatch($shortName, self::BIG_LETTER_START_REGEX)) { $shortNames[] = $shortName; } return null; } if ($node instanceof \PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode) { $shortNames[] = $node->name; } return null; }); return null; }); return $this->fqnizeShortNames($shortNames, $reflectionClass, $stmts); } /** * @param Node[] $stmts */ private function resolveNativeClassReflection(array $stmts) : ?\ReflectionClass { $firstClassLike = $this->betterNodeFinder->findFirstInstanceOf($stmts, \PhpParser\Node\Stmt\ClassLike::class); if (!$firstClassLike instanceof \PhpParser\Node\Stmt\ClassLike) { return null; } $className = (string) $this->nodeNameResolver->getName($firstClassLike); if (!$this->reflectionProvider->hasClass($className)) { return null; } $classReflection = $this->reflectionProvider->getClass($className); return $classReflection->getNativeReflection(); } /** * @param string[] $shortNames * @param Stmt[] $stmts * @return array */ private function fqnizeShortNames(array $shortNames, ?\ReflectionClass $reflectionClass, array $stmts) : array { $shortNamesToFullyQualifiedNames = []; foreach ($shortNames as $shortName) { $stmtsMatchedName = $this->useImportNameMatcher->matchNameWithStmts($shortName, $stmts); if ($reflectionClass instanceof \ReflectionClass) { $fullyQualifiedName = \RectorPrefix20220522\Nette\Utils\Reflection::expandClassName($shortName, $reflectionClass); } elseif (\is_string($stmtsMatchedName)) { $fullyQualifiedName = $stmtsMatchedName; } else { $fullyQualifiedName = $shortName; } $shortNamesToFullyQualifiedNames[$shortName] = $fullyQualifiedName; } return $shortNamesToFullyQualifiedNames; } }