99use PhpParser \Node \Expr \Assign ;
1010use PhpParser \Node \Expr \ConstFetch ;
1111use PhpParser \Node \Expr \FuncCall ;
12+ use PhpParser \Node \Expr \Variable ;
1213use PhpParser \Node \Name ;
1314use PhpParser \Node \Stmt ;
1415use PhpParser \Node \Stmt \Break_ ;
1516use PhpParser \Node \Stmt \Expression ;
1617use PhpParser \Node \Stmt \Foreach_ ;
1718use PhpParser \Node \Stmt \If_ ;
19+ use PhpParser \Node \Stmt \Return_ ;
20+ use Rector \Naming \Naming \VariableNaming ;
21+ use Rector \PHPStan \ScopeFetcher ;
1822use Rector \Rector \AbstractRector ;
1923use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
2024use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
2630 */
2731final class DowngradeArrayFindRector extends AbstractRector
2832{
33+ public function __construct (
34+ private readonly VariableNaming $ variableNaming
35+ ) {
36+ }
37+
2938 public function getNodeTypes (): array
3039 {
31- return [Expression::class];
40+ return [Expression::class, Return_::class ];
3241 }
3342
3443 public function getRuleDefinition (): RuleDefinition
@@ -56,28 +65,36 @@ public function getRuleDefinition(): RuleDefinition
5665 }
5766
5867 /**
59- * @param Expression $node
68+ * @param Expression|Return_ $node
6069 * @return Stmt[]|null
6170 */
6271 public function refactor (Node $ node ): ?array
6372 {
64- if (! $ node ->expr instanceof Assign) {
73+ if ($ node instanceof Return_ && ! $ node ->expr instanceof FuncCall) {
74+ return null ;
75+ }
76+
77+ if ($ node instanceof Expression && ! $ node ->expr instanceof Assign) {
6578 return null ;
6679 }
6780
68- if (! $ node ->expr ->expr instanceof FuncCall) {
81+ $ expr = $ node instanceof Expression && $ node ->expr instanceof Assign
82+ ? $ node ->expr ->expr
83+ : $ node ->expr ;
84+
85+ if (! $ expr instanceof FuncCall) {
6986 return null ;
7087 }
7188
72- if (! $ this ->isName ($ node -> expr -> expr , 'array_find ' )) {
89+ if (! $ this ->isName ($ expr , 'array_find ' )) {
7390 return null ;
7491 }
7592
76- if ($ node -> expr -> expr ->isFirstClassCallable ()) {
93+ if ($ expr ->isFirstClassCallable ()) {
7794 return null ;
7895 }
7996
80- $ args = $ node -> expr -> expr ->getArgs ();
97+ $ args = $ expr ->getArgs ();
8198 if (count ($ args ) !== 2 ) {
8299 return null ;
83100 }
@@ -86,17 +103,19 @@ public function refactor(Node $node): ?array
86103 return null ;
87104 }
88105
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+
89111 $ valueCond = $ args [1 ]->value ->expr ;
90112 $ if = new If_ ($ valueCond , [
91- 'stmts ' => [
92- new Expression (new Assign ($ node ->expr ->var , $ args [1 ]->value ->params [0 ]->var )),
93- new Break_ (),
94- ],
113+ 'stmts ' => [new Expression (new Assign ($ variable , $ args [1 ]->value ->params [0 ]->var )), new Break_ ()],
95114 ]);
96115
97- return [
116+ $ result = [
98117 // init
99- new Expression (new Assign ($ node -> expr -> var , new ConstFetch (new Name ('null ' )))),
118+ new Expression (new Assign ($ variable , new ConstFetch (new Name ('null ' )))),
100119
101120 // foreach loop
102121 new Foreach_ (
@@ -112,5 +131,11 @@ public function refactor(Node $node): ?array
112131 ],
113132 ),
114133 ];
134+
135+ if ($ node instanceof Return_) {
136+ $ result [] = new Return_ ($ variable );
137+ }
138+
139+ return $ result ;
115140 }
116141}
0 commit comments