Skip to content

Commit 3e8987d

Browse files
authored
Allow closures when calling throw_if (#57349)
* Allow closures when calling throw_if * Refactor docblock for throw_if/throw_unless
1 parent 627bb64 commit 3e8987d

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/Illuminate/Support/helpers.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,18 +398,24 @@ function tap($value, $callback = null)
398398
* Throw the given exception if the given condition is true.
399399
*
400400
* @template TValue
401+
* @template TParams of mixed
401402
* @template TException of \Throwable
403+
* @template TExceptionValue of TException|class-string<TException>|string
402404
*
403405
* @param TValue $condition
404-
* @param TException|class-string<TException>|string $exception
405-
* @param mixed ...$parameters
406+
* @param Closure(TParams): TExceptionValue|TExceptionValue $exception
407+
* @param TParams ...$parameters
406408
* @return ($condition is true ? never : ($condition is non-empty-mixed ? never : TValue))
407409
*
408410
* @throws TException
409411
*/
410412
function throw_if($condition, $exception = 'RuntimeException', ...$parameters)
411413
{
412414
if ($condition) {
415+
if ($exception instanceof Closure) {
416+
$exception = $exception(...$parameters);
417+
}
418+
413419
if (is_string($exception) && class_exists($exception)) {
414420
$exception = new $exception(...$parameters);
415421
}
@@ -426,11 +432,13 @@ function throw_if($condition, $exception = 'RuntimeException', ...$parameters)
426432
* Throw the given exception unless the given condition is true.
427433
*
428434
* @template TValue
435+
* @template TParams of mixed
429436
* @template TException of \Throwable
437+
* @template TExceptionValue of TException|class-string<TException>|string
430438
*
431439
* @param TValue $condition
432-
* @param TException|class-string<TException>|string $exception
433-
* @param mixed ...$parameters
440+
* @param Closure(TParams): TExceptionValue|TExceptionValue $exception
441+
* @param TParams ...$parameters
434442
* @return ($condition is false ? never : ($condition is non-empty-mixed ? TValue : never))
435443
*
436444
* @throws TException

tests/Support/SupportHelpersTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,30 @@ public function testThrowExceptionAsStringWithMessage()
832832
throw_if(true, LogicException::class, 'test');
833833
}
834834

835+
public function testThrowClosureException()
836+
{
837+
$this->expectException(\Exception::class);
838+
$this->expectExceptionMessage('test');
839+
840+
throw_if(true, fn () => new \Exception('test'));
841+
}
842+
843+
public function testThrowClosureWithParamsException()
844+
{
845+
$this->expectException(\Exception::class);
846+
$this->expectExceptionMessage('test');
847+
848+
throw_if(true, fn (string $message) => new \Exception($message), 'test');
849+
}
850+
851+
public function testThrowClosureStringWithParamsException()
852+
{
853+
$this->expectException(\Exception::class);
854+
$this->expectExceptionMessage('test');
855+
856+
throw_if(true, fn () => \Exception::class, 'test');
857+
}
858+
835859
public function testThrowUnless()
836860
{
837861
$this->expectException(LogicException::class);

0 commit comments

Comments
 (0)