forked from marc-mabe/php-enum-phpstan
-
Notifications
You must be signed in to change notification settings - Fork 0
refactoring & fix getValues return type #1
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
adaamz
merged 2 commits into
adaamz:get_value_return_extension
from
marc-mabe:get_value_return_extension
Apr 20, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.