Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-mabe committed Apr 2, 2022
1 parent 4c84136 commit 63d391c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
21 changes: 13 additions & 8 deletions src/EnumDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace MabeEnumPHPStan;

use MabeEnum\Enum;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
Expand Down Expand Up @@ -56,7 +56,7 @@ public function __construct()
};
}

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

Expand All @@ -82,23 +82,28 @@ public function getTypeFromStaticMethodCall(
StaticCall $staticCall,
Scope $scope
): Type {
if ($staticCall->class instanceof Expr) {
$callClass = $staticCall->class;

// The call class is not a name
// E.g. an expression on $enumClass::getValues()
if (!$callClass instanceof Name) {
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
}
$callClass = $staticCall->class->toString();

$callClassName = $callClass->toString();

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

if ($callClass === 'self') {
$callClass = $scope->getClassReflection()->getName();
if ($callClassName === 'self') {
$callClassName = $scope->getClassReflection()->getName();
}

$methodLower = strtolower($methodReflection->getName());
return $this->objectMethods[$methodLower]($callClass);
return $this->staticMethods[$methodLower]($callClassName);
}

public function getTypeFromMethodCall(
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": 33,
"line": 40,
"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": 45,
"line": 52,
"ignorable": true
},
{
"message": "Method MabeEnum\\PHPStan\\tests\\integration\\data\\EnumGetValuesReturnType\\MyEnum::selfGetValuesFail() should return array<int, null> but returns array<int, int|string>.",
"line": 63,
"line": 70,
"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": 86,
"line": 93,
"ignorable": true
}
]
]
10 changes: 5 additions & 5 deletions tests/integration/data/EnumGetValuesReturnType-7.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
[
{
"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,
"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>.",
"line": 21,
"ignorable": true
},
{
"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,
"line": 76,
"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,
"line": 99,
"ignorable": true
}
]
]
13 changes: 10 additions & 3 deletions tests/integration/data/EnumGetValuesReturnType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,22 @@ class Example
/** @var class-string<Enum> */
protected $enumClass = Enum::class;

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

/** @return array<int, null> */
public function staticBaseExprMethodFail(): array
{
return $this->enumClass::getValues();
}

/** @return array<int, null> */
public static function baseMethodValid(): array
public static function staticBaseMethodValid(): array
{
// It returns an empty array so the key/value types doesn't matter
return Enum::getValues();
}

Expand Down

0 comments on commit 63d391c

Please sign in to comment.