mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-08 20:22:24 +00:00
[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:
parent
adb6e3cd10
commit
33e5cab8d5
|
@ -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');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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));
|
||||
|
|
Loading…
Reference in New Issue
Block a user