1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Reflection;
4:
5: use PHPStan\BetterReflection\Reflection\Adapter\ReflectionEnumBackedCase;
6: use PHPStan\BetterReflection\Reflection\Adapter\ReflectionEnumUnitCase;
7: use PHPStan\Internal\DeprecatedAttributeHelper;
8: use PHPStan\Reflection\Deprecation\DeprecationProvider;
9: use PHPStan\TrinaryLogic;
10: use PHPStan\Turbo\ReferencedByTurboExtension;
11: use PHPStan\Type\Enum\EnumCaseObjectType;
12: use PHPStan\Type\Type;
13:
14: /**
15: * @api
16: */
17: #[ReferencedByTurboExtension(key: 'enumCaseReflection')]
18: final class EnumCaseReflection
19: {
20:
21: private bool $isDeprecated;
22:
23: private ?string $deprecatedDescription;
24:
25: /**
26: * @param list<AttributeReflection> $attributes
27: */
28: public function __construct(
29: private ClassReflection $declaringEnum,
30: private ReflectionEnumUnitCase|ReflectionEnumBackedCase $reflection,
31: private ?Type $backingValueType,
32: private array $attributes,
33: DeprecationProvider $deprecationProvider,
34: )
35: {
36: $deprecation = $deprecationProvider->getEnumCaseDeprecation($reflection);
37: if ($deprecation !== null) {
38: $this->isDeprecated = true;
39: $this->deprecatedDescription = $deprecation->getDescription();
40:
41: } elseif ($reflection->isDeprecated()) {
42: $attributes = $this->reflection->getBetterReflection()->getAttributes();
43: $this->isDeprecated = true;
44: $this->deprecatedDescription = DeprecatedAttributeHelper::getDeprecatedDescription($attributes);
45: } else {
46: $this->isDeprecated = false;
47: $this->deprecatedDescription = null;
48: }
49: }
50:
51: public function getDeclaringEnum(): ClassReflection
52: {
53: return $this->declaringEnum;
54: }
55:
56: public function getName(): string
57: {
58: return $this->reflection->getName();
59: }
60:
61: public function getEnumCaseObjectType(): EnumCaseObjectType
62: {
63: return new EnumCaseObjectType(
64: $this->declaringEnum->getName(),
65: $this->getName(),
66: );
67: }
68:
69: public function getBackingValueType(): ?Type
70: {
71: return $this->backingValueType;
72: }
73:
74: public function isDeprecated(): TrinaryLogic
75: {
76: return TrinaryLogic::createFromBoolean($this->isDeprecated);
77: }
78:
79: public function getDeprecatedDescription(): ?string
80: {
81: return $this->deprecatedDescription;
82: }
83:
84: /**
85: * @return list<AttributeReflection>
86: */
87: public function getAttributes(): array
88: {
89: return $this->attributes;
90: }
91:
92: }
93: