File tree Expand file tree Collapse file tree 2 files changed +40
-3
lines changed
rules/DowngradePhp80/Rector/Expression Expand file tree Collapse file tree 2 files changed +40
-3
lines changed Original file line number Diff line number Diff line change @@ -294,7 +294,11 @@ private function createSwitchStmts(
294294 } elseif ($ matchArm ->body instanceof Throw_) {
295295 $ stmts [] = new Expression ($ matchArm ->body );
296296 } elseif ($ node instanceof Return_) {
297- $ stmts [] = new Return_ ($ matchArm ->body );
297+ if ($ node ->expr instanceof Node \Expr \BinaryOp) {
298+ $ stmts [] = $ this ->replicateBinaryOp ($ node ->expr , $ matchArm ->body );
299+ } else {
300+ $ stmts [] = new Return_ ($ matchArm ->body );
301+ }
298302 } elseif ($ node instanceof Echo_) {
299303 $ stmts [] = new Echo_ ([$ matchArm ->body ]);
300304 $ stmts [] = new Break_ ();
@@ -314,4 +318,19 @@ private function createSwitchStmts(
314318
315319 return $ stmts ;
316320 }
321+
322+ private function replicateBinaryOp (Node \Expr \BinaryOp $ expr , Node \Expr $ body ): Return_
323+ {
324+ $ newExpr = clone $ expr ;
325+ // remove the match statement from the binary operation
326+ $ this ->traverseNodesWithCallable ($ newExpr , function (Node $ node ) use ($ body ): ?Node \Expr {
327+ if ($ node instanceof Match_) {
328+ return $ body ;
329+ }
330+
331+ return null ;
332+ });
333+
334+ return new Return_ ($ newExpr );
335+ }
317336}
Original file line number Diff line number Diff line change 22
33namespace Rector \Tests \Set \Fixture ;
44
5- final class MatchBoolean
5+ final class MatchInBinaryOp
66{
77 public function run ($ value , $ booleanFlag )
88 {
@@ -11,6 +11,14 @@ final class MatchBoolean
1111 default => false ,
1212 } && $ booleanFlag ;
1313 }
14+
15+ public function runTwo ($ value , $ booleanFlag )
16+ {
17+ return match (true ) {
18+ $ value === 2 => true ,
19+ default => false ,
20+ } || $ booleanFlag ;
21+ }
1422}
1523
1624?>
@@ -19,7 +27,7 @@ final class MatchBoolean
1927
2028namespace Rector \Tests \Set \Fixture ;
2129
22- final class MatchBoolean
30+ final class MatchInBinaryOp
2331{
2432 public function run ($ value , $ booleanFlag )
2533 {
@@ -30,6 +38,16 @@ final class MatchBoolean
3038 return false && $ booleanFlag ;
3139 }
3240 }
41+
42+ public function runTwo ($ value , $ booleanFlag )
43+ {
44+ switch (true ) {
45+ case $ value === 2 :
46+ return true || $ booleanFlag ;
47+ default :
48+ return false || $ booleanFlag ;
49+ }
50+ }
3351}
3452
3553?>
You can’t perform that action at this time.
0 commit comments