Skip to content

Commit

Permalink
verify node has parent for ParentConnectingVisitor
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Oct 21, 2022
1 parent 8d4801b commit ba39cbe
Showing 1 changed file with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\ParentConnectingVisitor;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpDocParser\NodeVisitor\CallableNodeVisitor;

/**
Expand Down Expand Up @@ -35,7 +36,27 @@ public function traverseNodesWithCallable(Node | array | null $nodes, callable $
$nodeTraverser = new NodeTraverser();
$callableNodeVisitor = new CallableNodeVisitor($callable);
$nodeTraverser->addVisitor($callableNodeVisitor);
$nodeTraverser->addVisitor(new ParentConnectingVisitor());

if ($this->shouldConnectParent($nodes)) {
$nodeTraverser->addVisitor(new ParentConnectingVisitor());
}

$nodeTraverser->traverse($nodes);
}

/**
* @param Node[] $nodes
*/
private function shouldConnectParent(array $nodes): bool
{
foreach ($nodes as $node) {
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);

if ($parentNode instanceof Node) {
return true;
}
}

return false;
}
}

0 comments on commit ba39cbe

Please sign in to comment.