Skip to content

Commit bd4a63d

Browse files
committed
[typed-collections] Add RemoveIfCollectionIdenticalToNullRector
1 parent 4d9d290 commit bd4a63d

5 files changed

Lines changed: 189 additions & 0 deletions

File tree

config/sets/typed-collections.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Rector\Doctrine\TypedCollections\Rector\FuncCall\ArrayMapOnCollectionToArrayRector;
2727
use Rector\Doctrine\TypedCollections\Rector\FuncCall\ArrayMergeOnCollectionToArrayRector;
2828
use Rector\Doctrine\TypedCollections\Rector\FuncCall\InArrayOnCollectionToContainsCallRector;
29+
use Rector\Doctrine\TypedCollections\Rector\If_\RemoveIfCollectionIdenticalToNullRector;
2930
use Rector\Doctrine\TypedCollections\Rector\If_\RemoveIfInstanceofCollectionRector;
3031
use Rector\Doctrine\TypedCollections\Rector\If_\RemoveIsArrayOnCollectionRector;
3132
use Rector\Doctrine\TypedCollections\Rector\MethodCall\AssertNullOnCollectionToAssertEmptyRector;
@@ -45,6 +46,7 @@
4546
RemoveCoalesceAssignOnCollectionRector::class,
4647
RemoveIfInstanceofCollectionRector::class,
4748
RemoveIsArrayOnCollectionRector::class,
49+
RemoveIfCollectionIdenticalToNullRector::class,
4850

4951
// collection method calls
5052
ArrayDimFetchAssignToAddCollectionCallRector::class,
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 SomeIf
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 SomeIf
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+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Doctrine\Tests\TypedCollections\Rector\If_\RemoveIfCollectionIdenticalToNullRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class RemoveIfCollectionIdenticalToNullRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Doctrine\TypedCollections\Rector\If_\RemoveIfCollectionIdenticalToNullRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(RemoveIfCollectionIdenticalToNullRector::class);
10+
};
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Doctrine\TypedCollections\Rector\If_;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
9+
use PhpParser\Node\Expr\BinaryOp\Identical;
10+
use PhpParser\Node\Expr\ConstFetch;
11+
use PhpParser\Node\Stmt\If_;
12+
use Rector\Doctrine\TypedCollections\TypeAnalyzer\CollectionTypeDetector;
13+
use Rector\Rector\AbstractRector;
14+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
15+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
16+
17+
/**
18+
* @see \Rector\Doctrine\Tests\TypedCollections\Rector\If_\RemoveIfCollectionIdenticalToNullRector\RemoveIfCollectionIdenticalToNullRectorTest
19+
*/
20+
final class RemoveIfCollectionIdenticalToNullRector extends AbstractRector
21+
{
22+
public function __construct(
23+
private readonly CollectionTypeDetector $collectionTypeDetector,
24+
) {
25+
26+
}
27+
28+
public function getNodeTypes(): array
29+
{
30+
return [If_::class];
31+
}
32+
33+
/**
34+
* @param If_ $node
35+
*/
36+
public function refactor(Node $node): ?If_
37+
{
38+
if (! $node->cond instanceof BooleanOr) {
39+
return null;
40+
}
41+
42+
$leftCondition = $node->cond->left;
43+
if (! $leftCondition instanceof Identical) {
44+
return null;
45+
}
46+
47+
if (! $leftCondition->right instanceof ConstFetch || ! $this->isName(
48+
$leftCondition->right->name,
49+
'null'
50+
)) {
51+
return null;
52+
}
53+
54+
if (! $this->collectionTypeDetector->isCollectionType($leftCondition->left)) {
55+
return null;
56+
}
57+
58+
$rightCondition = $node->cond->right;
59+
60+
$node->cond = $rightCondition;
61+
62+
return $node;
63+
}
64+
65+
public function getRuleDefinition(): RuleDefinition
66+
{
67+
return new RuleDefinition('Remove collection identical to null from if || condition', [
68+
new CodeSample(
69+
<<<'CODE_SAMPLE'
70+
use Doctrine\Common\Collections\Collection;
71+
72+
final class SomeClass
73+
{
74+
private Collection $collection;
75+
76+
public function someMethod()
77+
{
78+
if ($this->collection === null || $this->collection->isEmpty()) {
79+
return true;
80+
}
81+
82+
return false;
83+
}
84+
}
85+
CODE_SAMPLE
86+
,
87+
<<<'CODE_SAMPLE'
88+
use Doctrine\Common\Collections\Collection;
89+
90+
final class SomeClass
91+
{
92+
private Collection $collection;
93+
94+
public function someMethod()
95+
{
96+
if ($this->collection->isEmpty()) {
97+
return true;
98+
}
99+
100+
return false;
101+
}
102+
}
103+
CODE_SAMPLE
104+
)]);
105+
}
106+
}

0 commit comments

Comments
 (0)