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: * @internal
49: *
50: * @param EnumNode $node
51: * @param non-empty-string|null $namespace
52: *
53: * @psalm-suppress MoreSpecificImplementedParamType
54: */
55: public static function createFromNode(Reflector $reflector, $node, LocatedSource $locatedSource, ?string $namespace = null): self
56: {
57: $node = $node;
58: assert($node instanceof EnumNode);
59: return new self($reflector, $node, $locatedSource, $namespace);
60: }
61:
62: /** @param non-empty-string $name */
63: public function hasCase(string $name): bool
64: {
65: return array_key_exists($name, $this->cases);
66: }
67:
68: /** @param non-empty-string $name */
69: public function getCase(string $name): ?\PHPStan\BetterReflection\Reflection\ReflectionEnumCase
70: {
71: return $this->cases[$name] ?? null;
72: }
73:
74: /** @return array<non-empty-string, ReflectionEnumCase> */
75: public function getCases(): array
76: {
77: return $this->cases;
78: }
79:
80: /** @return array<non-empty-string, ReflectionEnumCase> */
81: private function createCases(EnumNode $node): array
82: {
83: $enumCasesNodes = array_filter($node->stmts, static fn (Node\Stmt $stmt): bool => $stmt instanceof Node\Stmt\EnumCase);
84:
85: return array_combine(
86: array_map(static fn (Node\Stmt\EnumCase $enumCaseNode): string => $enumCaseNode->name->toString(), $enumCasesNodes),
87: array_map(fn (Node\Stmt\EnumCase $enumCaseNode): ReflectionEnumCase => ReflectionEnumCase::createFromNode($this->reflector, $enumCaseNode, $this), $enumCasesNodes),
88: );
89: }
90:
91: public function isBacked(): bool
92: {
93: return $this->backingType !== null;
94: }
95:
96: public function getBackingType(): ReflectionNamedType
97: {
98: if ($this->backingType === null) {
99: throw new LogicException('This enum does not have a backing type available');
100: }
101:
102: return $this->backingType;
103: }
104:
105: private function createBackingType(EnumNode $node): ?\PHPStan\BetterReflection\Reflection\ReflectionNamedType
106: {
107: if ($node->scalarType === null) {
108: return null;
109: }
110:
111: $backingType = ReflectionNamedType::createFromNode($this->reflector, $this, $node->scalarType);
112: assert($backingType instanceof ReflectionNamedType);
113:
114: return $backingType;
115: }
116: }
117: