diff --git a/src/functions/option.php b/src/functions/option.php index 649aefa..576b453 100644 --- a/src/functions/option.php +++ b/src/functions/option.php @@ -114,7 +114,9 @@ function of(callable $callback, mixed $noneValue = null, bool $strict = true): O * ``` * * @template U + * @template E of \Throwable * @param callable():U $callback + * @param class-string $exceptionClass * @return Option * @throws \Throwable */ @@ -135,88 +137,6 @@ function tryOf( } } -/** - * Wrap a callable into one that transforms its result into an `Option`. - * It will be a `Some` option containing the result if it is different from `$noneValue` (default `null`). - * - * # Examples - * - * Successful execution: - * - * ``` - * self::assertEq(Option\ify(strtolower(...))("FRUITS"), Option\some("fruits")); - * ``` - * - * Convertion of `null` to `Option\None`: - * - * ``` - * self::assertEq(Option\ify(fn() => null)(), Option\none()); - * ``` - * - * @template U - * @param callable():U $callback - * @return \Closure(mixed...):Option - */ -function ify(callable $callback, mixed $noneValue = null, bool $strict = true): \Closure -{ - return static fn (...$args) => Option\fromValue($callback(...$args), $noneValue, $strict); -} - -/** - * Wrap a callable into one that transforms its result into an `Option` like `Option\ify()` does - * but also return `Option\None` if it an exception matching $exceptionClass was thrown. - * - * # Examples - * - * Successful execution: - * - * ``` - * self::assertEq(Option\tryIfy(strtolower(...))("FRUITS"), Option\some("fruits")); - * ``` - * - * Convertion of `null` to `Option\None`: - * - * ``` - * self::assertEq(Option\tryIfy(fn() => null)(), Option\none()); - * ``` - * - * Checked Exception: - * - * ``` - * self::assertEq(Option\tryIfy(fn () => new \DateTimeImmutable("nope"))(), Option\none()); - * ``` - * - * Unchecked Exception: - * - * ``` - * self::assertEq(Option\tryIfy(fn () => 1 / 0)(), Option\none()); - * // @throws DivisionByZeroError Division by zero - * ``` - * - * @template U - * @param callable():U $callback - * @return \Closure(mixed...):Option - */ -function tryIfy( - callable $callback, - mixed $noneValue = null, - bool $strict = true, - string $exceptionClass = \Exception::class, -): \Closure -{ - return static function (...$args) use ($callback, $noneValue, $strict, $exceptionClass): mixed { - try { - return Option\fromValue($callback(...$args), $noneValue, $strict); - } catch (\Throwable $th) { - if (\is_a($th, $exceptionClass)) { - return Option\none(); - } - - throw $th; - } - }; -} - /** * Converts from `Option>` to `Option`. * diff --git a/tests/Unit/Option/IfyTest.php b/tests/Unit/Option/IfyTest.php deleted file mode 100644 index 6a765bd..0000000 --- a/tests/Unit/Option/IfyTest.php +++ /dev/null @@ -1,72 +0,0 @@ - $expected - */ - public function testIfy(Option $expected, mixed $value, mixed $noneValue, bool $strict = true): void - { - Assert::assertEquals($expected, Option\ify(static fn () => $value, $noneValue, strict: $strict)()); - } - - /** - * @dataProvider fromValueMatrix - * @param Option $expected - */ - public function testTryOf(Option $expected, mixed $value, mixed $noneValue, bool $strict = true): void - { - Assert::assertEquals($expected, Option\tryIfy(static fn () => $value, $noneValue, strict: $strict)()); - } - - public function testOfDefaultToNull(): void - { - Assert::assertEquals(Option\none(), Option\ify(static fn () => null)()); - Assert::assertEquals(Option\some(1), Option\ify(static fn () => 1)()); - } - - public function testTryOfDefaultToNull(): void - { - Assert::assertEquals(Option\none(), Option\tryIfy(static fn () => null)()); - Assert::assertEquals(Option\some(1), Option\tryIfy(static fn () => 1)()); - } - - public function testOfDefaultToStrict(): void - { - $o = (object)[]; - - Assert::assertEquals(Option\none(), Option\ify(static fn () => $o, (object)[], strict: false)()); - Assert::assertEquals($o, Option\ify(static fn () => $o, (object)[])()->unwrap()); - } - - public function testTryOfDefaultToStrict(): void - { - $o = (object)[]; - - Assert::assertEquals(Option\none(), Option\tryIfy(static fn () => $o, (object)[], strict: false)()); - Assert::assertEquals($o, Option\tryIfy(static fn () => $o, (object)[])()->unwrap()); - } - - public function testTryOfExeptions(): void - { - // @phpstan-ignore-next-line - Assert::assertEquals(Option\none(), Option\tryIfy(static fn () => new \DateTimeImmutable("nope"))()); - - try { - // @phpstan-ignore-next-line - Option\tryIfy(static fn () => 1 / 0)(); - Assert::fail("An exception should have been thrown"); - } catch (\DivisionByZeroError) { - } - } -}