Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DowngradePhp80] Handle return throw ternary on DowngradeThrowExprRector #224

Merged
merged 5 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeThrowExprRector\Fixture;

final class ReturnThrowTernary
{
public function run($variable)
{
return $variable ?: throw new \InvalidArgumentException();
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeThrowExprRector\Fixture;

final class ReturnThrowTernary
{
public function run($variable)
{
if (!$variable) {
throw new \InvalidArgumentException();
}
return $variable;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,15 @@ private function refactorReturn(Return_ $return): ?array
return [new Expression($return->expr)];
}

if ($return->expr instanceof Ternary) {
$if = $this->refactorTernary($return->expr, null);
if (! $if instanceof If_) {
return null;
}

return [$if, new Return_($return->expr->cond)];
}

return null;
}

Expand Down
Loading