66
77use PhpParser \Node ;
88use PhpParser \Node \FunctionLike ;
9- use PhpParser \Node \Stmt ;
9+ use PhpParser \Node \Stmt \ Class_ ;
1010use PhpParser \Node \Stmt \Return_ ;
11- use PhpParser \Node \ Stmt \ Switch_ ;
11+ use PhpParser \NodeVisitor ;
1212use PhpParser \NodeVisitorAbstract ;
13- use Rector \Contract \PhpParser \Node \StmtsAwareInterface ;
1413use Rector \NodeTypeResolver \Node \AttributeKey ;
1514use Rector \NodeTypeResolver \PHPStan \Scope \Contract \NodeVisitor \ScopeResolverNodeVisitorInterface ;
15+ use Rector \PhpDocParser \NodeTraverser \SimpleCallableNodeTraverser ;
1616
1717final class ByRefReturnNodeVisitor extends NodeVisitorAbstract implements ScopeResolverNodeVisitorInterface
1818{
19+ public function __construct (
20+ private readonly SimpleCallableNodeTraverser $ simpleCallableNodeTraverser
21+ ) {
22+ }
23+
1924 public function enterNode (Node $ node ): ?Node
2025 {
2126 if (! $ node instanceof FunctionLike) {
@@ -31,37 +36,21 @@ public function enterNode(Node $node): ?Node
3136 return null ;
3237 }
3338
34- $ this ->setByRefAttribute ($ stmts );
35-
36- return null ;
37- }
38-
39- /**
40- * @param Stmt[] $stmts
41- */
42- private function setByRefAttribute (array $ stmts ): void
43- {
44- foreach ($ stmts as $ stmt ) {
45- if ($ stmt instanceof FunctionLike) {
46- continue ;
47- }
48-
49- if ($ stmt instanceof StmtsAwareInterface && $ stmt ->stmts !== null ) {
50- $ this ->setByRefAttribute ($ stmt ->stmts );
51- continue ;
52- }
39+ $ this ->simpleCallableNodeTraverser ->traverseNodesWithCallable (
40+ $ stmts ,
41+ static function (Node $ node ): int |null |Node {
42+ if ($ node instanceof Class_ || $ node instanceof FunctionLike) {
43+ return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN ;
44+ }
5345
54- if ($ stmt instanceof Switch_) {
55- foreach ($ stmt ->cases as $ case ) {
56- $ this ->setByRefAttribute ($ case ->stmts );
46+ if (! $ node instanceof Return_) {
47+ return null ;
5748 }
5849
59- continue ;
60- }
50+ $ node ->setAttribute (AttributeKey::IS_BYREF_RETURN , true );
51+ return $ node ;
52+ });
6153
62- if ($ stmt instanceof Return_) {
63- $ stmt ->setAttribute (AttributeKey::IS_BYREF_RETURN , true );
64- }
65- }
54+ return null ;
6655 }
6756}
0 commit comments