Skip to content

Commit a0cd25e

Browse files
committed
Updated for constant types
1 parent f23194b commit a0cd25e

File tree

4 files changed

+50
-47
lines changed

4 files changed

+50
-47
lines changed

src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php

Lines changed: 8 additions & 10 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
{
@@ -34,20 +33,19 @@ public function processNode(Node $node, Scope $scope): array
3433
}
3534

3635
$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-
];
36+
if (!$leftType instanceof ConstantBooleanType) {
37+
return [];
4238
}
4339

44-
if ($leftType instanceof FalseBooleanType) {
40+
if ($leftType->getValue()) {
4541
return [
46-
'You should use assertFalse() instead of assertSame() when expecting "false"',
42+
'You should use assertTrue() instead of assertSame() when expecting "true"',
4743
];
4844
}
4945

50-
return [];
46+
return [
47+
'You should use assertFalse() instead of assertSame() when expecting "false"',
48+
];
5149
}
5250

5351
}

src/Type/PHPUnit/CreateMockDynamicReturnTypeExtension.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpParser\Node\Expr\MethodCall;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\MethodReflection;
8+
use PHPStan\Type\Constant\ConstantStringType;
89
use PHPStan\Type\ObjectType;
910
use PHPStan\Type\Type;
1011
use PHPStan\Type\TypeCombinator;
@@ -38,25 +39,12 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
3839
if (!isset($methodCall->args[$argumentIndex])) {
3940
return $methodReflection->getReturnType();
4041
}
41-
$arg = $methodCall->args[$argumentIndex]->value;
42-
if (!($arg instanceof \PhpParser\Node\Expr\ClassConstFetch)) {
42+
$argType = $scope->getType($methodCall->args[$argumentIndex]->value);
43+
if (!$argType instanceof ConstantStringType) {
4344
return $methodReflection->getReturnType();
4445
}
4546

46-
$class = $arg->class;
47-
if (!($class instanceof \PhpParser\Node\Name)) {
48-
return $methodReflection->getReturnType();
49-
}
50-
51-
$class = (string) $class;
52-
53-
if ($class === 'static') {
54-
return $methodReflection->getReturnType();
55-
}
56-
57-
if ($class === 'self') {
58-
$class = $scope->getClassReflection()->getName();
59-
}
47+
$class = $argType->getValue();
6048

6149
return TypeCombinator::intersect(
6250
new ObjectType($class),

src/Type/PHPUnit/GetMockBuilderDynamicReturnTypeExtension.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpParser\Node\Expr\MethodCall;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\MethodReflection;
8+
use PHPStan\Type\Constant\ConstantStringType;
89
use PHPStan\Type\Type;
910
use PHPStan\Type\TypeWithClassName;
1011

@@ -27,24 +28,12 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
2728
if (count($methodCall->args) === 0) {
2829
return $mockBuilderType;
2930
}
30-
$arg = $methodCall->args[0]->value;
31-
if (!($arg instanceof \PhpParser\Node\Expr\ClassConstFetch)) {
31+
$argType = $scope->getType($methodCall->args[0]->value);
32+
if (!$argType instanceof ConstantStringType) {
3233
return $mockBuilderType;
3334
}
3435

35-
$class = $arg->class;
36-
if (!($class instanceof \PhpParser\Node\Name)) {
37-
return $mockBuilderType;
38-
}
39-
40-
$class = (string) $class;
41-
if ($class === 'static') {
42-
return $mockBuilderType;
43-
}
44-
45-
if ($class === 'self') {
46-
$class = $scope->getClassReflection()->getName();
47-
}
36+
$class = $argType->getValue();
4837

4938
if (!$mockBuilderType instanceof TypeWithClassName) {
5039
throw new \PHPStan\ShouldNotHappenException();

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)