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

Changed CollectionTrait::shuffle() to return all elements in collection. #14763

Merged
merged 3 commits into from
Jul 5, 2020

Conversation

othercorey
Copy link
Member

Fixes #14757

@othercorey othercorey added this to the 4.0.10 milestone Jul 3, 2020
@othercorey
Copy link
Member Author

gethostbynamel() is returning 2 addresses for 'localhost' now:

1) Cake\Test\TestCase\Network\SocketTest::testAddresses
Failed asserting that two arrays are identical.
--- Expected
+++ Actual
@@ @@
 Array &0 (
     0 => '127.0.0.1'
+    1 => '127.0.0.1'
 )

@@ -330,7 +330,7 @@ public function sumOf($matcher = null)
*/
public function shuffle(): CollectionInterface
{
$elements = $this->toArray();
$elements = $this->toList();
Copy link
Member

Choose a reason for hiding this comment

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

While this is for a bug fix, it's a change in behavior that could surprise people, so safer to target 4.1 and document in migration guide.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added it to 4.0.x in case we wanted to backport to 3.9.x. Up to you though.

Copy link
Member

Choose a reason for hiding this comment

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

Dropping the keys is both good and bad. It will fix one but and potentially create another for existing users. One benefit of this change is it makes the behavior consistent with PHP's shuffle() method which also drops keys.

Copy link
Member Author

Choose a reason for hiding this comment

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

All I can say is this is the current documentation for shuffle(). To me, that suggests that you want all the elements. If users need to have unique keys only, then they can re-create the collection first.

     * Returns a new collection with the elements placed in a random order,
     * this function does not preserve the original keys in the collection.

@ADmad
Copy link
Member

ADmad commented Jul 3, 2020

Here's an alternate fix which doesn't unnecessarily reset keys for all collections:

diff --git a/src/Collection/CollectionTrait.php b/src/Collection/CollectionTrait.php
index 1a7690c652..12e113da83 100644
--- a/src/Collection/CollectionTrait.php
+++ b/src/Collection/CollectionTrait.php
@@ -330,7 +330,12 @@ trait CollectionTrait
      */
     public function shuffle(): CollectionInterface
     {
-        $elements = $this->toArray();
+        $preserveKeys = true;
+        $iterator = $this->unwrap();
+        if (get_class($iterator) === AppendIterator::class) {
+            $preserveKeys = false;
+        }
+        $elements = $this->toArray($preserveKeys);
         shuffle($elements);
 
         return $this->newCollection($elements);

@othercorey
Copy link
Member Author

The shuffle will reset keys regardless.

@@ -330,7 +330,7 @@ public function sumOf($matcher = null)
*/
public function shuffle(): CollectionInterface
{
$elements = $this->toArray();
$elements = $this->toList();
Copy link
Member

Choose a reason for hiding this comment

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

Dropping the keys is both good and bad. It will fix one but and potentially create another for existing users. One benefit of this change is it makes the behavior consistent with PHP's shuffle() method which also drops keys.

Comment on lines +937 to +938
$this->assertContainsEquals(1, $result);
$this->assertContainsEquals(2, $result);
Copy link
Member

Choose a reason for hiding this comment

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

It might also be good to assert that there is no a key anymore.

@ADmad
Copy link
Member

ADmad commented Jul 4, 2020

The shuffle will reset keys regardless.

In that case I rescind my alternative fix :)

@markstory markstory merged commit cfd8cbd into master Jul 5, 2020
@markstory markstory deleted the collection-shuffle branch July 5, 2020 01:32
othercorey pushed a commit that referenced this pull request Jul 5, 2020
Changed CollectionTrait::shuffle() to return all elements in collection.
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.

Collection : Append and shuffle problem
3 participants