NameResolver.php 8.0 KB

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