diff --git a/rules-tests/TypedCollections/Rector/If_/RemoveIfCollectionIdenticalToNullRector/Fixture/not_identical_null.php.inc b/rules-tests/TypedCollections/Rector/If_/RemoveIfCollectionIdenticalToNullRector/Fixture/not_identical_null.php.inc new file mode 100644 index 00000000..a535260d --- /dev/null +++ b/rules-tests/TypedCollections/Rector/If_/RemoveIfCollectionIdenticalToNullRector/Fixture/not_identical_null.php.inc @@ -0,0 +1,43 @@ +items !== null && $this->items->isEmpty()) { + return true; + } + + return false; + } +} + +?> +----- +items->isEmpty()) { + return true; + } + + return false; + } +} + +?> diff --git a/rules/TypedCollections/Rector/If_/RemoveIfCollectionIdenticalToNullRector.php b/rules/TypedCollections/Rector/If_/RemoveIfCollectionIdenticalToNullRector.php index 9d74a591..76f43c23 100644 --- a/rules/TypedCollections/Rector/If_/RemoveIfCollectionIdenticalToNullRector.php +++ b/rules/TypedCollections/Rector/If_/RemoveIfCollectionIdenticalToNullRector.php @@ -5,11 +5,14 @@ namespace Rector\Doctrine\TypedCollections\Rector\If_; use PhpParser\Node; +use PhpParser\Node\Expr; +use PhpParser\Node\Expr\BinaryOp\BooleanAnd; use PhpParser\Node\Expr\BinaryOp\BooleanOr; use PhpParser\Node\Expr\BinaryOp\Identical; -use PhpParser\Node\Expr\ConstFetch; +use PhpParser\Node\Expr\BinaryOp\NotIdentical; use PhpParser\Node\Stmt\If_; use Rector\Doctrine\TypedCollections\TypeAnalyzer\CollectionTypeDetector; +use Rector\PhpParser\Node\Value\ValueResolver; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -21,6 +24,7 @@ final class RemoveIfCollectionIdenticalToNullRector extends AbstractRector { public function __construct( private readonly CollectionTypeDetector $collectionTypeDetector, + private readonly ValueResolver $valueResolver, ) { } @@ -35,31 +39,25 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?If_ { - if (! $node->cond instanceof BooleanOr) { - return null; - } + if ($node->cond instanceof BooleanOr) { + $changedCond = $this->refactorBooleanOr($node->cond); + if ($changedCond instanceof Expr) { + $node->cond = $changedCond; - $leftCondition = $node->cond->left; - if (! $leftCondition instanceof Identical) { - return null; + return $node; + } } - if (! $leftCondition->right instanceof ConstFetch || ! $this->isName( - $leftCondition->right->name, - 'null' - )) { - return null; - } + if ($node->cond instanceof BooleanAnd) { + $changedCond = $this->refactorBooleanAnd($node->cond); + if ($changedCond instanceof Expr) { + $node->cond = $changedCond; - if (! $this->collectionTypeDetector->isCollectionType($leftCondition->left)) { - return null; + return $node; + } } - $rightCondition = $node->cond->right; - - $node->cond = $rightCondition; - - return $node; + return null; } public function getRuleDefinition(): RuleDefinition @@ -103,4 +101,40 @@ public function someMethod() CODE_SAMPLE )]); } + + private function refactorBooleanOr(BooleanOr $booleanOr): ?Expr + { + $leftCondition = $booleanOr->left; + if (! $leftCondition instanceof Identical) { + return null; + } + + if (! $this->valueResolver->isNull(($leftCondition->right))) { + return null; + } + + if (! $this->collectionTypeDetector->isCollectionType($leftCondition->left)) { + return null; + } + + return $booleanOr->right; + } + + private function refactorBooleanAnd(BooleanAnd $booleanAnd): ?Expr + { + $leftCondition = $booleanAnd->left; + if (! $leftCondition instanceof NotIdentical) { + return null; + } + + if (! $this->valueResolver->isNull($leftCondition->right)) { + return null; + } + + if (! $this->collectionTypeDetector->isCollectionType($leftCondition->left)) { + return null; + } + + return $booleanAnd->right; + } }