Skip to content

Commit

Permalink
[NodeTraverser] Clean up call ->addVisitor() after NodeTraverser crea…
Browse files Browse the repository at this point in the history
…tion (#6690)
  • Loading branch information
samsonasik authored Jan 22, 2025
1 parent 4a954af commit d5d3bcd
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 20 deletions.
4 changes: 1 addition & 3 deletions src/Comments/NodeTraverser/CommentRemovingNodeTraverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ final class CommentRemovingNodeTraverser extends NodeTraverser
{
public function __construct(CommentRemovingNodeVisitor $commentRemovingNodeVisitor)
{
parent::__construct();

$this->addVisitor($commentRemovingNodeVisitor);
parent::__construct($commentRemovingNodeVisitor);
}
}
4 changes: 1 addition & 3 deletions src/NodeTypeResolver/NodeScopeAndMetadataDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ public function __construct(
private PHPStanNodeScopeResolver $phpStanNodeScopeResolver,
private FileWithoutNamespaceNodeTraverser $fileWithoutNamespaceNodeTraverser
) {
$this->nodeTraverser = new NodeTraverser();

// needed for format preserving printing
$this->nodeTraverser->addVisitor($cloningVisitor);
$this->nodeTraverser = new NodeTraverser($cloningVisitor);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,7 @@ public function __construct(
private NodeNameResolver $nodeNameResolver,
private ClassAnalyzer $classAnalyzer
) {
$this->nodeTraverser = new NodeTraverser();

foreach ($nodeVisitors as $nodeVisitor) {
$this->nodeTraverser->addVisitor($nodeVisitor);
}
$this->nodeTraverser = new NodeTraverser(...$nodeVisitors);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ public function traverseNodesWithCallable(Node | array | null $node, callable $c
return;
}

$nodeTraverser = new NodeTraverser();

$callableNodeVisitor = new CallableNodeVisitor($callable);
$nodeTraverser->addVisitor($callableNodeVisitor);
$nodeTraverser = new NodeTraverser($callableNodeVisitor);

$nodes = $node instanceof Node ? [$node] : $node;
$nodeTraverser->traverse($nodes);
Expand Down
3 changes: 1 addition & 2 deletions src/PhpParser/Parser/SimplePhpParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public function __construct()
$parserFactory = new ParserFactory();
$this->phpParser = $parserFactory->createForNewestSupportedVersion();

$this->nodeTraverser = new NodeTraverser();
$this->nodeTraverser->addVisitor(new AssignedToNodeVisitor());
$this->nodeTraverser = new NodeTraverser(new AssignedToNodeVisitor());
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/PostRector/Application/PostFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ public function traverse(array $stmts, File $file): array
continue;
}

$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor($postRector);
$nodeTraverser = new NodeTraverser($postRector);
$stmts = $nodeTraverser->traverse($stmts);
}

Expand Down
3 changes: 1 addition & 2 deletions tests/NodeManipulator/ClassDependencyManipulatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,8 @@ public function testConstantProperties(): void
private function setNamespacedName(Class_ $class): void
{
$nameResolver = new NameResolver();
$nodeTraverser = new NodeTraverser();
$nodeTraverser = new NodeTraverser($nameResolver);

$nodeTraverser->addVisitor($nameResolver);
$nodeTraverser->traverse([$class]);
}

Expand Down

0 comments on commit d5d3bcd

Please sign in to comment.