Skip to content

Commit dce07db

Browse files
authored
[CodingStyle] Deprecate NullableCompareToNullRector (#8268)
1 parent ec55677 commit dce07db

11 files changed

Lines changed: 8 additions & 251 deletions

File tree

‎rules-tests/CodingStyle/Rector/If_/NullableCompareToNullRector/Fixture/fixture.php.inc‎

Lines changed: 0 additions & 27 deletions
This file was deleted.

‎rules-tests/CodingStyle/Rector/If_/NullableCompareToNullRector/Fixture/fixture3.php.inc‎

Lines changed: 0 additions & 49 deletions
This file was deleted.

‎rules-tests/CodingStyle/Rector/If_/NullableCompareToNullRector/Fixture/keep_nullable_bool.php.inc‎

Lines changed: 0 additions & 13 deletions
This file was deleted.

‎rules-tests/CodingStyle/Rector/If_/NullableCompareToNullRector/Fixture/skip_fixture2.php.inc‎

Lines changed: 0 additions & 6 deletions
This file was deleted.

‎rules-tests/CodingStyle/Rector/If_/NullableCompareToNullRector/Fixture/skip_mixed.php.inc‎

Lines changed: 0 additions & 18 deletions
This file was deleted.

‎rules-tests/CodingStyle/Rector/If_/NullableCompareToNullRector/Fixture/skip_nullable_scalars.php.inc‎

Lines changed: 0 additions & 42 deletions
This file was deleted.

‎rules-tests/CodingStyle/Rector/If_/NullableCompareToNullRector/Fixture/skip_phpdoc.php.inc‎

Lines changed: 0 additions & 11 deletions
This file was deleted.

‎rules-tests/CodingStyle/Rector/If_/NullableCompareToNullRector/NullableCompareToNullRectorTest.php‎

Lines changed: 0 additions & 28 deletions
This file was deleted.

‎rules-tests/CodingStyle/Rector/If_/NullableCompareToNullRector/config/configured_rule.php‎

Lines changed: 0 additions & 9 deletions
This file was deleted.

‎rules/CodingStyle/Rector/If_/NullableCompareToNullRector.php‎

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,17 @@
55
namespace Rector\CodingStyle\Rector\If_;
66

77
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;
128
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;
1511
use Rector\Rector\AbstractRector;
1612
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1713
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
1814

1915
/**
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.
2117
*/
22-
final class NullableCompareToNullRector extends AbstractRector
18+
final class NullableCompareToNullRector extends AbstractRector implements DeprecatedInterface
2319
{
2420
public function getRuleDefinition(): RuleDefinition
2521
{
@@ -62,43 +58,9 @@ public function getNodeTypes(): array
6258
*/
6359
public function refactor(Node $node): ?Node
6460
{
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+
));
10365
}
10466
}

0 commit comments

Comments
 (0)