1616use Rector \PhpParser \Node \CustomNode \FileWithoutNamespace ;
1717use Rector \PhpParser \Node \FileNode ;
1818use Rector \VersionBonding \PhpVersionedFilter ;
19+ use Webmozart \Assert \Assert ;
1920
2021/**
2122 * Based on native NodeTraverser class, but heavily customized for Rector needs.
@@ -54,9 +55,13 @@ public function __construct(
5455 /**
5556 * @param RectorInterface[] $rectors
5657 * @api used in tests to update the active rules
58+ *
59+ * @internal Used only in Rector core, not supported outside. Might change any time.
5760 */
5861 public function refreshPhpRectors (array $ rectors ): void
5962 {
63+ Assert::allIsInstanceOf ($ rectors , RectorInterface::class);
64+
6065 $ this ->rectors = $ rectors ;
6166 $ this ->visitors = [];
6267 $ this ->visitorsPerNodeClass = [];
@@ -72,11 +77,14 @@ public function getVisitorsForNode(Node $node): array
7277 {
7378 $ nodeClass = $ node ::class;
7479
80+ if (! $ this ->areNodeVisitorsPrepared ) {
81+ $ this ->prepareNodeVisitors ();
82+ }
83+
7584 if (! isset ($ this ->visitorsPerNodeClass [$ nodeClass ])) {
7685 $ this ->visitorsPerNodeClass [$ nodeClass ] = [];
7786
78- /** @var RectorInterface $visitor */
79- foreach ($ this ->visitors as $ visitor ) {
87+ foreach ($ this ->rectors as $ visitor ) {
8088 foreach ($ visitor ->getNodeTypes () as $ nodeType ) {
8189 // BC layer matching
8290 if ($ nodeType === FileWithoutNamespace::class && $ nodeClass === FileNode::class) {
@@ -95,12 +103,9 @@ public function getVisitorsForNode(Node $node): array
95103 return $ this ->visitorsPerNodeClass [$ nodeClass ];
96104 }
97105
98- /**
99- * @deprecated @todo remove completely, even in tests to avoid mutable state
100- */
101106 public function addVisitor (NodeVisitor $ visitor ): void
102107 {
103- $ this -> visitors [] = $ visitor ;
108+ throw new ShouldNotHappenException ( ' The immutable node traverser does not support adding visitors. ' ) ;
104109 }
105110
106111 public function removeVisitor (NodeVisitor $ visitor ): void
0 commit comments