Skip to content

Commit ddfbbf0

Browse files
authored
restore type-narrowing for throw_* helpers (#53154) (#53164)
1 parent 231091c commit ddfbbf0

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-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 ($condition is non-empty-mixed ? never : TValue)
396+
* @return ($condition is true ? never : ($condition is non-empty-mixed ? 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 ($condition is non-empty-mixed ? TValue : never)
424+
* @return ($condition is false ? never : ($condition is non-empty-mixed ? TValue : never))
425425
*
426426
* @throws TException
427427
*/

types/Support/Helpers.php

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

44-
function testThrowIf(float|int $foo): void
44+
function testThrowIf(float|int $foo, DateTime $bar = null): void
4545
{
4646
assertType('never', throw_if(true, Exception::class));
4747
assertType('bool', throw_if(false, Exception::class));
4848
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+
54+
// Truthy/falsey argument
55+
throw_if($bar);
56+
assertType('null', $bar);
4957
assertType('null', throw_if(null, Exception::class));
5058
assertType('string', throw_if('', Exception::class));
5159
assertType('never', throw_if('foo', Exception::class));
5260
}
5361

54-
function testThrowUnless(float|int $foo): void
62+
function testThrowUnless(float|int $foo, DateTime $bar = null): void
5563
{
5664
assertType('bool', throw_unless(true, Exception::class));
5765
assertType('never', throw_unless(false, Exception::class));
5866
assertType('true', throw_unless(empty($foo)));
5967
throw_unless(is_int($foo));
68+
assertType('int', $foo);
69+
throw_unless($foo == false);
70+
assertType('0', $foo);
71+
throw_unless($bar instanceof DateTime);
72+
assertType('DateTime', $bar);
73+
74+
// Truthy/falsey argument
6075
assertType('never', throw_unless(null, Exception::class));
6176
assertType('never', throw_unless('', Exception::class));
6277
assertType('string', throw_unless('foo', Exception::class));

0 commit comments

Comments
 (0)