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
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Rector\Doctrine\Tests\TypedCollections\Rector\If_\RemoveIfCollectionIdenticalToNullRector\Fixture;

use Doctrine\Common\Collections\Collection;

final class NotIdenticalNull
{
public Collection $items;

public function someMethod()
{
if ($this->items !== null && $this->items->isEmpty()) {
return true;
}

return false;
}
}

?>
-----
<?php

namespace Rector\Doctrine\Tests\TypedCollections\Rector\If_\RemoveIfCollectionIdenticalToNullRector\Fixture;

use Doctrine\Common\Collections\Collection;

final class NotIdenticalNull
{
public Collection $items;

public function someMethod()
{
if ($this->items->isEmpty()) {
return true;
}

return false;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,6 +24,7 @@ final class RemoveIfCollectionIdenticalToNullRector extends AbstractRector
{
public function __construct(
private readonly CollectionTypeDetector $collectionTypeDetector,
private readonly ValueResolver $valueResolver,
) {

}
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
}