Skip to content
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"psr-4": {
"TH\\Maybe\\": "src/"
},
"files": ["src/functions/option.php", "src/functions/result.php"]
"files": ["src/functions/internal.php", "src/functions/option.php", "src/functions/result.php"]
},
"autoload-dev": {
"psr-4": {
Expand Down
31 changes: 12 additions & 19 deletions src/functions/internal.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,22 @@
namespace TH\Maybe\Internal;

/**
* Call $callback with $exception if it matches one of $exceptionClasses
* and return its value, or rethrow it otherwise.
* Check if the type of the given value is any of the passed classes.
*
* @template E of \Throwable
* @template T
* @param E $error
* @param callable(E): T $callback
* @param class-string<E> $exceptionClasses
* @throws \Throwable
* @return T
* @internal
* @nodoc
* @param iterable<class-string<T>> $classes
* @psalm-assert-if-false !T $value
* @psalm-assert-if-true T $value
*/
function trap(
\Throwable $error,
callable $callback,
string ...$exceptionClasses,
): mixed {
foreach ($exceptionClasses as $exceptionClass) {
if (\is_a($error, $exceptionClass)) {
return $callback($error);
function isOfAnyClass(
object $value,
iterable $classes,
): bool {
foreach ($classes as $class) {
if (\is_a($value, $class)) {
return true;
}
}

throw $error;
return false;
}
7 changes: 4 additions & 3 deletions src/functions/option.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use TH\Maybe\Option;
use TH\Maybe\Result;
use TH\Maybe\Tests\Helpers\IgnoreUnusedResults;
use function TH\Maybe\Internal\isOfAnyClass;

/**
* Return a `Option\Some` option containing `$value`.
Expand Down Expand Up @@ -116,20 +117,20 @@ function of(callable $callback, mixed $noneValue = null, bool $strict = true): O
* @template U
* @template E of \Throwable
* @param callable():U $callback
* @param class-string<E> $exceptionClass
* @param list<class-string<E>>|class-string<E> $exceptionClass
* @return Option<U>
* @throws \Throwable
*/
function tryOf(
callable $callback,
mixed $noneValue = null,
bool $strict = true,
string $exceptionClass = \Exception::class,
array|string $exceptionClass = \Exception::class,
): Option {
try {
return Option\of($callback, $noneValue, $strict);
} catch (\Throwable $th) {
if (\is_a($th, $exceptionClass)) {
if (isOfAnyClass($th, (array) $exceptionClass)) {
return Option\none();
}

Expand Down
7 changes: 4 additions & 3 deletions src/functions/result.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use TH\Maybe\Option;
use TH\Maybe\Result;
use TH\Maybe\Tests\Helpers\IgnoreUnusedResults;
use function TH\Maybe\Internal\isOfAnyClass;

/**
* Return a `Result\Ok` Result containing `$value`.
Expand Down Expand Up @@ -78,18 +79,18 @@ function err(mixed $value): Result\Err
* @template U
* @template E of \Throwable
* @param callable(mixed...):U $callback
* @param class-string<E> $exceptionClass
* @param list<class-string<E>>|class-string<E> $exceptionClass
* @return Result<U,E>
* @throws \Throwable
*/
#[ExamplesSetup(IgnoreUnusedResults::class)]
function trap(callable $callback, string $exceptionClass = \Exception::class): Result
function trap(callable $callback, array|string $exceptionClass = \Exception::class): Result
{
try {
/** @var Result<U,E> */
return Result\ok($callback());
} catch (\Throwable $th) {
if (\is_a($th, $exceptionClass)) {
if (isOfAnyClass($th, (array) $exceptionClass)) {
return Result\err($th);
}

Expand Down
30 changes: 29 additions & 1 deletion tests/Unit/Option/OfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function testTryOfDefaultToStrict(): void
Assert::assertEquals($o, Option\tryOf(static fn () => $o, (object)[])->unwrap());
}

public function testTryOfExeptions(): void
public function testTryOfExceptions(): void
{
// @phpstan-ignore-next-line
Assert::assertEquals(Option\none(), Option\tryOf(static fn () => new \DateTimeImmutable("nope")));
Expand All @@ -69,4 +69,32 @@ public function testTryOfExeptions(): void
} catch (\DivisionByZeroError) {
}
}

public function testTryOfExceptionsWithExpectedClass(): void
{
Assert::assertEquals(
Option\none(),
// @phpstan-ignore-next-line
Option\tryOf(static fn () => 1 / 0, exceptionClass: \ArithmeticError::class),
);
Assert::assertEquals(
Option\none(),
// @phpstan-ignore-next-line
Option\tryOf(static fn () => 1 / 0, exceptionClass: [\LogicException::class, \ArithmeticError::class]),
);

try {
// @phpstan-ignore-next-line
Option\tryOf(static fn () => 1 / 0, exceptionClass: [\LogicException::class, \RuntimeException::class]);
Assert::fail("An exception should have been thrown");
} catch (\DivisionByZeroError) {
}

try {
// @phpstan-ignore-next-line
Option\tryOf(static fn () => 1 / 0, exceptionClass: \LogicException::class);
Assert::fail("An exception should have been thrown");
} catch (\DivisionByZeroError) {
}
}
}