Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions lib/PhpParser/NodeTraverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down