Skip to content

Commit

Permalink
[EarlyReturn] Handle crash on assign in if else before on RemoveAlway…
Browse files Browse the repository at this point in the history
…sElseRector (#2822)

Co-authored-by: Shin <2082119+shinsenter@users.noreply.github.com>
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
3 people committed Aug 23, 2022
1 parent 837c226 commit 54feb0d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Rector\Tests\EarlyReturn\Rector\If_\RemoveAlwaysElseRector\Fixture;

function assignInIfElseBefore($can_order_products, $offset, $limit)
{
if (!empty($can_order_products)) {
if (!empty($limit)) {
if (!empty($offset)) {
$can_order_products = array_slice($can_order_products, $offset, $limit);
} else {
$can_order_products = array_slice($can_order_products, 0, $limit);
}

if (!empty($can_order_products)) {
return $can_order_products;
} else {
return false;
}
} else {
return $can_order_products;
}
}

return false;
}

?>
-----
<?php

namespace Rector\Tests\EarlyReturn\Rector\If_\RemoveAlwaysElseRector\Fixture;

function assignInIfElseBefore($can_order_products, $offset, $limit)
{
if (!empty($can_order_products)) {
if (!empty($limit)) {
if (!empty($offset)) {
$can_order_products = array_slice($can_order_products, $offset, $limit);
} else {
$can_order_products = array_slice($can_order_products, 0, $limit);
}

if (!empty($can_order_products)) {
return $can_order_products;
}
return false;
}
return $can_order_products;
}

return false;
}

?>
4 changes: 4 additions & 0 deletions rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ private function doesLastStatementBreakFlow(If_ | ElseIf_ $node): bool
{
$lastStmt = end($node->stmts);

if ($lastStmt instanceof If_ && $lastStmt->else instanceof Else_) {
return $this->doesLastStatementBreakFlow($lastStmt);
}

return ! ($lastStmt instanceof Return_
|| $lastStmt instanceof Throw_
|| $lastStmt instanceof Continue_
Expand Down

0 comments on commit 54feb0d

Please sign in to comment.