Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addition of rule for cases better represented with assertSameSize() method #207

Open
wants to merge 3 commits into
base: 1.4.x
Choose a base branch
from
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ It also contains this strict framework-specific rules (can be enabled separately
* 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.
* If you enable [bleeding edge](https://phpstan.org/blog/what-is-bleeding-edge), check that you are not using `assertSame()` with `count($variable)` as first and second parameter. `assertSameSize()`should be used instead.

## How to document mock objects in phpDocs?

Expand Down
7 changes: 6 additions & 1 deletion rules.neon
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
rules:
- PHPStan\Rules\PHPUnit\AssertSameBooleanExpectedRule
- PHPStan\Rules\PHPUnit\AssertSameNullExpectedRule
- PHPStan\Rules\PHPUnit\AssertSameWithCountRule
- PHPStan\Rules\PHPUnit\MockMethodCallRule
- PHPStan\Rules\PHPUnit\ShouldCallParentMethodsRule

services:
-
class: PHPStan\Rules\PHPUnit\AssertSameWithCountRule
arguments:
bleedingEdge: %featureToggles.bleedingEdge%
tags:
- phpstan.rules.rule
- class: PHPStan\Rules\PHPUnit\ClassCoversExistsRule
- class: PHPStan\Rules\PHPUnit\ClassMethodCoversExistsRule
-
Expand Down
104 changes: 83 additions & 21 deletions src/Rules/PHPUnit/AssertSameWithCountRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Countable;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\NodeAbstract;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
Expand All @@ -17,6 +18,14 @@
class AssertSameWithCountRule implements Rule
{

/** @var bool */
private $bleedingEdge;

public function __construct(bool $bleedingEdge)
{
$this->bleedingEdge = $bleedingEdge;
}

public function getNodeType(): string
{
return NodeAbstract::class;
Expand All @@ -37,36 +46,89 @@ public function processNode(Node $node, Scope $scope): array

$right = $node->getArgs()[1]->value;

if (
$right instanceof Node\Expr\FuncCall
&& $right->name instanceof Node\Name
&& $right->name->toLowerString() === 'count'
) {
return [
RuleErrorBuilder::message('You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).')
->identifier('phpunit.assertCount')
->build(),
];
$rightIsCountFuncCall = $this->isCountFuncCall($right);
$rightIsCountMethodCall = $this->isCountMethodCall($right) && $this->argIsCountable($right, $scope);
if (!($rightIsCountFuncCall || $rightIsCountMethodCall)) {
return [];
}

if (
$right instanceof Node\Expr\MethodCall
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH this diff is very messy. I can't tell if the no-bleedingEdge behaviour is preserved or not. Can you split the changes in multiple commits? First extract logic into isCount... methods without changing the behaviour, and then introduce bleedingEdge-on behaviour that will be a new separate if statement. Can you please do that? Thank you.

&& $right->name instanceof Node\Identifier
&& $right->name->toLowerString() === 'count'
&& count($right->getArgs()) === 0
) {
$type = $scope->getType($right->var);
$leftIsCountFuncCall = $leftIsCountMethodCall = false;
if ($this->bleedingEdge) {
$left = $node->getArgs()[0]->value;
$leftIsCountFuncCall = $this->isCountFuncCall($left);
$leftIsCountMethodCall = $this->isCountMethodCall($left) && $this->argIsCountable($left, $scope);
}

if ((new ObjectType(Countable::class))->isSuperTypeOf($type)->yes()) {
if ($rightIsCountFuncCall) {
if ($leftIsCountFuncCall) {
return [
RuleErrorBuilder::message('You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, $variable->count()).')
->identifier('phpunit.assertCount')
RuleErrorBuilder::message('You should use assertSameSize($expected, $variable) instead of assertSame(count($expected), count($variable)).')
->identifier('phpunit.assertSameSize')
->build(),
];
} elseif ($leftIsCountMethodCall) {
return [
RuleErrorBuilder::message('You should use assertSameSize($expected, $variable) instead of assertSame($expected->count(), count($variable)).')
->identifier('phpunit.assertSameSize')
->build(),
];
}

return [
RuleErrorBuilder::message('You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).')
->identifier('phpunit.assertCount')
->build(),
];
}

if ($leftIsCountFuncCall) {
return [
RuleErrorBuilder::message('You should use assertSameSize($expected, $variable) instead of assertSame(count($expected), $variable->count()).')
->identifier('phpunit.assertSameSize')
->build(),
];
} elseif ($leftIsCountMethodCall) {
return [
RuleErrorBuilder::message('You should use assertSameSize($expected, $variable) instead of assertSame($expected->count(), $variable->count()).')
->identifier('phpunit.assertSameSize')
->build(),
];
}

return [];
return [
RuleErrorBuilder::message('You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, $variable->count()).')
->identifier('phpunit.assertCount')
->build(),
];
}

/**
* @phpstan-assert-if-true Node\Expr\FuncCall $expr
*/
private function isCountFuncCall(Node\Expr $expr): bool
{
return $expr instanceof Node\Expr\FuncCall
&& $expr->name instanceof Node\Name
&& $expr->name->toLowerString() === 'count';
}

/**
* @phpstan-assert-if-true Node\Expr\MethodCall $expr
*/
private function isCountMethodCall(Node\Expr $expr): bool
{
return $expr instanceof Node\Expr\MethodCall
&& $expr->name instanceof Node\Identifier
&& $expr->name->toLowerString() === 'count'
&& count($expr->getArgs()) === 0;
}

private function argIsCountable(MethodCall $methodCall, Scope $scope): bool
{
$type = $scope->getType($methodCall->var);
$countableType = new ObjectType(Countable::class);

return $countableType->isSuperTypeOf($type)->yes();
}

}
22 changes: 19 additions & 3 deletions tests/Rules/PHPUnit/AssertSameWithCountRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AssertSameWithCountRuleTest extends RuleTestCase

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

public function testRule(): void
Expand All @@ -23,13 +23,29 @@ public function testRule(): void
'You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).',
10,
],
[
'You should use assertSameSize($expected, $variable) instead of assertSame(count($expected), count($variable)).',
15,
],
[
'You should use assertSameSize($expected, $variable) instead of assertSame($expected->count(), count($variable)).',
23,
],
[
'You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).',
22,
35,
],
[
'You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, $variable->count()).',
30,
43,
],
[
'You should use assertSameSize($expected, $variable) instead of assertSame(count($expected), $variable->count()).',
51,
],
[
'You should use assertSameSize($expected, $variable) instead of assertSame($expected->count(), $variable->count()).',
61,
],
]);
}
Expand Down
31 changes: 31 additions & 0 deletions tests/Rules/PHPUnit/data/assert-same-count.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ public function testAssertSameWithCount()
$this->assertSame(5, count([1, 2, 3]));
}

public function testAssertSameWithCountExpectedWithCount()
{
$this->assertSame(count([10, 20]), count([1, 2, 3]));
}

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

$this->assertSame($foo->bar->count(), count([1, 2, 3]));
}

public function testAssertSameWithCountMethodIsOK()
{
$foo = new \stdClass();
Expand All @@ -30,6 +43,24 @@ public function testAssertSameWithCountMethodForCountableVariableIsNotOK()
$this->assertSame(5, $foo->bar->count());
}

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

$this->assertSame(count([10, 20]), $foo->bar->count());
}

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

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

}

class Bar implements \Countable {
Expand Down