-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Conversation
|
@@ -330,7 +330,7 @@ public function sumOf($matcher = null) | |||
*/ | |||
public function shuffle(): CollectionInterface | |||
{ | |||
$elements = $this->toArray(); | |||
$elements = $this->toList(); |
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.
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.
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 added it to 4.0.x in case we wanted to backport to 3.9.x. Up to you though.
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.
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.
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.
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.
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); |
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(); |
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.
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.
$this->assertContainsEquals(1, $result); | ||
$this->assertContainsEquals(2, $result); |
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 might also be good to assert that there is no a
key anymore.
In that case I rescind my alternative fix :) |
Changed CollectionTrait::shuffle() to return all elements in collection.
Fixes #14757