-
Notifications
You must be signed in to change notification settings - Fork 0
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
Add chunk to collection #35
Conversation
README.md
Outdated
@@ -33,6 +33,12 @@ composer install | |||
composer test-coverage | |||
``` | |||
|
|||
To run tests in a docker container (without coverage): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a tutorial on docker. Remove this part please
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not mind one way or the other, if you desire I shall remove it: however I thought it could help next person who would like to contribute to this repository. I found it easier to test the different versions of php with docker using this command instead of having to manually install them on the host machine.
docs/collection-interfaces.md
Outdated
public function chunk(int $size): array | ||
``` | ||
|
||
This method [mirrors the behavior of `array_chunk`](https://www.php.net/manual/function.array-chunk.php) and returns a zero indexed numeric array of the current Collection based on the chunk size provided. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
of the current collection <- This is english, we don't need to capitalize the nouns (we can but here it doesn't make sense)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was meant as a proper noun, which in English is capitalized, however you are right: in the context of this documentation we can strive for consistency.
docs/collection-trait.md
Outdated
@@ -95,6 +95,10 @@ $collection->toArray(); | |||
|
|||
Internally it is call the `ArrayIterator::append` and returning the instance to allow fluent calls. | |||
|
|||
## chunk | |||
|
|||
This method [mirrors the behavior of `array_chunk`](https://www.php.net/manual/function.array-chunk.php) and returns a zero indexed numeric array of the current Collection based on the chunk size provided. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is the same of the interface and is not giving any detail of the implementation done in the trait (and also the same "Collection With Uppercase On The First Letter")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will update!
src/CollectionTrait.php
Outdated
|
||
return array_map( | ||
static fn(array $chunk) => self::fromIterable($chunk), | ||
array_chunk($this->toArray(), $size) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can not just assume that user toArray
implementation will yield a valid array that can be used on the fromIterable
method (which will call the append
method for each member of the iterable).
You need to chunk the result of $this->getArrayCopy()
instead, as that method will return an array with all the items in the collection, which should be valid "entries" for the append
method.
array_chunk($this->getArrayCopy(), $size)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the tip!
tests/CollectionTest.php
Outdated
public function testChunkEach(Collection $collectionToChunk, int $chunkSize, array $expectedChunks): void | ||
{ | ||
$expectedLambda = function(CollectionStub $collection) use ($expectedChunks): void { | ||
static $i = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not a fan of static variables. Also this is not the "expected" lambda, This is the lambda (anonymous) function.
So please redo this to not depend on static vars on the anonymous function and instead pass it by reference
$i = 0;
$collection->eachChunk(
$chunkSize,
function(CollectionStub $collection) use (&$i, $expectedChunks): void {
self::assertEquals($collection, $expectedChunks[$i++]);
}
);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will refactor.
tests/CollectionTest.php
Outdated
@@ -485,6 +485,55 @@ public function testReverse(): void | |||
self::assertEquals([5, 4, 3, 2, 1], CollectionStub::fromIterable([1, 2, 3, 4, 5])->reverse()->toArray()); | |||
} | |||
|
|||
#[DataProvider('chunkDataProvider')] | |||
public function testChunk(Collection $collectionToChunk, int $chunkSize, array $expectedChunks): void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to be verbose. $collection
is a valid name as we are TESTING the CHUNKING of a COLLECTION (so no ambiguity here, let's keep it simple):
#[DataProvider('chunkDataProvider')]
public function testChunk(Collection $collection, int $chunkSize, array $expectedChunks): void
{
self::assertEquals($expectedChunks, $collection->chunk($chunkSize));
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will de-verbose it.
tests/CollectionTest.php
Outdated
@@ -553,6 +602,57 @@ public static function eachDataProvider(): array | |||
]; | |||
} | |||
|
|||
#[DataProvider('chunkEachDataProvider')] | |||
public function testChunkEach(Collection $collectionToChunk, int $chunkSize, array $expectedChunks): void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment as on testChunk. Rename to $collection
please.
docs/collection-trait.md
Outdated
@@ -117,6 +121,10 @@ Finally, it will return the duplicated collection, optionally calling the `uniqu | |||
|
|||
Internally, this method will iterate through each item of a collection, optionally rewind it at the end of the iteration, calling the anonymous function for each element. | |||
|
|||
## eachChunk | |||
|
|||
This method chunks the collection and executes the given anonymous function with each chunk. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same thing here.
docs/collection-trait.md
Outdated
@@ -95,6 +95,10 @@ $collection->toArray(); | |||
|
|||
Internally it is call the `ArrayIterator::append` and returning the instance to allow fluent calls. | |||
|
|||
## chunk | |||
|
|||
Internally this method chunks [a copy](https://www.php.net/manual/arrayobject.getarraycopy.php) of the collection with the [`array_chunk` php function](https://www.php.net/manual/function.array-chunk.php), returning a zero indexed array of collections of the same type as the initial one. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be aligned with the rest of the docs, change the way the PHP function is linked. Also you are linking the getArrayCopy
of the wrong class.
Suggestion:
Internally this method chunks the collection (by getting a copy with [getArrayCopy](https://www.php.net/manual/en/arrayiterator.getarraycopy.php) method) with the PHP [array_chunk](https://www.php.net/manual/function.array-chunk.php) function, returning a zero indexed array of collections of the same type as the initial one.
tests/CollectionTest.php
Outdated
#[DataProvider('chunkDataProvider')] | ||
public function testChunk(Collection $collection, int $chunkSize, array $expectedChunks): void | ||
{ | ||
self::assertEquals( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Single line
Quality Gate passedIssues Measures |
Description
The objective of this PR is to allow the user of the library to break a
Collection
into smaller chunks and optionally to operate on these chunks with an anonymous function.e.g.