Chris@13: nameContext = new NameContext($errorHandler ?? new ErrorHandler\Throwing); Chris@13: $this->preserveOriginalNames = $options['preserveOriginalNames'] ?? false; Chris@13: $this->replaceNodes = $options['replaceNodes'] ?? true; Chris@13: } Chris@13: Chris@13: /** Chris@13: * Get name resolution context. Chris@13: * Chris@13: * @return NameContext Chris@13: */ Chris@13: public function getNameContext() : NameContext { Chris@13: return $this->nameContext; Chris@0: } Chris@0: Chris@0: public function beforeTraverse(array $nodes) { Chris@13: $this->nameContext->startNamespace(); Chris@13: return null; Chris@0: } Chris@0: Chris@0: public function enterNode(Node $node) { Chris@0: if ($node instanceof Stmt\Namespace_) { Chris@13: $this->nameContext->startNamespace($node->name); Chris@0: } elseif ($node instanceof Stmt\Use_) { Chris@0: foreach ($node->uses as $use) { Chris@0: $this->addAlias($use, $node->type, null); Chris@0: } Chris@0: } elseif ($node instanceof Stmt\GroupUse) { Chris@0: foreach ($node->uses as $use) { Chris@0: $this->addAlias($use, $node->type, $node->prefix); Chris@0: } Chris@0: } elseif ($node instanceof Stmt\Class_) { Chris@0: if (null !== $node->extends) { Chris@0: $node->extends = $this->resolveClassName($node->extends); Chris@0: } Chris@0: Chris@0: foreach ($node->implements as &$interface) { Chris@0: $interface = $this->resolveClassName($interface); Chris@0: } Chris@0: Chris@0: if (null !== $node->name) { Chris@0: $this->addNamespacedName($node); Chris@0: } Chris@0: } elseif ($node instanceof Stmt\Interface_) { Chris@0: foreach ($node->extends as &$interface) { Chris@0: $interface = $this->resolveClassName($interface); Chris@0: } Chris@0: Chris@0: $this->addNamespacedName($node); Chris@0: } elseif ($node instanceof Stmt\Trait_) { Chris@0: $this->addNamespacedName($node); Chris@0: } elseif ($node instanceof Stmt\Function_) { Chris@0: $this->addNamespacedName($node); Chris@0: $this->resolveSignature($node); Chris@0: } elseif ($node instanceof Stmt\ClassMethod Chris@0: || $node instanceof Expr\Closure Chris@0: ) { Chris@0: $this->resolveSignature($node); Chris@17: } elseif ($node instanceof Stmt\Property) { Chris@17: if (null !== $node->type) { Chris@17: $node->type = $this->resolveType($node->type); Chris@17: } Chris@0: } elseif ($node instanceof Stmt\Const_) { Chris@0: foreach ($node->consts as $const) { Chris@0: $this->addNamespacedName($const); Chris@0: } Chris@0: } elseif ($node instanceof Expr\StaticCall Chris@0: || $node instanceof Expr\StaticPropertyFetch Chris@0: || $node instanceof Expr\ClassConstFetch Chris@0: || $node instanceof Expr\New_ Chris@0: || $node instanceof Expr\Instanceof_ Chris@0: ) { Chris@0: if ($node->class instanceof Name) { Chris@0: $node->class = $this->resolveClassName($node->class); Chris@0: } Chris@0: } elseif ($node instanceof Stmt\Catch_) { Chris@0: foreach ($node->types as &$type) { Chris@0: $type = $this->resolveClassName($type); Chris@0: } Chris@0: } elseif ($node instanceof Expr\FuncCall) { Chris@0: if ($node->name instanceof Name) { Chris@13: $node->name = $this->resolveName($node->name, Stmt\Use_::TYPE_FUNCTION); Chris@0: } Chris@0: } elseif ($node instanceof Expr\ConstFetch) { Chris@13: $node->name = $this->resolveName($node->name, Stmt\Use_::TYPE_CONSTANT); Chris@0: } elseif ($node instanceof Stmt\TraitUse) { Chris@0: foreach ($node->traits as &$trait) { Chris@0: $trait = $this->resolveClassName($trait); Chris@0: } Chris@0: Chris@0: foreach ($node->adaptations as $adaptation) { Chris@0: if (null !== $adaptation->trait) { Chris@0: $adaptation->trait = $this->resolveClassName($adaptation->trait); Chris@0: } Chris@0: Chris@0: if ($adaptation instanceof Stmt\TraitUseAdaptation\Precedence) { Chris@0: foreach ($adaptation->insteadof as &$insteadof) { Chris@0: $insteadof = $this->resolveClassName($insteadof); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@13: Chris@13: return null; Chris@0: } Chris@0: Chris@13: private function addAlias(Stmt\UseUse $use, $type, Name $prefix = null) { Chris@0: // Add prefix for group uses Chris@0: $name = $prefix ? Name::concat($prefix, $use->name) : $use->name; Chris@0: // Type is determined either by individual element or whole use declaration Chris@0: $type |= $use->type; Chris@0: Chris@13: $this->nameContext->addAlias( Chris@13: $name, (string) $use->getAlias(), $type, $use->getAttributes() Chris@13: ); Chris@0: } Chris@0: Chris@0: /** @param Stmt\Function_|Stmt\ClassMethod|Expr\Closure $node */ Chris@0: private function resolveSignature($node) { Chris@0: foreach ($node->params as $param) { Chris@0: $param->type = $this->resolveType($param->type); Chris@0: } Chris@0: $node->returnType = $this->resolveType($node->returnType); Chris@0: } Chris@0: Chris@0: private function resolveType($node) { Chris@0: if ($node instanceof Node\NullableType) { Chris@0: $node->type = $this->resolveType($node->type); Chris@0: return $node; Chris@0: } Chris@0: if ($node instanceof Name) { Chris@0: return $this->resolveClassName($node); Chris@0: } Chris@0: return $node; Chris@0: } Chris@0: Chris@13: /** Chris@13: * Resolve name, according to name resolver options. Chris@13: * Chris@13: * @param Name $name Function or constant name to resolve Chris@13: * @param int $type One of Stmt\Use_::TYPE_* Chris@13: * Chris@13: * @return Name Resolved name, or original name with attribute Chris@13: */ Chris@13: protected function resolveName(Name $name, int $type) : Name { Chris@13: if (!$this->replaceNodes) { Chris@13: $resolvedName = $this->nameContext->getResolvedName($name, $type); Chris@13: if (null !== $resolvedName) { Chris@13: $name->setAttribute('resolvedName', $resolvedName); Chris@13: } else { Chris@13: $name->setAttribute('namespacedName', FullyQualified::concat( Chris@13: $this->nameContext->getNamespace(), $name, $name->getAttributes())); Chris@13: } Chris@13: return $name; Chris@13: } Chris@13: Chris@0: if ($this->preserveOriginalNames) { Chris@0: // Save the original name Chris@0: $originalName = $name; Chris@0: $name = clone $originalName; Chris@0: $name->setAttribute('originalName', $originalName); Chris@0: } Chris@0: Chris@13: $resolvedName = $this->nameContext->getResolvedName($name, $type); Chris@13: if (null !== $resolvedName) { Chris@13: return $resolvedName; Chris@0: } Chris@0: Chris@13: // unqualified names inside a namespace cannot be resolved at compile-time Chris@13: // add the namespaced version of the name as an attribute Chris@13: $name->setAttribute('namespacedName', FullyQualified::concat( Chris@13: $this->nameContext->getNamespace(), $name, $name->getAttributes())); Chris@13: return $name; Chris@0: } Chris@0: Chris@13: protected function resolveClassName(Name $name) { Chris@13: return $this->resolveName($name, Stmt\Use_::TYPE_NORMAL); Chris@0: } Chris@0: Chris@0: protected function addNamespacedName(Node $node) { Chris@13: $node->namespacedName = Name::concat( Chris@13: $this->nameContext->getNamespace(), (string) $node->name); Chris@0: } Chris@0: }