Skip to content

Commit

Permalink
Don't take subnodes by reference when traversing
Browse files Browse the repository at this point in the history
Explicitly assign the property where necessary to avoid the
creation of unnecessary reference-wrappers everywhere.
  • Loading branch information
nikic committed May 28, 2023
1 parent 53f6717 commit 16c766e
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/PhpParser/NodeTraverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ public function traverse(array $nodes): array {
*/
protected function traverseNode(Node $node): void {
foreach ($node->getSubNodeNames() as $name) {
$subNode =& $node->$name;
$subNode = $node->$name;

if (\is_array($subNode)) {
$subNode = $this->traverseArray($subNode);
$node->$name = $this->traverseArray($subNode);
if ($this->stopTraversal) {
break;
}
Expand All @@ -105,7 +105,7 @@ protected function traverseNode(Node $node): void {
if (null !== $return) {
if ($return instanceof Node) {
$this->ensureReplacementReasonable($subNode, $return);
$subNode = $return;
$subNode = $node->$name = $return;
} elseif (NodeVisitor::DONT_TRAVERSE_CHILDREN === $return) {
$traverseChildren = false;
} elseif (NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN === $return) {
Expand All @@ -115,7 +115,7 @@ protected function traverseNode(Node $node): void {
$this->stopTraversal = true;
break 2;
} elseif (NodeVisitor::REPLACE_WITH_NULL === $return) {
$subNode = null;
$node->$name = null;
continue 2;
} else {
throw new \LogicException(
Expand All @@ -139,12 +139,12 @@ protected function traverseNode(Node $node): void {
if (null !== $return) {
if ($return instanceof Node) {
$this->ensureReplacementReasonable($subNode, $return);
$subNode = $return;
$subNode = $node->$name = $return;
} elseif (NodeVisitor::STOP_TRAVERSAL === $return) {
$this->stopTraversal = true;
break 2;
} elseif (NodeVisitor::REPLACE_WITH_NULL === $return) {
$subNode = null;
$node->$name = null;
break;
} elseif (\is_array($return)) {
throw new \LogicException(
Expand Down

0 comments on commit 16c766e

Please sign in to comment.