Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.x] Types: Collection chunk without preserving keys #54924

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1442,7 +1442,7 @@ public function firstOrFail($key = null, $operator = null, $value = null)
*
* @param int $size
* @param bool $preserveKeys
* @return static<int, static>
* @return ($preserveKeys is true ? static<int, static> : static<int, static<int, TValue>>)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this syntax supported by both psalm and phpstan?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, yeah:

  1. Psalm
  2. PHPStan

It's not new to the framework either:

* @return ($condition is true ? never : ($condition is non-empty-mixed ? never : TValue))

*/
public function chunk($size, $preserveKeys = true)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Collections/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1377,7 +1377,7 @@ public function firstOrFail($key = null, $operator = null, $value = null)
*
* @param int $size
* @param bool $preserveKeys
* @return static<int, static>
* @return ($preserveKeys is true ? static<int, static> : static<int, static<int, TValue>>)
*/
public function chunk($size, $preserveKeys = true)
{
Expand Down
12 changes: 6 additions & 6 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2148,18 +2148,18 @@ public function testChunkWhenGivenLessThanZero($collection)
#[DataProvider('collectionClassProvider')]
public function testChunkPreservingKeys($collection)
{
$data = new $collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
$data = new $collection(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5]);

$this->assertEquals(
[[0 => 1, 1 => 2, 2 => 3], [3 => 4, 4 => 5, 5 => 6], [6 => 7, 7 => 8, 8 => 9], [9 => 10]],
$data->chunk(3)->toArray()
[['a' => 1, 'b' => 2], ['c' => 3, 'd' => 4], ['e' => 5]],
$data->chunk(2)->toArray()
);

$data = new $collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
$data = new $collection([1, 2, 3, 4, 5]);

$this->assertEquals(
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]],
$data->chunk(3, false)->toArray()
[[0 => 1, 1 => 2], [0 => 3, 1 => 4], [0 => 5]],
$data->chunk(2, false)->toArray()
);
}

Expand Down
8 changes: 6 additions & 2 deletions types/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Users implements Arrayable
{
public function toArray(): array
{
return [new User()];
return [new User];
}
}

Expand All @@ -21,14 +21,16 @@ public function toArray(): array
/** @var Traversable<int, string> $traversable */
$traversable = new ArrayIterator(['string']);

$associativeCollection = collect(['John' => new User]);

class Invokable
{
public function __invoke(): string
{
return 'Taylor';
}
}
$invokable = new Invokable();
$invokable = new Invokable;

assertType('Illuminate\Support\Collection<int, User>', $collection);

Expand Down Expand Up @@ -806,6 +808,8 @@ function ($collection, $count) {

assertType('Illuminate\Support\Collection<int, Illuminate\Support\Collection<int, string>>', $collection::make(['string'])->chunk(1));
assertType('Illuminate\Support\Collection<int, Illuminate\Support\Collection<int, User>>', $collection->chunk(2));
assertType('Illuminate\Support\Collection<int, Illuminate\Support\Collection<string, User>>', $associativeCollection->chunk(2));
assertType('Illuminate\Support\Collection<int, Illuminate\Support\Collection<int, User>>', $associativeCollection->chunk(2, false));

assertType('Illuminate\Support\Collection<int, Illuminate\Support\Collection<int, User>>', $collection->chunkWhile(function ($user, $int, $collection) {
assertType('User', $user);
Expand Down
10 changes: 7 additions & 3 deletions types/Support/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@ class Users implements Arrayable
{
public function toArray(): array
{
return [new User()];
return [new User];
}
}

$collection = new LazyCollection([new User]);
$arrayable = new Users();
$arrayable = new Users;
/** @var iterable<int, int> $iterable */
$iterable = [1];
/** @var Traversable<int, string> $traversable */
$traversable = new ArrayIterator(['string']);
$generator = function () {
yield new User();
yield new User;
};

$associativeCollection = new LazyCollection(['Sam' => new User]);

assertType('Illuminate\Support\LazyCollection<int, User>', $collection);

assertType("Illuminate\Support\LazyCollection<int, 'string'>", new LazyCollection(['string']));
Expand Down Expand Up @@ -665,6 +667,8 @@ public function toArray(): array

assertType('Illuminate\Support\LazyCollection<int, Illuminate\Support\LazyCollection<int, string>>', $collection::make(['string'])->chunk(1));
assertType('Illuminate\Support\LazyCollection<int, Illuminate\Support\LazyCollection<int, User>>', $collection->chunk(2));
assertType('Illuminate\Support\LazyCollection<int, Illuminate\Support\LazyCollection<string, User>>', $associativeCollection->chunk(2));
assertType('Illuminate\Support\LazyCollection<int, Illuminate\Support\LazyCollection<int, User>>', $associativeCollection->chunk(2, false));

assertType('Illuminate\Support\LazyCollection<int, Illuminate\Support\LazyCollection<int, User>>', $collection->chunkWhile(function ($user, $int, $collection) {
assertType('User', $user);
Expand Down
Loading