Lines
100.00%
19 / 19
Functions and Methods
100.00%
4 / 4
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| AttributeKind | 100.00% | 19 / 19 | 100.00% | 4 / 4 | 21 | 100.00% | 1 / 1 | |||
| ofValue | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 7 | |||||
| widen | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 2 | |||||
| getReaderMethodName | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 6 | |||||
| getPhpType | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 6 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Codegen\Attribute; | |
| 6 | ||
| 7 | use Stewart\Codegen\Php\PhpType; | |
| 8 | ||
| 9 | enum AttributeKind | |
| 10 | { | |
| 11 | case Bool; | |
| 12 | case Float; | |
| 13 | case String; | |
| 14 | case Array; | |
| 15 | case Mixed; | |
| 16 | ||
| 17 | public static function ofValue(mixed $value): ?self | |
| 18 | { | |
| 19 | return match (true) { | |
| 20 | $value === null => null, | |
| 21 | \is_bool($value) => self::Bool, | |
| 22 | // An int reading may be followed by a fraction at run time, so every number is a float. | |
| 23 | \is_int($value), \is_float($value) => self::Float, | |
| 24 | \is_string($value) => self::String, | |
| 25 | \is_array($value) => self::Array, | |
| 26 | default => self::Mixed, | |
| 27 | }; | |
| 28 | } | |
| 29 | ||
| 30 | public function widen(self $other): self | |
| 31 | { | |
| 32 | return $this === $other ? $this : self::Mixed; | |
| 33 | } | |
| 34 | ||
| 35 | public function getReaderMethodName(): string | |
| 36 | { | |
| 37 | return match ($this) { | |
| 38 | self::Bool => 'getBoolAttribute', | |
| 39 | self::Float => 'getFloatAttribute', | |
| 40 | self::String => 'getStringAttribute', | |
| 41 | self::Array => 'getArrayAttribute', | |
| 42 | self::Mixed => 'getAttribute', | |
| 43 | }; | |
| 44 | } | |
| 45 | ||
| 46 | public function getPhpType(): PhpType | |
| 47 | { | |
| 48 | return match ($this) { | |
| 49 | self::Bool => PhpType::fromNative('?bool'), | |
| 50 | self::Float => PhpType::fromNative('?float'), | |
| 51 | self::String => PhpType::fromNative('?string'), | |
| 52 | self::Array => PhpType::fromNative('?array', 'array<array-key, mixed>|null'), | |
| 53 | self::Mixed => PhpType::mixed(), | |
| 54 | }; | |
| 55 | } | |
| 56 | } |