diff --git a/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/cases_mix_up_default.php.inc b/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/cases_mix_up_default.php.inc new file mode 100644 index 00000000000..ed488abd9ca --- /dev/null +++ b/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/cases_mix_up_default.php.inc @@ -0,0 +1,38 @@ + +----- + 'foo', + default => 'bar', + }; + } +} + +?> diff --git a/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/mutiple_cases.php.inc b/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/mutiple_cases.php.inc index 1b3667d77d6..9abeb48fb82 100644 --- a/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/mutiple_cases.php.inc +++ b/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/mutiple_cases.php.inc @@ -9,6 +9,7 @@ final class MultipleCases switch ($value) { case 'v1': case 'v2': + case 'v3': return 100; default: return 1000; @@ -27,7 +28,7 @@ final class MultipleCases public function run($value) { return match ($value) { - 'v1', 'v2' => 100, + 'v1', 'v2', 'v3' => 100, default => 1000, }; } diff --git a/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/mutiple_cases2.php.inc b/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/mutiple_cases2.php.inc new file mode 100644 index 00000000000..345ed2d13c6 --- /dev/null +++ b/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/mutiple_cases2.php.inc @@ -0,0 +1,42 @@ + +----- + 100, + 'v4', 'v5', 'v6' => 200, + default => 1000, + }; + } +} + +?> diff --git a/rules/Php80/NodeResolver/SwitchExprsResolver.php b/rules/Php80/NodeResolver/SwitchExprsResolver.php index 59c3b0ed440..e42579af1ea 100644 --- a/rules/Php80/NodeResolver/SwitchExprsResolver.php +++ b/rules/Php80/NodeResolver/SwitchExprsResolver.php @@ -22,17 +22,21 @@ final class SwitchExprsResolver */ public function resolve(Switch_ $switch): array { - $condAndExpr = []; + $condAndExpr = []; + $collectionEmptyCasesCond = []; - $previousCondExpr = null; - foreach ($switch->cases as $case) { + foreach ($switch->cases as $key => $case) { if (! $this->isValidCase($case)) { return []; } - // prepend to previous one + if ($case->stmts === [] && $case->cond instanceof Expr) { + $collectionEmptyCasesCond[$key] = $case->cond; + } + } + + foreach ($switch->cases as $key => $case) { if ($case->stmts === []) { - $previousCondExpr = $case->cond; continue; } @@ -42,12 +46,20 @@ public function resolve(Switch_ $switch): array } $condExprs = []; - if ($previousCondExpr instanceof Expr) { - $condExprs[] = $previousCondExpr; - $previousCondExpr = null; - } if ($case->cond !== null) { + $emptyCasesCond = []; + + foreach ($collectionEmptyCasesCond as $i => $collectionEmptyCaseCond) { + if ($i > $key) { + break; + } + + $emptyCasesCond[$i] = $collectionEmptyCaseCond; + unset($collectionEmptyCasesCond[$i]); + } + + $condExprs = $emptyCasesCond; $condExprs[] = $case->cond; }