|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\DeadCode\Rector\BooleanOr; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr; |
| 9 | +use PhpParser\Node\Expr\BinaryOp\BooleanOr; |
| 10 | +use PhpParser\Node\Expr\BinaryOp\Identical; |
| 11 | +use PhpParser\Node\Expr\BooleanNot; |
| 12 | +use PhpParser\Node\Expr\FuncCall; |
| 13 | +use PhpParser\Node\Expr\Variable; |
| 14 | +use PHPStan\Type\Type; |
| 15 | +use PHPStan\Type\TypeCombinator; |
| 16 | +use Rector\PhpParser\Node\Value\ValueResolver; |
| 17 | +use Rector\Rector\AbstractRector; |
| 18 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 19 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 20 | + |
| 21 | +/** |
| 22 | + * @see \Rector\Tests\DeadCode\Rector\BooleanOr\RemoveRedundantNullableTypeCheckRector\RemoveRedundantNullableTypeCheckRectorTest |
| 23 | + */ |
| 24 | +final class RemoveRedundantNullableTypeCheckRector extends AbstractRector |
| 25 | +{ |
| 26 | + public function __construct( |
| 27 | + private readonly ValueResolver $valueResolver |
| 28 | + ) { |
| 29 | + } |
| 30 | + |
| 31 | + public function getRuleDefinition(): RuleDefinition |
| 32 | + { |
| 33 | + return new RuleDefinition( |
| 34 | + 'Remove is_<type>() check that can never fail next to null compare on a nullable value', |
| 35 | + [ |
| 36 | + new CodeSample( |
| 37 | + <<<'CODE_SAMPLE' |
| 38 | +class SomeClass |
| 39 | +{ |
| 40 | + public function run(?string $value) |
| 41 | + { |
| 42 | + if ($value === null || ! is_string($value)) { |
| 43 | + return; |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | +CODE_SAMPLE |
| 48 | + , |
| 49 | + <<<'CODE_SAMPLE' |
| 50 | +class SomeClass |
| 51 | +{ |
| 52 | + public function run(?string $value) |
| 53 | + { |
| 54 | + if ($value === null) { |
| 55 | + return; |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | +CODE_SAMPLE |
| 60 | + ), |
| 61 | + ] |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @return array<class-string<Node>> |
| 67 | + */ |
| 68 | + public function getNodeTypes(): array |
| 69 | + { |
| 70 | + return [BooleanOr::class]; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @param BooleanOr $node |
| 75 | + */ |
| 76 | + public function refactor(Node $node): ?Expr |
| 77 | + { |
| 78 | + if (! $node->left instanceof Identical) { |
| 79 | + return null; |
| 80 | + } |
| 81 | + |
| 82 | + $nullComparedExpr = $this->matchNullComparedExpr($node->left); |
| 83 | + if (! $nullComparedExpr instanceof Variable) { |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + if (! $node->right instanceof BooleanNot) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + $funcCall = $node->right->expr; |
| 92 | + if (! $funcCall instanceof FuncCall) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + |
| 96 | + if ($funcCall->isFirstClassCallable()) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + if (count($funcCall->getArgs()) !== 1) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + if (! $this->nodeComparator->areNodesEqual($funcCall->getArgs()[0]->value, $nullComparedExpr)) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + $funcCallName = $this->getName($funcCall); |
| 109 | + if ($funcCallName === null) { |
| 110 | + return null; |
| 111 | + } |
| 112 | + |
| 113 | + $comparedType = $this->getType($nullComparedExpr); |
| 114 | + if (! TypeCombinator::containsNull($comparedType)) { |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + if (! $this->isAlwaysMatchingType($funcCallName, TypeCombinator::removeNull($comparedType))) { |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + return $node->left; |
| 123 | + } |
| 124 | + |
| 125 | + private function matchNullComparedExpr(Identical $identical): ?Expr |
| 126 | + { |
| 127 | + if ($this->valueResolver->isNull($identical->left)) { |
| 128 | + return $identical->right; |
| 129 | + } |
| 130 | + |
| 131 | + if ($this->valueResolver->isNull($identical->right)) { |
| 132 | + return $identical->left; |
| 133 | + } |
| 134 | + |
| 135 | + return null; |
| 136 | + } |
| 137 | + |
| 138 | + private function isAlwaysMatchingType(string $funcCallName, Type $type): bool |
| 139 | + { |
| 140 | + return match ($funcCallName) { |
| 141 | + 'is_string' => $type->isString() |
| 142 | + ->yes(), |
| 143 | + 'is_int', 'is_integer', 'is_long' => $type->isInteger() |
| 144 | + ->yes(), |
| 145 | + 'is_float', 'is_double' => $type->isFloat() |
| 146 | + ->yes(), |
| 147 | + 'is_bool' => $type->isBoolean() |
| 148 | + ->yes(), |
| 149 | + 'is_array' => $type->isArray() |
| 150 | + ->yes(), |
| 151 | + 'is_object' => $type->isObject() |
| 152 | + ->yes(), |
| 153 | + default => false, |
| 154 | + }; |
| 155 | + } |
| 156 | +} |
0 commit comments