|
| 1 | +<?php |
| 2 | + |
| 3 | +declare (strict_types=1); |
| 4 | +namespace Rector\DeadCode\Rector\If_; |
| 5 | + |
| 6 | +use PhpParser\Node; |
| 7 | +use PhpParser\Node\Expr\BinaryOp\BooleanAnd; |
| 8 | +use PhpParser\Node\Stmt\Else_; |
| 9 | +use PhpParser\Node\Stmt\If_; |
| 10 | +use PhpParser\NodeVisitor; |
| 11 | +use Rector\DeadCode\SideEffect\SideEffectNodeDetector; |
| 12 | +use Rector\EarlyReturn\NodeTransformer\ConditionInverter; |
| 13 | +use Rector\Rector\AbstractRector; |
| 14 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 15 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 16 | +/** |
| 17 | + * @see \Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\RemoveDeadIfBlockRectorTest |
| 18 | + */ |
| 19 | +final class RemoveDeadIfBlockRector extends AbstractRector |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @readonly |
| 23 | + */ |
| 24 | + private ConditionInverter $conditionInverter; |
| 25 | + /** |
| 26 | + * @readonly |
| 27 | + */ |
| 28 | + private SideEffectNodeDetector $sideEffectNodeDetector; |
| 29 | + public function __construct(ConditionInverter $conditionInverter, SideEffectNodeDetector $sideEffectNodeDetector) |
| 30 | + { |
| 31 | + $this->conditionInverter = $conditionInverter; |
| 32 | + $this->sideEffectNodeDetector = $sideEffectNodeDetector; |
| 33 | + } |
| 34 | + public function getRuleDefinition(): RuleDefinition |
| 35 | + { |
| 36 | + return new RuleDefinition('Remove if, elseif, and else blocks that do not do anything', [new CodeSample(<<<'CODE_SAMPLE' |
| 37 | +class SomeClass |
| 38 | +{ |
| 39 | + public function run($value, $differentValue) |
| 40 | + { |
| 41 | + if ($value) { |
| 42 | + } elseif ($differentValue) { |
| 43 | + } else { |
| 44 | + } |
| 45 | +
|
| 46 | + if ($differentValue) { |
| 47 | + echo 'different'; |
| 48 | + } elseif ($value) { |
| 49 | + } else { |
| 50 | + } |
| 51 | +
|
| 52 | + if ($differentValue) { |
| 53 | + } elseif ($value) { |
| 54 | + echo 'value'; |
| 55 | + } else { |
| 56 | + } |
| 57 | +
|
| 58 | + return $differentValue; |
| 59 | + } |
| 60 | +} |
| 61 | +CODE_SAMPLE |
| 62 | +, <<<'CODE_SAMPLE' |
| 63 | +class SomeClass |
| 64 | +{ |
| 65 | + public function run($value, $differentValue) |
| 66 | + { |
| 67 | + if ($differentValue) { |
| 68 | + echo 'different'; |
| 69 | + } |
| 70 | +
|
| 71 | + if (!$differentValue && $value) { |
| 72 | + echo 'value'; |
| 73 | + } |
| 74 | +
|
| 75 | + return $differentValue; |
| 76 | + } |
| 77 | +} |
| 78 | +CODE_SAMPLE |
| 79 | +)]); |
| 80 | + } |
| 81 | + /** |
| 82 | + * @return array<class-string<Node>> |
| 83 | + */ |
| 84 | + public function getNodeTypes(): array |
| 85 | + { |
| 86 | + return [If_::class]; |
| 87 | + } |
| 88 | + /** |
| 89 | + * @param If_ $node |
| 90 | + * @return NodeVisitor::REMOVE_NODE|null|If_ |
| 91 | + */ |
| 92 | + public function refactor(Node $node) |
| 93 | + { |
| 94 | + if ($node->else instanceof Else_ && $node->else->stmts === []) { |
| 95 | + $node->else = null; |
| 96 | + return $this->refactor($node) ?? $node; |
| 97 | + } |
| 98 | + if ($node->elseifs !== []) { |
| 99 | + foreach ($node->elseifs as $elseif) { |
| 100 | + $keep_elseifs = array_filter($node->elseifs, fn($elseif) => $elseif->stmts !== [] || $this->sideEffectNodeDetector->detect($elseif->cond)); |
| 101 | + if (count($node->elseifs) !== count($keep_elseifs)) { |
| 102 | + $node->elseifs = $keep_elseifs; |
| 103 | + return $this->refactor($node) ?? $node; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + if ($node->stmts !== []) { |
| 108 | + return null; |
| 109 | + } |
| 110 | + // Skip commented blocks because we can't know |
| 111 | + // if the comment will make sense after merging. |
| 112 | + if ($node->getComments() !== []) { |
| 113 | + return null; |
| 114 | + } |
| 115 | + if ($this->sideEffectNodeDetector->detect($node->cond)) { |
| 116 | + return null; |
| 117 | + } |
| 118 | + // When the if body is blank but it has an elseif, |
| 119 | + // merge the negated if condition with the elseif condition |
| 120 | + if ($node->elseifs !== []) { |
| 121 | + $firstElseIf = $node->elseifs[0]; |
| 122 | + $cond = new BooleanAnd($this->conditionInverter->createInvertedCondition($node->cond), $firstElseIf->cond); |
| 123 | + $if = new If_($cond, ['stmts' => $firstElseIf->stmts]); |
| 124 | + if (count($node->elseifs) > 1) { |
| 125 | + $if->elseifs = \array_slice($node->elseifs, 1); |
| 126 | + } |
| 127 | + return $this->refactor($if) ?? $if; |
| 128 | + } |
| 129 | + if ($node->else instanceof Else_) { |
| 130 | + $node->cond = $this->conditionInverter->createInvertedCondition($node->cond); |
| 131 | + $node->stmts = $node->else->stmts; |
| 132 | + $node->else = null; |
| 133 | + return $node; |
| 134 | + } |
| 135 | + return NodeVisitor::REMOVE_NODE; |
| 136 | + } |
| 137 | +} |
0 commit comments