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

Add chunk to collection #35

Merged
merged 11 commits into from
Nov 18, 2024
Merged

Add chunk to collection #35

merged 11 commits into from
Nov 18, 2024

Conversation

tdashton
Copy link
Contributor

@tdashton tdashton commented Nov 15, 2024

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.

$myUuids = MyUuidCollection::fromIterable([
    'a96d520d-bfb7-4a91-ac37-9f6fc3c3963a',
    '35bb7a38-abb7-4ebc-bb6a-d256cb0b1337',
    '89535491-bb46-429d-98f0-683d657b1cc8',
    '45ae5ce5-1840-4fea-a93f-342e0c9b12d1'
]);

$myUuids->eachChunk(
    2, // size
    function(Uuids $myUuidChunk) {
        $this->publisher->publish(
            $myUuidChunk
        );
    }
);
       

@tdashton tdashton requested a review from a team as a code owner November 15, 2024 13:01
README.md Outdated
@@ -33,6 +33,12 @@ composer install
composer test-coverage
```

To run tests in a docker container (without coverage):
Copy link
Collaborator

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

Copy link
Contributor Author

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.

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.
Copy link
Collaborator

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)

Copy link
Contributor Author

@tdashton tdashton Nov 18, 2024

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.

@@ -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.
Copy link
Collaborator

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")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will update!


return array_map(
static fn(array $chunk) => self::fromIterable($chunk),
array_chunk($this->toArray(), $size)
Copy link
Collaborator

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the tip!

public function testChunkEach(Collection $collectionToChunk, int $chunkSize, array $expectedChunks): void
{
$expectedLambda = function(CollectionStub $collection) use ($expectedChunks): void {
static $i = 0;
Copy link
Collaborator

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++]);
            }
        );

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will refactor.

@@ -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
Copy link
Collaborator

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));
    }

Copy link
Contributor Author

@tdashton tdashton Nov 18, 2024

Choose a reason for hiding this comment

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

Will de-verbose it.

@@ -553,6 +602,57 @@ public static function eachDataProvider(): array
];
}

#[DataProvider('chunkEachDataProvider')]
public function testChunkEach(Collection $collectionToChunk, int $chunkSize, array $expectedChunks): void
Copy link
Collaborator

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.

@@ -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.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same thing here.

@@ -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.
Copy link
Collaborator

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.

#[DataProvider('chunkDataProvider')]
public function testChunk(Collection $collection, int $chunkSize, array $expectedChunks): void
{
self::assertEquals(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Single line

@tdashton tdashton merged commit f41e949 into master Nov 18, 2024
8 checks passed
@tdashton tdashton deleted the makeCollectionChunkable branch November 18, 2024 14:38
@tdashton tdashton mentioned this pull request Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants