Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
11 / 11 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| EnumSymbol | |
100.00% |
11 / 11 |
|
100.00% |
4 / 4 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getBackingType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getInterfaces | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getAutoloadAliasArray | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BrianHenryIE\Strauss\Types; |
| 4 | |
| 5 | use BrianHenryIE\Strauss\Files\FileBase; |
| 6 | |
| 7 | /** |
| 8 | * @phpstan-import-type EnumAliasArray from AutoloadAliasInterface |
| 9 | */ |
| 10 | class EnumSymbol extends NamespacedSymbol implements AutoloadAliasInterface |
| 11 | { |
| 12 | /** |
| 13 | * The enum's backing type, 'string' or 'int'; null for pure enums. |
| 14 | */ |
| 15 | protected ?string $backingType; |
| 16 | |
| 17 | /** |
| 18 | * @var string[] |
| 19 | */ |
| 20 | protected array $interfaces; |
| 21 | |
| 22 | /** |
| 23 | * @param string $fqdnEnumName |
| 24 | * @param FileBase $sourceFile |
| 25 | * @param NamespaceSymbol $namespace |
| 26 | * @param ?string $backingType |
| 27 | * @param string[] $interfaces |
| 28 | */ |
| 29 | public function __construct( |
| 30 | string $fqdnEnumName, |
| 31 | FileBase $sourceFile, |
| 32 | NamespaceSymbol $namespace, |
| 33 | ?string $backingType = null, |
| 34 | array $interfaces = [] |
| 35 | ) { |
| 36 | parent::__construct($fqdnEnumName, $sourceFile, $namespace); |
| 37 | |
| 38 | $this->backingType = $backingType; |
| 39 | $this->interfaces = $interfaces; |
| 40 | } |
| 41 | |
| 42 | public function getBackingType(): ?string |
| 43 | { |
| 44 | return $this->backingType; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @return string[] |
| 49 | */ |
| 50 | public function getInterfaces(): array |
| 51 | { |
| 52 | return $this->interfaces; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Enums are final and cannot be extended, so the `class OriginalName extends NewName {}` approach used for |
| 57 | * classes is impossible. Instead, `autoload_aliases.php` calls `class_alias()` — which works for enums on |
| 58 | * PHP >= 8.1 — making the original name a true alias of the renamed enum: case identity (`===`), |
| 59 | * `match` arms, `::cases()`, `::from()`, and `instanceof` the enum itself all behave identically. |
| 60 | * Unlike the class shim, the alias cannot re-implement the enum's original interface names, so |
| 61 | * `instanceof` against an original (aliased) interface name is false; only the renamed interface matches. |
| 62 | * |
| 63 | * @see AliasAutoloader::autoload() |
| 64 | * |
| 65 | * @return EnumAliasArray |
| 66 | */ |
| 67 | public function getAutoloadAliasArray(): array |
| 68 | { |
| 69 | return array ( |
| 70 | 'type' => 'enum', |
| 71 | 'enumname' => $this->getOriginalLocalName(), |
| 72 | 'namespace' => $this->namespace->getOriginalFqdnName(), |
| 73 | 'concrete' => $this->getReplacementFqdnName(), |
| 74 | ); |
| 75 | } |
| 76 | } |