NameResolver.php 9.5 KB

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