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