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
Expand Up @@ -18,23 +18,3 @@ class EmptyIfWithElseIfAndElse
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture;

class EmptyIfWithElseIfAndElse
{
public function calculatePrice($qty, $cond, $price1, $price2)
{
if ($qty >= 1 && !$cond) {
$total = $qty * $price1;
} else {
$total = $qty * $price2;
}

return $total;
}
}

?>
6 changes: 6 additions & 0 deletions rules/DeadCode/Rector/If_/RemoveDeadIfBlockRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ public function refactor(Node $node): int|null|If_
// When the if body is blank but it has an elseif,
// merge the negated if condition with the elseif condition
if ($node->elseifs !== []) {
// A trailing else would run when the negated if condition is true,
// which the original empty if body never did, so keep the code as is.
if ($node->else instanceof Else_) {
return null;
}

$firstElseIf = $node->elseifs[0];
$cond = new BooleanAnd(
$this->conditionInverter->createInvertedCondition($node->cond),
Expand Down
Loading