diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index a66f6adfcb14..5d473c26575b 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -181,10 +181,14 @@ public static function exists($array, $key) /** * Return the first element in an array passing a given truth test. * - * @param iterable $array - * @param callable|null $callback - * @param mixed $default - * @return mixed + * @template TKey + * @template TValue + * @template TFirstDefault + * + * @param iterable $array + * @param (callable(TValue, TKey): bool)|null $callback + * @param TFirstDefault|(\Closure(): TFirstDefault) $default + * @return TValue|TFirstDefault */ public static function first($array, callable $callback = null, $default = null) { diff --git a/types/Support/Arr.php b/types/Support/Arr.php new file mode 100644 index 000000000000..c2cd33580d95 --- /dev/null +++ b/types/Support/Arr.php @@ -0,0 +1,56 @@ + $iterable */ +$iterable = []; +/** @var Traversable $traversable */ +$traversable = []; + +assertType('User|null', Arr::first($array)); +assertType('User|null', Arr::first($array, function ($user) { + assertType('User', $user); + + return true; +})); +assertType('string|User', Arr::first($array, function ($user) { + assertType('User', $user); + + return false; +}, 'string')); +assertType('string|User', Arr::first($array, null, function () { + return 'string'; +})); + +assertType('User|null', Arr::first($iterable)); +assertType('User|null', Arr::first($iterable, function ($user) { + assertType('User', $user); + + return true; +})); +assertType('string|User', Arr::first($iterable, function ($user) { + assertType('User', $user); + + return false; +}, 'string')); +assertType('string|User', Arr::first($iterable, null, function () { + return 'string'; +})); + +assertType('User|null', Arr::first($traversable)); +assertType('User|null', Arr::first($traversable, function ($user) { + assertType('User', $user); + + return true; +})); +assertType('string|User', Arr::first($traversable, function ($user) { + assertType('User', $user); + + return false; +}, 'string')); +assertType('string|User', Arr::first($traversable, null, function () { + return 'string'; +}));