|
5 | 5 | namespace Rector\CodingStyle\Rector\If_; |
6 | 6 |
|
7 | 7 | use PhpParser\Node; |
8 | | -use PhpParser\Node\Expr; |
9 | | -use PhpParser\Node\Expr\BinaryOp\Identical; |
10 | | -use PhpParser\Node\Expr\BinaryOp\NotIdentical; |
11 | | -use PhpParser\Node\Expr\BooleanNot; |
12 | 8 | use PhpParser\Node\Stmt\If_; |
13 | | -use PHPStan\Type\TypeCombinator; |
14 | | -use PHPStan\Type\UnionType; |
| 9 | +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; |
| 10 | +use Rector\Exception\ShouldNotHappenException; |
15 | 11 | use Rector\Rector\AbstractRector; |
16 | 12 | use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
17 | 13 | use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
18 | 14 |
|
19 | 15 | /** |
20 | | - * @see \Rector\Tests\CodingStyle\Rector\If_\NullableCompareToNullRector\NullableCompareToNullRectorTest |
| 16 | + * @deprecated This rule is deprecated, as comparing a nullable object to null is ambiguous and weak. Use an "instanceof" check instead, e.g. the instanceof rule set. |
21 | 17 | */ |
22 | | -final class NullableCompareToNullRector extends AbstractRector |
| 18 | +final class NullableCompareToNullRector extends AbstractRector implements DeprecatedInterface |
23 | 19 | { |
24 | 20 | public function getRuleDefinition(): RuleDefinition |
25 | 21 | { |
@@ -62,43 +58,9 @@ public function getNodeTypes(): array |
62 | 58 | */ |
63 | 59 | public function refactor(Node $node): ?Node |
64 | 60 | { |
65 | | - if ($node->cond instanceof BooleanNot && $this->isNullableNonScalarType($node->cond->expr)) { |
66 | | - $node->cond = new Identical($node->cond->expr, $this->nodeFactory->createNull()); |
67 | | - |
68 | | - return $node; |
69 | | - } |
70 | | - |
71 | | - if ($this->isNullableNonScalarType($node->cond)) { |
72 | | - $node->cond = new NotIdentical($node->cond, $this->nodeFactory->createNull()); |
73 | | - |
74 | | - return $node; |
75 | | - } |
76 | | - |
77 | | - return null; |
78 | | - } |
79 | | - |
80 | | - private function isNullableNonScalarType(Expr $expr): bool |
81 | | - { |
82 | | - $nativeType = $this->nodeTypeResolver->getNativeType($expr); |
83 | | - |
84 | | - // is non-nullable? |
85 | | - if (! TypeCombinator::containsNull($nativeType)) { |
86 | | - return false; |
87 | | - } |
88 | | - |
89 | | - if (! $nativeType instanceof UnionType) { |
90 | | - return false; |
91 | | - } |
92 | | - |
93 | | - // is array? |
94 | | - foreach ($nativeType->getTypes() as $subType) { |
95 | | - if ($subType->isArray()->yes()) { |
96 | | - return false; |
97 | | - } |
98 | | - } |
99 | | - |
100 | | - $nativeType = TypeCombinator::removeNull($nativeType); |
101 | | - return ! $nativeType->isScalar() |
102 | | - ->yes(); |
| 61 | + throw new ShouldNotHappenException(sprintf( |
| 62 | + '"%s" is deprecated, as null compare is ambiguous and weak; use an "instanceof" check instead', |
| 63 | + self::class |
| 64 | + )); |
103 | 65 | } |
104 | 66 | } |
0 commit comments