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