NameResolver.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 Node\PropertyHook) {
  101. foreach ($node->params as $param) {
  102. $param->type = $this->resolveType($param->type);
  103. $this->resolveAttrGroups($param);
  104. }
  105. $this->resolveAttrGroups($node);
  106. } elseif ($node instanceof Stmt\Const_) {
  107. foreach ($node->consts as $const) {
  108. $this->addNamespacedName($const);
  109. }
  110. } elseif ($node instanceof Stmt\ClassConst) {
  111. if (null !== $node->type) {
  112. $node->type = $this->resolveType($node->type);
  113. }
  114. $this->resolveAttrGroups($node);
  115. } elseif ($node instanceof Stmt\EnumCase) {
  116. $this->resolveAttrGroups($node);
  117. } elseif ($node instanceof Expr\StaticCall
  118. || $node instanceof Expr\StaticPropertyFetch
  119. || $node instanceof Expr\ClassConstFetch
  120. || $node instanceof Expr\New_
  121. || $node instanceof Expr\Instanceof_
  122. ) {
  123. if ($node->class instanceof Name) {
  124. $node->class = $this->resolveClassName($node->class);
  125. }
  126. } elseif ($node instanceof Stmt\Catch_) {
  127. foreach ($node->types as &$type) {
  128. $type = $this->resolveClassName($type);
  129. }
  130. } elseif ($node instanceof Expr\FuncCall) {
  131. if ($node->name instanceof Name) {
  132. $node->name = $this->resolveName($node->name, Stmt\Use_::TYPE_FUNCTION);
  133. }
  134. } elseif ($node instanceof Expr\ConstFetch) {
  135. $node->name = $this->resolveName($node->name, Stmt\Use_::TYPE_CONSTANT);
  136. } elseif ($node instanceof Stmt\TraitUse) {
  137. foreach ($node->traits as &$trait) {
  138. $trait = $this->resolveClassName($trait);
  139. }
  140. foreach ($node->adaptations as $adaptation) {
  141. if (null !== $adaptation->trait) {
  142. $adaptation->trait = $this->resolveClassName($adaptation->trait);
  143. }
  144. if ($adaptation instanceof Stmt\TraitUseAdaptation\Precedence) {
  145. foreach ($adaptation->insteadof as &$insteadof) {
  146. $insteadof = $this->resolveClassName($insteadof);
  147. }
  148. }
  149. }
  150. }
  151. return null;
  152. }
  153. /** @param Stmt\Use_::TYPE_* $type */
  154. private function addAlias(Node\UseItem $use, int $type, ?Name $prefix = null): void {
  155. // Add prefix for group uses
  156. $name = $prefix ? Name::concat($prefix, $use->name) : $use->name;
  157. // Type is determined either by individual element or whole use declaration
  158. $type |= $use->type;
  159. $this->nameContext->addAlias(
  160. $name, (string) $use->getAlias(), $type, $use->getAttributes()
  161. );
  162. }
  163. /** @param Stmt\Function_|Stmt\ClassMethod|Expr\Closure|Expr\ArrowFunction $node */
  164. private function resolveSignature($node): void {
  165. foreach ($node->params as $param) {
  166. $param->type = $this->resolveType($param->type);
  167. $this->resolveAttrGroups($param);
  168. }
  169. $node->returnType = $this->resolveType($node->returnType);
  170. }
  171. /**
  172. * @template T of Node\Identifier|Name|Node\ComplexType|null
  173. * @param T $node
  174. * @return T
  175. */
  176. private function resolveType(?Node $node): ?Node {
  177. if ($node instanceof Name) {
  178. return $this->resolveClassName($node);
  179. }
  180. if ($node instanceof Node\NullableType) {
  181. $node->type = $this->resolveType($node->type);
  182. return $node;
  183. }
  184. if ($node instanceof Node\UnionType || $node instanceof Node\IntersectionType) {
  185. foreach ($node->types as &$type) {
  186. $type = $this->resolveType($type);
  187. }
  188. return $node;
  189. }
  190. return $node;
  191. }
  192. /**
  193. * Resolve name, according to name resolver options.
  194. *
  195. * @param Name $name Function or constant name to resolve
  196. * @param Stmt\Use_::TYPE_* $type One of Stmt\Use_::TYPE_*
  197. *
  198. * @return Name Resolved name, or original name with attribute
  199. */
  200. protected function resolveName(Name $name, int $type): Name {
  201. if (!$this->replaceNodes) {
  202. $resolvedName = $this->nameContext->getResolvedName($name, $type);
  203. if (null !== $resolvedName) {
  204. $name->setAttribute('resolvedName', $resolvedName);
  205. } else {
  206. $name->setAttribute('namespacedName', FullyQualified::concat(
  207. $this->nameContext->getNamespace(), $name, $name->getAttributes()));
  208. }
  209. return $name;
  210. }
  211. if ($this->preserveOriginalNames) {
  212. // Save the original name
  213. $originalName = $name;
  214. $name = clone $originalName;
  215. $name->setAttribute('originalName', $originalName);
  216. }
  217. $resolvedName = $this->nameContext->getResolvedName($name, $type);
  218. if (null !== $resolvedName) {
  219. return $resolvedName;
  220. }
  221. // unqualified names inside a namespace cannot be resolved at compile-time
  222. // add the namespaced version of the name as an attribute
  223. $name->setAttribute('namespacedName', FullyQualified::concat(
  224. $this->nameContext->getNamespace(), $name, $name->getAttributes()));
  225. return $name;
  226. }
  227. protected function resolveClassName(Name $name): Name {
  228. return $this->resolveName($name, Stmt\Use_::TYPE_NORMAL);
  229. }
  230. protected function addNamespacedName(Node $node): void {
  231. $node->namespacedName = Name::concat(
  232. $this->nameContext->getNamespace(), (string) $node->name);
  233. }
  234. protected function resolveAttrGroups(Node $node): void {
  235. foreach ($node->attrGroups as $attrGroup) {
  236. foreach ($attrGroup->attrs as $attr) {
  237. $attr->name = $this->resolveClassName($attr->name);
  238. }
  239. }
  240. }
  241. }