|
| 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\Constant\ConstantArrayType; |
| 15 | +use PHPStan\Type\ConstantTypeHelper; |
| 16 | +use PHPStan\Type\DynamicMethodReturnTypeExtension; |
| 17 | +use PHPStan\Type\Type; |
| 18 | +use PHPStan\Type\TypeCombinator; |
| 19 | + |
| 20 | +class EnumDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension |
| 21 | +{ |
| 22 | + /** |
| 23 | + * Buffer of all types of enumeration values |
| 24 | + * @phpstan-var array<class-string<Enum>, Type[]> |
| 25 | + */ |
| 26 | + private $enumValueTypesBuffer = []; |
| 27 | + |
| 28 | + /** |
| 29 | + * Buffer of all types of enumeration ordinals |
| 30 | + * @phpstan-var array<class-string<Enum>, Type[]> |
| 31 | + */ |
| 32 | + private $enumOrdinalTypesBuffer = []; |
| 33 | + |
| 34 | + public function getClass(): string |
| 35 | + { |
| 36 | + return Enum::class; |
| 37 | + } |
| 38 | + |
| 39 | + public function isMethodSupported(MethodReflection $methodReflection): bool |
| 40 | + { |
| 41 | + $supportedMethods = ['getvalue']; |
| 42 | + if (method_exists(Enum::class, 'getValues')) { |
| 43 | + array_push($supportedMethods, 'getvalues'); |
| 44 | + } |
| 45 | + |
| 46 | + return in_array(strtolower($methodReflection->getName()), $supportedMethods, true); |
| 47 | + } |
| 48 | + |
| 49 | + public function getTypeFromMethodCall( |
| 50 | + MethodReflection $methodReflection, |
| 51 | + MethodCall $methodCall, |
| 52 | + Scope $scope |
| 53 | + ): Type { |
| 54 | + $callType = $scope->getType($methodCall->var); |
| 55 | + $callClasses = $callType->getReferencedClasses(); |
| 56 | + $methodName = strtolower($methodReflection->getName()); |
| 57 | + $returnTypes = []; |
| 58 | + foreach ($callClasses as $callClass) { |
| 59 | + if (!is_subclass_of($callClass, Enum::class, true)) { |
| 60 | + $returnTypes[] = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants()) |
| 61 | + ->getReturnType(); |
| 62 | + } else { |
| 63 | + switch ($methodName) { |
| 64 | + case 'getvalue': |
| 65 | + $returnTypes[] = $this->enumGetValueReturnType($callClass); |
| 66 | + break; |
| 67 | + case 'getvalues': |
| 68 | + $returnTypes[] = $this->enumGetValuesReturnType($callClass); |
| 69 | + break; |
| 70 | + default: |
| 71 | + throw new ShouldNotHappenException("Method {$methodName} is not supported"); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return TypeCombinator::union(...$returnTypes); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Returns types of all values of an enumeration |
| 81 | + * @phpstan-param class-string<Enum> $enumeration |
| 82 | + * @return Type[] |
| 83 | + */ |
| 84 | + private function enumValueTypes(string $enumeration): array |
| 85 | + { |
| 86 | + if (isset($this->enumValueTypesBuffer[$enumeration])) { |
| 87 | + return $this->enumValueTypesBuffer[$enumeration]; |
| 88 | + } |
| 89 | + |
| 90 | + $values = array_values($enumeration::getConstants()); |
| 91 | + $types = array_map([ConstantTypeHelper::class, 'getTypeFromValue'], $values); |
| 92 | + |
| 93 | + return $this->enumValueTypesBuffer[$enumeration] = $types; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Returns types of all ordinals of an enumeration |
| 98 | + * @phpstan-param class-string<Enum> $enumeration |
| 99 | + * @return Type[] |
| 100 | + */ |
| 101 | + private function enumOrdinalTypes(string $enumeration): array |
| 102 | + { |
| 103 | + if (isset($this->enumOrdinalTypesBuffer[$enumeration])) { |
| 104 | + return $this->enumOrdinalTypesBuffer[$enumeration]; |
| 105 | + } |
| 106 | + |
| 107 | + $ordinals = array_keys($enumeration::getOrdinals()); |
| 108 | + $types = array_map([ConstantTypeHelper::class, 'getTypeFromValue'], $ordinals); |
| 109 | + |
| 110 | + return $this->enumOrdinalTypesBuffer[$enumeration] = $types; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Returns return type of Enum::getValue() |
| 115 | + * @phpstan-param class-string<Enum> $enumeration |
| 116 | + */ |
| 117 | + private function enumGetValueReturnType(string $enumeration): Type |
| 118 | + { |
| 119 | + return TypeCombinator::union(...$this->enumValueTypes($enumeration)); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Returns return type of Enum::getValues() |
| 124 | + * @phpstan-param class-string<Enum> $enumeration |
| 125 | + */ |
| 126 | + private function enumGetValuesReturnType(string $enumeration): ArrayType |
| 127 | + { |
| 128 | + $keyTypes = $this->enumOrdinalTypes($enumeration); |
| 129 | + $valueTypes = $this->enumValueTypes($enumeration); |
| 130 | + return new ConstantArrayType($keyTypes, $valueTypes, count($keyTypes)); |
| 131 | + } |
| 132 | +} |
0 commit comments