[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 <leo@getpop.org>
Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Abdul Malik Ikhsan 2021-10-18 15:39:54 +07:00 committed by GitHub
parent adb6e3cd10
commit 33e5cab8d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 1 deletions

View File

@ -0,0 +1,38 @@
<?php
namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector\Fixture;
final class DemoFile
{
public function run($param)
{
return match ($param) {
'a' => 'hi',
'b' => 'bye',
default => throw new Exception('no salutation'),
};
}
}
?>
-----
<?php
namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector\Fixture;
final class DemoFile
{
public function run($param)
{
switch ($param) {
case 'a':
return 'hi';
case 'b':
return 'bye';
default:
throw new Exception('no salutation');
}
}
}
?>

View File

@ -9,6 +9,7 @@ use PhpParser\Node\Expr;
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 @@ CODE_SAMPLE
{
$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));