Skip to content

Commit

Permalink
[DowngradePhp80] Handle Throw Exception on DowngradeMatchToSwitchRect…
Browse files Browse the repository at this point in the history
…or (#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 rectorphp/rector#6750

* [ci-review] Rector Rectify

Co-authored-by: Leonardo Losoviz <leo@getpop.org>
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
3 people committed Oct 18, 2021
1 parent adb6e3c commit 33e5cab
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
@@ -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');
}
}
}

?>
Expand Up @@ -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_;
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 33e5cab

Please sign in to comment.