Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4a3c437

Browse files
AssertSameBooleanExpectedRule, AssertSameNullExpectedRule - report only ConstFetch
* Add more tests for AssertSameBooleanExpectedRule * Report only direct true|false for assertTrue()|asertFalse() * Add more tests for AssertSameNullExpectedRule * Report only direct null for assertNull()
1 parent 2832aad commit 4a3c437

File tree

6 files changed

+117
-20
lines changed

6 files changed

+117
-20
lines changed

‎src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
namespace PHPStan\Rules\PHPUnit;
44

55
use PhpParser\Node;
6+
use PhpParser\Node\Expr\ConstFetch;
67
use PhpParser\Node\Expr\MethodCall;
78
use PhpParser\Node\Expr\StaticCall;
89
use PhpParser\NodeAbstract;
910
use PHPStan\Analyser\Scope;
1011
use PHPStan\Rules\Rule;
11-
use PHPStan\Type\Constant\ConstantBooleanType;
1212
use function count;
1313
use function strtolower;
1414

@@ -39,20 +39,24 @@ public function processNode(Node $node, Scope $scope): array
3939
return [];
4040
}
4141

42-
$leftType = $scope->getType($node->getArgs()[0]->value);
43-
if (!$leftType instanceof ConstantBooleanType) {
42+
$expectedArgumentValue = $node->getArgs()[0]->value;
43+
if (!($expectedArgumentValue instanceof ConstFetch)) {
4444
return [];
4545
}
4646

47-
if ($leftType->getValue()) {
47+
if ($expectedArgumentValue->name->toLowerString() === 'true') {
4848
return [
4949
'You should use assertTrue() instead of assertSame() when expecting "true"',
5050
];
5151
}
5252

53-
return [
54-
'You should use assertFalse() instead of assertSame() when expecting "false"',
55-
];
53+
if ($expectedArgumentValue->name->toLowerString() === 'false') {
54+
return [
55+
'You should use assertFalse() instead of assertSame() when expecting "false"',
56+
];
57+
}
58+
59+
return [];
5660
}
5761

5862
}

‎src/Rules/PHPUnit/AssertSameNullExpectedRule.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
namespace PHPStan\Rules\PHPUnit;
44

55
use PhpParser\Node;
6+
use PhpParser\Node\Expr\ConstFetch;
67
use PhpParser\Node\Expr\MethodCall;
78
use PhpParser\Node\Expr\StaticCall;
89
use PhpParser\NodeAbstract;
910
use PHPStan\Analyser\Scope;
1011
use PHPStan\Rules\Rule;
11-
use PHPStan\Type\NullType;
1212
use function count;
1313
use function strtolower;
1414

@@ -39,9 +39,12 @@ public function processNode(Node $node, Scope $scope): array
3939
return [];
4040
}
4141

42-
$leftType = $scope->getType($node->getArgs()[0]->value);
42+
$expectedArgumentValue = $node->getArgs()[0]->value;
43+
if (!($expectedArgumentValue instanceof ConstFetch)) {
44+
return [];
45+
}
4346

44-
if ($leftTypeinstanceof NullType) {
47+
if ($expectedArgumentValue->name->toLowerString() === 'null') {
4548
return [
4649
'You should use assertNull() instead of assertSame(null, $actual).',
4750
];

‎tests/Rules/PHPUnit/AssertSameBooleanExpectedRuleTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ public function testRule(): void
2929
],
3030
[
3131
'You should use assertTrue() instead of assertSame() when expecting "true"',
32-
14,
32+
26,
3333
],
3434
[
35-
'You should use assertFalse() instead of assertSame() when expecting "false"',
36-
17,
35+
'You should use assertTrue() instead of assertSame() when expecting "true"',
36+
74,
3737
],
3838
[
39-
'You should use assertTrue() instead of assertSame() when expecting "true"',
40-
26,
39+
'You should use assertFalse() instead of assertSame() when expecting "false"',
40+
75,
4141
],
4242
]);
4343
}

‎tests/Rules/PHPUnit/AssertSameNullExpectedRuleTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ public function testRule(): void
2525
],
2626
[
2727
'You should use assertNull() instead of assertSame(null, $actual).',
28-
13,
28+
24,
2929
],
3030
[
3131
'You should use assertNull() instead of assertSame(null, $actual).',
32-
24,
32+
60,
3333
],
3434
]);
3535
}

‎tests/Rules/PHPUnit/data/assert-same-boolean-expected.php

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ public function testAssertSameWithBooleanAsExpected()
1111
$this->assertSame(false, 'a');
1212

1313
$truish = true;
14-
$this->assertSame($truish, true);
14+
$this->assertSame($truish, true);// using variable is OK
1515

1616
$falsish = false;
17-
$this->assertSame($falsish, false);
17+
$this->assertSame($falsish, false);// using variable is OK
1818

1919
/** @var bool $a */
2020
$a = null;
@@ -26,4 +26,56 @@ public function testAssertSameIsDetectedWithDirectAssertAccess()
2626
\PHPUnit\Framework\Assert::assertSame(true, 'foo');
2727
}
2828

29+
public function testConstants(): void
30+
{
31+
\PHPUnit\Framework\Assert::assertSame(PHPSTAN_PHPUNIT_TRUE, 'foo');
32+
\PHPUnit\Framework\Assert::assertSame(PHPSTAN_PHPUNIT_FALSE, 'foo');
33+
}
34+
35+
private const TRUE = true;
36+
private const FALSE = false;
37+
38+
public function testClassConstants(): void
39+
{
40+
\PHPUnit\Framework\Assert::assertSame(self::TRUE, 'foo');
41+
\PHPUnit\Framework\Assert::assertSame(self::FALSE, 'foo');
42+
}
43+
44+
public function returnBool(): bool
45+
{
46+
return true;
47+
}
48+
49+
/**
50+
* @return true
51+
*/
52+
public function returnTrue(): bool
53+
{
54+
return true;
55+
}
56+
57+
/**
58+
* @return false
59+
*/
60+
public function returnFalse(): bool
61+
{
62+
return false;
63+
}
64+
65+
public function testMethodCalls(): void
66+
{
67+
\PHPUnit\Framework\Assert::assertSame($this->returnTrue(), 'foo');
68+
\PHPUnit\Framework\Assert::assertSame($this->returnFalse(), 'foo');
69+
\PHPUnit\Framework\Assert::assertSame($this->returnBool(), 'foo');
70+
}
71+
72+
public function testNonLowercase(): void
73+
{
74+
\PHPUnit\Framework\Assert::assertSame(True, 'foo');
75+
\PHPUnit\Framework\Assert::assertSame(False, 'foo');
76+
}
77+
2978
}
79+
80+
const PHPSTAN_PHPUNIT_TRUE = true;
81+
const PHPSTAN_PHPUNIT_FALSE = false;

‎tests/Rules/PHPUnit/data/assert-same-null-expected.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public function testAssertSameWithNullAsExpected()
1010
$this->assertSame(null, 'a');
1111

1212
$a = null;
13-
$this->assertSame($a, 'b');
13+
$this->assertSame($a, 'b');// using variable is OK
1414

1515
$this->assertSame('a', 'b'); // OK
1616

@@ -24,4 +24,42 @@ public function testAssertSameIsDetectedWithDirectAssertAccess()
2424
\PHPUnit\Framework\Assert::assertSame(null, 'foo');
2525
}
2626

27+
public function testConstant(): void
28+
{
29+
\PHPUnit\Framework\Assert::assertSame(PHPSTAN_PHPUNIT_NULL, 'foo');
30+
}
31+
32+
private const NULL = null;
33+
34+
public function testClassConstant(): void
35+
{
36+
\PHPUnit\Framework\Assert::assertSame(self::NULL, 'foo');
37+
}
38+
39+
public function returnNullable(): ?string
40+
{
41+
42+
}
43+
44+
/**
45+
* @return null
46+
*/
47+
public function returnNull()
48+
{
49+
return null;
50+
}
51+
52+
public function testMethodCalls(): void
53+
{
54+
\PHPUnit\Framework\Assert::assertSame($this->returnNull(), 'foo');
55+
\PHPUnit\Framework\Assert::assertSame($this->returnNullable(), 'foo');
56+
}
57+
58+
public function testNonLowercase(): void
59+
{
60+
\PHPUnit\Framework\Assert::assertSame(Null, 'foo');
61+
}
62+
2763
}
64+
65+
const PHPSTAN_PHPUNIT_NULL = null;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /