Lines
79.16%
19 / 24
Functions and Methods
63.63%
7 / 11
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| RawMap | 79.16% | 19 / 24 | 63.63% | 7 / 11 | 24.99 | 0.00% | 0 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| fromValue | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 2 | |||||
| hasKey | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| listKeys | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| readString | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 3 | |||||
| readBool | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| readScalar | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 2 | |||||
| readNumber | 0.00% | 0 / 2 | 0.00% | 0 / 1 | 12 | |||||
| readMap | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| listMaps | 83.33% | 5 / 6 | 0.00% | 0 / 1 | 3.04 | |||||
| listStrings | 83.33% | 5 / 6 | 0.00% | 0 / 1 | 3.04 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Codegen\Snapshot; | |
| 6 | ||
| 7 | /** @internal */ | |
| 8 | final readonly class RawMap | |
| 9 | { | |
| 10 | /** @param array<array-key, mixed> $values */ | |
| 11 | private function __construct(private array $values) {} | |
| 12 | ||
| 13 | public static function fromValue(mixed $value): self | |
| 14 | { | |
| 15 | return new self(\is_array($value) ? $value : []); | |
| 16 | } | |
| 17 | ||
| 18 | public function hasKey(string $key): bool | |
| 19 | { | |
| 20 | return \array_key_exists($key, $this->values); | |
| 21 | } | |
| 22 | ||
| 23 | /** @return list<string> */ | |
| 24 | public function listKeys(): array | |
| 25 | { | |
| 26 | return array_map(strval(...), array_keys($this->values)); | |
| 27 | } | |
| 28 | ||
| 29 | public function readString(string $key): ?string | |
| 30 | { | |
| 31 | $value = $this->values[$key] ?? null; | |
| 32 | ||
| 33 | return \is_string($value) && $value !== '' ? $value : null; | |
| 34 | } | |
| 35 | ||
| 36 | public function readBool(string $key): bool | |
| 37 | { | |
| 38 | return ($this->values[$key] ?? null) === true; | |
| 39 | } | |
| 40 | ||
| 41 | public function readScalar(string $key): bool|int|float|string|null | |
| 42 | { | |
| 43 | $value = $this->values[$key] ?? null; | |
| 44 | ||
| 45 | return \is_scalar($value) ? $value : null; | |
| 46 | } | |
| 47 | ||
| 48 | public function readNumber(string $key): int|float|null | |
| 49 | { | |
| 50 | $value = $this->values[$key] ?? null; | |
| 51 | ||
| 52 | return \is_int($value) || \is_float($value) ? $value : null; | |
| 53 | } | |
| 54 | ||
| 55 | public function readMap(string $key): self | |
| 56 | { | |
| 57 | return self::fromValue($this->values[$key] ?? null); | |
| 58 | } | |
| 59 | ||
| 60 | /** @return list<RawMap> */ | |
| 61 | public function listMaps(string $key): array | |
| 62 | { | |
| 63 | $value = $this->values[$key] ?? null; | |
| 64 | ||
| 65 | if (!\is_array($value)) { | |
| 66 | return []; | |
| 67 | } | |
| 68 | ||
| 69 | if (!array_is_list($value)) { | |
| 70 | return [self::fromValue($value)]; | |
| 71 | } | |
| 72 | ||
| 73 | return array_values(array_map(self::fromValue(...), array_filter($value, \is_array(...)))); | |
| 74 | } | |
| 75 | ||
| 76 | /** @return list<string> */ | |
| 77 | public function listStrings(string $key): array | |
| 78 | { | |
| 79 | $value = $this->values[$key] ?? null; | |
| 80 | ||
| 81 | if (\is_string($value)) { | |
| 82 | return [$value]; | |
| 83 | } | |
| 84 | ||
| 85 | if (!\is_array($value)) { | |
| 86 | return []; | |
| 87 | } | |
| 88 | ||
| 89 | return array_values(array_filter($value, \is_string(...))); | |
| 90 | } | |
| 91 | } |