Skip to content

Commit 46dc7a4

Browse files
authored
feat: narrow types for throw_if and throw_unless (#53005)
1 parent 2587311 commit 46dc7a4

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/Illuminate/Support/helpers.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ function tap($value, $callback = null)
393393
* @param TValue $condition
394394
* @param TException|class-string<TException>|string $exception
395395
* @param mixed ...$parameters
396-
* @return TValue
396+
* @return ($condition is true ? never : TValue)
397397
*
398398
* @throws TException
399399
*/
@@ -421,7 +421,7 @@ function throw_if($condition, $exception = 'RuntimeException', ...$parameters)
421421
* @param TValue $condition
422422
* @param TException|class-string<TException>|string $exception
423423
* @param mixed ...$parameters
424-
* @return TValue
424+
* @return ($condition is true ? TValue : never)
425425
*
426426
* @throws TException
427427
*/

types/Support/Helpers.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,27 @@
4141
}));
4242
assertType('Illuminate\Support\HigherOrderTapProxy', tap(new User()));
4343

44-
assertType('bool', throw_if(true, Exception::class));
44+
function testThrowIf(float|int $foo): void
45+
{
46+
assertType('never', throw_if(true, Exception::class));
47+
assertType('bool', throw_if(false, Exception::class));
48+
assertType('false', throw_if(empty($foo)));
49+
throw_if(is_float($foo));
50+
assertType('int', $foo);
51+
throw_if($foo == false);
52+
assertType('int<min, -1>|int<1, max>', $foo);
53+
}
4554

46-
assertType('bool', throw_unless(true, Exception::class));
55+
function testThrowUnless(float|int $foo): void
56+
{
57+
assertType('bool', throw_unless(true, Exception::class));
58+
assertType('never', throw_unless(false, Exception::class));
59+
assertType('true', throw_unless(empty($foo)));
60+
throw_unless(is_int($foo));
61+
assertType('int', $foo);
62+
throw_unless($foo == false);
63+
assertType('0', $foo);
64+
}
4765

4866
assertType('int', transform('filled', fn () => 1, true));
4967
assertType('int', transform(['filled'], fn () => 1));

0 commit comments

Comments
 (0)