Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

54 changes: 8 additions & 46 deletions rules/CodingStyle/Rector/If_/NullableCompareToNullRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,17 @@
namespace Rector\CodingStyle\Rector\If_;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Stmt\If_;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\CodingStyle\Rector\If_\NullableCompareToNullRector\NullableCompareToNullRectorTest
* @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.
*/
final class NullableCompareToNullRector extends AbstractRector
final class NullableCompareToNullRector extends AbstractRector implements DeprecatedInterface
{
public function getRuleDefinition(): RuleDefinition
{
Expand Down Expand Up @@ -62,43 +58,9 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if ($node->cond instanceof BooleanNot && $this->isNullableNonScalarType($node->cond->expr)) {
$node->cond = new Identical($node->cond->expr, $this->nodeFactory->createNull());

return $node;
}

if ($this->isNullableNonScalarType($node->cond)) {
$node->cond = new NotIdentical($node->cond, $this->nodeFactory->createNull());

return $node;
}

return null;
}

private function isNullableNonScalarType(Expr $expr): bool
{
$nativeType = $this->nodeTypeResolver->getNativeType($expr);

// is non-nullable?
if (! TypeCombinator::containsNull($nativeType)) {
return false;
}

if (! $nativeType instanceof UnionType) {
return false;
}

// is array?
foreach ($nativeType->getTypes() as $subType) {
if ($subType->isArray()->yes()) {
return false;
}
}

$nativeType = TypeCombinator::removeNull($nativeType);
return ! $nativeType->isScalar()
->yes();
throw new ShouldNotHappenException(sprintf(
'"%s" is deprecated, as null compare is ambiguous and weak; use an "instanceof" check instead',
self::class
));
}
}
2 changes: 0 additions & 2 deletions src/Config/Level/CodingStyleLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Rector\CodingStyle\Rector\FuncCall\StrictArraySearchRector;
use Rector\CodingStyle\Rector\FuncCall\StrictInArrayRector;
use Rector\CodingStyle\Rector\FuncCall\VersionCompareFuncCallToConstantRector;
use Rector\CodingStyle\Rector\If_\NullableCompareToNullRector;
use Rector\CodingStyle\Rector\Property\SplitGroupedPropertiesRector;
use Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector;
use Rector\CodingStyle\Rector\Stmt\RemoveUselessAliasInUseStatementRector;
Expand Down Expand Up @@ -55,7 +54,6 @@ final class CodingStyleLevel
SeparateMultiUseImportsRector::class,
NewlineBetweenClassLikeStmtsRector::class,
NewlineAfterStatementRector::class,
NullableCompareToNullRector::class,
ConsistentImplodeRector::class,
SimplifyQuoteEscapeRector::class,
StringClassNameToClassConstantRector::class,
Expand Down
Loading