NameResolver.php 10 KB

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