Skip to content
Merged
Show file tree
Hide file tree
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
9 changes: 6 additions & 3 deletions src/Illuminate/Collections/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,13 @@ function value($value, ...$args)
/**
* Return a value if the given condition is true.
*
* @template TValue
* @template TDefault
*
* @param mixed $condition
* @param \Closure|mixed $value
* @param \Closure|mixed $default
* @return mixed
* @param TValue|\Closure(): TValue $value
* @param TDefault|\Closure(): TDefault $default
* @return ($condition is true ? TValue : ($condition is positive-int ? TValue : ($condition is non-falsy-string ? TValue : ($condition is non-empty-array ? TValue : ($condition is callable ? TValue|TDefault : TDefault)))))
Comment thread
marcreichel marked this conversation as resolved.
Outdated
*/
function when($condition, $value, $default = null)
{
Expand Down
21 changes: 21 additions & 0 deletions types/Collections/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,24 @@

return 42;
}, true));

assertType("'foo'", when(true, 'foo'));
assertType("'foo'", when(true, 'foo', 42));
assertType('null', when(false, 'foo'));
assertType('42', when(false, 'foo', 42));
assertType("'foo'", when(true, fn () => 'foo'));
assertType("'foo'", when(true, fn () => 'foo', fn () => 42));
assertType('null', when(false, fn () => 'foo'));
assertType('42', when(false, fn () => 'foo', fn () => 42));
assertType("'foo'", when(1, 'foo', 42));
assertType("'foo'", when(42, 'foo'));
assertType('null', when(0, 'foo'));
assertType('null', when(-42, 'foo'));
assertType('null', when(null, 'foo'));
assertType('42', when(['foo'], 42));
assertType('null', when([], 42));
assertType('42|null', when(random_int(0, 1), 42));
assertType('42|1337', when(random_int(0, 1), 42, 1337));
assertType("array{'bar'}|array{'foo'}", when(random_int(0, 1), ['foo'], ['bar']));
assertType('42|null', when(fn () => random_int(0, 1), 42));
assertType('42|1337', when(fn () => random_int(0, 1), 42, 1337));
Loading