Skip to content
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,42 @@
<?php

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

final class IfElseIfElse
{
public function run($value, $formatForEmail, $time_context, $direction)
{
if ($value == 0) {
return $value;
} elseif ($formatForEmail) {
$value .= '|' . 1;

return $time_context . '&nbsp;&nbsp;//&nbsp;&nbsp;' . $direction . $value . ' minute(s)';
} else {
return $value;
}
}
}

?>
-----
<?php

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

final class IfElseIfElse
{
public function run($value, $formatForEmail, $time_context, $direction)
{
if ($value == 0) {
return $value;
}
if ($formatForEmail) {
$value .= '|' . 1;
return $time_context . '&nbsp;&nbsp;//&nbsp;&nbsp;' . $direction . $value . ' minute(s)';
}
return $value;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ class NestedIfWithTerminatingElseIfAndElse
if ($cond3) {
return 'bar';
}
else {
return 'baz';
}
return 'baz';
}
foo();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ class ProcessEmptyReturnLast
$value = 55;
return 10;
}
else {
return;
}
return;
}

public function secondRun($value)
Expand Down
25 changes: 18 additions & 7 deletions rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?array
{
if ($this->doesLastStatementBreakFlow($node)) {
if ($this->doesNotLastStatementBreakFlow($node)) {
return null;
}

Expand Down Expand Up @@ -108,7 +108,7 @@ private function handleElseIfs(If_ $if): array
$currentElseIf = array_shift($if->elseifs);

// If the last statement in the `elseif` breaks flow, merge it into the original `if` and stop processing
if ($this->doesLastStatementBreakFlow($currentElseIf)) {
if ($this->doesNotLastStatementBreakFlow($currentElseIf)) {
$this->updateIfWithElseIf($if, $currentElseIf);
$nodesToReturn = [...$nodesToReturn, $if, ...$this->getStatementsElseIfs($if)];

Expand All @@ -130,7 +130,18 @@ private function handleElseIfs(If_ $if): array
}

if ($originalIf->else instanceof Else_) {
$nodesToReturn[] = $originalIf->else;
$mergeStmts = true;
foreach ($nodesToReturn as $nodeToReturn) {
if ($this->doesNotLastStatementBreakFlow($nodeToReturn)) {
$nodesToReturn[] = $originalIf->else;
$mergeStmts = false;
break;
}
}

if ($mergeStmts) {
$nodesToReturn = array_merge($nodesToReturn, $originalIf->else->stmts);
}
}

return $nodesToReturn;
Expand All @@ -143,7 +154,7 @@ private function getStatementsElseIfs(If_ $if): array
{
$statements = [];
foreach ($if->elseifs as $key => $elseif) {
if ($this->doesLastStatementBreakFlow($elseif) && $elseif->stmts !== []) {
if ($this->doesNotLastStatementBreakFlow($elseif) && $elseif->stmts !== []) {
continue;
}

Expand All @@ -154,17 +165,17 @@ private function getStatementsElseIfs(If_ $if): array
return $statements;
}

private function doesLastStatementBreakFlow(If_ | ElseIf_ | Else_ $node): bool
private function doesNotLastStatementBreakFlow(If_ | ElseIf_ | Else_ $node): bool
{
$lastStmt = end($node->stmts);

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

foreach ($lastStmt->elseifs as $elseIf) {
if ($this->doesLastStatementBreakFlow($elseIf)) {
if ($this->doesNotLastStatementBreakFlow($elseIf)) {
return true;
}
}
Expand Down