Skip to content

Commit 7d87cde

Browse files
authored
Improve collection return types (#56599)
1 parent ab9e249 commit 7d87cde

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/Illuminate/Collections/Collection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ public function select($keys)
10051005
* Get and remove the last N items from the collection.
10061006
*
10071007
* @param int $count
1008-
* @return static<int, TValue>|TValue|null
1008+
* @return ($count is 1 ? TValue|null : static<int, TValue>)
10091009
*/
10101010
public function pop($count = 1)
10111011
{
@@ -1127,7 +1127,7 @@ public function put($key, $value)
11271127
*
11281128
* @param (callable(self<TKey, TValue>): int)|int|null $number
11291129
* @param bool $preserveKeys
1130-
* @return static<int, TValue>|TValue
1130+
* @return ($number is null ? TValue : static<int, TValue>)
11311131
*
11321132
* @throws \InvalidArgumentException
11331133
*/
@@ -1250,7 +1250,7 @@ public function after($value, $strict = false)
12501250
* Get and remove the first N items from the collection.
12511251
*
12521252
* @param int<0, max> $count
1253-
* @return static<int, TValue>|TValue|null
1253+
* @return ($count is 1 ? TValue|null : static<int, TValue>)
12541254
*
12551255
* @throws \InvalidArgumentException
12561256
*/

types/Support/Collection.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -691,8 +691,8 @@ function ($collection, $count) {
691691
assertType('Illuminate\Support\Collection<int, string>', $collection->make(['string'])->concat(['string']));
692692
assertType('Illuminate\Support\Collection<int, int|string>', $collection->make([1])->concat(['string']));
693693

694-
assertType('Illuminate\Support\Collection<int, int>|int', $collection->make([1])->random(2));
695-
assertType('Illuminate\Support\Collection<int, string>|string', $collection->make(['string'])->random());
694+
assertType('Illuminate\Support\Collection<int, int>', $collection::make([1])->random(2));
695+
assertType('string', $collection::make(['string'])->random());
696696

697697
assertType('1', $collection
698698
->reduce(function ($null, $user) {
@@ -997,8 +997,10 @@ function ($collection, $count) {
997997
assertType('Illuminate\Support\Collection<int, User>', $collection->forget(1));
998998
assertType('Illuminate\Support\Collection<int, User>', $collection->forget([1, 2]));
999999

1000-
assertType('Illuminate\Support\Collection<int, User>|User|null', $collection->pop(1));
1001-
assertType('Illuminate\Support\Collection<int, string>|string|null', $collection::make([
1000+
assertType('User|null', $collection->pop());
1001+
assertType('Illuminate\Support\Collection<int, User>', $collection->pop(2));
1002+
1003+
assertType('Illuminate\Support\Collection<int, string>', $collection::make([
10021004
'string-key-1' => 'string-value-1',
10031005
'string-key-2' => 'string-value-2',
10041006
])->pop(2));
@@ -1020,8 +1022,8 @@ function ($collection, $count) {
10201022
'string-key-1' => 'string-value-1',
10211023
])->put('string-key-2', 'string-value-2'));
10221024

1023-
assertType('Illuminate\Support\Collection<int, User>|User|null', $collection->shift(1));
1024-
assertType('Illuminate\Support\Collection<int, string>|string|null', $collection::make([
1025+
assertType('User|null', $collection->shift());
1026+
assertType('Illuminate\Support\Collection<int, string>', $collection::make([
10251027
'string-key-1' => 'string-value-1',
10261028
'string-key-2' => 'string-value-2',
10271029
])->shift(2));

0 commit comments

Comments
 (0)