Skip to content

Commit a16ded4

Browse files
[DowngradePhp80] Handle return throw ternary on DowngradeThrowExprRector (#224)
* [DowngradePhp80] Handle return throw ternary on DowngradeThrowExprRector * [ci-review] Rector Rectify * Fix phpstan * Fix phpstan * Fix phpstan --------- Co-authored-by: GitHub Action <[email protected]>
1 parent efef745 commit a16ded4

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeThrowExprRector\Fixture;
4+
5+
final class ReturnThrowTernary
6+
{
7+
public function run($variable)
8+
{
9+
return $variable ?: throw new \InvalidArgumentException();
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeThrowExprRector\Fixture;
18+
19+
final class ReturnThrowTernary
20+
{
21+
public function run($variable)
22+
{
23+
if (!$variable) {
24+
throw new \InvalidArgumentException();
25+
}
26+
return $variable;
27+
}
28+
}
29+
30+
?>

rules/DowngradePhp80/Rector/Expression/DowngradeThrowExprRector.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,15 @@ private function refactorReturn(Return_ $return): ?array
211211
return [new Expression($return->expr)];
212212
}
213213

214+
if ($return->expr instanceof Ternary) {
215+
$if = $this->refactorTernary($return->expr, null);
216+
if (! $if instanceof If_) {
217+
return null;
218+
}
219+
220+
return [$if, new Return_($return->expr->cond)];
221+
}
222+
214223
return null;
215224
}
216225

0 commit comments

Comments
 (0)