| 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: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | final class ReflectionEnumBackedCase extends CoreReflectionEnumBackedCase |
| 23: | { |
| 24: | public function __construct(private BetterReflectionEnumCase $betterReflectionEnumCase) |
| 25: | { |
| 26: | unset($this->name); |
| 27: | unset($this->class); |
| 28: | } |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | public function getName(): string |
| 35: | { |
| 36: | return $this->betterReflectionEnumCase->getName(); |
| 37: | } |
| 38: | |
| 39: | public function getBetterReflection(): BetterReflectionEnumCase |
| 40: | { |
| 41: | return $this->betterReflectionEnumCase; |
| 42: | } |
| 43: | |
| 44: | public function hasType(): bool |
| 45: | { |
| 46: | return false; |
| 47: | } |
| 48: | |
| 49: | public function getType(): ?ReflectionType |
| 50: | { |
| 51: | return null; |
| 52: | } |
| 53: | |
| 54: | |
| 55: | public function getValue(): UnitEnum |
| 56: | { |
| 57: | throw Exception\NotImplementedBecauseItTriggersAutoloading::create(); |
| 58: | } |
| 59: | |
| 60: | public function isPublic(): bool |
| 61: | { |
| 62: | return true; |
| 63: | } |
| 64: | |
| 65: | public function isPrivate(): bool |
| 66: | { |
| 67: | return false; |
| 68: | } |
| 69: | |
| 70: | public function isProtected(): bool |
| 71: | { |
| 72: | return false; |
| 73: | } |
| 74: | |
| 75: | public function getModifiers(): int |
| 76: | { |
| 77: | return self::IS_PUBLIC; |
| 78: | } |
| 79: | |
| 80: | public function getDeclaringClass(): ReflectionClass |
| 81: | { |
| 82: | return new ReflectionClass($this->betterReflectionEnumCase->getDeclaringClass()); |
| 83: | } |
| 84: | |
| 85: | public function getDocComment(): string|false |
| 86: | { |
| 87: | return $this->betterReflectionEnumCase->getDocComment() ?? false; |
| 88: | } |
| 89: | |
| 90: | |
| 91: | public function __toString(): string |
| 92: | { |
| 93: | return $this->betterReflectionEnumCase->__toString(); |
| 94: | } |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | |
| 100: | |
| 101: | public function getAttributes(string|null $name = null, int $flags = 0): array |
| 102: | { |
| 103: | if ($flags !== 0 && $flags !== ReflectionAttribute::IS_INSTANCEOF) { |
| 104: | throw new ValueError('Argument #2 ($flags) must be a valid attribute filter flag'); |
| 105: | } |
| 106: | |
| 107: | if ($name !== null && $flags !== 0) { |
| 108: | $attributes = $this->betterReflectionEnumCase->getAttributesByInstance($name); |
| 109: | } elseif ($name !== null) { |
| 110: | $attributes = $this->betterReflectionEnumCase->getAttributesByName($name); |
| 111: | } else { |
| 112: | $attributes = $this->betterReflectionEnumCase->getAttributes(); |
| 113: | } |
| 114: | |
| 115: | |
| 116: | return array_map(static fn (BetterReflectionAttribute $betterReflectionAttribute): ReflectionAttribute|FakeReflectionAttribute => ReflectionAttributeFactory::create($betterReflectionAttribute), $attributes); |
| 117: | } |
| 118: | |
| 119: | public function isFinal(): bool |
| 120: | { |
| 121: | return true; |
| 122: | } |
| 123: | |
| 124: | public function isEnumCase(): bool |
| 125: | { |
| 126: | return true; |
| 127: | } |
| 128: | |
| 129: | public function getEnum(): ReflectionEnum |
| 130: | { |
| 131: | return new ReflectionEnum($this->betterReflectionEnumCase->getDeclaringEnum()); |
| 132: | } |
| 133: | |
| 134: | public function hasBackingValue(): bool |
| 135: | { |
| 136: | return $this->betterReflectionEnumCase->hasValueExpression(); |
| 137: | } |
| 138: | |
| 139: | public function getBackingValue(): int|string |
| 140: | { |
| 141: | return $this->betterReflectionEnumCase->getValue(); |
| 142: | } |
| 143: | |
| 144: | public function isDeprecated(): bool |
| 145: | { |
| 146: | return $this->betterReflectionEnumCase->isDeprecated(); |
| 147: | } |
| 148: | |
| 149: | public function getValueExpression(): Expr |
| 150: | { |
| 151: | return $this->betterReflectionEnumCase->getValueExpression(); |
| 152: | } |
| 153: | |
| 154: | public function __get(string $name): mixed |
| 155: | { |
| 156: | if ($name === 'name') { |
| 157: | return $this->betterReflectionEnumCase->getName(); |
| 158: | } |
| 159: | |
| 160: | if ($name === 'class') { |
| 161: | return $this->betterReflectionEnumCase->getDeclaringClass()->getName(); |
| 162: | } |
| 163: | |
| 164: | throw new OutOfBoundsException(sprintf('Property %s::$%s does not exist.', self::class, $name)); |
| 165: | } |
| 166: | } |
| 167: | |