From 1baa36c973c57196bc79e3f487ad7fbc152e2534 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Sz=C3=A9pe?= Date: Thu, 26 Oct 2023 02:25:55 +0200 Subject: [PATCH 1/2] Flow of execution --- src/Processor/Ip.php | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/Processor/Ip.php b/src/Processor/Ip.php index be1d60f..2d65fe2 100644 --- a/src/Processor/Ip.php +++ b/src/Processor/Ip.php @@ -35,29 +35,33 @@ public function getOutputTypes(): array */ public function coerce(mixed $value): ?IpAddress { + // unhappy path: if (something wrong) terminate; if (!class_exists(IpAddress::class)) { throw Exceptional::ComponentUnavailable( - 'IP validation requires decodelabs-compass package' + 'IP validation requires decodelabs/compass package' ); } + // unhappy path: if (something wrong) terminate; if ($value === null) { return null; } + // unhappy path: if (something wrong) terminate; if ( - is_int($value) || - $value instanceof BigInteger || - is_string($value) || - $value instanceof IpAddress + ! is_int($value) && + ! $value instanceof BigInteger && + ! is_string($value) && + ! $value instanceof IpAddress ) { - return IpAddress::parse($value); + throw Exceptional::UnexpectedValue( + 'Could not coerce value to Compass IP', + null, + $value + ); } - throw Exceptional::UnexpectedValue( - 'Could not coerce value to Compass IP', - null, - $value - ); + // unhappy path 🍏 + return IpAddress::parse($value); } } From 3451b837e4aab1a4d8de28036ad3c9b5f7a75292 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Sz=C3=A9pe?= Date: Thu, 26 Oct 2023 02:32:06 +0200 Subject: [PATCH 2/2] fix typo --- src/Processor/Ip.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Processor/Ip.php b/src/Processor/Ip.php index 2d65fe2..8802cff 100644 --- a/src/Processor/Ip.php +++ b/src/Processor/Ip.php @@ -61,7 +61,7 @@ public function coerce(mixed $value): ?IpAddress ); } - // unhappy path 🍏 + // the happy path 🍏 return IpAddress::parse($value); } }