Skip to content
This repository has been archived by the owner on Sep 20, 2021. It is now read-only.

Commit

Permalink
feat(option) Cannot call Some with null.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hywan committed Aug 18, 2017
2 parents 953ea08 + c8aeec0 commit d5c59f6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
namespace Hoa\Option;

use Hoa\Consistency;
use RuntimeException;

/**
* Class \Hoa\Option.
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions Test/Unit/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d5c59f6

Please sign in to comment.