Skip to content

Commit 63d391c

Browse files
committed
fixup
1 parent 4c84136 commit 63d391c

File tree

4 files changed

+33
-21
lines changed

4 files changed

+33
-21
lines changed

src/EnumDynamicReturnTypeExtension.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace MabeEnumPHPStan;
44

55
use MabeEnum\Enum;
6-
use PhpParser\Node\Expr;
76
use PhpParser\Node\Expr\MethodCall;
87
use PhpParser\Node\Expr\StaticCall;
8+
use PhpParser\Node\Name;
99
use PHPStan\Analyser\Scope;
1010
use PHPStan\Reflection\MethodReflection;
1111
use PHPStan\Reflection\ParametersAcceptorSelector;
@@ -56,7 +56,7 @@ public function __construct()
5656
};
5757
}
5858

59-
// static methods cann be called like object methods
59+
// static methods can be called like object methods
6060
$this->objectMethods = array_merge($this->objectMethods, $this->staticMethods);
6161
}
6262

@@ -82,23 +82,28 @@ public function getTypeFromStaticMethodCall(
8282
StaticCall $staticCall,
8383
Scope $scope
8484
): Type {
85-
if ($staticCall->class instanceof Expr) {
85+
$callClass = $staticCall->class;
86+
87+
// The call class is not a name
88+
// E.g. an expression on $enumClass::getValues()
89+
if (!$callClass instanceof Name) {
8690
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
8791
}
88-
$callClass = $staticCall->class->toString();
92+
93+
$callClassName = $callClass->toString();
8994

9095
// Can't detect possible types on static::*()
9196
// as it depends on defined enumerators of unknown inherited classes
92-
if ($callClass === 'static') {
97+
if ($callClassName === 'static') {
9398
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
9499
}
95100

96-
if ($callClass === 'self') {
97-
$callClass = $scope->getClassReflection()->getName();
101+
if ($callClassName === 'self') {
102+
$callClassName = $scope->getClassReflection()->getName();
98103
}
99104

100105
$methodLower = strtolower($methodReflection->getName());
101-
return $this->objectMethods[$methodLower]($callClass);
106+
return $this->staticMethods[$methodLower]($callClassName);
102107
}
103108

104109
public function getTypeFromMethodCall(
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
[
22
{
33
"message": "Method MabeEnum\\PHPStan\\tests\\integration\\data\\EnumGetValuesReturnType\\Example::staticMethodFail() should return array<int, null> but returns array<int, float|int|string>.",
4-
"line": 33,
4+
"line": 40,
55
"ignorable": true
66
},
77
{
88
"message": "Method MabeEnum\\PHPStan\\tests\\integration\\data\\EnumGetValuesReturnType\\Example::objectMethodFail() should return array<int, null> but returns array<int, float|int|string>.",
9-
"line": 45,
9+
"line": 52,
1010
"ignorable": true
1111
},
1212
{
1313
"message": "Method MabeEnum\\PHPStan\\tests\\integration\\data\\EnumGetValuesReturnType\\MyEnum::selfGetValuesFail() should return array<int, null> but returns array<int, int|string>.",
14-
"line": 63,
14+
"line": 70,
1515
"ignorable": true
1616
},
1717
{
1818
"message": "Method MabeEnum\\PHPStan\\tests\\integration\\data\\EnumGetValuesReturnType\\MyInheritedEnum::inheritSelfGetValuesFail() should return array<int, null> but returns array<int, float|int|string>.",
19-
"line": 86,
19+
"line": 93,
2020
"ignorable": true
2121
}
22-
]
22+
]
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
[
22
{
3-
"message": "Method MabeEnum\\PHPStan\\tests\\integration\\data\\EnumGetValuesReturnType\\Example::getDynamicValues() should return array<int, float|int|string> but returns array<int, array<int|string, mixed>|bool|float|int|string|null>.",
4-
"line": 15,
3+
"message": "Method MabeEnum\\PHPStan\\tests\\integration\\data\\EnumGetValuesReturnType\\Example::staticBaseExprMethodFail() should return array<int, null> but returns array<int, array<int|string, mixed>|bool|float|int|string|null>.",
4+
"line": 21,
55
"ignorable": true
66
},
77
{
88
"message": "Method MabeEnum\\PHPStan\\tests\\integration\\data\\EnumGetValuesReturnType\\MyEnum::staticGetValuesFail() should return array<int, null> but returns array<int, array<int|string, mixed>|bool|float|int|string|null>.",
9-
"line": 69,
9+
"line": 76,
1010
"ignorable": true
1111
},
1212
{
1313
"message": "Method MabeEnum\\PHPStan\\tests\\integration\\data\\EnumGetValuesReturnType\\MyInheritedEnum::inheritStaticGetValuesFail() should return array<int, null> but returns array<int, array<int|string, mixed>|bool|float|int|string|null>.",
14-
"line": 92,
14+
"line": 99,
1515
"ignorable": true
1616
}
17-
]
17+
]

tests/integration/data/EnumGetValuesReturnType.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,22 @@ class Example
99
/** @var class-string<Enum> */
1010
protected $enumClass = Enum::class;
1111

12-
/** @return array<int, float|int|string> */
13-
public function getDynamicValues(): array
12+
/** @return array<int, bool|float|int|string|null|array<int|string, mixed>> */
13+
public function staticBaseExprMethodValid(): array
14+
{
15+
return $this->enumClass::getValues();
16+
}
17+
18+
/** @return array<int, null> */
19+
public function staticBaseExprMethodFail(): array
1420
{
1521
return $this->enumClass::getValues();
1622
}
1723

1824
/** @return array<int, null> */
19-
public static function baseMethodValid(): array
25+
public static function staticBaseMethodValid(): array
2026
{
27+
// It returns an empty array so the key/value types doesn't matter
2128
return Enum::getValues();
2229
}
2330

0 commit comments

Comments
 (0)