From c95279a9b9071f22255bea8307426ead504daf30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Rish=C3=B8j?= Date: Mon, 14 Oct 2024 22:47:51 +0200 Subject: [PATCH] restore type-narrowing for `throw_*` helpers (#53154) --- src/Illuminate/Support/helpers.php | 4 ++-- types/Support/Helpers.php | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Support/helpers.php b/src/Illuminate/Support/helpers.php index 5abe5befd5b0..6567f5ff922d 100644 --- a/src/Illuminate/Support/helpers.php +++ b/src/Illuminate/Support/helpers.php @@ -393,7 +393,7 @@ function tap($value, $callback = null) * @param TValue $condition * @param TException|class-string|string $exception * @param mixed ...$parameters - * @return ($condition is non-empty-mixed ? never : TValue) + * @return ($condition is true ? never : ($condition is non-empty-mixed ? never : TValue)) * * @throws TException */ @@ -421,7 +421,7 @@ function throw_if($condition, $exception = 'RuntimeException', ...$parameters) * @param TValue $condition * @param TException|class-string|string $exception * @param mixed ...$parameters - * @return ($condition is non-empty-mixed ? TValue : never) + * @return ($condition is true ? TValue : ($condition is non-empty-mixed ? TValue : never)) * * @throws TException */ diff --git a/types/Support/Helpers.php b/types/Support/Helpers.php index d0201de6172d..fd0484c3c36b 100644 --- a/types/Support/Helpers.php +++ b/types/Support/Helpers.php @@ -46,6 +46,10 @@ function testThrowIf(float|int $foo): void assertType('never', throw_if(true, Exception::class)); assertType('bool', throw_if(false, Exception::class)); assertType('false', throw_if(empty($foo))); + throw_if(is_float($foo)); + assertType('int', $foo); + throw_if($foo == false); + assertType('int|int<1, max>', $foo); assertType('null', throw_if(null, Exception::class)); assertType('string', throw_if('', Exception::class)); assertType('never', throw_if('foo', Exception::class)); @@ -57,6 +61,9 @@ function testThrowUnless(float|int $foo): void assertType('never', throw_unless(false, Exception::class)); assertType('true', throw_unless(empty($foo))); throw_unless(is_int($foo)); + assertType('int', $foo); + throw_unless($foo == false); + assertType('0', $foo); assertType('never', throw_unless(null, Exception::class)); assertType('never', throw_unless('', Exception::class)); assertType('string', throw_unless('foo', Exception::class));