| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace PHPStan\BetterReflection\Reflection; |
| 6: | |
| 7: | use LogicException; |
| 8: | use PhpParser\Node; |
| 9: | use PhpParser\Node\Stmt\Class_ as ClassNode; |
| 10: | use PhpParser\Node\Stmt\Enum_ as EnumNode; |
| 11: | use PhpParser\Node\Stmt\Interface_ as InterfaceNode; |
| 12: | use PhpParser\Node\Stmt\Trait_ as TraitNode; |
| 13: | use PHPStan\BetterReflection\Reflector\Reflector; |
| 14: | use PHPStan\BetterReflection\SourceLocator\Located\LocatedSource; |
| 15: | |
| 16: | use function array_combine; |
| 17: | use function array_filter; |
| 18: | use function array_key_exists; |
| 19: | use function array_map; |
| 20: | use function assert; |
| 21: | |
| 22: | |
| 23: | class ReflectionEnum extends ReflectionClass |
| 24: | { |
| 25: | private Reflector $reflector; |
| 26: | |
| 27: | |
| 28: | |
| 29: | private $backingType; |
| 30: | |
| 31: | |
| 32: | private array $cases; |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | private function __construct(Reflector $reflector, EnumNode $node, LocatedSource $locatedSource, ?string $namespace = null) |
| 40: | { |
| 41: | $this->reflector = $reflector; |
| 42: | parent::__construct($reflector, $node, $locatedSource, $namespace); |
| 43: | $this->backingType = $this->createBackingType($node); |
| 44: | $this->cases = $this->createCases($node); |
| 45: | } |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | public function exportToCache(): array |
| 51: | { |
| 52: | return array_merge(parent::exportToCache(), [ |
| 53: | 'backingType' => ($nullsafeVariable1 = $this->backingType) ? $nullsafeVariable1->exportToCache() : null, |
| 54: | 'cases' => array_map( |
| 55: | static fn (ReflectionEnumCase $case) => $case->exportToCache(), |
| 56: | $this->cases, |
| 57: | ), |
| 58: | ]); |
| 59: | } |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | public static function importFromCache(Reflector $reflector, array $data): self |
| 66: | { |
| 67: | $ref = parent::importFromCache($reflector, $data); |
| 68: | $ref->backingType = $data['backingType'] !== null |
| 69: | ? ReflectionNamedType::importFromCache($reflector, $data['backingType'], $ref) |
| 70: | : null; |
| 71: | $ref->cases = array_map( |
| 72: | static fn ($caseData) => ReflectionEnumCase::importFromCache($reflector, $caseData, $ref), |
| 73: | $data['cases'], |
| 74: | ); |
| 75: | |
| 76: | return $ref; |
| 77: | } |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | public static function createFromNode(Reflector $reflector, $node, LocatedSource $locatedSource, ?string $namespace = null): self |
| 86: | { |
| 87: | assert($node instanceof EnumNode); |
| 88: | return new self($reflector, $node, $locatedSource, $namespace); |
| 89: | } |
| 90: | |
| 91: | |
| 92: | public function hasCase(string $name): bool |
| 93: | { |
| 94: | return array_key_exists($name, $this->cases); |
| 95: | } |
| 96: | |
| 97: | |
| 98: | public function getCase(string $name): ?\PHPStan\BetterReflection\Reflection\ReflectionEnumCase |
| 99: | { |
| 100: | return $this->cases[$name] ?? null; |
| 101: | } |
| 102: | |
| 103: | |
| 104: | public function getCases(): array |
| 105: | { |
| 106: | return $this->cases; |
| 107: | } |
| 108: | |
| 109: | |
| 110: | private function createCases(EnumNode $node): array |
| 111: | { |
| 112: | $enumCasesNodes = array_filter($node->stmts, static fn (Node\Stmt $stmt): bool => $stmt instanceof Node\Stmt\EnumCase); |
| 113: | |
| 114: | return array_combine( |
| 115: | array_map(static fn (Node\Stmt\EnumCase $enumCaseNode): string => $enumCaseNode->name->toString(), $enumCasesNodes), |
| 116: | array_map(fn (Node\Stmt\EnumCase $enumCaseNode): ReflectionEnumCase => ReflectionEnumCase::createFromNode($this->reflector, $enumCaseNode, $this), $enumCasesNodes), |
| 117: | ); |
| 118: | } |
| 119: | |
| 120: | public function isBacked(): bool |
| 121: | { |
| 122: | return $this->backingType !== null; |
| 123: | } |
| 124: | |
| 125: | public function getBackingType(): ReflectionNamedType |
| 126: | { |
| 127: | if ($this->backingType === null) { |
| 128: | throw new LogicException('This enum does not have a backing type available'); |
| 129: | } |
| 130: | |
| 131: | return $this->backingType; |
| 132: | } |
| 133: | |
| 134: | private function createBackingType(EnumNode $node): ?\PHPStan\BetterReflection\Reflection\ReflectionNamedType |
| 135: | { |
| 136: | if ($node->scalarType === null) { |
| 137: | return null; |
| 138: | } |
| 139: | |
| 140: | $backingType = ReflectionNamedType::createFromNode($this->reflector, $this, $node->scalarType); |
| 141: | assert($backingType instanceof ReflectionNamedType); |
| 142: | |
| 143: | return $backingType; |
| 144: | } |
| 145: | } |
| 146: | |