-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from marc-mabe/get_value_return_extension
refactoring & fix getValues return type
- Loading branch information
Showing
23 changed files
with
680 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MabeEnumPHPStan; | ||
|
||
use MabeEnum\Enum; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Reflection\ParametersAcceptorSelector; | ||
use PHPStan\ShouldNotHappenException; | ||
use PHPStan\Type\ArrayType; | ||
use PHPStan\Type\Constant\ConstantArrayType; | ||
use PHPStan\Type\ConstantTypeHelper; | ||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
|
||
class EnumDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
/** | ||
* Buffer of all types of enumeration values | ||
* @phpstan-var array<class-string<Enum>, Type[]> | ||
*/ | ||
private $enumValueTypesBuffer = []; | ||
|
||
/** | ||
* Buffer of all types of enumeration ordinals | ||
* @phpstan-var array<class-string<Enum>, Type[]> | ||
*/ | ||
private $enumOrdinalTypesBuffer = []; | ||
|
||
public function getClass(): string | ||
{ | ||
return Enum::class; | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
$supportedMethods = ['getvalue']; | ||
if (method_exists(Enum::class, 'getValues')) { | ||
array_push($supportedMethods, 'getvalues'); | ||
} | ||
|
||
return in_array(strtolower($methodReflection->getName()), $supportedMethods, true); | ||
} | ||
|
||
public function getTypeFromMethodCall( | ||
MethodReflection $methodReflection, | ||
MethodCall $methodCall, | ||
Scope $scope | ||
): Type { | ||
$callType = $scope->getType($methodCall->var); | ||
$callClasses = $callType->getReferencedClasses(); | ||
$methodName = strtolower($methodReflection->getName()); | ||
$returnTypes = []; | ||
foreach ($callClasses as $callClass) { | ||
if (!is_subclass_of($callClass, Enum::class, true)) { | ||
$returnTypes[] = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants()) | ||
->getReturnType(); | ||
} else { | ||
switch ($methodName) { | ||
case 'getvalue': | ||
$returnTypes[] = $this->enumGetValueReturnType($callClass); | ||
break; | ||
case 'getvalues': | ||
$returnTypes[] = $this->enumGetValuesReturnType($callClass); | ||
break; | ||
default: | ||
throw new ShouldNotHappenException("Method {$methodName} is not supported"); | ||
} | ||
} | ||
} | ||
|
||
return TypeCombinator::union(...$returnTypes); | ||
} | ||
|
||
/** | ||
* Returns types of all values of an enumeration | ||
* @phpstan-param class-string<Enum> $enumeration | ||
* @return Type[] | ||
*/ | ||
private function enumValueTypes(string $enumeration): array | ||
{ | ||
if (isset($this->enumValueTypesBuffer[$enumeration])) { | ||
return $this->enumValueTypesBuffer[$enumeration]; | ||
} | ||
|
||
$values = array_values($enumeration::getConstants()); | ||
$types = array_map([ConstantTypeHelper::class, 'getTypeFromValue'], $values); | ||
|
||
return $this->enumValueTypesBuffer[$enumeration] = $types; | ||
} | ||
|
||
/** | ||
* Returns types of all ordinals of an enumeration | ||
* @phpstan-param class-string<Enum> $enumeration | ||
* @return Type[] | ||
*/ | ||
private function enumOrdinalTypes(string $enumeration): array | ||
{ | ||
if (isset($this->enumOrdinalTypesBuffer[$enumeration])) { | ||
return $this->enumOrdinalTypesBuffer[$enumeration]; | ||
} | ||
|
||
$ordinals = array_keys($enumeration::getOrdinals()); | ||
$types = array_map([ConstantTypeHelper::class, 'getTypeFromValue'], $ordinals); | ||
|
||
return $this->enumOrdinalTypesBuffer[$enumeration] = $types; | ||
} | ||
|
||
/** | ||
* Returns return type of Enum::getValue() | ||
* @phpstan-param class-string<Enum> $enumeration | ||
*/ | ||
private function enumGetValueReturnType(string $enumeration): Type | ||
{ | ||
return TypeCombinator::union(...$this->enumValueTypes($enumeration)); | ||
} | ||
|
||
/** | ||
* Returns return type of Enum::getValues() | ||
* @phpstan-param class-string<Enum> $enumeration | ||
*/ | ||
private function enumGetValuesReturnType(string $enumeration): ArrayType | ||
{ | ||
$keyTypes = $this->enumOrdinalTypes($enumeration); | ||
$valueTypes = $this->enumValueTypes($enumeration); | ||
return new ConstantArrayType($keyTypes, $valueTypes, count($keyTypes)); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MabeEnumPHPStanTest\Assets; | ||
|
||
use MabeEnum\Enum; | ||
|
||
class AllTypeEnum extends Enum | ||
{ | ||
const NIL = null; | ||
const BOOL = true; | ||
const INT = 1; | ||
const FLOAT = 1.1; | ||
const STR = 'str'; | ||
const ARR = [null, true, 1, 1.1, 'str', []]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MabeEnumPHPStanTest\Assets; | ||
|
||
use MabeEnum\Enum; | ||
|
||
class ArrayTypeEnum extends Enum | ||
{ | ||
const ARRAY = [[]]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MabeEnumPHPStanTest\Assets; | ||
|
||
use MabeEnum\Enum; | ||
|
||
class BoolTypeEnum extends Enum | ||
{ | ||
const BOOL_TRUE = true; | ||
const BOOL_FALSE = false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MabeEnumPHPStanTest\Assets; | ||
|
||
use MabeEnum\Enum; | ||
|
||
class DocCommentEnum extends Enum | ||
{ | ||
/** | ||
* With doc block | ||
* | ||
* @var string | ||
*/ | ||
const WITH_DOC_BLOCK = 'with doc block'; | ||
|
||
const WITHOUT_DOC_BLOCK = 'without doc block'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MabeEnumPHPStanTest\Assets; | ||
|
||
use MabeEnum\Enum; | ||
|
||
class FloatTypeEnum extends Enum | ||
{ | ||
const FLOAT11 = 1.1; | ||
const FLOAT12 = 1.2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MabeEnumPHPStanTest\Assets; | ||
|
||
use MabeEnum\Enum; | ||
|
||
class IntTypeEnum extends Enum | ||
{ | ||
const INT0 = 0; | ||
const INT1 = 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MabeEnumPHPStanTest\Assets; | ||
|
||
use MabeEnum\Enum; | ||
|
||
class NullTypeEnum extends Enum | ||
{ | ||
const NULL = null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MabeEnumPHPStanTest\Assets; | ||
|
||
use MabeEnum\Enum; | ||
|
||
class StrTypeEnum extends Enum | ||
{ | ||
const STR1 = 'str1'; | ||
const STR2 = 'str2'; | ||
} |
Oops, something went wrong.