|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace MabeEnumPHPStan; |
| 6 | + |
| 7 | +use MabeEnum\Enum; |
| 8 | +use PhpParser\Node\Expr\MethodCall; |
| 9 | +use PHPStan\Analyser\Scope; |
| 10 | +use PHPStan\Reflection\MethodReflection; |
| 11 | +use PHPStan\Reflection\ParametersAcceptorSelector; |
| 12 | +use PHPStan\ShouldNotHappenException; |
| 13 | +use PHPStan\Type\ArrayType; |
| 14 | +use PHPStan\Type\DynamicMethodReturnTypeExtension; |
| 15 | +use PHPStan\Type\IntegerType; |
| 16 | +use PHPStan\Type\Type; |
| 17 | +use PHPStan\Type\TypeCombinator; |
| 18 | + |
| 19 | +class EnumDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Buffer of known return types of Enum::getValues() |
| 23 | + * @var Type[] |
| 24 | + * @phpstan-var array<class-string<Enum>, Type> |
| 25 | + */ |
| 26 | + private $enumValuesTypeBuffer = []; |
| 27 | + |
| 28 | + public function getClass(): string |
| 29 | + { |
| 30 | + return Enum::class; |
| 31 | + } |
| 32 | + |
| 33 | + public function isMethodSupported(MethodReflection $methodReflection): bool |
| 34 | + { |
| 35 | + $supportedMethods = ['getvalue']; |
| 36 | + if (method_exists(Enum::class, 'getValues')) { |
| 37 | + array_push($supportedMethods, 'getvalues'); |
| 38 | + } |
| 39 | + |
| 40 | + return in_array(strtolower($methodReflection->getName()), $supportedMethods, true); |
| 41 | + } |
| 42 | + |
| 43 | + public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type |
| 44 | + { |
| 45 | + $enumType = $scope->getType($methodCall->var); |
| 46 | + $methodName = $methodReflection->getName(); |
| 47 | + $methodClasses = $enumType->getReferencedClasses(); |
| 48 | + if (count($methodClasses) !== 1) { |
| 49 | + return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); |
| 50 | + } |
| 51 | + |
| 52 | + $enumeration = $methodClasses[0]; |
| 53 | + |
| 54 | + switch (strtolower($methodName)) { |
| 55 | + case 'getvalue': |
| 56 | + return $this->getEnumValuesType($enumeration, $scope); |
| 57 | + case 'getvalues': |
| 58 | + return new ArrayType( |
| 59 | + new IntegerType(), |
| 60 | + $this->getEnumValuesType($enumeration, $scope) |
| 61 | + ); |
| 62 | + default: |
| 63 | + throw new ShouldNotHappenException("Method {$methodName} is not supported"); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Returns union type of all values of an enumeration |
| 69 | + * @phpstan-param class-string<Enum> $enumClass |
| 70 | + */ |
| 71 | + private function getEnumValuesType(string $enumeration, Scope $scope): Type |
| 72 | + { |
| 73 | + if (isset($this->enumValuesTypeBuffer[$enumeration])) { |
| 74 | + return $this->enumValuesTypeBuffer[$enumeration]; |
| 75 | + } |
| 76 | + |
| 77 | + $values = array_values($enumeration::getConstants()); |
| 78 | + $types = array_map(function ($value) use ($scope): Type { |
| 79 | + return $scope->getTypeFromValue($value); |
| 80 | + }, $values); |
| 81 | + |
| 82 | + return $this->enumValuesTypeBuffer[$enumeration] = TypeCombinator::union(...$types); |
| 83 | + } |
| 84 | +} |
0 commit comments