NameResolver.php 8.3 KB

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