1: <?php
2:
3: declare(strict_types=1);
4:
5: namespace PHPStan\BetterReflection\Reflection\Adapter;
6:
7: use OutOfBoundsException;
8: use ReflectionEnumUnitCase as CoreReflectionEnumUnitCase;
9: use PHPStan\BetterReflection\Reflection\ReflectionAttribute as BetterReflectionAttribute;
10: use PHPStan\BetterReflection\Reflection\ReflectionEnumCase as BetterReflectionEnumCase;
11: use UnitEnum;
12: use ValueError;
13:
14: use function array_map;
15: use function sprintf;
16:
17: /**
18: * @psalm-suppress PropertyNotSetInConstructor
19: * @psalm-immutable
20: */
21: final class ReflectionEnumUnitCase extends CoreReflectionEnumUnitCase
22: {
23: public function __construct(private BetterReflectionEnumCase $betterReflectionEnumCase)
24: {
25: unset($this->name);
26: unset($this->class);
27: }
28:
29: /**
30: * Get the name of the reflection (e.g. if this is a ReflectionClass this
31: * will be the class name).
32: */
33: public function getName(): string
34: {
35: return $this->betterReflectionEnumCase->getName();
36: }
37:
38: public function getValue(): UnitEnum
39: {
40: throw new Exception\NotImplemented('Not implemented');
41: }
42:
43: public function isPublic(): bool
44: {
45: return true;
46: }
47:
48: public function isPrivate(): bool
49: {
50: return false;
51: }
52:
53: public function isProtected(): bool
54: {
55: return false;
56: }
57:
58: public function getModifiers(): int
59: {
60: return self::IS_PUBLIC;
61: }
62:
63: public function getDeclaringClass(): ReflectionClass
64: {
65: return new ReflectionClass($this->betterReflectionEnumCase->getDeclaringClass());
66: }
67:
68: public function getDocComment(): string|false
69: {
70: return $this->betterReflectionEnumCase->getDocComment() ?? false;
71: }
72:
73: /** @return non-empty-string */
74: public function __toString(): string
75: {
76: return $this->betterReflectionEnumCase->__toString();
77: }
78:
79: /**
80: * @param class-string|null $name
81: *
82: * @return list<ReflectionAttribute|FakeReflectionAttribute>
83: */
84: public function getAttributes(string|null $name = null, int $flags = 0): array
85: {
86: if ($flags !== 0 && $flags !== ReflectionAttribute::IS_INSTANCEOF) {
87: throw new ValueError('Argument #2 ($flags) must be a valid attribute filter flag');
88: }
89:
90: if ($name !== null && $flags !== 0) {
91: $attributes = $this->betterReflectionEnumCase->getAttributesByInstance($name);
92: } elseif ($name !== null) {
93: $attributes = $this->betterReflectionEnumCase->getAttributesByName($name);
94: } else {
95: $attributes = $this->betterReflectionEnumCase->getAttributes();
96: }
97:
98: /** @psalm-suppress ImpureFunctionCall */
99: return array_map(static fn (BetterReflectionAttribute $betterReflectionAttribute): ReflectionAttribute|FakeReflectionAttribute => ReflectionAttributeFactory::create($betterReflectionAttribute), $attributes);
100: }
101:
102: public function isFinal(): bool
103: {
104: return true;
105: }
106:
107: public function isEnumCase(): bool
108: {
109: return true;
110: }
111:
112: public function getEnum(): ReflectionEnum
113: {
114: return new ReflectionEnum($this->betterReflectionEnumCase->getDeclaringEnum());
115: }
116:
117: public function __get(string $name): mixed
118: {
119: if ($name === 'name') {
120: return $this->betterReflectionEnumCase->getName();
121: }
122:
123: if ($name === 'class') {
124: return $this->betterReflectionEnumCase->getDeclaringClass()->getName();
125: }
126:
127: throw new OutOfBoundsException(sprintf('Property %s::$%s does not exist.', self::class, $name));
128: }
129: }
130: