Skip to content

Commit 9ea8247

Browse files
committed
[typed-collections] Add AssertSameCountOnCollectionToAssertCountRector
1 parent 151ef4f commit 9ea8247

File tree

7 files changed

+199
-2
lines changed

7 files changed

+199
-2
lines changed

config/sets/typed-collections.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Rector\Doctrine\TypedCollections\Rector\If_\RemoveIfInstanceofCollectionRector;
2929
use Rector\Doctrine\TypedCollections\Rector\If_\RemoveIsArrayOnCollectionRector;
3030
use Rector\Doctrine\TypedCollections\Rector\MethodCall\AssertNullOnCollectionToAssertEmptyRector;
31+
use Rector\Doctrine\TypedCollections\Rector\MethodCall\AssertSameCountOnCollectionToAssertCountRector;
3132
use Rector\Doctrine\TypedCollections\Rector\MethodCall\SetArrayToNewCollectionRector;
3233
use Rector\Doctrine\TypedCollections\Rector\New_\RemoveNewArrayCollectionWrapRector;
3334
use Rector\Doctrine\TypedCollections\Rector\NullsafeMethodCall\RemoveNullsafeOnCollectionRector;
@@ -82,7 +83,10 @@
8283

8384
// cleanup
8485
RemoveNullsafeOnCollectionRector::class,
85-
AssertNullOnCollectionToAssertEmptyRector::class,
86+
87+
// test assertions
8688
RemoveAssertNotNullOnCollectionRector::class,
89+
AssertNullOnCollectionToAssertEmptyRector::class,
90+
AssertSameCountOnCollectionToAssertCountRector::class,
8791
]);
8892
};
Lines changed: 28 additions & 0 deletions
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\MethodCall\AssertSameCountOnCollectionToAssertCountRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class AssertSameCountOnCollectionToAssertCountRectorTest 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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Rector\Doctrine\Tests\TypedCollections\Rector\MethodCall\AssertSameCountOnCollectionToAssertCountRector\Fixture;
4+
5+
use Doctrine\Common\Collections\Collection;
6+
use PHPUnit\Framework\TestCase;
7+
8+
final class AssertCount extends TestCase
9+
{
10+
public Collection $items;
11+
12+
public function someMethod()
13+
{
14+
$this->assertSame(10, $this->items->count());
15+
}
16+
}
17+
18+
?>
19+
-----
20+
<?php
21+
22+
namespace Rector\Doctrine\Tests\TypedCollections\Rector\MethodCall\AssertSameCountOnCollectionToAssertCountRector\Fixture;
23+
24+
use Doctrine\Common\Collections\Collection;
25+
use PHPUnit\Framework\TestCase;
26+
27+
final class AssertCount extends TestCase
28+
{
29+
public Collection $items;
30+
31+
public function someMethod()
32+
{
33+
$this->assertCount(10, $this->items);
34+
}
35+
}
36+
37+
?>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rector\Doctrine\Tests\TypedCollections\Rector\MethodCall\AssertSameCountOnCollectionToAssertCountRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipAssertCountOnSomethingElse extends TestCase
8+
{
9+
public array $items;
10+
11+
public function someMethod()
12+
{
13+
$this->assertSame(10, $this->items->count());
14+
}
15+
}
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\MethodCall\AssertSameCountOnCollectionToAssertCountRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(AssertSameCountOnCollectionToAssertCountRector::class);
10+
};

rules/TypedCollections/Rector/Expression/RemoveAssertNotNullOnCollectionRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
namespace Rector\Doctrine\TypedCollections\Rector\Expression;
66

7-
use PHPUnit\Framework\Assert;
87
use PhpParser\Node;
98
use PhpParser\Node\Expr\StaticCall;
109
use PhpParser\Node\Stmt\Expression;
1110
use PhpParser\NodeVisitor;
11+
use PHPUnit\Framework\Assert;
1212
use Rector\Doctrine\TypedCollections\TypeAnalyzer\CollectionTypeDetector;
1313
use Rector\PHPUnit\Enum\PHPUnitClassName;
1414
use Rector\Rector\AbstractRector;
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Doctrine\TypedCollections\Rector\MethodCall;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Arg;
9+
use PhpParser\Node\Expr\MethodCall;
10+
use PhpParser\Node\Identifier;
11+
use Rector\Doctrine\TypedCollections\TypeAnalyzer\CollectionTypeDetector;
12+
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
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\MethodCall\AssertSameCountOnCollectionToAssertCountRector\AssertSameCountOnCollectionToAssertCountRectorTest
19+
*/
20+
final class AssertSameCountOnCollectionToAssertCountRector extends AbstractRector
21+
{
22+
public function __construct(
23+
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
24+
private readonly CollectionTypeDetector $collectionTypeDetector
25+
) {
26+
}
27+
28+
public function getRuleDefinition(): RuleDefinition
29+
{
30+
return new RuleDefinition(
31+
'Change $this->assertSame(5, $collection->count()) to $this->assertCount(5, $collection) in tests',
32+
[
33+
new CodeSample(
34+
<<<'CODE_SAMPLE'
35+
use Doctrine\Common\Collections\Collection;
36+
37+
final class SomeClass extends \PHPUnit\Framework\TestCase
38+
{
39+
private Collection $items;
40+
41+
public function test(): void
42+
{
43+
$this->assertSame(5, $this->items->count());
44+
}
45+
}
46+
CODE_SAMPLE
47+
,
48+
<<<'CODE_SAMPLE'
49+
use Doctrine\Common\Collections\Collection;
50+
51+
final class SomeClass extends \PHPUnit\Framework\TestCase
52+
{
53+
private Collection $items;
54+
55+
public function test(): void
56+
{
57+
$this->assertCount(5, $this->items);
58+
}
59+
}
60+
CODE_SAMPLE
61+
)]
62+
);
63+
}
64+
65+
public function getNodeTypes(): array
66+
{
67+
return [MethodCall::class];
68+
69+
}
70+
71+
/**
72+
* @param MethodCall $node
73+
*/
74+
public function refactor(Node $node): MethodCall|null
75+
{
76+
if ($node->isFirstClassCallable()) {
77+
return null;
78+
}
79+
80+
if (! $this->isName($node->name, 'assertSame')) {
81+
return null;
82+
}
83+
84+
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
85+
return null;
86+
}
87+
88+
$comparedArg = $node->getArgs()[1]
89+
->value;
90+
91+
if ($comparedArg instanceof MethodCall && $this->isName(
92+
$comparedArg->name,
93+
'count'
94+
) && $this->collectionTypeDetector->isCollectionType($comparedArg->var)) {
95+
$node->name = new Identifier('assertCount');
96+
$node->args[1] = new Arg($comparedArg->var);
97+
98+
return $node;
99+
}
100+
101+
return null;
102+
}
103+
}

0 commit comments

Comments
 (0)