diff --git a/lib/PhpParser/NodeTraverser.php b/lib/PhpParser/NodeTraverser.php index bb3d6ddbfa..c59050845d 100644 --- a/lib/PhpParser/NodeTraverser.php +++ b/lib/PhpParser/NodeTraverser.php @@ -110,7 +110,8 @@ protected function traverseNode(Node $node): void { $traverseChildren = true; $visitorIndex = -1; - foreach ($this->visitors as $visitorIndex => $visitor) { + $visitors = $this->getVisitorsForNode($subNode); + foreach ($visitors as $visitorIndex => $visitor) { $return = $visitor->enterNode($subNode); if (null !== $return) { if ($return instanceof Node) { @@ -143,7 +144,7 @@ protected function traverseNode(Node $node): void { } for (; $visitorIndex >= 0; --$visitorIndex) { - $visitor = $this->visitors[$visitorIndex]; + $visitor = $visitors[$visitorIndex]; $return = $visitor->leaveNode($subNode); if (null !== $return) { @@ -192,7 +193,8 @@ protected function traverseArray(array $nodes): array { $traverseChildren = true; $visitorIndex = -1; - foreach ($this->visitors as $visitorIndex => $visitor) { + $visitors = $this->getVisitorsForNode($node); + foreach ($visitors as $visitorIndex => $visitor) { $return = $visitor->enterNode($node); if (null !== $return) { if ($return instanceof Node) { @@ -231,7 +233,7 @@ protected function traverseArray(array $nodes): array { } for (; $visitorIndex >= 0; --$visitorIndex) { - $visitor = $this->visitors[$visitorIndex]; + $visitor = $visitors[$visitorIndex]; $return = $visitor->leaveNode($node); if (null !== $return) { @@ -268,6 +270,15 @@ protected function traverseArray(array $nodes): array { return $nodes; } + /** + * This method give ability to customize which visitors are applied to a given node in child class. + * + * @return NodeVisitor[] + */ + protected function getVisitorsForNode(Node $node): array { + return $this->visitors; + } + private function ensureReplacementReasonable(Node $old, Node $new): void { if ($old instanceof Node\Stmt && $new instanceof Node\Expr) { throw new \LogicException(