Skip to content

Commit 1826fb4

Browse files
Improve AssertCompareOnCountableWithMethodToAssertCountRector
1 parent 4dfb448 commit 1826fb4

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

rules-tests/CodeQuality/Rector/MethodCall/AssertCompareOnCountableWithMethodToAssertCountRector/Fixture/count.php.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ final class Count extends \PHPUnit\Framework\TestCase
1313
$this->assertEquals(5, $collection->count());
1414
\PHPUnit\Framework\TestCase::assertSame(5, $collection->count());
1515
self::assertSame(5, $collection->count());
16+
17+
$this->assertNotSame(6, $collection->count());
18+
$this->assertNotEquals(6, $collection->count());
19+
\PHPUnit\Framework\TestCase::assertNotSame(6, $collection->count());
20+
self::assertNotSame(6, $collection->count());
1621
}
1722
}
1823

@@ -33,6 +38,11 @@ final class Count extends \PHPUnit\Framework\TestCase
3338
$this->assertCount(5, $collection);
3439
\PHPUnit\Framework\TestCase::assertCount(5, $collection);
3540
self::assertCount(5, $collection);
41+
42+
$this->assertNotCount(6, $collection);
43+
$this->assertNotCount(6, $collection);
44+
\PHPUnit\Framework\TestCase::assertNotCount(6, $collection);
45+
self::assertNotCount(6, $collection);
3646
}
3747
}
3848

rules-tests/CodeQuality/Rector/MethodCall/AssertCompareOnCountableWithMethodToAssertCountRector/Fixture/direct_count_call.php.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ final class DirectCountCall extends \PHPUnit\Framework\TestCase
88
{
99
$items = [1, 2, 3];
1010
$this->assertSame(3, count($items));
11+
$this->assertNotSame(4, count($items));
1112
}
1213
}
1314

@@ -23,6 +24,7 @@ final class DirectCountCall extends \PHPUnit\Framework\TestCase
2324
{
2425
$items = [1, 2, 3];
2526
$this->assertCount(3, $items);
27+
$this->assertNotCount(4, $items);
2628
}
2729
}
2830

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertCompareOnCountableWithMethodToAssertCountRector\Fixture;
4+
5+
final class OtherCountCall extends \PHPUnit\Framework\TestCase
6+
{
7+
public function test()
8+
{
9+
$this->assertEquals(10, iterator_count($something));
10+
11+
$count = 92;
12+
$this->assertNotEquals($count, sizeof($something), 'third argument');
13+
}
14+
}
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertCompareOnCountableWithMethodToAssertCountRector\Fixture;
21+
22+
final class OtherCountCall extends \PHPUnit\Framework\TestCase
23+
{
24+
public function test()
25+
{
26+
$this->assertCount(10, $something);
27+
28+
$count = 92;
29+
$this->assertNotCount($count, $something, 'third argument');
30+
}
31+
}
32+
33+
?>

rules/CodeQuality/Rector/MethodCall/AssertCompareOnCountableWithMethodToAssertCountRector.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public function getRuleDefinition(): RuleDefinition
4040
$this->assertCount(1, $countable);
4141
CODE_SAMPLE
4242
),
43+
new CodeSample(
44+
'$this->assertSame(10, count($anything), "message");',
45+
'$this->assertCount(10, $anything, "message");'
46+
),
4347
]
4448
);
4549
}
@@ -57,7 +61,10 @@ public function getNodeTypes(): array
5761
*/
5862
public function refactor(Node $node): MethodCall|StaticCall|null
5963
{
60-
if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames($node, ['assertSame', 'assertEquals'])) {
64+
if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames(
65+
$node,
66+
['assertSame', 'assertNotSame', 'assertEquals', 'assertNotEquals']
67+
)) {
6168
return null;
6269
}
6370

@@ -72,12 +79,15 @@ public function refactor(Node $node): MethodCall|StaticCall|null
7279

7380
$comparedExpr = $assertArgs[1]->value;
7481

75-
if ($comparedExpr instanceof FuncCall && $this->isName($comparedExpr->name, 'count')) {
82+
if (
83+
$comparedExpr instanceof FuncCall
84+
&& $this->isNames($comparedExpr->name, ['count', 'sizeof', 'iterator_count'])
85+
) {
7686
$countArg = $comparedExpr->getArgs()[0];
7787
$assertArgs[1] = new Arg($countArg->value);
7888

7989
$node->args = $assertArgs;
80-
$node->name = new Identifier('assertCount');
90+
$this->renameMethod($node);
8191

8292
return $node;
8393
}
@@ -94,10 +104,19 @@ public function refactor(Node $node): MethodCall|StaticCall|null
94104
$args[1] = new Arg($comparedExpr->var);
95105

96106
$node->args = $args;
97-
$node->name = new Identifier('assertCount');
107+
$this->renameMethod($node);
98108
}
99109
}
100110

101111
return null;
102112
}
113+
114+
private function renameMethod(MethodCall|StaticCall $node): void
115+
{
116+
if ($this->isNames($node->name, ['assertSame', 'assertEquals'])) {
117+
$node->name = new Identifier('assertCount');
118+
} elseif ($this->isNames($node->name, ['assertNotSame', 'assertNotEquals'])) {
119+
$node->name = new Identifier('assertNotCount');
120+
}
121+
}
103122
}

0 commit comments

Comments
 (0)