NameResolver.php 9.4 KB

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