Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] feat: refine return type for throw_if and throw_unless to reflect actual behavior with "falsey" values #53154

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
let return type reflect actual behavior with falsey values
crishoj authored Oct 14, 2024
commit 4f81bf9e4fd508dd829ecf4db1773ff9dc6017c6
4 changes: 2 additions & 2 deletions src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
@@ -393,7 +393,7 @@ function tap($value, $callback = null)
* @param TValue $condition
* @param TException|class-string<TException>|string $exception
* @param mixed ...$parameters
* @return ($condition is true ? never : TValue)
* @return ($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<TException>|string $exception
* @param mixed ...$parameters
* @return ($condition is true ? TValue : never)
* @return ($condition is non-empty-mixed ? TValue : never)
*
* @throws TException
*/

Unchanged files with check annotations Beta

assertType('bool', throw_if(false, Exception::class));
assertType('false', throw_if(empty($foo)));
throw_if(is_float($foo));
assertType('int', $foo);

Check failure on line 50 in types/Support/Helpers.php

GitHub Actions / Types

Expected type int, actual: float|int
throw_if($foo == false);
assertType('int<min, -1>|int<1, max>', $foo);

Check failure on line 52 in types/Support/Helpers.php

GitHub Actions / Types

Expected type int<min, -1>|int<1, max>, actual: float|int
}
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);

Check failure on line 61 in types/Support/Helpers.php

GitHub Actions / Types

Expected type int, actual: float|int
throw_unless($foo == false);
assertType('0', $foo);

Check failure on line 63 in types/Support/Helpers.php

GitHub Actions / Types

Expected type 0, actual: float|int
}
assertType('int', transform('filled', fn () => 1, true));