NameResolver.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\NodeVisitor;
  3. use PhpParser\ErrorHandler;
  4. use PhpParser\NameContext;
  5. use PhpParser\Node;
  6. use PhpParser\Node\Expr;
  7. use PhpParser\Node\Name;
  8. use PhpParser\Node\Name\FullyQualified;
  9. use PhpParser\Node\Stmt;
  10. use PhpParser\NodeVisitorAbstract;
  11. class NameResolver extends NodeVisitorAbstract {
  12. /** @var NameContext Naming context */
  13. protected NameContext $nameContext;
  14. /** @var bool Whether to preserve original names */
  15. protected bool $preserveOriginalNames;
  16. /** @var bool Whether to replace resolved nodes in place, or to add resolvedNode attributes */
  17. protected bool $replaceNodes;
  18. /**
  19. * Constructs a name resolution visitor.
  20. *
  21. * Options:
  22. * * preserveOriginalNames (default false): An "originalName" attribute will be added to
  23. * all name nodes that underwent resolution.
  24. * * replaceNodes (default true): Resolved names are replaced in-place. Otherwise, a
  25. * resolvedName attribute is added. (Names that cannot be statically resolved receive a
  26. * namespacedName attribute, as usual.)
  27. *
  28. * @param ErrorHandler|null $errorHandler Error handler
  29. * @param array{preserveOriginalNames?: bool, replaceNodes?: bool} $options Options
  30. */
  31. public function __construct(?ErrorHandler $errorHandler = null, array $options = []) {
  32. $this->nameContext = new NameContext($errorHandler ?? new ErrorHandler\Throwing());
  33. $this->preserveOriginalNames = $options['preserveOriginalNames'] ?? false;
  34. $this->replaceNodes = $options['replaceNodes'] ?? true;
  35. }
  36. /**
  37. * Get name resolution context.
  38. */
  39. public function getNameContext(): NameContext {
  40. return $this->nameContext;
  41. }
  42. public function beforeTraverse(array $nodes): ?array {
  43. $this->nameContext->startNamespace();
  44. return null;
  45. }
  46. public function enterNode(Node $node) {
  47. if ($node instanceof Stmt\Namespace_) {
  48. $this->nameContext->startNamespace($node->name);
  49. } elseif ($node instanceof Stmt\Use_) {
  50. foreach ($node->uses as $use) {
  51. $this->addAlias($use, $node->type, null);
  52. }
  53. } elseif ($node instanceof Stmt\GroupUse) {
  54. foreach ($node->uses as $use) {
  55. $this->addAlias($use, $node->type, $node->prefix);
  56. }
  57. } elseif ($node instanceof Stmt\Class_) {
  58. if (null !== $node->extends) {
  59. $node->extends = $this->resolveClassName($node->extends);
  60. }
  61. foreach ($node->implements as &$interface) {
  62. $interface = $this->resolveClassName($interface);
  63. }
  64. $this->resolveAttrGroups($node);
  65. if (null !== $node->name) {
  66. $this->addNamespacedName($node);
  67. } else {
  68. $node->namespacedName = null;
  69. }
  70. } elseif ($node instanceof Stmt\Interface_) {
  71. foreach ($node->extends as &$interface) {
  72. $interface = $this->resolveClassName($interface);
  73. }
  74. $this->resolveAttrGroups($node);
  75. $this->addNamespacedName($node);
  76. } elseif ($node instanceof Stmt\Enum_) {
  77. foreach ($node->implements as &$interface) {
  78. $interface = $this->resolveClassName($interface);
  79. }
  80. $this->resolveAttrGroups($node);
  81. $this->addNamespacedName($node);
  82. } elseif ($node instanceof Stmt\Trait_) {
  83. $this->resolveAttrGroups($node);
  84. $this->addNamespacedName($node);
  85. } elseif ($node instanceof Stmt\Function_) {
  86. $this->resolveSignature($node);
  87. $this->resolveAttrGroups($node);
  88. $this->addNamespacedName($node);
  89. } elseif ($node instanceof Stmt\ClassMethod
  90. || $node instanceof Expr\Closure
  91. || $node instanceof Expr\ArrowFunction
  92. ) {
  93. $this->resolveSignature($node);
  94. $this->resolveAttrGroups($node);
  95. } elseif ($node instanceof Stmt\Property) {
  96. if (null !== $node->type) {
  97. $node->type = $this->resolveType($node->type);
  98. }
  99. $this->resolveAttrGroups($node);
  100. } elseif ($node instanceof Stmt\Const_) {
  101. foreach ($node->consts as $const) {
  102. $this->addNamespacedName($const);
  103. }
  104. } elseif ($node instanceof Stmt\ClassConst) {
  105. if (null !== $node->type) {
  106. $node->type = $this->resolveType($node->type);
  107. }
  108. $this->resolveAttrGroups($node);
  109. } elseif ($node instanceof Stmt\EnumCase) {
  110. $this->resolveAttrGroups($node);
  111. } elseif ($node instanceof Expr\StaticCall
  112. || $node instanceof Expr\StaticPropertyFetch
  113. || $node instanceof Expr\ClassConstFetch
  114. || $node instanceof Expr\New_
  115. || $node instanceof Expr\Instanceof_
  116. ) {
  117. if ($node->class instanceof Name) {
  118. $node->class = $this->resolveClassName($node->class);
  119. }
  120. } elseif ($node instanceof Stmt\Catch_) {
  121. foreach ($node->types as &$type) {
  122. $type = $this->resolveClassName($type);
  123. }
  124. } elseif ($node instanceof Expr\FuncCall) {
  125. if ($node->name instanceof Name) {
  126. $node->name = $this->resolveName($node->name, Stmt\Use_::TYPE_FUNCTION);
  127. }
  128. } elseif ($node instanceof Expr\ConstFetch) {
  129. $node->name = $this->resolveName($node->name, Stmt\Use_::TYPE_CONSTANT);
  130. } elseif ($node instanceof Stmt\TraitUse) {
  131. foreach ($node->traits as &$trait) {
  132. $trait = $this->resolveClassName($trait);
  133. }
  134. foreach ($node->adaptations as $adaptation) {
  135. if (null !== $adaptation->trait) {
  136. $adaptation->trait = $this->resolveClassName($adaptation->trait);
  137. }
  138. if ($adaptation instanceof Stmt\TraitUseAdaptation\Precedence) {
  139. foreach ($adaptation->insteadof as &$insteadof) {
  140. $insteadof = $this->resolveClassName($insteadof);
  141. }
  142. }
  143. }
  144. }
  145. return null;
  146. }
  147. /** @param Stmt\Use_::TYPE_* $type */
  148. private function addAlias(Node\UseItem $use, int $type, ?Name $prefix = null): void {
  149. // Add prefix for group uses
  150. $name = $prefix ? Name::concat($prefix, $use->name) : $use->name;
  151. // Type is determined either by individual element or whole use declaration
  152. $type |= $use->type;
  153. $this->nameContext->addAlias(
  154. $name, (string) $use->getAlias(), $type, $use->getAttributes()
  155. );
  156. }
  157. /** @param Stmt\Function_|Stmt\ClassMethod|Expr\Closure|Expr\ArrowFunction $node */
  158. private function resolveSignature($node): void {
  159. foreach ($node->params as $param) {
  160. $param->type = $this->resolveType($param->type);
  161. $this->resolveAttrGroups($param);
  162. }
  163. $node->returnType = $this->resolveType($node->returnType);
  164. }
  165. /**
  166. * @template T of Node\Identifier|Name|Node\ComplexType|null
  167. * @param T $node
  168. * @return T
  169. */
  170. private function resolveType(?Node $node): ?Node {
  171. if ($node instanceof Name) {
  172. return $this->resolveClassName($node);
  173. }
  174. if ($node instanceof Node\NullableType) {
  175. $node->type = $this->resolveType($node->type);
  176. return $node;
  177. }
  178. if ($node instanceof Node\UnionType || $node instanceof Node\IntersectionType) {
  179. foreach ($node->types as &$type) {
  180. $type = $this->resolveType($type);
  181. }
  182. return $node;
  183. }
  184. return $node;
  185. }
  186. /**
  187. * Resolve name, according to name resolver options.
  188. *
  189. * @param Name $name Function or constant name to resolve
  190. * @param Stmt\Use_::TYPE_* $type One of Stmt\Use_::TYPE_*
  191. *
  192. * @return Name Resolved name, or original name with attribute
  193. */
  194. protected function resolveName(Name $name, int $type): Name {
  195. if (!$this->replaceNodes) {
  196. $resolvedName = $this->nameContext->getResolvedName($name, $type);
  197. if (null !== $resolvedName) {
  198. $name->setAttribute('resolvedName', $resolvedName);
  199. } else {
  200. $name->setAttribute('namespacedName', FullyQualified::concat(
  201. $this->nameContext->getNamespace(), $name, $name->getAttributes()));
  202. }
  203. return $name;
  204. }
  205. if ($this->preserveOriginalNames) {
  206. // Save the original name
  207. $originalName = $name;
  208. $name = clone $originalName;
  209. $name->setAttribute('originalName', $originalName);
  210. }
  211. $resolvedName = $this->nameContext->getResolvedName($name, $type);
  212. if (null !== $resolvedName) {
  213. return $resolvedName;
  214. }
  215. // unqualified names inside a namespace cannot be resolved at compile-time
  216. // add the namespaced version of the name as an attribute
  217. $name->setAttribute('namespacedName', FullyQualified::concat(
  218. $this->nameContext->getNamespace(), $name, $name->getAttributes()));
  219. return $name;
  220. }
  221. protected function resolveClassName(Name $name): Name {
  222. return $this->resolveName($name, Stmt\Use_::TYPE_NORMAL);
  223. }
  224. protected function addNamespacedName(Node $node): void {
  225. $node->namespacedName = Name::concat(
  226. $this->nameContext->getNamespace(), (string) $node->name);
  227. }
  228. protected function resolveAttrGroups(Node $node): void {
  229. foreach ($node->attrGroups as $attrGroup) {
  230. foreach ($attrGroup->attrs as $attr) {
  231. $attr->name = $this->resolveClassName($attr->name);
  232. }
  233. }
  234. }
  235. }