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: /**
26: * @var \PHPStan\BetterReflection\Reflection\ReflectionNamedType|null
27: */
28: private $backingType;
29:
30: /** @var array<non-empty-string, ReflectionEnumCase> */
31: private $cases;
32: /**
33: * @var \PHPStan\BetterReflection\Reflector\Reflector
34: */
35: private $reflector;
36: /** @phpcs:disable Generic.CodeAnalysis.UselessOverridingMethod.Found */
37: private function __construct(Reflector $reflector, EnumNode $node, LocatedSource $locatedSource, ?string $namespace = null)
38: {
39: $this->reflector = $reflector;
40: parent::__construct($reflector, $node, $locatedSource, $namespace);
41: $this->backingType = $this->createBackingType($node);
42: $this->cases = $this->createCases($node);
43: }
44: /**
45: * @internal
46: * @param ClassNode|InterfaceNode|TraitNode|EnumNode $node
47: * @return $this
48: */
49: public static function createFromNode(Reflector $reflector, $node, LocatedSource $locatedSource, ?string $namespace = null): \PHPStan\BetterReflection\Reflection\ReflectionClass
50: {
51: $node = $node;
52: assert($node instanceof EnumNode);
53: return new self($reflector, $node, $locatedSource, $namespace);
54: }
55:
56: /** @param non-empty-string $name */
57: public function hasCase(string $name): bool
58: {
59: return array_key_exists($name, $this->cases);
60: }
61:
62: /** @param non-empty-string $name */
63: public function getCase(string $name): ?\PHPStan\BetterReflection\Reflection\ReflectionEnumCase
64: {
65: return $this->cases[$name] ?? null;
66: }
67:
68: /** @return array<non-empty-string, ReflectionEnumCase> */
69: public function getCases(): array
70: {
71: return $this->cases;
72: }
73:
74: /** @return array<non-empty-string, ReflectionEnumCase> */
75: private function createCases(EnumNode $node): array
76: {
77: $enumCasesNodes = array_filter($node->stmts, static function (Node\Stmt $stmt) : bool {
78: return $stmt instanceof Node\Stmt\EnumCase;
79: });
80:
81: return array_combine(array_map(static function (Node\Stmt\EnumCase $enumCaseNode): string {
82: $enumCaseName = $enumCaseNode->name->toString();
83: assert($enumCaseName !== '');
84:
85: return $enumCaseName;
86: }, $enumCasesNodes), array_map(function (Node\Stmt\EnumCase $enumCaseNode) : ReflectionEnumCase {
87: return ReflectionEnumCase::createFromNode($this->reflector, $enumCaseNode, $this);
88: }, $enumCasesNodes));
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: