Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.31% |
12 / 13 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| InvalidTypeException | |
92.31% |
12 / 13 |
|
66.67% |
2 / 3 |
3.00 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 | |||
| chainException | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fromTypehint | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Exceptions; |
| 3 | |
| 4 | use Apie\Core\ValueObjects\Utils; |
| 5 | use ReflectionNamedType; |
| 6 | use ReflectionUnionType; |
| 7 | use Throwable; |
| 8 | |
| 9 | /** |
| 10 | * Exception thrown when a specific type is expected, but the input is not the correct type. |
| 11 | */ |
| 12 | class InvalidTypeException extends ApieException |
| 13 | { |
| 14 | private mixed $input; |
| 15 | |
| 16 | private string $expected; |
| 17 | |
| 18 | public function __construct(mixed $input, string $expected, ?Throwable $previous = null) |
| 19 | { |
| 20 | $this->input = $input; |
| 21 | $this->expected = $expected; |
| 22 | parent::__construct( |
| 23 | sprintf( |
| 24 | 'Type %s is not expected, expected %s', |
| 25 | Utils::displayMixedAsString($input), |
| 26 | $expected |
| 27 | ), |
| 28 | 0, |
| 29 | $previous |
| 30 | ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Used to chain the exception. This is probably needed when serializing objects to know |
| 35 | * where it went wrong. |
| 36 | */ |
| 37 | public static function chainException(self $previous): self |
| 38 | { |
| 39 | return new self($previous->input, $previous->expected, $previous); |
| 40 | } |
| 41 | |
| 42 | public static function fromTypehint(mixed $input, ReflectionUnionType|ReflectionNamedType $typehint): self |
| 43 | { |
| 44 | return new self($input, $typehint->getName()); |
| 45 | } |
| 46 | } |