diff --git a/Option.php b/Option.php index 3e8107f..1dd4265 100644 --- a/Option.php +++ b/Option.php @@ -37,6 +37,7 @@ namespace Hoa\Option; use Hoa\Consistency; +use RuntimeException; /** * Class \Hoa\Option. @@ -88,6 +89,13 @@ private function __construct($value) */ public static function some($value): self { + if (null === $value) { + throw new RuntimeException( + 'Called `' . __METHOD__ . '` with a `null` value, forbidden. ' . + 'Use `' . __CLASS__ . '::none` instead.' + ); + } + return new self($value); } @@ -174,7 +182,7 @@ public function isNone(): bool public function expect(string $errorMessage) { if (true === $this->isNone()) { - throw new \RuntimeException($errorMessage); + throw new RuntimeException($errorMessage); } return $this->_value; diff --git a/Test/Unit/Option.php b/Test/Unit/Option.php index 513d7a4..bdf4ea8 100644 --- a/Test/Unit/Option.php +++ b/Test/Unit/Option.php @@ -89,6 +89,19 @@ public function case_some() ->isEqualTo(42); } + public function case_some_null() + { + $this + ->exception(function () { + Some(null); + }) + ->isInstanceOf(RuntimeException::class) + ->hasMessage( + 'Called `' . SUT::class . '::some` with a `null` value, forbidden. ' . + 'Use `' . SUT::class . '::none` instead.' + ); + } + public function case_none() { $this