Skip to content

Commit cbc048b

Browse files
committed
Update for latest PHPStan master
1 parent b26d14d commit cbc048b

File tree

4 files changed

+52
-48
lines changed

4 files changed

+52
-48
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()) {
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: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPStan\Type\ObjectType;
99
use PHPStan\Type\Type;
1010
use PHPStan\Type\TypeCombinator;
11+
use PHPStan\Type\TypeWithClassName;
1112

1213
class CreateMockDynamicReturnTypeExtension implements \PHPStan\Type\DynamicMethodReturnTypeExtension
1314
{
@@ -38,25 +39,13 @@ 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)) {
43-
return $methodReflection->getReturnType();
44-
}
4542

46-
$class = $arg->class;
47-
if (!($class instanceof \PhpParser\Node\Name)) {
43+
$argType = $scope->getType($methodCall->args[$argumentIndex]->value);
44+
if (!($argType instanceof TypeWithClassName)) {
4845
return $methodReflection->getReturnType();
4946
}
5047

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-
}
48+
$class = $argType->getClassName();
6049

6150
return TypeCombinator::intersect(
6251
new ObjectType($class),

src/Type/PHPUnit/GetMockBuilderDynamicReturnTypeExtension.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,12 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
2727
if (count($methodCall->args) === 0) {
2828
return $mockBuilderType;
2929
}
30-
$arg = $methodCall->args[0]->value;
31-
if (!($arg instanceof \PhpParser\Node\Expr\ClassConstFetch)) {
30+
$argType = $scope->getType($methodCall->args[0]->value);
31+
if (!($argType instanceof TypeWithClassName)) {
3232
return $mockBuilderType;
3333
}
3434

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-
}
35+
$class = $argType->getClassName();
4836

4937
if (!$mockBuilderType instanceof TypeWithClassName) {
5038
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)