Skip to content

Commit

Permalink
[10.x] Improve collection method return types (laravel#46250)
Browse files Browse the repository at this point in the history
* Improve `Collection::getOrPut()` return type

* Improve `Collection::pipeInto()` and `LazyCollection::pipeInto()` return types

* Improve `Collection::value()` return type
  • Loading branch information
axlon authored Mar 1, 2023
1 parent 1dff6a3 commit 3c5ae94
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,9 +456,11 @@ public function get($key, $default = null)
/**
* Get an item from the collection by key or add it to collection if it does not exist.
*
* @template TGetOrPutValue
*
* @param mixed $key
* @param mixed $value
* @return mixed
* @param TGetOrPutValue|(\Closure(): TGetOrPutValue) $value
* @return TValue|TGetOrPutValue
*/
public function getOrPut($key, $value)
{
Expand Down
6 changes: 4 additions & 2 deletions src/Illuminate/Collections/Enumerable.php
Original file line number Diff line number Diff line change
Expand Up @@ -1078,8 +1078,10 @@ public function pipe(callable $callback);
/**
* Pass the collection into a new class.
*
* @param class-string $class
* @return mixed
* @template TPipeIntoValue
*
* @param class-string<TPipeIntoValue> $class
* @return TPipeIntoValue
*/
public function pipeInto($class);

Expand Down
12 changes: 8 additions & 4 deletions src/Illuminate/Collections/Traits/EnumeratesValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,11 @@ public function firstWhere($key, $operator = null, $value = null)
/**
* Get a single key's value from the first matching item in the collection.
*
* @template TValueDefault
*
* @param string $key
* @param mixed $default
* @return mixed
* @param TValueDefault|(\Closure(): TValueDefault) $default
* @return TValue|TValueDefault
*/
public function value($key, $default = null)
{
Expand Down Expand Up @@ -694,8 +696,10 @@ public function pipe(callable $callback)
/**
* Pass the collection into a new class.
*
* @param class-string $class
* @return mixed
* @template TPipeIntoValue
*
* @param class-string<TPipeIntoValue> $class
* @return TPipeIntoValue
*/
public function pipeInto($class)
{
Expand Down
9 changes: 8 additions & 1 deletion types/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,10 @@ function ($collection, $count) {
assertType('User|null', $collection->firstWhere('string', 'string'));
assertType('User|null', $collection->firstWhere('string', 'string', 'string'));

assertType('User|null', $collection->value('string'));
assertType('string|User', $collection->value('string', 'string'));
assertType('string|User', $collection->value('string', fn () => 'string'));

assertType('Illuminate\Support\Collection<string, int>', $collection::make(['string'])->flip());

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

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

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

assertType('string|User', $collection->getOrPut(0, 'string'));
assertType('string|User', $collection->getOrPut(0, fn () => 'string'));

assertType('Illuminate\Support\Collection<int, User>', $collection->forget(1));
assertType('Illuminate\Support\Collection<int, User>', $collection->forget([1, 2]));

Expand Down
2 changes: 1 addition & 1 deletion types/Support/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@
return 1;
}));

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

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

0 comments on commit 3c5ae94

Please sign in to comment.