[DeadCode] Add RemoveDeadIfBlocksRector
Removes ifs, elseifs, and elses with no body. Combines them with others when appropriate.
class RemoveDeadIfBlock
{
public function run($condition)
{
- if ($value) {
- } elseif ($differentValue) {
- if ($differentValue) {
- } else {
- }
-
if ($differentValue) {
echo 'different';
- } elseif ($value) {
- } else {
}
- if ($differentValue) {
- } elseif ($value) {
+ if (!$differentValue && $value) {
echo 'value';
- } else {
}
return $differentValue;
}
}
We avoid merging if into following conditions when the if itself has a comment, because we can't tell if the comment will still be correct for the negated condition.
At first, I tried adding logic to RemoveDeadIfForeachForRector to remove elseifs and elses. That worked, but as I added code it was clunky to have so much unrelated if logic in a for and foreach rector. So I moved it to a brand new rector with a simpler implementation. I borrowed significant code from RemoveAlwaysTrueIfConditionRector as well.
This can serve as support for smarter rectors like "RemoveAlwaysTrueIf" and my WIP "RemoveAlwaysFalseIf". Properly modifying ifs requires a lot of extra logic that complicates the implementation of those rectors. In the future, they can do something simpler like removing the body statements but leaving the if structure in place. Then this rector can perform the restructuring of the if.