Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/Rules/PHPUnit/AssertSameWithCountRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ public function processNode(Node $node, Scope $scope): array
return [
RuleErrorBuilder::message('You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).')
->identifier('phpunit.assertCount')
->fixNode($node, static function (CallLike $node) use ($scope) {
$newArgs = self::rewriteArgs($node->args, $scope);
if ($newArgs === null) {
return $node;
}

$node->name = new Node\Identifier('assertCount');
$node->args = $newArgs;

return $node;
})
->build(),
];
}
Expand All @@ -58,6 +69,17 @@ public function processNode(Node $node, Scope $scope): array
return [
RuleErrorBuilder::message('You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, $variable->count()).')
->identifier('phpunit.assertCount')
->fixNode($node, static function (CallLike $node) use ($scope) {
$newArgs = self::rewriteArgs($node->args, $scope);
if ($newArgs === null) {
return $node;
}

$node->name = new Node\Identifier('assertCount');
$node->args = $newArgs;

return $node;
})
->build(),
];
}
Expand Down Expand Up @@ -109,4 +131,36 @@ private static function isNormalCount(Node\Expr\FuncCall $countFuncCall, Type $c
return $isNormalCount;
}

/**
* @param array<Node\Arg|Node\VariadicPlaceholder> $args
* @return list<Node\Arg|Node\VariadicPlaceholder>
*/
private static function rewriteArgs(array $args, Scope $scope): ?array
{
$newArgs = [];
for ($i = 0; $i < count($args); $i++) {

if (
$args[$i] instanceof Node\Arg
&& $args[$i]->value instanceof CallLike
) {
$value = $args[$i]->value;
if (self::isCountFunctionCall($value, $scope)) {
if (count($value->getArgs()) !== 1) {
return null;
}

$newArgs[] = new Node\Arg($value->getArgs()[0]->value);
continue;
} elseif (self::isCountableMethodCall($value, $scope)) {
$newArgs[] = new Node\Arg($value->var);
continue;
}
}

$newArgs[] = $args[$i];
}
return $newArgs;
}

}
5 changes: 5 additions & 0 deletions tests/Rules/PHPUnit/AssertSameWithCountRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public function testRule(): void
]);
}

public function testFix(): void
{
$this->fix(__DIR__ . '/data/assert-same-count-fixable.php', __DIR__ . '/data/assert-same-count-fixable.php.fixed');
}

/**
* @return string[]
*/
Expand Down
42 changes: 42 additions & 0 deletions tests/Rules/PHPUnit/data/assert-same-count-fixable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types = 1);

namespace ExampleTestCaseFix;

use const COUNT_RECURSIVE;

class AssertSameWithCountTestCase extends \PHPUnit\Framework\TestCase
{

public function testAssertSameWithCount()
{
$this->assertSame(5, count([1, 2, 3]));
}

public function testAssertSameWithCountRecursive($x)
{
$this->assertSame(5, count([1, 2, 3, $x], COUNT_RECURSIVE));
}

public function testAssertSameWithCountMethodForCountableVariableIsNotOK()
{
$bar = new \ExampleTestCaseFix\Bar ();

$this->assertSame(5, $bar->count());
}

public function testAssertSameWithCountMethodForCountablePropertyFetchIsNotOK()
{
$foo = new \stdClass();
$foo->bar = new Bar ();

$this->assertSame(5, $foo->bar->count());
}

}

class Bar implements \Countable {
public function count(): int
{
return 1;
}
}
42 changes: 42 additions & 0 deletions tests/Rules/PHPUnit/data/assert-same-count-fixable.php.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types = 1);

namespace ExampleTestCaseFix;

use const COUNT_RECURSIVE;

class AssertSameWithCountTestCase extends \PHPUnit\Framework\TestCase
{

public function testAssertSameWithCount()
{
$this->assertCount(5, [1, 2, 3]);
}

public function testAssertSameWithCountRecursive($x)
{
$this->assertSame(5, count([1, 2, 3, $x], COUNT_RECURSIVE));
}

public function testAssertSameWithCountMethodForCountableVariableIsNotOK()
{
$bar = new \ExampleTestCaseFix\Bar ();

$this->assertCount(5, $bar);
}

public function testAssertSameWithCountMethodForCountablePropertyFetchIsNotOK()
{
$foo = new \stdClass();
$foo->bar = new Bar ();

$this->assertCount(5, $foo->bar);
}

}

class Bar implements \Countable {
public function count(): int
{
return 1;
}
}