NodeTraverser.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php declare(strict_types=1);
  2. namespace PhpParser;
  3. class NodeTraverser implements NodeTraverserInterface {
  4. /**
  5. * @deprecated Use NodeVisitor::DONT_TRAVERSE_CHILDREN instead.
  6. */
  7. public const DONT_TRAVERSE_CHILDREN = NodeVisitor::DONT_TRAVERSE_CHILDREN;
  8. /**
  9. * @deprecated Use NodeVisitor::STOP_TRAVERSAL instead.
  10. */
  11. public const STOP_TRAVERSAL = NodeVisitor::STOP_TRAVERSAL;
  12. /**
  13. * @deprecated Use NodeVisitor::REMOVE_NODE instead.
  14. */
  15. public const REMOVE_NODE = NodeVisitor::REMOVE_NODE;
  16. /**
  17. * @deprecated Use NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN instead.
  18. */
  19. public const DONT_TRAVERSE_CURRENT_AND_CHILDREN = NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
  20. /** @var list<NodeVisitor> Visitors */
  21. protected array $visitors = [];
  22. /** @var bool Whether traversal should be stopped */
  23. protected bool $stopTraversal;
  24. /**
  25. * Create a traverser with the given visitors.
  26. *
  27. * @param NodeVisitor ...$visitors Node visitors
  28. */
  29. public function __construct(NodeVisitor ...$visitors) {
  30. $this->visitors = $visitors;
  31. }
  32. /**
  33. * Adds a visitor.
  34. *
  35. * @param NodeVisitor $visitor Visitor to add
  36. */
  37. public function addVisitor(NodeVisitor $visitor): void {
  38. $this->visitors[] = $visitor;
  39. }
  40. /**
  41. * Removes an added visitor.
  42. */
  43. public function removeVisitor(NodeVisitor $visitor): void {
  44. $index = array_search($visitor, $this->visitors);
  45. if ($index !== false) {
  46. array_splice($this->visitors, $index, 1, []);
  47. }
  48. }
  49. /**
  50. * Traverses an array of nodes using the registered visitors.
  51. *
  52. * @param Node[] $nodes Array of nodes
  53. *
  54. * @return Node[] Traversed array of nodes
  55. */
  56. public function traverse(array $nodes): array {
  57. $this->stopTraversal = false;
  58. foreach ($this->visitors as $visitor) {
  59. if (null !== $return = $visitor->beforeTraverse($nodes)) {
  60. $nodes = $return;
  61. }
  62. }
  63. $nodes = $this->traverseArray($nodes);
  64. for ($i = \count($this->visitors) - 1; $i >= 0; --$i) {
  65. $visitor = $this->visitors[$i];
  66. if (null !== $return = $visitor->afterTraverse($nodes)) {
  67. $nodes = $return;
  68. }
  69. }
  70. return $nodes;
  71. }
  72. /**
  73. * Recursively traverse a node.
  74. *
  75. * @param Node $node Node to traverse.
  76. */
  77. protected function traverseNode(Node $node): void {
  78. foreach ($node->getSubNodeNames() as $name) {
  79. $subNode = $node->$name;
  80. if (\is_array($subNode)) {
  81. $node->$name = $this->traverseArray($subNode);
  82. if ($this->stopTraversal) {
  83. break;
  84. }
  85. continue;
  86. }
  87. if (!$subNode instanceof Node) {
  88. continue;
  89. }
  90. $traverseChildren = true;
  91. $visitorIndex = -1;
  92. foreach ($this->visitors as $visitorIndex => $visitor) {
  93. $return = $visitor->enterNode($subNode);
  94. if (null !== $return) {
  95. if ($return instanceof Node) {
  96. $this->ensureReplacementReasonable($subNode, $return);
  97. $subNode = $node->$name = $return;
  98. } elseif (NodeVisitor::DONT_TRAVERSE_CHILDREN === $return) {
  99. $traverseChildren = false;
  100. } elseif (NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN === $return) {
  101. $traverseChildren = false;
  102. break;
  103. } elseif (NodeVisitor::STOP_TRAVERSAL === $return) {
  104. $this->stopTraversal = true;
  105. break 2;
  106. } elseif (NodeVisitor::REPLACE_WITH_NULL === $return) {
  107. $node->$name = null;
  108. continue 2;
  109. } else {
  110. throw new \LogicException(
  111. 'enterNode() returned invalid value of type ' . gettype($return)
  112. );
  113. }
  114. }
  115. }
  116. if ($traverseChildren) {
  117. $this->traverseNode($subNode);
  118. if ($this->stopTraversal) {
  119. break;
  120. }
  121. }
  122. for (; $visitorIndex >= 0; --$visitorIndex) {
  123. $visitor = $this->visitors[$visitorIndex];
  124. $return = $visitor->leaveNode($subNode);
  125. if (null !== $return) {
  126. if ($return instanceof Node) {
  127. $this->ensureReplacementReasonable($subNode, $return);
  128. $subNode = $node->$name = $return;
  129. } elseif (NodeVisitor::STOP_TRAVERSAL === $return) {
  130. $this->stopTraversal = true;
  131. break 2;
  132. } elseif (NodeVisitor::REPLACE_WITH_NULL === $return) {
  133. $node->$name = null;
  134. break;
  135. } elseif (\is_array($return)) {
  136. throw new \LogicException(
  137. 'leaveNode() may only return an array ' .
  138. 'if the parent structure is an array'
  139. );
  140. } else {
  141. throw new \LogicException(
  142. 'leaveNode() returned invalid value of type ' . gettype($return)
  143. );
  144. }
  145. }
  146. }
  147. }
  148. }
  149. /**
  150. * Recursively traverse array (usually of nodes).
  151. *
  152. * @param array $nodes Array to traverse
  153. *
  154. * @return array Result of traversal (may be original array or changed one)
  155. */
  156. protected function traverseArray(array $nodes): array {
  157. $doNodes = [];
  158. foreach ($nodes as $i => $node) {
  159. if (!$node instanceof Node) {
  160. if (\is_array($node)) {
  161. throw new \LogicException('Invalid node structure: Contains nested arrays');
  162. }
  163. continue;
  164. }
  165. $traverseChildren = true;
  166. $visitorIndex = -1;
  167. foreach ($this->visitors as $visitorIndex => $visitor) {
  168. $return = $visitor->enterNode($node);
  169. if (null !== $return) {
  170. if ($return instanceof Node) {
  171. $this->ensureReplacementReasonable($node, $return);
  172. $nodes[$i] = $node = $return;
  173. } elseif (\is_array($return)) {
  174. $doNodes[] = [$i, $return];
  175. continue 2;
  176. } elseif (NodeVisitor::REMOVE_NODE === $return) {
  177. $doNodes[] = [$i, []];
  178. continue 2;
  179. } elseif (NodeVisitor::DONT_TRAVERSE_CHILDREN === $return) {
  180. $traverseChildren = false;
  181. } elseif (NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN === $return) {
  182. $traverseChildren = false;
  183. break;
  184. } elseif (NodeVisitor::STOP_TRAVERSAL === $return) {
  185. $this->stopTraversal = true;
  186. break 2;
  187. } elseif (NodeVisitor::REPLACE_WITH_NULL === $return) {
  188. throw new \LogicException(
  189. 'REPLACE_WITH_NULL can not be used if the parent structure is an array');
  190. } else {
  191. throw new \LogicException(
  192. 'enterNode() returned invalid value of type ' . gettype($return)
  193. );
  194. }
  195. }
  196. }
  197. if ($traverseChildren) {
  198. $this->traverseNode($node);
  199. if ($this->stopTraversal) {
  200. break;
  201. }
  202. }
  203. for (; $visitorIndex >= 0; --$visitorIndex) {
  204. $visitor = $this->visitors[$visitorIndex];
  205. $return = $visitor->leaveNode($node);
  206. if (null !== $return) {
  207. if ($return instanceof Node) {
  208. $this->ensureReplacementReasonable($node, $return);
  209. $nodes[$i] = $node = $return;
  210. } elseif (\is_array($return)) {
  211. $doNodes[] = [$i, $return];
  212. break;
  213. } elseif (NodeVisitor::REMOVE_NODE === $return) {
  214. $doNodes[] = [$i, []];
  215. break;
  216. } elseif (NodeVisitor::STOP_TRAVERSAL === $return) {
  217. $this->stopTraversal = true;
  218. break 2;
  219. } elseif (NodeVisitor::REPLACE_WITH_NULL === $return) {
  220. throw new \LogicException(
  221. 'REPLACE_WITH_NULL can not be used if the parent structure is an array');
  222. } else {
  223. throw new \LogicException(
  224. 'leaveNode() returned invalid value of type ' . gettype($return)
  225. );
  226. }
  227. }
  228. }
  229. }
  230. if (!empty($doNodes)) {
  231. while (list($i, $replace) = array_pop($doNodes)) {
  232. array_splice($nodes, $i, 1, $replace);
  233. }
  234. }
  235. return $nodes;
  236. }
  237. private function ensureReplacementReasonable(Node $old, Node $new): void {
  238. if ($old instanceof Node\Stmt && $new instanceof Node\Expr) {
  239. throw new \LogicException(
  240. "Trying to replace statement ({$old->getType()}) " .
  241. "with expression ({$new->getType()}). Are you missing a " .
  242. "Stmt_Expression wrapper?"
  243. );
  244. }
  245. if ($old instanceof Node\Expr && $new instanceof Node\Stmt) {
  246. throw new \LogicException(
  247. "Trying to replace expression ({$old->getType()}) " .
  248. "with statement ({$new->getType()})"
  249. );
  250. }
  251. }
  252. }