Skip to content

Commit 669990e

Browse files
mhujerondrejmirtes
authored andcommitted
AssertSameWithCountRule
1 parent f9c16dd commit 669990e

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ It also contains this strict framework-specific rules (can be enabled separately
2323
* Check that you are not using `assertSame()` with `true` as expected value. `assertTrue()` should be used instead.
2424
* Check that you are not using `assertSame()` with `false` as expected value. `assertFalse()` should be used instead.
2525
* Check that you are not using `assertSame()` with `null` as expected value. `assertNull()` should be used instead.
26+
* Check that you are not using `assertSame()` with `count($variable)` as second parameter. `assertCount($variable)` should be used instead.
2627

2728
## How to document mock objects in phpDocs?
2829

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\PHPUnit;
4+
5+
use PhpParser\Node;
6+
use PHPStan\Analyser\Scope;
7+
8+
class AssertSameWithCountRule implements \PHPStan\Rules\Rule
9+
{
10+
11+
public function getNodeType(): string
12+
{
13+
return \PhpParser\NodeAbstract::class;
14+
}
15+
16+
/**
17+
* @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall $node
18+
* @param \PHPStan\Analyser\Scope $scope
19+
* @return string[] errors
20+
*/
21+
public function processNode(Node $node, Scope $scope): array
22+
{
23+
if (!AssertRuleHelper::isMethodOrStaticCallOnTestCase($node, $scope)) {
24+
return [];
25+
}
26+
27+
if (count($node->args) < 2) {
28+
return [];
29+
}
30+
if (!is_string($node->name) || strtolower($node->name) !== 'assertsame') {
31+
return [];
32+
}
33+
34+
$right = $node->args[1]->value;
35+
36+
if (
37+
$right instanceof Node\Expr\FuncCall
38+
&& $right->name instanceof Node\Name
39+
&& strtolower($right->name->toString()) === 'count'
40+
) {
41+
return [
42+
'You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).',
43+
];
44+
}
45+
46+
return [];
47+
}
48+
49+
}

strictRules.neon

+4
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ services:
77
class: PHPStan\Rules\PHPUnit\AssertSameNullExpectedRule
88
tags:
99
- phpstan.rules.rule
10+
-
11+
class: PHPStan\Rules\PHPUnit\AssertSameWithCountRule
12+
tags:
13+
- phpstan.rules.rule
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\PHPUnit;
4+
5+
use PHPStan\Rules\Rule;
6+
7+
class AssertSameWithCountRuleTest extends \PHPStan\Testing\RuleTestCase
8+
{
9+
10+
protected function getRule(): Rule
11+
{
12+
return new AssertSameWithCountRule();
13+
}
14+
15+
public function testRule()
16+
{
17+
$this->analyse([__DIR__ . '/data/assert-same-count.php'], [
18+
[
19+
'You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).',
20+
10,
21+
],
22+
]);
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ExampleTestCase;
4+
5+
class AssertSameWithCountTestCase extends \PHPUnit\Framework\TestCase
6+
{
7+
8+
public function testAssertSameWithCount()
9+
{
10+
$this->assertSame(5, count([1, 2, 3]));
11+
}
12+
13+
public function testAssertSameWithCountMethodIsOK()
14+
{
15+
$this->assertSame(5, $this->count()); // OK
16+
}
17+
18+
}

0 commit comments

Comments
 (0)