Skip to content

Commit 11355a4

Browse files
authored
[11.x] feat: refine return type for throw_if and throw_unless to reflect actual behavior with "falsey" values (#53154)
* let return type reflect actual behavior with falsey values * let type assertions reflect actual behavior with falsey values
1 parent 1d7e000 commit 11355a4

File tree

2 files changed

+8
-9
lines changed

2 files changed

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

types/Support/Helpers.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@ function testThrowIf(float|int $foo): void
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);
49+
assertType('null', throw_if(null, Exception::class));
50+
assertType('string', throw_if('', Exception::class));
51+
assertType('never', throw_if('foo', Exception::class));
5352
}
5453

5554
function testThrowUnless(float|int $foo): void
@@ -58,9 +57,9 @@ function testThrowUnless(float|int $foo): void
5857
assertType('never', throw_unless(false, Exception::class));
5958
assertType('true', throw_unless(empty($foo)));
6059
throw_unless(is_int($foo));
61-
assertType('int', $foo);
62-
throw_unless($foo == false);
63-
assertType('0', $foo);
60+
assertType('never', throw_unless(null, Exception::class));
61+
assertType('never', throw_unless('', Exception::class));
62+
assertType('string', throw_unless('foo', Exception::class));
6463
}
6564

6665
assertType('int', transform('filled', fn () => 1, true));

0 commit comments

Comments
 (0)