Skip to content

Commit 3c5ae94

Browse files
authored
[10.x] Improve collection method return types (#46250)
* Improve `Collection::getOrPut()` return type * Improve `Collection::pipeInto()` and `LazyCollection::pipeInto()` return types * Improve `Collection::value()` return type
1 parent 1dff6a3 commit 3c5ae94

File tree

5 files changed

+25
-10
lines changed

5 files changed

+25
-10
lines changed

src/Illuminate/Collections/Collection.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,11 @@ public function get($key, $default = null)
456456
/**
457457
* Get an item from the collection by key or add it to collection if it does not exist.
458458
*
459+
* @template TGetOrPutValue
460+
*
459461
* @param mixed $key
460-
* @param mixed $value
461-
* @return mixed
462+
* @param TGetOrPutValue|(\Closure(): TGetOrPutValue) $value
463+
* @return TValue|TGetOrPutValue
462464
*/
463465
public function getOrPut($key, $value)
464466
{

src/Illuminate/Collections/Enumerable.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,8 +1078,10 @@ public function pipe(callable $callback);
10781078
/**
10791079
* Pass the collection into a new class.
10801080
*
1081-
* @param class-string $class
1082-
* @return mixed
1081+
* @template TPipeIntoValue
1082+
*
1083+
* @param class-string<TPipeIntoValue> $class
1084+
* @return TPipeIntoValue
10831085
*/
10841086
public function pipeInto($class);
10851087

src/Illuminate/Collections/Traits/EnumeratesValues.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,11 @@ public function firstWhere($key, $operator = null, $value = null)
296296
/**
297297
* Get a single key's value from the first matching item in the collection.
298298
*
299+
* @template TValueDefault
300+
*
299301
* @param string $key
300-
* @param mixed $default
301-
* @return mixed
302+
* @param TValueDefault|(\Closure(): TValueDefault) $default
303+
* @return TValue|TValueDefault
302304
*/
303305
public function value($key, $default = null)
304306
{
@@ -694,8 +696,10 @@ public function pipe(callable $callback)
694696
/**
695697
* Pass the collection into a new class.
696698
*
697-
* @param class-string $class
698-
* @return mixed
699+
* @template TPipeIntoValue
700+
*
701+
* @param class-string<TPipeIntoValue> $class
702+
* @return TPipeIntoValue
699703
*/
700704
public function pipeInto($class)
701705
{

types/Support/Collection.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,10 @@ function ($collection, $count) {
493493
assertType('User|null', $collection->firstWhere('string', 'string'));
494494
assertType('User|null', $collection->firstWhere('string', 'string', 'string'));
495495

496+
assertType('User|null', $collection->value('string'));
497+
assertType('string|User', $collection->value('string', 'string'));
498+
assertType('string|User', $collection->value('string', fn () => 'string'));
499+
496500
assertType('Illuminate\Support\Collection<string, int>', $collection::make(['string'])->flip());
497501

498502
assertType('Illuminate\Support\Collection<(int|string), Illuminate\Support\Collection<(int|string), User>>', $collection->groupBy('name'));
@@ -875,7 +879,7 @@ function ($collection, $count) {
875879
return 1;
876880
}));
877881

878-
assertType('mixed', $collection->pipeInto(User::class));
882+
assertType('User', $collection->pipeInto(User::class));
879883

880884
assertType('Illuminate\Support\Collection<(int|string), mixed>', $collection->make(['string' => 'string'])->pluck('string'));
881885
assertType('Illuminate\Support\Collection<(int|string), mixed>', $collection->make(['string' => 'string'])->pluck('string', 'string'));
@@ -951,6 +955,9 @@ function ($collection, $count) {
951955
return 'string';
952956
}));
953957

958+
assertType('string|User', $collection->getOrPut(0, 'string'));
959+
assertType('string|User', $collection->getOrPut(0, fn () => 'string'));
960+
954961
assertType('Illuminate\Support\Collection<int, User>', $collection->forget(1));
955962
assertType('Illuminate\Support\Collection<int, User>', $collection->forget([1, 2]));
956963

types/Support/LazyCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@
740740
return 1;
741741
}));
742742

743-
assertType('mixed', $collection->pipeInto(User::class));
743+
assertType('User', $collection->pipeInto(User::class));
744744

745745
assertType('Illuminate\Support\LazyCollection<int, mixed>', $collection->make(['string' => 'string'])->pluck('string'));
746746
assertType('Illuminate\Support\LazyCollection<int, mixed>', $collection->make(['string' => 'string'])->pluck('string', 'string'));

0 commit comments

Comments
 (0)