1313use PhpParser \Node \Stmt \Interface_ ;
1414use PhpParser \Node \Stmt \Property ;
1515use PhpParser \Node \Stmt \Trait_ ;
16- use PhpParser \NodeTraverser ;
17- use PhpParser \NodeVisitor ;
18- use PhpParser \NodeVisitor \CloningVisitor ;
19- use PHPStan \Analyser \MutatingScope ;
2016use PHPStan \Type \ObjectType ;
2117use PHPStan \Type \Type ;
22- use Rector \Application \ChangedNodeScopeRefresher ;
2318use Rector \Application \Provider \CurrentFileProvider ;
2419use Rector \BetterPhpDocParser \Comment \CommentsMerger ;
25- use Rector \ChangesReporting \ValueObject \RectorWithLineChange ;
26- use Rector \Contract \Rector \HTMLAverseRectorInterface ;
2720use Rector \Contract \Rector \RectorInterface ;
2821use Rector \Exception \ShouldNotHappenException ;
29- use Rector \NodeDecorator \CreatedByRuleDecorator ;
3022use Rector \NodeNameResolver \NodeNameResolver ;
31- use Rector \NodeTypeResolver \Node \AttributeKey ;
3223use Rector \NodeTypeResolver \NodeTypeResolver ;
3324use Rector \PhpDocParser \NodeTraverser \SimpleCallableNodeTraverser ;
3425use Rector \PhpParser \Comparing \NodeComparator ;
3526use Rector \PhpParser \Node \NodeFactory ;
36- use Rector \PhpParser \NodeVisitor \PhpDocInfoRemovingNodeVisitor ;
37- use Rector \Skipper \Skipper \Skipper ;
38- use Rector \Skipper \ValueObject \SkipMatch ;
3927use Rector \ValueObject \Application \File ;
4028
4129abstract class AbstractRector implements RectorInterface
4230{
43- private const string EMPTY_NODE_ARRAY_MESSAGE = <<<CODE_SAMPLE
44- Array of nodes cannot be empty. Ensure "%s->refactor()" returns non-empty array for Nodes.
45-
46- A) Direct return null for no change:
47-
48- return null;
49-
50- B) Remove the Node:
51-
52- return \PhpParser\NodeVisitor::REMOVE_NODE;
53- CODE_SAMPLE ;
54-
5531 protected NodeNameResolver $ nodeNameResolver ;
5632
5733 protected NodeTypeResolver $ nodeTypeResolver ;
@@ -60,114 +36,30 @@ abstract class AbstractRector implements RectorInterface
6036
6137 protected NodeComparator $ nodeComparator ;
6238
63- /**
64- * @internal Use getFile() instead.
65- */
66- protected File $ file ;
67-
68- protected Skipper $ skipper ;
69-
70- private ChangedNodeScopeRefresher $ changedNodeScopeRefresher ;
71-
7239 private SimpleCallableNodeTraverser $ simpleCallableNodeTraverser ;
7340
7441 private CurrentFileProvider $ currentFileProvider ;
7542
7643 private CommentsMerger $ commentsMerger ;
7744
78- private CreatedByRuleDecorator $ createdByRuleDecorator ;
79-
8045 public function autowire (
8146 NodeNameResolver $ nodeNameResolver ,
8247 NodeTypeResolver $ nodeTypeResolver ,
8348 SimpleCallableNodeTraverser $ simpleCallableNodeTraverser ,
8449 NodeFactory $ nodeFactory ,
85- Skipper $ skipper ,
8650 NodeComparator $ nodeComparator ,
8751 CurrentFileProvider $ currentFileProvider ,
88- CreatedByRuleDecorator $ createdByRuleDecorator ,
89- ChangedNodeScopeRefresher $ changedNodeScopeRefresher ,
9052 CommentsMerger $ commentsMerger
9153 ): void {
9254 $ this ->nodeNameResolver = $ nodeNameResolver ;
9355 $ this ->nodeTypeResolver = $ nodeTypeResolver ;
9456 $ this ->simpleCallableNodeTraverser = $ simpleCallableNodeTraverser ;
9557 $ this ->nodeFactory = $ nodeFactory ;
96- $ this ->skipper = $ skipper ;
9758 $ this ->nodeComparator = $ nodeComparator ;
9859 $ this ->currentFileProvider = $ currentFileProvider ;
99- $ this ->createdByRuleDecorator = $ createdByRuleDecorator ;
100- $ this ->changedNodeScopeRefresher = $ changedNodeScopeRefresher ;
10160 $ this ->commentsMerger = $ commentsMerger ;
10261 }
10362
104- /**
105- * @return NodeVisitor::REMOVE_NODE|Node|null|Node[]
106- */
107- final public function enterNode (Node $ node ): int |Node |null |array
108- {
109- // keep $this->file populated for BC; refactor() is only ever reached through here
110- $ this ->file = $ this ->getFile ();
111-
112- if (is_a ($ this , HTMLAverseRectorInterface::class, true ) && $ this ->file ->containsHTML ()) {
113- return null ;
114- }
115-
116- $ filePath = $ this ->file ->getFilePath ();
117-
118- // node already changed by this rule in a previous pass → hard skip
119- if ($ this ->skipper ->shouldSkipCurrentNode (static ::class, $ node )) {
120- return null ;
121- }
122-
123- // class/path skip is configured for this rule and file: run the rule on a deep clone to learn
124- // whether it would actually have changed anything. Only a skip that prevents a real change
125- // counts as used; the original node is left untouched, so the file stays skipped either way.
126- $ skipMatch = $ this ->skipper ->matchSkip ($ this , $ filePath );
127- if ($ skipMatch instanceof SkipMatch) {
128- if ($ this ->refactor ($ this ->cloneNode ($ node )) !== null ) {
129- $ this ->skipper ->markSkipUsed ($ skipMatch );
130- }
131-
132- return null ;
133- }
134-
135- // ensure origNode pulled before refactor to avoid changed during refactor, ref https://3v4l.org/YMEGN
136- $ originalNode = $ node ->getAttribute (AttributeKey::ORIGINAL_NODE ) ?? $ node ;
137-
138- $ refactoredNodeOrState = $ this ->refactor ($ node );
139-
140- // nothing to change → continue
141- if ($ refactoredNodeOrState === null ) {
142- return null ;
143- }
144-
145- if ($ refactoredNodeOrState === []) {
146- $ errorMessage = sprintf (self ::EMPTY_NODE_ARRAY_MESSAGE , static ::class);
147- throw new ShouldNotHappenException ($ errorMessage );
148- }
149-
150- $ isState = is_int ($ refactoredNodeOrState );
151-
152- if ($ isState ) {
153- $ this ->createdByRuleDecorator ->decorate ($ node , $ originalNode , static ::class);
154-
155- // only remove node is supported
156- if ($ refactoredNodeOrState !== NodeVisitor::REMOVE_NODE ) {
157- // @todo warn about unsupported state in the future
158- return null ;
159- }
160-
161- // notify this rule changed code
162- $ rectorWithLineChange = new RectorWithLineChange (static ::class, $ originalNode ->getStartLine ());
163- $ this ->file ->addRectorClassWithLine ($ rectorWithLineChange );
164-
165- return $ refactoredNodeOrState ;
166- }
167-
168- return $ this ->postRefactorProcess ($ originalNode , $ node , $ refactoredNodeOrState , $ filePath );
169- }
170-
17163 protected function getFile (): File
17264 {
17365 $ file = $ this ->currentFileProvider ->getFile ();
@@ -247,48 +139,4 @@ protected function mirrorComments(Node $newNode, Node $oldNode): void
247139 {
248140 $ this ->commentsMerger ->mirrorComments ($ newNode , $ oldNode );
249141 }
250-
251- /**
252- * Deep clone, so a skipped rule can be probed on the clone without mutating the real node.
253- */
254- private function cloneNode (Node $ node ): Node
255- {
256- $ nodeTraverser = new NodeTraverser (new CloningVisitor (), new PhpDocInfoRemovingNodeVisitor ());
257- return $ nodeTraverser ->traverse ([$ node ])[0 ];
258- }
259-
260- /**
261- * @param Node|Node[] $refactoredNode
262- * @return Node|Node[]
263- */
264- private function postRefactorProcess (
265- Node $ originalNode ,
266- Node $ node ,
267- Node |array $ refactoredNode ,
268- string $ filePath
269- ): Node |array {
270- /** @var non-empty-array<Node>|Node $refactoredNode */
271- $ this ->createdByRuleDecorator ->decorate ($ refactoredNode , $ originalNode , static ::class);
272-
273- $ rectorWithLineChange = new RectorWithLineChange (static ::class, $ originalNode ->getStartLine ());
274- $ this ->file ->addRectorClassWithLine ($ rectorWithLineChange );
275-
276- /** @var MutatingScope|null $currentScope */
277- $ currentScope = $ node ->getAttribute (AttributeKey::SCOPE );
278- $ this ->refreshScopeNodes ($ refactoredNode , $ filePath , $ currentScope );
279-
280- return $ refactoredNode ;
281- }
282-
283- /**
284- * @param Node[]|Node $node
285- */
286- private function refreshScopeNodes (array |Node $ node , string $ filePath , ?MutatingScope $ mutatingScope ): void
287- {
288- $ nodes = $ node instanceof Node ? [$ node ] : $ node ;
289-
290- foreach ($ nodes as $ node ) {
291- $ this ->changedNodeScopeRefresher ->refresh ($ node , $ filePath , $ mutatingScope );
292- }
293- }
294142}
0 commit comments