Skip to content

Commit

Permalink
AssertSameWithCountRule
Browse files Browse the repository at this point in the history
  • Loading branch information
mhujer authored and ondrejmirtes committed Dec 27, 2017
1 parent f9c16dd commit 669990e
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ It also contains this strict framework-specific rules (can be enabled separately
* Check that you are not using `assertSame()` with `true` as expected value. `assertTrue()` should be used instead.
* Check that you are not using `assertSame()` with `false` as expected value. `assertFalse()` should be used instead.
* Check that you are not using `assertSame()` with `null` as expected value. `assertNull()` should be used instead.
* Check that you are not using `assertSame()` with `count($variable)` as second parameter. `assertCount($variable)` should be used instead.

## How to document mock objects in phpDocs?

Expand Down
49 changes: 49 additions & 0 deletions src/Rules/PHPUnit/AssertSameWithCountRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\PHPUnit;

use PhpParser\Node;
use PHPStan\Analyser\Scope;

class AssertSameWithCountRule implements \PHPStan\Rules\Rule
{

public function getNodeType(): string
{
return \PhpParser\NodeAbstract::class;
}

/**
* @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall $node
* @param \PHPStan\Analyser\Scope $scope
* @return string[] errors
*/
public function processNode(Node $node, Scope $scope): array
{
if (!AssertRuleHelper::isMethodOrStaticCallOnTestCase($node, $scope)) {
return [];
}

if (count($node->args) < 2) {
return [];
}
if (!is_string($node->name) || strtolower($node->name) !== 'assertsame') {
return [];
}

$right = $node->args[1]->value;

if (
$right instanceof Node\Expr\FuncCall
&& $right->name instanceof Node\Name
&& strtolower($right->name->toString()) === 'count'
) {
return [
'You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).',
];
}

return [];
}

}
4 changes: 4 additions & 0 deletions strictRules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ services:
class: PHPStan\Rules\PHPUnit\AssertSameNullExpectedRule
tags:
- phpstan.rules.rule
-
class: PHPStan\Rules\PHPUnit\AssertSameWithCountRule
tags:
- phpstan.rules.rule
25 changes: 25 additions & 0 deletions tests/Rules/PHPUnit/AssertSameWithCountRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\PHPUnit;

use PHPStan\Rules\Rule;

class AssertSameWithCountRuleTest extends \PHPStan\Testing\RuleTestCase
{

protected function getRule(): Rule
{
return new AssertSameWithCountRule();
}

public function testRule()
{
$this->analyse([__DIR__ . '/data/assert-same-count.php'], [
[
'You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).',
10,
],
]);
}

}
18 changes: 18 additions & 0 deletions tests/Rules/PHPUnit/data/assert-same-count.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types = 1);

namespace ExampleTestCase;

class AssertSameWithCountTestCase extends \PHPUnit\Framework\TestCase
{

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

public function testAssertSameWithCountMethodIsOK()
{
$this->assertSame(5, $this->count()); // OK
}

}

0 comments on commit 669990e

Please sign in to comment.