-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[11.x] Add generics for Arr::first() (#50514)
* Add generics for Arr::first() * Add TKey
- Loading branch information
Showing
2 changed files
with
64 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Arr; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
$array = [new User]; | ||
/** @var iterable<int, User> $iterable */ | ||
$iterable = []; | ||
/** @var Traversable<int, User> $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'; | ||
})); |