Skip to content

Commit ac37965

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

6 files changed

Lines changed: 193 additions & 1 deletion

File tree

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

0 commit comments

Comments
 (0)