Skip to content

Commit

Permalink
consistent approach to exceptions (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
akondas authored May 19, 2024
1 parent 69e4584 commit 9ec74be
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 21 deletions.
11 changes: 9 additions & 2 deletions src/Collection/GenericList/Nil.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Munus\Collection\GenericList;

use Munus\Collection\GenericList;
use Munus\Exception\NoSuchElementException;

/**
* @template T
Expand Down Expand Up @@ -32,13 +33,19 @@ public function isEmpty(): bool
return true;
}

/**
* @throws NoSuchElementException
*/
public function head()
{
throw new \RuntimeException('head of empty list');
throw new NoSuchElementException('head of empty list');
}

/**
* @throws NoSuchElementException
*/
public function tail()
{
throw new \RuntimeException('tail of empty list');
throw new NoSuchElementException('tail of empty list');
}
}
5 changes: 4 additions & 1 deletion src/Collection/Iterator/StreamIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Munus\Collection\Iterator;
use Munus\Collection\Stream;
use Munus\Collection\Stream\Cons;
use Munus\Exception\NoSuchElementException;
use Munus\Lazy;

/**
Expand Down Expand Up @@ -43,12 +44,14 @@ public function hasNext(): bool
}

/**
* @throws NoSuchElementException
*
* @return T
*/
public function next()
{
if (!$this->hasNext()) {
throw new \LogicException('next() on empty iterator');
throw new NoSuchElementException('next() on empty iterator');
}
/** @var Cons<T> $stream */
$stream = $this->current->get();
Expand Down
6 changes: 5 additions & 1 deletion src/Collection/Set.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Munus\Collection;

use Munus\Collection\Iterator\ArrayIterator;
use Munus\Exception\NoSuchElementException;

/**
* @template T
Expand Down Expand Up @@ -199,14 +200,17 @@ public function iterator(): Iterator
return new ArrayIterator($this->elements);
}

/**
* @throws NoSuchElementException
*/
public function head()
{
reset($this->elements);

$element = current($this->elements);

if ($element === false) {
throw new \RuntimeException('Set is empty');
throw new NoSuchElementException('Set is empty');
}

return $element;
Expand Down
11 changes: 9 additions & 2 deletions src/Collection/Stream/EmptyStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Munus\Collection\Iterator;
use Munus\Collection\Stream;
use Munus\Collection\Traversable;
use Munus\Exception\NoSuchElementException;

/**
* Empty is better but it is reserved keyword.
Expand All @@ -31,14 +32,20 @@ public function length(): int
return 0;
}

/**
* @throws NoSuchElementException
*/
public function head()
{
throw new \RuntimeException('head of empty stream');
throw new NoSuchElementException('head of empty stream');
}

/**
* @throws NoSuchElementException
*/
public function tail()
{
throw new \RuntimeException('tail of empty stream');
throw new NoSuchElementException('tail of empty stream');
}

public function isEmpty(): bool
Expand Down
5 changes: 4 additions & 1 deletion src/Control/Either/Left.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Munus\Control\Either;

use Munus\Control\Either;
use Munus\Exception\NoSuchElementException;

/**
* @template L
Expand Down Expand Up @@ -38,11 +39,13 @@ public function isRight(): bool
}

/**
* @throws NoSuchElementException
*
* @return R
*/
public function get()
{
throw new \BadMethodCallException('get() on Left');
throw new NoSuchElementException('get() on Left');
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Control/Either/Right.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Munus\Control\Either;

use Munus\Control\Either;
use Munus\Exception\NoSuchElementException;

/**
* @template L
Expand Down Expand Up @@ -46,10 +47,12 @@ public function get()
}

/**
* @throws NoSuchElementException
*
* @return L
*/
public function getLeft()
{
throw new \BadMethodCallException('getLeft() on Right');
throw new NoSuchElementException('getLeft() on Right');
}
}
5 changes: 4 additions & 1 deletion src/Control/Option/None.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Munus\Control\Option;

use Munus\Control\Option;
use Munus\Exception\NoSuchElementException;

/**
* @template T
Expand All @@ -19,11 +20,13 @@ public function isEmpty(): bool
}

/**
* @throws NoSuchElementException
*
* @return T
*/
public function get()
{
throw new \RuntimeException('No value present');
throw new NoSuchElementException('No value present');
}

public function equals($object): bool
Expand Down
5 changes: 4 additions & 1 deletion src/Control/TryTo/Failure.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Munus\Control\TryTo;

use Munus\Control\TryTo;
use Munus\Exception\NoSuchElementException;

/**
* @template T
Expand Down Expand Up @@ -34,11 +35,13 @@ public function isFailure(): bool
}

/**
* @throws NoSuchElementException
*
* @return T
*/
public function get()
{
throw new \BadMethodCallException('get() on Failure');
throw new NoSuchElementException('get() on Failure');
}

public function getCause(): \Throwable
Expand Down
6 changes: 5 additions & 1 deletion src/Control/TryTo/Success.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Munus\Control\TryTo;

use Munus\Control\TryTo;
use Munus\Exception\NoSuchElementException;

/**
* @template T
Expand Down Expand Up @@ -44,9 +45,12 @@ public function get()
return $this->value;
}

/**
* @throws NoSuchElementException
*/
public function getCause(): \Throwable
{
throw new \BadMethodCallException('getCause() on Success');
throw new NoSuchElementException('getCause() on Success');
}

public function isEmpty(): bool
Expand Down
5 changes: 3 additions & 2 deletions tests/Collection/GenericListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Munus\Collection\Stream\Collectors;
use Munus\Collection\Traversable;
use Munus\Control\Option;
use Munus\Exception\NoSuchElementException;
use Munus\Tests\Stub\Event;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -259,14 +260,14 @@ public function testIndexOfOnEmptyList(): void

public function testHeadOfEmptyList(): void
{
$this->expectException(\RuntimeException::class);
$this->expectException(NoSuchElementException::class);

GenericList::empty()->head();
}

public function testTailOfEmptyList(): void
{
$this->expectException(\RuntimeException::class);
$this->expectException(NoSuchElementException::class);

GenericList::empty()->tail();
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Collection/SetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Munus\Collection\Stream\Collectors;
use Munus\Collection\Traversable;
use Munus\Control\Option;
use Munus\Exception\NoSuchElementException;
use PHPUnit\Framework\TestCase;

final class SetTest extends TestCase
Expand Down Expand Up @@ -112,7 +113,7 @@ public function testEmptySetDoesntHaveHead(): void
{
$set = Set::empty();

$this->expectException(\RuntimeException::class);
$this->expectException(NoSuchElementException::class);

$set->head();
}
Expand Down
5 changes: 3 additions & 2 deletions tests/Collection/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Munus\Collection\Stream\Collectors;
use Munus\Collection\Traversable;
use Munus\Control\Option;
use Munus\Exception\NoSuchElementException;
use Munus\Lazy;
use Munus\Tests\Stub\Event;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -317,14 +318,14 @@ public function testIndexOfOnEmptyStream(): void

public function testHeadOfEmptyStream(): void
{
$this->expectException(\RuntimeException::class);
$this->expectException(NoSuchElementException::class);

Stream::empty()->head();
}

public function testTailOfEmptyStream(): void
{
$this->expectException(\RuntimeException::class);
$this->expectException(NoSuchElementException::class);

Stream::empty()->tail();
}
Expand Down
5 changes: 3 additions & 2 deletions tests/Control/EitherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Munus\Control\Either\Left;
use Munus\Control\Either\Right;
use Munus\Control\Option;
use Munus\Exception\NoSuchElementException;
use Munus\Tests\Stub\Expect;
use Munus\Tests\Stub\Failure;
use Munus\Tests\Stub\Result;
Expand Down Expand Up @@ -39,7 +40,7 @@ public function testLeftGetOrElse(): void

public function testLeftThrowExceptionOnGet(): void
{
$this->expectException(\BadMethodCallException::class);
$this->expectException(NoSuchElementException::class);
(new Left(null))->get();
}

Expand All @@ -58,7 +59,7 @@ public function testRightIsRight(): void

public function testRightThrowExceptionOnGetLeft(): void
{
$this->expectException(\BadMethodCallException::class);
$this->expectException(NoSuchElementException::class);
(new Right(null))->getLeft();
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Control/OptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Munus\Collection\Stream;
use Munus\Collection\Stream\Collectors;
use Munus\Control\Option;
use Munus\Exception\NoSuchElementException;
use Munus\Lazy;
use Munus\Tests\Stub\Expect;
use Munus\Tests\Stub\Success;
Expand Down Expand Up @@ -191,7 +192,7 @@ public function testOptionIfPresent(): void

public function testOptionOfNoneGet(): void
{
$this->expectException(\RuntimeException::class);
$this->expectException(NoSuchElementException::class);

Option::none()->get();
}
Expand Down
5 changes: 3 additions & 2 deletions tests/Control/TryToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Munus\Collection\Stream\Collectors;
use Munus\Control\Option;
use Munus\Control\TryTo;
use Munus\Exception\NoSuchElementException;
use Munus\Tests\Stub\Expect;
use Munus\Tests\Stub\Result;
use Munus\Tests\Stub\Success;
Expand Down Expand Up @@ -290,14 +291,14 @@ public function testTryToIteratorFailure(): void

public function testTryToGetCauseOnSuccess(): void
{
$this->expectException(\BadMethodCallException::class);
$this->expectException(NoSuchElementException::class);

TryTo::run(function () {return 'munus'; })->getCause();
}

public function testTryToGetOnFailure(): void
{
$this->expectException(\BadMethodCallException::class);
$this->expectException(NoSuchElementException::class);

TryTo::run(fn () => throw new \RuntimeException())->get();
}
Expand Down

0 comments on commit 9ec74be

Please sign in to comment.