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: /** @psalm-immutable */
23: class ReflectionEnum extends ReflectionClass
24: {
25: private Reflector $reflector;
26: /**
27: * @var \PHPStan\BetterReflection\Reflection\ReflectionNamedType|null
28: */
29: private $backingType;
30:
31: /** @var array<non-empty-string, ReflectionEnumCase> */
32: private array $cases;
33:
34: /**
35: * @param non-empty-string|null $namespace
36: *
37: * @phpcs:disable Generic.CodeAnalysis.UselessOverridingMethod.Found
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: * @return array<string, mixed>
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: * @param array<string, mixed> $data
63: * @return static
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: * @internal
81: *
82: * @param non-empty-string|null $namespace
83: * @param ClassNode|InterfaceNode|TraitNode|EnumNode $node
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: /** @param non-empty-string $name */
92: public function hasCase(string $name): bool
93: {
94: return array_key_exists($name, $this->cases);
95: }
96:
97: /** @param non-empty-string $name */
98: public function getCase(string $name): ?\PHPStan\BetterReflection\Reflection\ReflectionEnumCase
99: {
100: return $this->cases[$name] ?? null;
101: }
102:
103: /** @return array<non-empty-string, ReflectionEnumCase> */
104: public function getCases(): array
105: {
106: return $this->cases;
107: }
108:
109: /** @return array<non-empty-string, ReflectionEnumCase> */
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: