NodeTraverser.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace PhpParser;
  3. class NodeTraverser implements NodeTraverserInterface
  4. {
  5. /**
  6. * If NodeVisitor::enterNode() returns DONT_TRAVERSE_CHILDREN, child nodes
  7. * of the current node will not be traversed for any visitors.
  8. *
  9. * For subsequent visitors enterNode() will still be called on the current
  10. * node and leaveNode() will also be invoked for the current node.
  11. */
  12. const DONT_TRAVERSE_CHILDREN = 1;
  13. /**
  14. * If NodeVisitor::enterNode() or NodeVisitor::leaveNode() returns
  15. * STOP_TRAVERSAL, traversal is aborted.
  16. *
  17. * The afterTraverse() method will still be invoked.
  18. */
  19. const STOP_TRAVERSAL = 2;
  20. /**
  21. * If NodeVisitor::leaveNode() returns REMOVE_NODE for a node that occurs
  22. * in an array, it will be removed from the array.
  23. *
  24. * For subsequent visitors leaveNode() will still be invoked for the
  25. * removed node.
  26. */
  27. const REMOVE_NODE = false;
  28. /** @var NodeVisitor[] Visitors */
  29. protected $visitors;
  30. /** @var bool Whether traversal should be stopped */
  31. protected $stopTraversal;
  32. /**
  33. * Constructs a node traverser.
  34. */
  35. public function __construct() {
  36. $this->visitors = array();
  37. }
  38. /**
  39. * Adds a visitor.
  40. *
  41. * @param NodeVisitor $visitor Visitor to add
  42. */
  43. public function addVisitor(NodeVisitor $visitor) {
  44. $this->visitors[] = $visitor;
  45. }
  46. /**
  47. * Removes an added visitor.
  48. *
  49. * @param NodeVisitor $visitor
  50. */
  51. public function removeVisitor(NodeVisitor $visitor) {
  52. foreach ($this->visitors as $index => $storedVisitor) {
  53. if ($storedVisitor === $visitor) {
  54. unset($this->visitors[$index]);
  55. break;
  56. }
  57. }
  58. }
  59. /**
  60. * Traverses an array of nodes using the registered visitors.
  61. *
  62. * @param Node[] $nodes Array of nodes
  63. *
  64. * @return Node[] Traversed array of nodes
  65. */
  66. public function traverse(array $nodes) {
  67. $this->stopTraversal = false;
  68. foreach ($this->visitors as $visitor) {
  69. if (null !== $return = $visitor->beforeTraverse($nodes)) {
  70. $nodes = $return;
  71. }
  72. }
  73. $nodes = $this->traverseArray($nodes);
  74. foreach ($this->visitors as $visitor) {
  75. if (null !== $return = $visitor->afterTraverse($nodes)) {
  76. $nodes = $return;
  77. }
  78. }
  79. return $nodes;
  80. }
  81. protected function traverseNode(Node $node) {
  82. foreach ($node->getSubNodeNames() as $name) {
  83. $subNode =& $node->$name;
  84. if (is_array($subNode)) {
  85. $subNode = $this->traverseArray($subNode);
  86. if ($this->stopTraversal) {
  87. break;
  88. }
  89. } elseif ($subNode instanceof Node) {
  90. $traverseChildren = true;
  91. foreach ($this->visitors as $visitor) {
  92. $return = $visitor->enterNode($subNode);
  93. if (self::DONT_TRAVERSE_CHILDREN === $return) {
  94. $traverseChildren = false;
  95. } else if (self::STOP_TRAVERSAL === $return) {
  96. $this->stopTraversal = true;
  97. break 2;
  98. } else if (null !== $return) {
  99. $subNode = $return;
  100. }
  101. }
  102. if ($traverseChildren) {
  103. $subNode = $this->traverseNode($subNode);
  104. if ($this->stopTraversal) {
  105. break;
  106. }
  107. }
  108. foreach ($this->visitors as $visitor) {
  109. $return = $visitor->leaveNode($subNode);
  110. if (self::STOP_TRAVERSAL === $return) {
  111. $this->stopTraversal = true;
  112. break 2;
  113. } else if (null !== $return) {
  114. if (is_array($return)) {
  115. throw new \LogicException(
  116. 'leaveNode() may only return an array ' .
  117. 'if the parent structure is an array'
  118. );
  119. }
  120. $subNode = $return;
  121. }
  122. }
  123. }
  124. }
  125. return $node;
  126. }
  127. protected function traverseArray(array $nodes) {
  128. $doNodes = array();
  129. foreach ($nodes as $i => &$node) {
  130. if (is_array($node)) {
  131. $node = $this->traverseArray($node);
  132. if ($this->stopTraversal) {
  133. break;
  134. }
  135. } elseif ($node instanceof Node) {
  136. $traverseChildren = true;
  137. foreach ($this->visitors as $visitor) {
  138. $return = $visitor->enterNode($node);
  139. if (self::DONT_TRAVERSE_CHILDREN === $return) {
  140. $traverseChildren = false;
  141. } else if (self::STOP_TRAVERSAL === $return) {
  142. $this->stopTraversal = true;
  143. break 2;
  144. } else if (null !== $return) {
  145. $node = $return;
  146. }
  147. }
  148. if ($traverseChildren) {
  149. $node = $this->traverseNode($node);
  150. if ($this->stopTraversal) {
  151. break;
  152. }
  153. }
  154. foreach ($this->visitors as $visitor) {
  155. $return = $visitor->leaveNode($node);
  156. if (self::REMOVE_NODE === $return) {
  157. $doNodes[] = array($i, array());
  158. break;
  159. } else if (self::STOP_TRAVERSAL === $return) {
  160. $this->stopTraversal = true;
  161. break 2;
  162. } elseif (is_array($return)) {
  163. $doNodes[] = array($i, $return);
  164. break;
  165. } elseif (null !== $return) {
  166. $node = $return;
  167. }
  168. }
  169. }
  170. }
  171. if (!empty($doNodes)) {
  172. while (list($i, $replace) = array_pop($doNodes)) {
  173. array_splice($nodes, $i, 1, $replace);
  174. }
  175. }
  176. return $nodes;
  177. }
  178. }