Skip to content

Commit f9c16dd

Browse files
mhujerondrejmirtes
authored andcommitted
AssertSameBooleanExpectedRule
1 parent 88d4efc commit f9c16dd

File tree

5 files changed

+121
-1
lines changed

5 files changed

+121
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ It also contains this framework-specific rule (can be enabled separately):
1818

1919
* Check that both values passed to `assertSame()` method are of the same type.
2020

21-
It also contains this strict framework-specific rule (can be enabled separately):
21+
It also contains this strict framework-specific rules (can be enabled separately):
2222

23+
* Check that you are not using `assertSame()` with `true` as expected value. `assertTrue()` should be used instead.
24+
* Check that you are not using `assertSame()` with `false` as expected value. `assertFalse()` should be used instead.
2325
* Check that you are not using `assertSame()` with `null` as expected value. `assertNull()` should be used instead.
2426

2527
## How to document mock objects in phpDocs?
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\PHPUnit;
4+
5+
use PhpParser\Node;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Type\FalseBooleanType;
8+
use PHPStan\Type\TrueBooleanType;
9+
10+
class AssertSameBooleanExpectedRule implements \PHPStan\Rules\Rule
11+
{
12+
13+
public function getNodeType(): string
14+
{
15+
return \PhpParser\NodeAbstract::class;
16+
}
17+
18+
/**
19+
* @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall $node
20+
* @param \PHPStan\Analyser\Scope $scope
21+
* @return string[] errors
22+
*/
23+
public function processNode(Node $node, Scope $scope): array
24+
{
25+
if (!AssertRuleHelper::isMethodOrStaticCallOnTestCase($node, $scope)) {
26+
return [];
27+
}
28+
29+
if (count($node->args) < 2) {
30+
return [];
31+
}
32+
if (!is_string($node->name) || strtolower($node->name) !== 'assertsame') {
33+
return [];
34+
}
35+
36+
$leftType = $scope->getType($node->args[0]->value);
37+
38+
if ($leftType instanceof TrueBooleanType) {
39+
return [
40+
'You should use assertTrue() instead of assertSame() when expecting "true"',
41+
];
42+
}
43+
44+
if ($leftType instanceof FalseBooleanType) {
45+
return [
46+
'You should use assertFalse() instead of assertSame() when expecting "false"',
47+
];
48+
}
49+
50+
return [];
51+
}
52+
53+
}

strictRules.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
services:
2+
-
3+
class: PHPStan\Rules\PHPUnit\AssertSameBooleanExpectedRule
4+
tags:
5+
- phpstan.rules.rule
26
-
37
class: PHPStan\Rules\PHPUnit\AssertSameNullExpectedRule
48
tags:
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\PHPUnit;
4+
5+
use PHPStan\Rules\Rule;
6+
7+
class AssertSameBooleanExpectedRuleTest extends \PHPStan\Testing\RuleTestCase
8+
{
9+
10+
protected function getRule(): Rule
11+
{
12+
return new AssertSameBooleanExpectedRule();
13+
}
14+
15+
public function testRule()
16+
{
17+
$this->analyse([__DIR__ . '/data/assert-same-boolean-expected.php'], [
18+
[
19+
'You should use assertTrue() instead of assertSame() when expecting "true"',
20+
10,
21+
],
22+
[
23+
'You should use assertFalse() instead of assertSame() when expecting "false"',
24+
11,
25+
],
26+
[
27+
'You should use assertTrue() instead of assertSame() when expecting "true"',
28+
14,
29+
],
30+
[
31+
'You should use assertFalse() instead of assertSame() when expecting "false"',
32+
17,
33+
],
34+
]);
35+
}
36+
37+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ExampleTestCase;
4+
5+
class AssertSameBooleanExpectedTestCase extends \PHPUnit\Framework\TestCase
6+
{
7+
8+
public function testAssertSameWithBooleanAsExpected()
9+
{
10+
$this->assertSame(true, 'a');
11+
$this->assertSame(false, 'a');
12+
13+
$truish = true;
14+
$this->assertSame($truish, true);
15+
16+
$falsish = false;
17+
$this->assertSame($falsish, false);
18+
19+
/** @var bool $a */
20+
$a = null;
21+
$this->assertSame($a, 'b'); // OK
22+
}
23+
24+
}

0 commit comments

Comments
 (0)