Skip to content

Commit 938e4ea

Browse files
committed
Support string arguments
1 parent b26d14d commit 938e4ea

File tree

3 files changed

+54
-25
lines changed

3 files changed

+54
-25
lines changed

src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
7-
use PHPStan\Type\FalseBooleanType;
8-
use PHPStan\Type\TrueBooleanType;
7+
use PHPStan\Type\Constant\ConstantBooleanType;
98

109
class AssertSameBooleanExpectedRule implements \PHPStan\Rules\Rule
1110
{
@@ -35,16 +34,16 @@ public function processNode(Node $node, Scope $scope): array
3534

3635
$leftType = $scope->getType($node->args[0]->value);
3736

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-
];
37+
if ($leftType instanceof ConstantBooleanType) {
38+
if ($leftType->getValue() === true) {
39+
return [
40+
'You should use assertTrue() instead of assertSame() when expecting "true"',
41+
];
42+
} else {
43+
return [
44+
'You should use assertFalse() instead of assertSame() when expecting "false"',
45+
];
46+
}
4847
}
4948

5049
return [];

src/Type/PHPUnit/CreateMockDynamicReturnTypeExtension.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,19 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
3939
return $methodReflection->getReturnType();
4040
}
4141
$arg = $methodCall->args[$argumentIndex]->value;
42-
if (!($arg instanceof \PhpParser\Node\Expr\ClassConstFetch)) {
43-
return $methodReflection->getReturnType();
44-
}
42+
if ($arg instanceof \PhpParser\Node\Expr\ClassConstFetch) {
43+
$class = $arg->class;
44+
if (!($class instanceof \PhpParser\Node\Name)) {
45+
return $methodReflection->getReturnType();
46+
}
4547

46-
$class = $arg->class;
47-
if (!($class instanceof \PhpParser\Node\Name)) {
48+
$class = (string) $class;
49+
} elseif ($arg instanceof \PhpParser\Node\Scalar\String_) {
50+
$class = $arg->value;
51+
} else {
4852
return $methodReflection->getReturnType();
4953
}
5054

51-
$class = (string) $class;
52-
5355
if ($class === 'static') {
5456
return $methodReflection->getReturnType();
5557
}

tests/Rules/PHPUnit/AssertSameDifferentTypesRuleTest.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,69 @@ public function testRule(): void
1616
{
1717
$this->analyse([__DIR__ . '/data/assert-same.php'], [
1818
[
19-
'Call to assertSame() with different types string and int will always result in test failure.',
19+
'Call to assertSame() with different types string and int(1) will always result in test failure.',
2020
10,
2121
],
2222
[
2323
'Call to assertSame() with different types string and stdClass will always result in test failure.',
2424
11,
2525
],
2626
[
27-
'Call to assertSame() with different types int and string will always result in test failure.',
27+
'Call to assertSame() with different types int(1) and string will always result in test failure.',
2828
12,
2929
],
3030
[
3131
'Call to assertSame() with different types string and int will always result in test failure.',
3232
13,
3333
],
3434
[
35-
'Call to assertSame() with different types array<int, string> and array<int, int> will always result in test failure.',
35+
'Call to assertSame() with different types array<int(0)|int(1), string> and array<int(0)|int(1), int(1)|int(2)> will always result in test failure.',
3636
14,
3737
],
3838
[
39-
'Call to assertSame() with different types string and int will always result in test failure.',
39+
'Call to assertSame() with different types string and int(2) will always result in test failure.',
4040
16,
4141
],
4242
[
43-
'Call to assertSame() with different types string and int will always result in test failure.',
43+
'Call to assertSame() with different types string and int(2) will always result in test failure.',
4444
17,
4545
],
4646
[
47-
'Call to assertSame() with different types string and int will always result in test failure.',
47+
'Call to assertSame() with different types string and int(2) will always result in test failure.',
4848
18,
4949
],
5050
[
5151
'Call to assertSame() with different types array<string> and array<int> will always result in test failure.',
5252
39,
5353
],
54+
[
55+
'Call to assertSame() with different types array<int(0), string> and array<int(0)|int(1), string> will always result in test failure.',
56+
45,
57+
],
58+
[
59+
'Call to assertSame() with different types string and string will always result in test failure.',
60+
47,
61+
],
62+
[
63+
'Call to assertSame() with different types array<int(0), string> and array<int(0)|int(1), int(1)|string> will always result in test failure.',
64+
51,
65+
],
66+
[
67+
'Call to assertSame() with different types array<int(0)|int(1)|int(2), float(3.000000)|int(2)|string> and array<int(0)|int(1), int(1)|string> will always result in test failure.',
68+
52,
69+
],
70+
[
71+
'Call to assertSame() with different types int(1) and int(2) will always result in test failure.',
72+
53,
73+
],
74+
[
75+
'Call to assertSame() with different types int(1) and int(2) will always result in test failure.',
76+
54,
77+
],
78+
[
79+
'Call to assertSame() with different types int(1) and int(2) will always result in test failure.',
80+
55,
81+
],
5482
]);
5583
}
5684

0 commit comments

Comments
 (0)