No Description

NameResolver.php 7.3KB

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