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