| Total Complexity | 6 |
| Total Lines | 50 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 9 | abstract class Enum |
||
|
|
|||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var array<string,array<string,scalar>> |
||
| 13 | */ |
||
| 14 | protected static array $constCacheArray = []; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @return array<string,scalar> |
||
| 18 | * @codeCoverageIgnore |
||
| 19 | */ |
||
| 20 | protected static function getConstants(): array |
||
| 21 | { |
||
| 22 | $calledClass = get_called_class(); |
||
| 23 | if (!array_key_exists($calledClass, self::$constCacheArray)) { |
||
| 24 | try { |
||
| 25 | $reflect = new ReflectionClass($calledClass); |
||
| 26 | |||
| 27 | /** @var array<string,scalar> $values */ |
||
| 28 | $values = $reflect->getConstants(); |
||
| 29 | |||
| 30 | self::$constCacheArray[$calledClass] = $values; |
||
| 31 | } catch (ReflectionException $e) { |
||
| 32 | return []; |
||
| 33 | } |
||
| 34 | } |
||
| 35 | |||
| 36 | return self::$constCacheArray[$calledClass]; |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param string $value |
||
| 41 | * |
||
| 42 | * @return bool |
||
| 43 | */ |
||
| 44 | 510 | public static function isValidValue(string $value): bool |
|
| 45 | { |
||
| 46 | 510 | $values = array_values(self::getConstants()); |
|
| 47 | |||
| 48 | 510 | return in_array($value, $values, true); |
|
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Return all values |
||
| 53 | * |
||
| 54 | * @return array<scalar> |
||
| 55 | */ |
||
| 56 | 24 | public static function values(): array |
|
| 57 | { |
||
| 58 | 24 | return array_values(self::getConstants()); |
|
| 59 | } |
||
| 61 |