-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f9c16dd
commit 669990e
Showing
5 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 []; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
], | ||
]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |