Skip to content

Commit

Permalink
fix: Collection::flatMap callback can return Enumerable (#970)
Browse files Browse the repository at this point in the history
  • Loading branch information
bastien-phi committed Oct 18, 2021
1 parent 60f19f8 commit d4766ed
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Fixed

- Fixed template resolution when `flatMap`'s callback returns an `Enumerable`

## [0.7.13] - 2021-10-14

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion stubs/Collection.stub
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Collection implements \ArrayAccess, Enumerable

/**
* @template TReturn
* @param callable(TValue, TKey): array<TReturn> $callback
* @param callable(TValue, TKey): (array<TReturn>|\Illuminate\Support\Enumerable<array-key, TReturn>) $callback
* @return static<TKey, TReturn>
*/
public function flatMap(callable $callback) {}
Expand Down
14 changes: 14 additions & 0 deletions tests/Features/ReturnTypes/CollectionStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\User;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Support\Collection as SupportCollection;
use function PHPStan\Testing\assertType;
use Traversable;

class CollectionStub
Expand Down Expand Up @@ -156,6 +157,19 @@ public function testFlatMap(EloquentCollection $collection)
});
}

/**
* @param EloquentCollection<User> $collection
*/
public function testFlatMapWithCollection(EloquentCollection $collection): void
{
assertType(
'Illuminate\Support\Collection<int, App\Account>',
$collection->flatMap(function (User $user, int $id) {
return $user->accounts;
})
);
}

/**
* @param EloquentCollection<User> $items
* @return EloquentCollection<User>
Expand Down

0 comments on commit d4766ed

Please sign in to comment.