Skip to content

Commit 4d28cda

Browse files
committed
[typed-collections] Add RemoveAssertNotNullOnCollectionRector
1 parent b805102 commit 4d28cda

5 files changed

Lines changed: 169 additions & 0 deletions

File tree

config/sets/typed-collections.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Rector\Doctrine\TypedCollections\Rector\ClassMethod\ReturnArrayToNewArrayCollectionRector;
2121
use Rector\Doctrine\TypedCollections\Rector\ClassMethod\ReturnCollectionDocblockRector;
2222
use Rector\Doctrine\TypedCollections\Rector\Empty_\EmptyOnCollectionToIsEmptyCallRector;
23+
use Rector\Doctrine\TypedCollections\Rector\Expression\RemoveAssertNotNullOnCollectionRector;
2324
use Rector\Doctrine\TypedCollections\Rector\Expression\RemoveCoalesceAssignOnCollectionRector;
2425
use Rector\Doctrine\TypedCollections\Rector\FuncCall\ArrayMapOnCollectionToArrayRector;
2526
use Rector\Doctrine\TypedCollections\Rector\FuncCall\ArrayMergeOnCollectionToArrayRector;
@@ -82,5 +83,6 @@
8283
// cleanup
8384
RemoveNullsafeOnCollectionRector::class,
8485
AssertNullOnCollectionToAssertEmptyRector::class,
86+
RemoveAssertNotNullOnCollectionRector::class,
8587
]);
8688
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Rector\Doctrine\Tests\TypedCollections\Rector\Expression\RemoveAssertNotNullOnCollectionRector\Fixture;
4+
5+
use Doctrine\Common\Collections\Collection;
6+
7+
final class SomeClass
8+
{
9+
public Collection $items;
10+
11+
public function someMethod()
12+
{
13+
\PHPUnit\Framework\Assert::assertNotNull($this->items);
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\Doctrine\Tests\TypedCollections\Rector\Expression\RemoveAssertNotNullOnCollectionRector\Fixture;
22+
23+
use Doctrine\Common\Collections\Collection;
24+
25+
final class SomeClass
26+
{
27+
public Collection $items;
28+
29+
public function someMethod()
30+
{
31+
}
32+
}
33+
34+
?>
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\Expression\RemoveAssertNotNullOnCollectionRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class RemoveAssertNotNullOnCollectionRectorTest 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\Expression\RemoveAssertNotNullOnCollectionRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(RemoveAssertNotNullOnCollectionRector::class);
10+
};
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Doctrine\TypedCollections\Rector\Expression;
6+
7+
use PHPUnit\Framework\Assert;
8+
use PhpParser\Node;
9+
use PhpParser\Node\Expr\StaticCall;
10+
use PhpParser\Node\Stmt\Expression;
11+
use PhpParser\NodeVisitor;
12+
use Rector\Doctrine\TypedCollections\TypeAnalyzer\CollectionTypeDetector;
13+
use Rector\PHPUnit\Enum\PHPUnitClassName;
14+
use Rector\Rector\AbstractRector;
15+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
16+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
17+
18+
/**
19+
* @see \Rector\Doctrine\Tests\TypedCollections\Rector\Expression\RemoveAssertNotNullOnCollectionRector\RemoveAssertNotNullOnCollectionRectorTest
20+
*/
21+
final class RemoveAssertNotNullOnCollectionRector extends AbstractRector
22+
{
23+
public function __construct(
24+
private readonly CollectionTypeDetector $collectionTypeDetector,
25+
) {
26+
}
27+
28+
public function getRuleDefinition(): RuleDefinition
29+
{
30+
return new RuleDefinition(
31+
'Remove ' . Assert::class . '::assertNotNull() on a Collection type',
32+
[
33+
new CodeSample(
34+
<<<'CODE_SAMPLE'
35+
use Doctrine\Common\Collections\Collection;
36+
37+
class SomeClass
38+
{
39+
public function run(Collection $collection): void
40+
{
41+
Assert::assertNotNull($collection);
42+
}
43+
}
44+
CODE_SAMPLE
45+
,
46+
<<<'CODE_SAMPLE'
47+
use Doctrine\Common\Collections\Collection;
48+
49+
class SomeClass
50+
{
51+
public function run(Collection $collection): void
52+
{
53+
}
54+
}
55+
CODE_SAMPLE
56+
),
57+
]
58+
);
59+
}
60+
61+
public function getNodeTypes(): array
62+
{
63+
return [Expression::class];
64+
}
65+
66+
/**
67+
* @param Expression $node
68+
*/
69+
public function refactor(Node $node): ?int
70+
{
71+
if (! $node->expr instanceof StaticCall) {
72+
return null;
73+
}
74+
75+
$staticCall = $node->expr;
76+
if (! $this->isName($staticCall->name, 'assertNotNull')) {
77+
return null;
78+
}
79+
80+
if (! $this->isName($staticCall->class, PHPUnitClassName::ASSERT)) {
81+
return null;
82+
}
83+
84+
if (count($staticCall->args) !== 1) {
85+
return null;
86+
}
87+
88+
$firstArg = $staticCall->getArgs()[0];
89+
if (! $this->collectionTypeDetector->isCollectionType($firstArg->value)) {
90+
return null;
91+
}
92+
93+
return NodeVisitor::REMOVE_NODE;
94+
}
95+
}

0 commit comments

Comments
 (0)