@@ -114,7 +114,9 @@ function of(callable $callback, mixed $noneValue = null, bool $strict = true): O
114114 * ```
115115 *
116116 * @template U
117+ * @template E of \Throwable
117118 * @param callable():U $callback
119+ * @param class-string<E> $exceptionClass
118120 * @return Option<U>
119121 * @throws \Throwable
120122 */
@@ -135,88 +137,6 @@ function tryOf(
135137 }
136138}
137139
138- /**
139- * Wrap a callable into one that transforms its result into an `Option`.
140- * It will be a `Some` option containing the result if it is different from `$noneValue` (default `null`).
141- *
142- * # Examples
143- *
144- * Successful execution:
145- *
146- * ```
147- * self::assertEq(Option\ify(strtolower(...))("FRUITS"), Option\some("fruits"));
148- * ```
149- *
150- * Convertion of `null` to `Option\None`:
151- *
152- * ```
153- * self::assertEq(Option\ify(fn() => null)(), Option\none());
154- * ```
155- *
156- * @template U
157- * @param callable():U $callback
158- * @return \Closure(mixed...):Option<U>
159- */
160- function ify (callable $ callback , mixed $ noneValue = null , bool $ strict = true ): \Closure
161- {
162- return static fn (...$ args ) => Option \fromValue ($ callback (...$ args ), $ noneValue , $ strict );
163- }
164-
165- /**
166- * Wrap a callable into one that transforms its result into an `Option` like `Option\ify()` does
167- * but also return `Option\None` if it an exception matching $exceptionClass was thrown.
168- *
169- * # Examples
170- *
171- * Successful execution:
172- *
173- * ```
174- * self::assertEq(Option\tryIfy(strtolower(...))("FRUITS"), Option\some("fruits"));
175- * ```
176- *
177- * Convertion of `null` to `Option\None`:
178- *
179- * ```
180- * self::assertEq(Option\tryIfy(fn() => null)(), Option\none());
181- * ```
182- *
183- * Checked Exception:
184- *
185- * ```
186- * self::assertEq(Option\tryIfy(fn () => new \DateTimeImmutable("nope"))(), Option\none());
187- * ```
188- *
189- * Unchecked Exception:
190- *
191- * ```
192- * self::assertEq(Option\tryIfy(fn () => 1 / 0)(), Option\none());
193- * // @throws DivisionByZeroError Division by zero
194- * ```
195- *
196- * @template U
197- * @param callable():U $callback
198- * @return \Closure(mixed...):Option<U>
199- */
200- function tryIfy (
201- callable $ callback ,
202- mixed $ noneValue = null ,
203- bool $ strict = true ,
204- string $ exceptionClass = \Exception::class,
205- ): \Closure
206- {
207- return static function (...$ args ) use ($ callback , $ noneValue , $ strict , $ exceptionClass ): mixed {
208- try {
209- return Option \fromValue ($ callback (...$ args ), $ noneValue , $ strict );
210- } catch (\Throwable $ th ) {
211- if (\is_a ($ th , $ exceptionClass )) {
212- return Option \none ();
213- }
214-
215- throw $ th ;
216- }
217- };
218- }
219-
220140/**
221141 * Converts from `Option<Option<T>>` to `Option<T>`.
222142 *
0 commit comments