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
60 changes: 4 additions & 56 deletions src/PhpParser/NodeTraverser/AbstractImmutableNodeTraverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,10 @@ protected function traverseNode(Node $node): void
}

$traverseChildren = true;
$visitorIndex = -1;
$currentNodeVisitors = $this->getVisitorsForNode($subNode);

foreach ($currentNodeVisitors as $visitorIndex => $visitor) {
$return = $visitor->enterNode($subNode);
foreach ($currentNodeVisitors as $currentNodeVisitor) {
$return = $currentNodeVisitor->enterNode($subNode);
if ($return !== null) {
if ($return instanceof Node) {
$originalSubNodeClass = $subNode::class;
Expand Down Expand Up @@ -140,30 +139,6 @@ protected function traverseNode(Node $node): void
break;
}
}

for (; $visitorIndex >= 0; --$visitorIndex) {
$visitor = $currentNodeVisitors[$visitorIndex];
$return = $visitor->leaveNode($subNode);
if ($return !== null) {
if ($return instanceof Node) {
$this->ensureReplacementReasonable($subNode, $return);
$subNode = $return;
$node->{$name} = $return;
} elseif ($return === NodeVisitor::STOP_TRAVERSAL) {
$this->stopTraversal = true;
break 2;
} elseif ($return === NodeVisitor::REPLACE_WITH_NULL) {
$node->{$name} = null;
break;
} elseif (\is_array($return)) {
throw new LogicException(
'leaveNode() may only return an array if the parent structure is an array'
);
} else {
throw new LogicException('leaveNode() returned invalid value of type ' . gettype($return));
}
}
}
}
}

Expand All @@ -184,11 +159,10 @@ protected function traverseArray(array $nodes): array
}

$traverseChildren = true;
$visitorIndex = -1;
$currentNodeVisitors = $this->getVisitorsForNode($node);

foreach ($currentNodeVisitors as $visitorIndex => $visitor) {
$return = $visitor->enterNode($node);
foreach ($currentNodeVisitors as $currentNodeVisitor) {
$return = $currentNodeVisitor->enterNode($node);
if ($return !== null) {
if ($return instanceof Node) {
$originalNodeNodeClass = $node::class;
Expand Down Expand Up @@ -229,32 +203,6 @@ protected function traverseArray(array $nodes): array
break;
}
}

for (; $visitorIndex >= 0; --$visitorIndex) {
$visitor = $currentNodeVisitors[$visitorIndex];
$return = $visitor->leaveNode($node);
if ($return !== null) {
if ($return instanceof Node) {
$this->ensureReplacementReasonable($node, $return);
$nodes[$i] = $node = $return;
} elseif (\is_array($return)) {
$doNodes[] = [$i, $return];
break;
} elseif ($return === NodeVisitor::REMOVE_NODE) {
$doNodes[] = [$i, []];
break;
} elseif ($return === NodeVisitor::STOP_TRAVERSAL) {
$this->stopTraversal = true;
break 2;
} elseif ($return === NodeVisitor::REPLACE_WITH_NULL) {
throw new LogicException(
'REPLACE_WITH_NULL can not be used if the parent structure is an array'
);
} else {
throw new LogicException('leaveNode() returned invalid value of type ' . gettype($return));
}
}
}
}

if ($doNodes !== []) {
Expand Down