From d5d3bcd946dc0565312a46e5941c033615373ce0 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 22 Jan 2025 10:31:14 +0700 Subject: [PATCH] [NodeTraverser] Clean up call ->addVisitor() after NodeTraverser creation (#6690) --- src/Comments/NodeTraverser/CommentRemovingNodeTraverser.php | 4 +--- src/NodeTypeResolver/NodeScopeAndMetadataDecorator.php | 4 +--- .../PHPStan/Scope/PHPStanNodeScopeResolver.php | 6 +----- .../NodeTraverser/SimpleCallableNodeTraverser.php | 4 +--- src/PhpParser/Parser/SimplePhpParser.php | 3 +-- src/PostRector/Application/PostFileProcessor.php | 3 +-- tests/NodeManipulator/ClassDependencyManipulatorTest.php | 3 +-- 7 files changed, 7 insertions(+), 20 deletions(-) diff --git a/src/Comments/NodeTraverser/CommentRemovingNodeTraverser.php b/src/Comments/NodeTraverser/CommentRemovingNodeTraverser.php index f1f4a76c7d2..5689b9affce 100644 --- a/src/Comments/NodeTraverser/CommentRemovingNodeTraverser.php +++ b/src/Comments/NodeTraverser/CommentRemovingNodeTraverser.php @@ -11,8 +11,6 @@ final class CommentRemovingNodeTraverser extends NodeTraverser { public function __construct(CommentRemovingNodeVisitor $commentRemovingNodeVisitor) { - parent::__construct(); - - $this->addVisitor($commentRemovingNodeVisitor); + parent::__construct($commentRemovingNodeVisitor); } } diff --git a/src/NodeTypeResolver/NodeScopeAndMetadataDecorator.php b/src/NodeTypeResolver/NodeScopeAndMetadataDecorator.php index 641a062ea0f..77dc42edea5 100644 --- a/src/NodeTypeResolver/NodeScopeAndMetadataDecorator.php +++ b/src/NodeTypeResolver/NodeScopeAndMetadataDecorator.php @@ -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); } /** diff --git a/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php b/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php index 05f1adbdb68..b37f912997c 100644 --- a/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php +++ b/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php @@ -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); } /** diff --git a/src/PhpDocParser/NodeTraverser/SimpleCallableNodeTraverser.php b/src/PhpDocParser/NodeTraverser/SimpleCallableNodeTraverser.php index c9779254fed..667659bb9df 100644 --- a/src/PhpDocParser/NodeTraverser/SimpleCallableNodeTraverser.php +++ b/src/PhpDocParser/NodeTraverser/SimpleCallableNodeTraverser.php @@ -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); diff --git a/src/PhpParser/Parser/SimplePhpParser.php b/src/PhpParser/Parser/SimplePhpParser.php index 8b51c09a77c..6520ea2baed 100644 --- a/src/PhpParser/Parser/SimplePhpParser.php +++ b/src/PhpParser/Parser/SimplePhpParser.php @@ -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()); } /** diff --git a/src/PostRector/Application/PostFileProcessor.php b/src/PostRector/Application/PostFileProcessor.php index 2232e2ee25f..945c8292f14 100644 --- a/src/PostRector/Application/PostFileProcessor.php +++ b/src/PostRector/Application/PostFileProcessor.php @@ -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); } diff --git a/tests/NodeManipulator/ClassDependencyManipulatorTest.php b/tests/NodeManipulator/ClassDependencyManipulatorTest.php index beec1f39651..6a339fb78b6 100644 --- a/tests/NodeManipulator/ClassDependencyManipulatorTest.php +++ b/tests/NodeManipulator/ClassDependencyManipulatorTest.php @@ -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]); }