Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
100.00% |
17 / 17 |
|
93.33% |
14 / 15 |
|
88.89% |
8 / 9 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| HydrationFailure | |
100.00% |
17 / 17 |
|
93.33% |
14 / 15 |
|
88.89% |
8 / 9 |
|
100.00% |
5 / 5 |
8.09 | |
100.00% |
1 / 1 |
| fromHydratableFailure | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| message | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| source | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| formatRowKeys | |
100.00% |
1 / 1 |
|
85.71% |
6 / 7 |
|
66.67% |
2 / 3 |
|
100.00% |
1 / 1 |
2.15 | |||
| valueType | |
100.00% |
5 / 5 |
|
100.00% |
5 / 5 |
|
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Celemas\Quma\Exception; |
| 6 | |
| 7 | use RuntimeException; |
| 8 | use Throwable; |
| 9 | |
| 10 | /** @api */ |
| 11 | class HydrationFailure extends RuntimeException |
| 12 | { |
| 13 | /** |
| 14 | * @param class-string $class |
| 15 | * @param list<string> $rowKeys |
| 16 | */ |
| 17 | public static function fromHydratableFailure( |
| 18 | string $class, |
| 19 | ?string $sourcePath, |
| 20 | array $rowKeys, |
| 21 | Throwable $previous, |
| 22 | ): self { |
| 23 | return new self( |
| 24 | self::message( |
| 25 | $class, |
| 26 | $sourcePath, |
| 27 | 'Hydratable::fromRow() failed. Row keys: ' . self::formatRowKeys($rowKeys) . '.', |
| 28 | ), |
| 29 | 0, |
| 30 | $previous, |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | /** @param class-string $class */ |
| 35 | protected static function message(string $class, ?string $sourcePath, string $detail): string |
| 36 | { |
| 37 | return "Could not hydrate {$class} from " . self::source($sourcePath) . ": {$detail}"; |
| 38 | } |
| 39 | |
| 40 | protected static function source(?string $sourcePath): string |
| 41 | { |
| 42 | return $sourcePath ?? 'ad-hoc SQL'; |
| 43 | } |
| 44 | |
| 45 | /** @param list<string> $rowKeys */ |
| 46 | protected static function formatRowKeys(array $rowKeys): string |
| 47 | { |
| 48 | return $rowKeys === [] ? '(none)' : implode(', ', $rowKeys); |
| 49 | } |
| 50 | |
| 51 | protected static function valueType(mixed $value): string |
| 52 | { |
| 53 | if (is_object($value)) { |
| 54 | return $value::class; |
| 55 | } |
| 56 | |
| 57 | if (is_resource($value)) { |
| 58 | return get_resource_type($value) . ' resource'; |
| 59 | } |
| 60 | |
| 61 | return get_debug_type($value); |
| 62 | } |
| 63 | } |