Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
6 / 6 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| TypeGuard | |
100.00% |
6 / 6 |
|
100.00% |
3 / 3 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| that | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isInstanceOf | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Phexium\Domain; |
| 6 | |
| 7 | use InvalidArgumentException; |
| 8 | |
| 9 | final readonly class TypeGuard |
| 10 | { |
| 11 | private function __construct( |
| 12 | private object $value, |
| 13 | ) {} |
| 14 | |
| 15 | public static function that(object $value): self |
| 16 | { |
| 17 | return new self($value); |
| 18 | } |
| 19 | |
| 20 | public function isInstanceOf(string $expected): void |
| 21 | { |
| 22 | if (!$this->value instanceof $expected) { |
| 23 | throw new InvalidArgumentException( |
| 24 | sprintf('Expected instance of %s, got %s', $expected, $this->value::class) |
| 25 | ); |
| 26 | } |
| 27 | } |
| 28 | } |