Skip to content
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ class SkipAnonymousClass
$nodes = $nodes ? [$nodes] : [];
}

$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor($this->createNodeVisitor($callable));
$nodeTraverser = new NodeTraverser(...$this->createNodeVisitor($callable));
$nodeTraverser->traverse($nodes);
}

Expand Down
56 changes: 6 additions & 50 deletions src/PhpParser/NodeTraverser/AbstractImmutableNodeTraverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,64 +14,25 @@
abstract class AbstractImmutableNodeTraverser implements NodeTraverserInterface
{
/**
* @deprecated Use NodeVisitor::DONT_TRAVERSE_CHILDREN instead.
*/
public const DONT_TRAVERSE_CHILDREN = NodeVisitor::DONT_TRAVERSE_CHILDREN;

/**
* @deprecated Use NodeVisitor::STOP_TRAVERSAL instead.
*/
public const STOP_TRAVERSAL = NodeVisitor::STOP_TRAVERSAL;

/**
* @deprecated Use NodeVisitor::REMOVE_NODE instead.
*/
public const REMOVE_NODE = NodeVisitor::REMOVE_NODE;

/**
* @deprecated Use NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN instead.
*/
public const DONT_TRAVERSE_CURRENT_AND_CHILDREN = NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;

/**
* @var list<NodeVisitor> Visitors
* @var list<NodeVisitor>
*/
protected array $visitors = [];

/**
* @var bool Whether traversal should be stopped
*/
protected bool $stopTraversal;
private bool $stopTraversal;

/**
* Create a traverser with the given visitors.
*
* @param NodeVisitor ...$visitors Node visitors
*/
public function __construct(NodeVisitor ...$visitors)
{
$this->visitors = $visitors;
}

/**
* Adds a visitor.
*
* @param NodeVisitor $visitor Visitor to add
*/
public function addVisitor(NodeVisitor $visitor): void
{
$this->visitors[] = $visitor;
}

/**
* Removes an added visitor.
*/
public function removeVisitor(NodeVisitor $visitor): void
{
$index = array_search($visitor, $this->visitors, true);
if ($index !== false) {
array_splice($this->visitors, $index, 1, []);
}
throw new LogicException('Removing visitors is not supported in ' . static::class);
}

/**
Expand Down Expand Up @@ -106,12 +67,7 @@ public function traverse(array $nodes): array
*/
abstract public function getVisitorsForNode(Node $node): array;

/**
* Recursively traverse a node.
*
* @param Node $node Node to traverse.
*/
protected function traverseNode(Node $node): void
private function traverseNode(Node $node): void
{
foreach ($node->getSubNodeNames() as $name) {
$subNode = $node->{$name};
Expand Down Expand Up @@ -193,9 +149,9 @@ protected function traverseNode(Node $node): void
*
* @param Node[] $nodes Array to traverse
*
* @return array Result of traversal (may be original array or changed one)
* @return Node[] Result of traversal (may be original array or changed one)
*/
protected function traverseArray(array $nodes): array
private function traverseArray(array $nodes): array
{
$doNodes = [];
foreach ($nodes as $i => $node) {
Expand Down
Loading