|
| 1 | +<?php |
| 2 | + |
| 3 | +declare (strict_types=1); |
| 4 | +namespace Rector\DeadCode\Rector\Ternary; |
| 5 | + |
| 6 | +use PhpParser\Node; |
| 7 | +use PhpParser\Node\Expr; |
| 8 | +use PhpParser\Node\Expr\Array_; |
| 9 | +use PhpParser\Node\Expr\BooleanNot; |
| 10 | +use PhpParser\Node\Expr\Ternary; |
| 11 | +use PhpParser\Node\Scalar\Int_; |
| 12 | +use PHPStan\Type\ArrayType; |
| 13 | +use PHPStan\Type\BooleanType; |
| 14 | +use PHPStan\Type\IntegerType; |
| 15 | +use Rector\PhpParser\Node\Value\ValueResolver; |
| 16 | +use Rector\Rector\AbstractRector; |
| 17 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 18 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 19 | +/** |
| 20 | + * @see \Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\RemoveUselessTernaryRectorTest |
| 21 | + */ |
| 22 | +final class RemoveUselessTernaryRector extends AbstractRector |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @readonly |
| 26 | + */ |
| 27 | + private ValueResolver $valueResolver; |
| 28 | + public function __construct(ValueResolver $valueResolver) |
| 29 | + { |
| 30 | + $this->valueResolver = $valueResolver; |
| 31 | + } |
| 32 | + public function getRuleDefinition(): RuleDefinition |
| 33 | + { |
| 34 | + return new RuleDefinition('Remove useless ternary if fallback is falsey of left code', [new CodeSample(<<<'CODE_SAMPLE' |
| 35 | +function go(bool $value) |
| 36 | +{ |
| 37 | + return $value ?: false; |
| 38 | +} |
| 39 | +CODE_SAMPLE |
| 40 | +, <<<'CODE_SAMPLE' |
| 41 | +function go(bool $value) |
| 42 | +{ |
| 43 | + return $value; |
| 44 | +} |
| 45 | +CODE_SAMPLE |
| 46 | +)]); |
| 47 | + } |
| 48 | + /** |
| 49 | + * @return array<class-string<Node>> |
| 50 | + */ |
| 51 | + public function getNodeTypes(): array |
| 52 | + { |
| 53 | + return [Ternary::class]; |
| 54 | + } |
| 55 | + /** |
| 56 | + * @param Ternary $node |
| 57 | + */ |
| 58 | + public function refactor(Node $node): ?Node |
| 59 | + { |
| 60 | + /** |
| 61 | + * if condition is negated, skip |
| 62 | + * switch negated ternary condition early via SwitchNegatedTernaryRector for that |
| 63 | + * if needed |
| 64 | + */ |
| 65 | + if ($node->cond instanceof BooleanNot) { |
| 66 | + return null; |
| 67 | + } |
| 68 | + $nativeType = $this->nodeTypeResolver->getNativeType($node->cond); |
| 69 | + if ($nativeType instanceof BooleanType && $node->if instanceof Expr && $this->valueResolver->isTrue($node->if) && $this->valueResolver->isFalse($node->else)) { |
| 70 | + return $node->cond; |
| 71 | + } |
| 72 | + if ($node->if instanceof Expr && !$this->nodeComparator->areNodesEqual($node->if, $node->cond)) { |
| 73 | + return null; |
| 74 | + } |
| 75 | + if ($nativeType instanceof BooleanType && $this->valueResolver->isFalse($node->else)) { |
| 76 | + return $node->cond; |
| 77 | + } |
| 78 | + if ($nativeType instanceof ArrayType && $node->else instanceof Array_ && $node->else->items === []) { |
| 79 | + return $node->cond; |
| 80 | + } |
| 81 | + if ($nativeType instanceof IntegerType && $node->else instanceof Int_ && $node->else->value === 0) { |
| 82 | + return $node->cond; |
| 83 | + } |
| 84 | + return null; |
| 85 | + } |
| 86 | +} |
0 commit comments