1616use PhpParser \Node \Stmt \Expression ;
1717use PhpParser \Node \Stmt \Foreach_ ;
1818use PhpParser \Node \Stmt \If_ ;
19+ use PhpParser \Node \Stmt \Return_ ;
1920use Rector \Naming \Naming \VariableNaming ;
2021use Rector \PHPStan \ScopeFetcher ;
2122use Rector \Rector \AbstractRector ;
@@ -36,7 +37,7 @@ public function __construct(
3637
3738 public function getNodeTypes (): array
3839 {
39- return [Expression::class];
40+ return [Expression::class, Return_::class ];
4041 }
4142
4243 public function getRuleDefinition (): RuleDefinition
@@ -64,28 +65,36 @@ public function getRuleDefinition(): RuleDefinition
6465 }
6566
6667 /**
67- * @param Expression $node
68+ * @param Expression|Return_ $node
6869 * @return Stmt[]|null
6970 */
7071 public function refactor (Node $ node ): ?array
7172 {
72- if (! $ node ->expr instanceof Assign ) {
73+ if ($ node instanceof Return_ && ! $ node ->expr instanceof FuncCall ) {
7374 return null ;
7475 }
7576
76- if (! $ node ->expr -> expr instanceof FuncCall ) {
77+ if ($ node instanceof Expression && ! $ node ->expr instanceof Assign ) {
7778 return null ;
7879 }
7980
80- if (! $ this ->isName ($ node ->expr ->expr , 'array_find_key ' )) {
81+ $ expr = $ node instanceof Expression && $ node ->expr instanceof Assign
82+ ? $ node ->expr ->expr
83+ : $ node ->expr ;
84+
85+ if (! $ expr instanceof FuncCall) {
86+ return null ;
87+ }
88+
89+ if (! $ this ->isName ($ expr , 'array_find_key ' )) {
8190 return null ;
8291 }
8392
84- if ($ node -> expr -> expr ->isFirstClassCallable ()) {
93+ if ($ expr ->isFirstClassCallable ()) {
8594 return null ;
8695 }
8796
88- $ args = $ node -> expr -> expr ->getArgs ();
97+ $ args = $ expr ->getArgs ();
8998 if (count ($ args ) !== 2 ) {
9099 return null ;
91100 }
@@ -94,19 +103,24 @@ public function refactor(Node $node): ?array
94103 return null ;
95104 }
96105
106+ $ scope = ScopeFetcher::fetch ($ node );
107+ $ variable = $ node instanceof Expression && $ node ->expr instanceof Assign
108+ ? $ node ->expr ->var
109+ : new Variable ($ this ->variableNaming ->createCountedValueName ('found ' , $ scope ));
110+
97111 $ keyVar = $ args [1 ]->value ->params [1 ]->var ?? new Variable ($ this ->variableNaming ->createCountedValueName (
98112 'idx ' ,
99- ScopeFetcher:: fetch ( $ node )
113+ $ scope
100114 ));
101115
102116 $ valueCond = $ args [1 ]->value ->expr ;
103117 $ if = new If_ ($ valueCond , [
104- 'stmts ' => [new Expression (new Assign ($ node -> expr -> var , $ keyVar )), new Break_ ()],
118+ 'stmts ' => [new Expression (new Assign ($ variable , $ keyVar )), new Break_ ()],
105119 ]);
106120
107- return [
121+ $ result = [
108122 // init
109- new Expression (new Assign ($ node -> expr -> var , new ConstFetch (new Name ('null ' )))),
123+ new Expression (new Assign ($ variable , new ConstFetch (new Name ('null ' )))),
110124
111125 // foreach loop
112126 new Foreach_ (
@@ -118,5 +132,11 @@ public function refactor(Node $node): ?array
118132 ]
119133 ),
120134 ];
135+
136+ if ($ node instanceof Return_) {
137+ $ result [] = new Return_ ($ variable );
138+ }
139+
140+ return $ result ;
121141 }
122142}
0 commit comments