Skip to content

Commit 360ed2a

Browse files
authored
add boolean and not null case (#436)
1 parent 398d913 commit 360ed2a

2 files changed

Lines changed: 97 additions & 20 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Rector\Doctrine\Tests\TypedCollections\Rector\If_\RemoveIfCollectionIdenticalToNullRector\Fixture;
4+
5+
use Doctrine\Common\Collections\Collection;
6+
7+
final class NotIdenticalNull
8+
{
9+
public Collection $items;
10+
11+
public function someMethod()
12+
{
13+
if ($this->items !== null && $this->items->isEmpty()) {
14+
return true;
15+
}
16+
17+
return false;
18+
}
19+
}
20+
21+
?>
22+
-----
23+
<?php
24+
25+
namespace Rector\Doctrine\Tests\TypedCollections\Rector\If_\RemoveIfCollectionIdenticalToNullRector\Fixture;
26+
27+
use Doctrine\Common\Collections\Collection;
28+
29+
final class NotIdenticalNull
30+
{
31+
public Collection $items;
32+
33+
public function someMethod()
34+
{
35+
if ($this->items->isEmpty()) {
36+
return true;
37+
}
38+
39+
return false;
40+
}
41+
}
42+
43+
?>

rules/TypedCollections/Rector/If_/RemoveIfCollectionIdenticalToNullRector.php

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
namespace Rector\Doctrine\TypedCollections\Rector\If_;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Expr;
9+
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
810
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
911
use PhpParser\Node\Expr\BinaryOp\Identical;
10-
use PhpParser\Node\Expr\ConstFetch;
12+
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
1113
use PhpParser\Node\Stmt\If_;
1214
use Rector\Doctrine\TypedCollections\TypeAnalyzer\CollectionTypeDetector;
15+
use Rector\PhpParser\Node\Value\ValueResolver;
1316
use Rector\Rector\AbstractRector;
1417
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1518
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -21,6 +24,7 @@ final class RemoveIfCollectionIdenticalToNullRector extends AbstractRector
2124
{
2225
public function __construct(
2326
private readonly CollectionTypeDetector $collectionTypeDetector,
27+
private readonly ValueResolver $valueResolver,
2428
) {
2529

2630
}
@@ -35,31 +39,25 @@ public function getNodeTypes(): array
3539
*/
3640
public function refactor(Node $node): ?If_
3741
{
38-
if (! $node->cond instanceof BooleanOr) {
39-
return null;
40-
}
42+
if ($node->cond instanceof BooleanOr) {
43+
$changedCond = $this->refactorBooleanOr($node->cond);
44+
if ($changedCond instanceof Expr) {
45+
$node->cond = $changedCond;
4146

42-
$leftCondition = $node->cond->left;
43-
if (! $leftCondition instanceof Identical) {
44-
return null;
47+
return $node;
48+
}
4549
}
4650

47-
if (! $leftCondition->right instanceof ConstFetch || ! $this->isName(
48-
$leftCondition->right->name,
49-
'null'
50-
)) {
51-
return null;
52-
}
51+
if ($node->cond instanceof BooleanAnd) {
52+
$changedCond = $this->refactorBooleanAnd($node->cond);
53+
if ($changedCond instanceof Expr) {
54+
$node->cond = $changedCond;
5355

54-
if (! $this->collectionTypeDetector->isCollectionType($leftCondition->left)) {
55-
return null;
56+
return $node;
57+
}
5658
}
5759

58-
$rightCondition = $node->cond->right;
59-
60-
$node->cond = $rightCondition;
61-
62-
return $node;
60+
return null;
6361
}
6462

6563
public function getRuleDefinition(): RuleDefinition
@@ -103,4 +101,40 @@ public function someMethod()
103101
CODE_SAMPLE
104102
)]);
105103
}
104+
105+
private function refactorBooleanOr(BooleanOr $booleanOr): ?Expr
106+
{
107+
$leftCondition = $booleanOr->left;
108+
if (! $leftCondition instanceof Identical) {
109+
return null;
110+
}
111+
112+
if (! $this->valueResolver->isNull(($leftCondition->right))) {
113+
return null;
114+
}
115+
116+
if (! $this->collectionTypeDetector->isCollectionType($leftCondition->left)) {
117+
return null;
118+
}
119+
120+
return $booleanOr->right;
121+
}
122+
123+
private function refactorBooleanAnd(BooleanAnd $booleanAnd): ?Expr
124+
{
125+
$leftCondition = $booleanAnd->left;
126+
if (! $leftCondition instanceof NotIdentical) {
127+
return null;
128+
}
129+
130+
if (! $this->valueResolver->isNull($leftCondition->right)) {
131+
return null;
132+
}
133+
134+
if (! $this->collectionTypeDetector->isCollectionType($leftCondition->left)) {
135+
return null;
136+
}
137+
138+
return $booleanAnd->right;
139+
}
106140
}

0 commit comments

Comments
 (0)