From 33e5cab8d53facaa6753fc26b755e9ce3634d940 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 18 Oct 2021 15:39:54 +0700 Subject: [PATCH] [DowngradePhp80] Handle Throw Exception on DowngradeMatchToSwitchRector (#1019) * Add failing test fixture for DowngradeMatchToSwitchRector + DowngradeThrowExprRector # Failing Test for DowngradeMatchToSwitchRector + DowngradeThrowExprRector Based on https://getrector.org/demo/1ec2fda5-2490-6c56-9d12-93cf2aafeb8a * update fixture * Closes #1018 Fixes https://github.com/rectorphp/rector/issues/6750 * [ci-review] Rector Rectify Co-authored-by: Leonardo Losoviz Co-authored-by: GitHub Action --- .../Fixture/throw_expression.php.inc | 38 +++++++++++++++++++ .../DowngradeMatchToSwitchRector.php | 5 ++- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 rules-tests/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector/Fixture/throw_expression.php.inc diff --git a/rules-tests/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector/Fixture/throw_expression.php.inc b/rules-tests/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector/Fixture/throw_expression.php.inc new file mode 100644 index 00000000000..72281814c41 --- /dev/null +++ b/rules-tests/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector/Fixture/throw_expression.php.inc @@ -0,0 +1,38 @@ + 'hi', + 'b' => 'bye', + default => throw new Exception('no salutation'), + }; + } +} + +?> +----- + diff --git a/rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php b/rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php index 43f211c450b..a6b4c3f1dff 100644 --- a/rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php +++ b/rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php @@ -9,6 +9,7 @@ use PhpParser\Node\Expr\ArrayItem; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\Match_; +use PhpParser\Node\Expr\Throw_; use PhpParser\Node\MatchArm; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Break_; @@ -163,7 +164,9 @@ private function createSwitchStmts(Expression | Return_ $node, MatchArm $matchAr { $stmts = []; - if ($node instanceof Expression) { + if ($matchArm->body instanceof Throw_) { + $stmts[] = new Expression($matchArm->body); + } elseif ($node instanceof Expression) { /** @var Assign $assign */ $assign = $node->expr; $stmts[] = new Expression(new Assign($assign->var, $matchArm->body));