NameResolver.php 8.9 KB

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