diff --git a/src/Processor/Ip.php b/src/Processor/Ip.php index be1d60f..8802cff 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 - ); + // the happy path 🍏 + return IpAddress::parse($value); } }