From 936b1166e3758bdef8b49d2239fccd00c5399417 Mon Sep 17 00:00:00 2001 From: Peter Fox Date: Sat, 11 Jan 2025 13:59:45 +0000 Subject: [PATCH 1/4] working test --- .../match_maintains_return_condition.php.inc | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 tests/Set/Fixture/match_maintains_return_condition.php.inc diff --git a/tests/Set/Fixture/match_maintains_return_condition.php.inc b/tests/Set/Fixture/match_maintains_return_condition.php.inc new file mode 100644 index 00000000..21fad928 --- /dev/null +++ b/tests/Set/Fixture/match_maintains_return_condition.php.inc @@ -0,0 +1,35 @@ + true, + default => false, + } && $booleanFlag; + } +} + +?> +----- + From 0971ffd3d27df20190de5539c7e6dc69973bd62d Mon Sep 17 00:00:00 2001 From: Peter Fox Date: Sat, 11 Jan 2025 14:25:16 +0000 Subject: [PATCH 2/4] Working fix --- .../DowngradeMatchToSwitchRector.php | 21 +++++++++++++++++- ...ion.php.inc => match_in_binary_op.php.inc} | 22 +++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) rename tests/Set/Fixture/{match_maintains_return_condition.php.inc => match_in_binary_op.php.inc} (51%) diff --git a/rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php b/rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php index 205f138a..dec872d1 100644 --- a/rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php +++ b/rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php @@ -294,7 +294,11 @@ private function createSwitchStmts( } elseif ($matchArm->body instanceof Throw_) { $stmts[] = new Expression($matchArm->body); } elseif ($node instanceof Return_) { - $stmts[] = new Return_($matchArm->body); + if ($node->expr instanceof Node\Expr\BinaryOp) { + $stmts[] = $this->replicateBinaryOp($node->expr, $matchArm->body); + } else { + $stmts[] = new Return_($matchArm->body); + } } elseif ($node instanceof Echo_) { $stmts[] = new Echo_([$matchArm->body]); $stmts[] = new Break_(); @@ -314,4 +318,19 @@ private function createSwitchStmts( return $stmts; } + + private function replicateBinaryOp(Node\Expr\BinaryOp $expr, Node\Expr $body): Return_ + { + $newExpr = clone $expr; + // remove the match statement from the binary operation + $this->traverseNodesWithCallable($newExpr, function (Node $node) use ($body): ?Node\Expr { + if ($node instanceof Match_) { + return $body; + } + + return null; + }); + + return new Return_($newExpr); + } } diff --git a/tests/Set/Fixture/match_maintains_return_condition.php.inc b/tests/Set/Fixture/match_in_binary_op.php.inc similarity index 51% rename from tests/Set/Fixture/match_maintains_return_condition.php.inc rename to tests/Set/Fixture/match_in_binary_op.php.inc index 21fad928..de44e5ff 100644 --- a/tests/Set/Fixture/match_maintains_return_condition.php.inc +++ b/tests/Set/Fixture/match_in_binary_op.php.inc @@ -2,7 +2,7 @@ namespace Rector\Tests\Set\Fixture; -final class MatchBoolean +final class MatchInBinaryOp { public function run($value, $booleanFlag) { @@ -11,6 +11,14 @@ final class MatchBoolean default => false, } && $booleanFlag; } + + public function runTwo($value, $booleanFlag) + { + return match (true) { + $value === 2 => true, + default => false, + } || $booleanFlag; + } } ?> @@ -19,7 +27,7 @@ final class MatchBoolean namespace Rector\Tests\Set\Fixture; -final class MatchBoolean +final class MatchInBinaryOp { public function run($value, $booleanFlag) { @@ -30,6 +38,16 @@ final class MatchBoolean return false && $booleanFlag; } } + + public function runTwo($value, $booleanFlag) + { + switch (true) { + case $value === 2: + return true || $booleanFlag; + default: + return false || $booleanFlag; + } + } } ?> From 0acc5519c427dab77a29d4876aa966224e270aa8 Mon Sep 17 00:00:00 2001 From: Peter Fox Date: Sat, 11 Jan 2025 14:35:16 +0000 Subject: [PATCH 3/4] rector fix --- .../Rector/Expression/DowngradeMatchToSwitchRector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php b/rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php index dec872d1..5b62025f 100644 --- a/rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php +++ b/rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php @@ -323,7 +323,7 @@ private function replicateBinaryOp(Node\Expr\BinaryOp $expr, Node\Expr $body): R { $newExpr = clone $expr; // remove the match statement from the binary operation - $this->traverseNodesWithCallable($newExpr, function (Node $node) use ($body): ?Node\Expr { + $this->traverseNodesWithCallable($newExpr, static function (Node $node) use ($body): ?Node\Expr { if ($node instanceof Match_) { return $body; } From 5827624a9e30a18ccec79459fdc0fb064f7fe5ee Mon Sep 17 00:00:00 2001 From: Peter Fox Date: Sat, 11 Jan 2025 15:19:39 +0000 Subject: [PATCH 4/4] fixes --- .../Expression/DowngradeMatchToSwitchRector.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php b/rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php index 5b62025f..5f7ba9bb 100644 --- a/rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php +++ b/rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php @@ -7,8 +7,10 @@ use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\ArrayItem; +use PhpParser\Node\Expr; use PhpParser\Node\Expr\ArrowFunction; use PhpParser\Node\Expr\Assign; +use PhpParser\Node\Expr\BinaryOp; use PhpParser\Node\Expr\CallLike; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\Match_; @@ -294,7 +296,7 @@ private function createSwitchStmts( } elseif ($matchArm->body instanceof Throw_) { $stmts[] = new Expression($matchArm->body); } elseif ($node instanceof Return_) { - if ($node->expr instanceof Node\Expr\BinaryOp) { + if ($node->expr instanceof BinaryOp) { $stmts[] = $this->replicateBinaryOp($node->expr, $matchArm->body); } else { $stmts[] = new Return_($matchArm->body); @@ -319,13 +321,13 @@ private function createSwitchStmts( return $stmts; } - private function replicateBinaryOp(Node\Expr\BinaryOp $expr, Node\Expr $body): Return_ + private function replicateBinaryOp(BinaryOp $binaryOp, Expr $expr): Return_ { - $newExpr = clone $expr; + $newExpr = clone $binaryOp; // remove the match statement from the binary operation - $this->traverseNodesWithCallable($newExpr, static function (Node $node) use ($body): ?Node\Expr { + $this->traverseNodesWithCallable($newExpr, static function (Node $node) use ($expr): ?Expr { if ($node instanceof Match_) { - return $body; + return $expr; } return null;