NameResolver.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace PhpParser\NodeVisitor;
  3. use PhpParser\NodeVisitorAbstract;
  4. use PhpParser\Error;
  5. use PhpParser\Node;
  6. use PhpParser\Node\Name;
  7. use PhpParser\Node\Name\FullyQualified;
  8. use PhpParser\Node\Expr;
  9. use PhpParser\Node\Stmt;
  10. class NameResolver extends NodeVisitorAbstract
  11. {
  12. /** @var null|Name Current namespace */
  13. protected $namespace;
  14. /** @var array Map of format [aliasType => [aliasName => originalName]] */
  15. protected $aliases;
  16. public function beforeTraverse(array $nodes) {
  17. $this->resetState();
  18. }
  19. public function enterNode(Node $node) {
  20. if ($node instanceof Stmt\Namespace_) {
  21. $this->resetState($node->name);
  22. } elseif ($node instanceof Stmt\Use_) {
  23. foreach ($node->uses as $use) {
  24. $this->addAlias($use, $node->type, null);
  25. }
  26. } elseif ($node instanceof Stmt\GroupUse) {
  27. foreach ($node->uses as $use) {
  28. $this->addAlias($use, $node->type, $node->prefix);
  29. }
  30. } elseif ($node instanceof Stmt\Class_) {
  31. if (null !== $node->extends) {
  32. $node->extends = $this->resolveClassName($node->extends);
  33. }
  34. foreach ($node->implements as &$interface) {
  35. $interface = $this->resolveClassName($interface);
  36. }
  37. if (null !== $node->name) {
  38. $this->addNamespacedName($node);
  39. }
  40. } elseif ($node instanceof Stmt\Interface_) {
  41. foreach ($node->extends as &$interface) {
  42. $interface = $this->resolveClassName($interface);
  43. }
  44. $this->addNamespacedName($node);
  45. } elseif ($node instanceof Stmt\Trait_) {
  46. $this->addNamespacedName($node);
  47. } elseif ($node instanceof Stmt\Function_) {
  48. $this->addNamespacedName($node);
  49. $this->resolveSignature($node);
  50. } elseif ($node instanceof Stmt\ClassMethod
  51. || $node instanceof Expr\Closure
  52. ) {
  53. $this->resolveSignature($node);
  54. } elseif ($node instanceof Stmt\Const_) {
  55. foreach ($node->consts as $const) {
  56. $this->addNamespacedName($const);
  57. }
  58. } elseif ($node instanceof Expr\StaticCall
  59. || $node instanceof Expr\StaticPropertyFetch
  60. || $node instanceof Expr\ClassConstFetch
  61. || $node instanceof Expr\New_
  62. || $node instanceof Expr\Instanceof_
  63. ) {
  64. if ($node->class instanceof Name) {
  65. $node->class = $this->resolveClassName($node->class);
  66. }
  67. } elseif ($node instanceof Stmt\Catch_) {
  68. $node->type = $this->resolveClassName($node->type);
  69. } elseif ($node instanceof Expr\FuncCall) {
  70. if ($node->name instanceof Name) {
  71. $node->name = $this->resolveOtherName($node->name, Stmt\Use_::TYPE_FUNCTION);
  72. }
  73. } elseif ($node instanceof Expr\ConstFetch) {
  74. $node->name = $this->resolveOtherName($node->name, Stmt\Use_::TYPE_CONSTANT);
  75. } elseif ($node instanceof Stmt\TraitUse) {
  76. foreach ($node->traits as &$trait) {
  77. $trait = $this->resolveClassName($trait);
  78. }
  79. foreach ($node->adaptations as $adaptation) {
  80. if (null !== $adaptation->trait) {
  81. $adaptation->trait = $this->resolveClassName($adaptation->trait);
  82. }
  83. if ($adaptation instanceof Stmt\TraitUseAdaptation\Precedence) {
  84. foreach ($adaptation->insteadof as &$insteadof) {
  85. $insteadof = $this->resolveClassName($insteadof);
  86. }
  87. }
  88. }
  89. }
  90. }
  91. protected function resetState(Name $namespace = null) {
  92. $this->namespace = $namespace;
  93. $this->aliases = array(
  94. Stmt\Use_::TYPE_NORMAL => array(),
  95. Stmt\Use_::TYPE_FUNCTION => array(),
  96. Stmt\Use_::TYPE_CONSTANT => array(),
  97. );
  98. }
  99. protected function addAlias(Stmt\UseUse $use, $type, Name $prefix = null) {
  100. // Add prefix for group uses
  101. $name = $prefix ? Name::concat($prefix, $use->name) : $use->name;
  102. // Type is determined either by individual element or whole use declaration
  103. $type |= $use->type;
  104. // Constant names are case sensitive, everything else case insensitive
  105. if ($type === Stmt\Use_::TYPE_CONSTANT) {
  106. $aliasName = $use->alias;
  107. } else {
  108. $aliasName = strtolower($use->alias);
  109. }
  110. if (isset($this->aliases[$type][$aliasName])) {
  111. $typeStringMap = array(
  112. Stmt\Use_::TYPE_NORMAL => '',
  113. Stmt\Use_::TYPE_FUNCTION => 'function ',
  114. Stmt\Use_::TYPE_CONSTANT => 'const ',
  115. );
  116. throw new Error(
  117. sprintf(
  118. 'Cannot use %s%s as %s because the name is already in use',
  119. $typeStringMap[$type], $name, $use->alias
  120. ),
  121. $use->getLine()
  122. );
  123. }
  124. $this->aliases[$type][$aliasName] = $name;
  125. }
  126. /** @param Stmt\Function_|Stmt\ClassMethod|Expr\Closure $node */
  127. private function resolveSignature($node) {
  128. foreach ($node->params as $param) {
  129. if ($param->type instanceof Name) {
  130. $param->type = $this->resolveClassName($param->type);
  131. }
  132. }
  133. if ($node->returnType instanceof Name) {
  134. $node->returnType = $this->resolveClassName($node->returnType);
  135. }
  136. }
  137. protected function resolveClassName(Name $name) {
  138. // don't resolve special class names
  139. if (in_array(strtolower($name->toString()), array('self', 'parent', 'static'))) {
  140. if (!$name->isUnqualified()) {
  141. throw new Error(
  142. sprintf("'\\%s' is an invalid class name", $name->toString()),
  143. $name->getLine()
  144. );
  145. }
  146. return $name;
  147. }
  148. // fully qualified names are already resolved
  149. if ($name->isFullyQualified()) {
  150. return $name;
  151. }
  152. $aliasName = strtolower($name->getFirst());
  153. if (!$name->isRelative() && isset($this->aliases[Stmt\Use_::TYPE_NORMAL][$aliasName])) {
  154. // resolve aliases (for non-relative names)
  155. $alias = $this->aliases[Stmt\Use_::TYPE_NORMAL][$aliasName];
  156. return FullyQualified::concat($alias, $name->slice(1), $name->getAttributes());
  157. }
  158. if (null !== $this->namespace) {
  159. // if no alias exists prepend current namespace
  160. return FullyQualified::concat($this->namespace, $name, $name->getAttributes());
  161. }
  162. return new FullyQualified($name->parts, $name->getAttributes());
  163. }
  164. protected function resolveOtherName(Name $name, $type) {
  165. // fully qualified names are already resolved
  166. if ($name->isFullyQualified()) {
  167. return $name;
  168. }
  169. // resolve aliases for qualified names
  170. $aliasName = strtolower($name->getFirst());
  171. if ($name->isQualified() && isset($this->aliases[Stmt\Use_::TYPE_NORMAL][$aliasName])) {
  172. $alias = $this->aliases[Stmt\Use_::TYPE_NORMAL][$aliasName];
  173. return FullyQualified::concat($alias, $name->slice(1), $name->getAttributes());
  174. }
  175. if ($name->isUnqualified()) {
  176. if ($type === Stmt\Use_::TYPE_CONSTANT) {
  177. // constant aliases are case-sensitive, function aliases case-insensitive
  178. $aliasName = $name->getFirst();
  179. }
  180. if (!isset($this->aliases[$type][$aliasName])) {
  181. // unqualified, unaliased names cannot be resolved at compile-time
  182. return $name;
  183. }
  184. // resolve unqualified aliases
  185. return new FullyQualified($this->aliases[$type][$aliasName], $name->getAttributes());
  186. }
  187. if (null !== $this->namespace) {
  188. // if no alias exists prepend current namespace
  189. return FullyQualified::concat($this->namespace, $name, $name->getAttributes());
  190. }
  191. return new FullyQualified($name->parts, $name->getAttributes());
  192. }
  193. protected function addNamespacedName(Node $node) {
  194. if (null !== $this->namespace) {
  195. $node->namespacedName = Name::concat($this->namespace, $node->name);
  196. } else {
  197. $node->namespacedName = new Name($node->name);
  198. }
  199. }
  200. }