Skip to content

MabeEnumPhpstan\EnumDynamicReturnTypeExtension::getTypeFromStaticMethodCall() cant handle static calls on variables #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/EnumDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MabeEnumPHPStan;

use MabeEnum\Enum;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\Scope;
Expand Down Expand Up @@ -81,6 +82,9 @@ public function getTypeFromStaticMethodCall(
StaticCall $staticCall,
Scope $scope
): Type {
if ($staticCall->class instanceof Expr) {
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
}
$callClass = $staticCall->class->toString();

// Can't detect possible types on static::*()
Expand Down
10 changes: 5 additions & 5 deletions tests/integration/data/EnumGetValuesReturnType-3.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
[
{
"message": "Method MabeEnum\\PHPStan\\tests\\integration\\data\\EnumGetValuesReturnType\\Example::staticMethodFail() should return array<int, null> but returns array<int, float|int|string>.",
"line": 24,
"line": 33,
"ignorable": true
},
{
"message": "Method MabeEnum\\PHPStan\\tests\\integration\\data\\EnumGetValuesReturnType\\Example::objectMethodFail() should return array<int, null> but returns array<int, float|int|string>.",
"line": 36,
"line": 45,
"ignorable": true
},
{
"message": "Method MabeEnum\\PHPStan\\tests\\integration\\data\\EnumGetValuesReturnType\\MyEnum::selfGetValuesFail() should return array<int, null> but returns array<int, int|string>.",
"line": 54,
"line": 63,
"ignorable": true
},
{
"message": "Method MabeEnum\\PHPStan\\tests\\integration\\data\\EnumGetValuesReturnType\\MyInheritedEnum::inheritSelfGetValuesFail() should return array<int, null> but returns array<int, float|int|string>.",
"line": 77,
"line": 86,
"ignorable": true
}
]
]
15 changes: 10 additions & 5 deletions tests/integration/data/EnumGetValuesReturnType-7.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
[
{
"message": "Method MabeEnum\\PHPStan\\tests\\integration\\data\\EnumGetValuesReturnType\\MyEnum::staticGetValuesFail() should return array<int, null> but returns array<int, array|bool|float|int|string|null>.",
"line": 60,
"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>.",
"line": 15,
"ignorable": true
},
{
"message": "Method MabeEnum\\PHPStan\\tests\\integration\\data\\EnumGetValuesReturnType\\MyInheritedEnum::inheritStaticGetValuesFail() should return array<int, null> but returns array<int, array|bool|float|int|string|null>.",
"line": 83,
"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>.",
"line": 69,
"ignorable": true
},
{
"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>.",
"line": 92,
"ignorable": true
}
]
]
9 changes: 9 additions & 0 deletions tests/integration/data/EnumGetValuesReturnType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@

class Example
{
/** @var class-string<Enum> */
protected $enumClass = Enum::class;

/** @return array<int, float|int|string> */
public function getDynamicValues(): array
{
return $this->enumClass::getValues();
}

/** @return array<int, null> */
public static function baseMethodValid(): array
{
Expand Down