1010use PhpParser \Node \Expr \BooleanNot ;
1111use PhpParser \Node \Expr \ConstFetch ;
1212use PhpParser \Node \Expr \FuncCall ;
13+ use PhpParser \Node \Expr \Variable ;
1314use PhpParser \Node \Name ;
1415use PhpParser \Node \Stmt ;
1516use PhpParser \Node \Stmt \Break_ ;
1617use PhpParser \Node \Stmt \Expression ;
1718use PhpParser \Node \Stmt \Foreach_ ;
1819use PhpParser \Node \Stmt \If_ ;
20+ use PhpParser \Node \Stmt \Return_ ;
21+ use Rector \Naming \Naming \VariableNaming ;
22+ use Rector \PHPStan \ScopeFetcher ;
1923use Rector \Rector \AbstractRector ;
2024use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
2125use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
2731 */
2832final class DowngradeArrayAllRector extends AbstractRector
2933{
34+ public function __construct (
35+ private VariableNaming $ variableNaming
36+ )
37+ {
38+ }
39+
3040 public function getNodeTypes (): array
3141 {
32- return [Expression::class];
42+ return [Expression::class, Return_::class ];
3343 }
3444
3545 public function getRuleDefinition (): RuleDefinition
@@ -57,28 +67,28 @@ public function getRuleDefinition(): RuleDefinition
5767 }
5868
5969 /**
60- * @param Expression $node
70+ * @param Expression|Return_ $node
6171 * @return Stmt[]|null
6272 */
6373 public function refactor (Node $ node ): ?array
6474 {
65- if (! $ node ->expr instanceof Assign) {
66- return null ;
67- }
75+ $ expr = $ node instanceof Expression && $ node ->expr instanceof Assign
76+ ? $ node -> expr -> expr
77+ : $ node -> expr ;
6878
69- if (! $ node -> expr -> expr instanceof FuncCall) {
79+ if (! $ expr instanceof FuncCall) {
7080 return null ;
7181 }
7282
73- if (! $ this ->isName ($ node -> expr -> expr , 'array_all ' )) {
83+ if (! $ this ->isName ($ expr , 'array_all ' )) {
7484 return null ;
7585 }
7686
77- if ($ node -> expr -> expr ->isFirstClassCallable ()) {
87+ if ($ expr ->isFirstClassCallable ()) {
7888 return null ;
7989 }
8090
81- $ args = $ node -> expr -> expr ->getArgs ();
91+ $ args = $ expr ->getArgs ();
8292 if (count ($ args ) !== 2 ) {
8393 return null ;
8494 }
@@ -87,17 +97,22 @@ public function refactor(Node $node): ?array
8797 return null ;
8898 }
8999
100+ $ scope = ScopeFetcher::fetch ($ node );
101+ $ variable = $ node instanceof Expression && $ node ->expr instanceof Assign
102+ ? $ node ->expr ->var
103+ : new Variable ($ this ->variableNaming ->createCountedValueName ('found ' , $ scope ));
104+
90105 $ valueCond = $ args [1 ]->value ->expr ;
91106 $ if = new If_ (new BooleanNot ($ valueCond ), [
92107 'stmts ' => [
93- new Expression (new Assign ($ node -> expr -> var , new ConstFetch (new Name ('false ' )))),
108+ new Expression (new Assign ($ variable , new ConstFetch (new Name ('false ' )))),
94109 new Break_ (),
95110 ],
96111 ]);
97112
98- return [
113+ $ result = [
99114 // init
100- new Expression (new Assign ($ node -> expr -> var , new ConstFetch (new Name ('true ' )))),
115+ new Expression (new Assign ($ variable , new ConstFetch (new Name ('true ' )))),
101116
102117 // foreach loop
103118 new Foreach_ (
@@ -113,5 +128,11 @@ public function refactor(Node $node): ?array
113128 ],
114129 ),
115130 ];
131+
132+ if ($ node instanceof Return_) {
133+ $ result [] = new Return_ ($ variable );
134+ }
135+
136+ return $ result ;
116137 }
117138}
0 commit comments