|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace StaticDeploy\Rector; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr; |
| 9 | +use PhpParser\Node\Expr\BinaryOp\BooleanAnd; |
| 10 | +use PhpParser\Node\Expr\ConstFetch; |
| 11 | +use PhpParser\Node\Name; |
| 12 | +use Rector\DeadCode\NodeAnalyzer\SafeLeftTypeBooleanAndOrAnalyzer; |
| 13 | +use Rector\PhpParser\Node\Value\ValueResolver; |
| 14 | +use Rector\Rector\AbstractRector; |
| 15 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 16 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 17 | + |
| 18 | +final class ReduceKnownBooleanAnd extends AbstractRector |
| 19 | +{ |
| 20 | + public function __construct( |
| 21 | + private readonly SafeLeftTypeBooleanAndOrAnalyzer $safeLeftTypeBooleanAndOrAnalyzer, |
| 22 | + private readonly ValueResolver $valueResolver |
| 23 | + ) { |
| 24 | + } |
| 25 | + |
| 26 | + public function getRuleDefinition(): RuleDefinition |
| 27 | + { |
| 28 | + return new RuleDefinition('Remove `and true` that has no added value', [ |
| 29 | + new CodeSample( |
| 30 | + <<<'CODE_SAMPLE' |
| 31 | +class SomeClass |
| 32 | +{ |
| 33 | + public function run() |
| 34 | + { |
| 35 | + return true && 5 === 1; |
| 36 | + } |
| 37 | +} |
| 38 | +CODE_SAMPLE |
| 39 | + , |
| 40 | + <<<'CODE_SAMPLE' |
| 41 | +class SomeClass |
| 42 | +{ |
| 43 | + public function run() |
| 44 | + { |
| 45 | + return 5 === 1; |
| 46 | + } |
| 47 | +} |
| 48 | +CODE_SAMPLE |
| 49 | + ), |
| 50 | + ]); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @return array<class-string<Node>> |
| 55 | + */ |
| 56 | + public function getNodeTypes(): array |
| 57 | + { |
| 58 | + return [BooleanAnd::class]; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @param BooleanAnd $node |
| 63 | + */ |
| 64 | + public function refactor(Node $node): ?Node |
| 65 | + { |
| 66 | + if ($this->isTrueOrBooleanAndTrues($node->right)) { |
| 67 | + return $node->left; |
| 68 | + } |
| 69 | + |
| 70 | + if (! $this->safeLeftTypeBooleanAndOrAnalyzer->isSafe($node)) { |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + $conditionStaticType = $this->getType($node->left); |
| 75 | + |
| 76 | + if ($conditionStaticType->isFalse()->yes()) { |
| 77 | + return new ConstFetch(new Name('false')); |
| 78 | + } |
| 79 | + |
| 80 | + if ($conditionStaticType->isTrue()->yes()) { |
| 81 | + return $node->right; |
| 82 | + } |
| 83 | + |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + private function isTrueOrBooleanAndTrues(Expr $expr): bool |
| 88 | + { |
| 89 | + if ($this->valueResolver->isTrue($expr)) { |
| 90 | + return true; |
| 91 | + } |
| 92 | + |
| 93 | + if (! $expr instanceof BooleanAnd) { |
| 94 | + return false; |
| 95 | + } |
| 96 | + |
| 97 | + if (! $this->isTrueOrBooleanAndTrues($expr->left)) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + return $this->isTrueOrBooleanAndTrues($expr->right); |
| 102 | + } |
| 103 | +} |
0 commit comments