Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for exception class name instead of supplier #25

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/Optional.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,32 @@ public function orElseGet(callable $otherSupplier): mixed
return static::isSupported($other) ? $other : throw new InvalidArgumentException('Other supplier must return supported other.');
}

public function orElseThrow(?callable $exceptionSupplier = null): mixed
/**
* @template E of Throwable
*
* @param null|class-string<E>|callable(): E $exceptionSupplier
*
* @return T
*
* @throws E
*/
public function orElseThrow(null|string|callable $exceptionSupplier = null): mixed
{
if ($exceptionSupplier === null) {
return $this->orElseThrow(static fn () => new Exception\CouldNotGetValueOfEmptyOptional());
}

if (is_string($exceptionSupplier)) {
/** @var class-string<E> $exceptionSupplier */
if (!class_exists($exceptionSupplier, autoload: true)) {
throw new InvalidArgumentException('Exception supplier must be existing class name.');
}
return $this->orElseThrow(static fn () => new $exceptionSupplier());
}

return $this->orElseGet(static function () use ($exceptionSupplier): never {
/** @var Throwable|mixed $exception */
$exception = $exceptionSupplier === null ? new Exception\CouldNotGetValueOfEmptyOptional() : $exceptionSupplier();
$exception = $exceptionSupplier();
if ($exception instanceof Throwable) {
throw $exception;
}
Expand Down
28 changes: 21 additions & 7 deletions tests/OptionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace PetrKnap\Optional;

use Exception as SomeException;
use DomainException as SomeException;
use LogicException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -213,20 +213,34 @@ public static function dataMethodOrElseGetWorks(): array
}

#[DataProvider('dataMethodOrElseThrowWorks')]
public function testMethodOrElseThrowWorks(Optional $optional, ?string $expectedValue, ?string $expectedException): void
public function testMethodOrElseThrowWorks(Optional $optional, null|string|callable $exceptionProvider, ?string $expectedValue, ?string $expectedException): void
{
if ($expectedException) {
self::expectException($expectedException);
}
self::assertSame($expectedValue, $optional->orElseThrow(static fn(): SomeException => new SomeException()));
self::assertSame($expectedValue, $optional->orElseThrow($exceptionProvider));
}

public static function dataMethodOrElseThrowWorks(): array
public static function dataMethodOrElseThrowWorks(): iterable
{
return self::makeDataSet([
[self::VALUE, null],
[null, SomeException::class],
$dataSet = self::makeDataSet([
[null, self::VALUE, null],
[null, null, SomeException::class],
]);
foreach ($dataSet as $name => $data) {
$data[3] = $data[3] === null ? null : Exception\CouldNotGetValueOfEmptyOptional::class;
yield "{$name} + null supplier" => $data;
}
$exceptionSupplier = static fn(): SomeException => new SomeException();
foreach ($dataSet as $name => $data) {
$data[1] = $exceptionSupplier;
yield "{$name} + callable supplier" => $data;
}
$exceptionSupplier = SomeException::class;
foreach ($dataSet as $name => $data) {
$data[1] = $exceptionSupplier;
yield "{$name} + string supplier" => $data;
}
}

private static function makeDataSet(array $args): array
Expand Down
Loading