From e243841350710358ac115bddd72c5aa4fbfb47ac Mon Sep 17 00:00:00 2001 From: Stephen Rees-Carter Date: Wed, 10 Jan 2024 15:35:08 +0100 Subject: [PATCH 1/4] Use Randomizer::pickArrayKeys() within Arr::random() --- src/Illuminate/Collections/Arr.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index d1e4a40ae686..5b83bbf81157 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -6,6 +6,7 @@ use ArrayAccess; use Illuminate\Support\Traits\Macroable; use InvalidArgumentException; +use Random\Randomizer; class Arr { @@ -661,24 +662,24 @@ public static function random($array, $number = null, $preserveKeys = false) ); } - if (is_null($number)) { - return $array[array_rand($array)]; + if (empty($array) || (! is_null($number) && $number <= 0)) { + return is_null($number) ? null : []; } - if ((int) $number === 0) { - return []; - } + $keys = (new Randomizer)->pickArrayKeys($array, $requested); - $keys = array_rand($array, $number); + if (is_null($number)) { + return $array[$keys[0]]; + } $results = []; if ($preserveKeys) { - foreach ((array) $keys as $key) { + foreach ($keys as $key) { $results[$key] = $array[$key]; } } else { - foreach ((array) $keys as $key) { + foreach ($keys as $key) { $results[] = $array[$key]; } } From caa17365e18d8c357eab397604656a1fbee92e45 Mon Sep 17 00:00:00 2001 From: Stephen Rees-Carter Date: Wed, 10 Jan 2024 15:48:42 +0100 Subject: [PATCH 2/4] Use Randomizer::shuffleArray() in Arr::shuffle() --- src/Illuminate/Collections/Arr.php | 13 ++----------- tests/Support/SupportArrTest.php | 24 ++++++++++++++++++++---- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index 5b83bbf81157..a3cc7fcb50b1 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -731,20 +731,11 @@ public static function set(&$array, $key, $value) * Shuffle the given array and return the result. * * @param array $array - * @param int|null $seed * @return array */ - public static function shuffle($array, $seed = null) + public static function shuffle($array) { - if (is_null($seed)) { - shuffle($array); - } else { - mt_srand($seed); - shuffle($array); - mt_srand(); - } - - return $array; + return (new Randomizer)->shuffleArray($array); } /** diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index 083ca5bbb306..160c440de74d 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -907,14 +907,30 @@ public function testSet() $this->assertEquals([1 => 'hAz'], Arr::set($array, 1, 'hAz')); } - public function testShuffleWithSeed() + public function testShuffle() { - $this->assertEquals( - Arr::shuffle(range(0, 100, 10), 1234), - Arr::shuffle(range(0, 100, 10), 1234) + $input = ['a', 'b', 'c', 'd', 'e', 'f']; + + $this->assertNotEquals( + $input, + Arr::shuffle($input) + ); + + $this->assertNotEquals( + Arr::shuffle($input), + Arr::shuffle($input) ); } + public function testShuffleKeepsSameValues() + { + $input = ['a', 'b', 'c', 'd', 'e', 'f']; + $shuffled = Arr::shuffle($input); + sort($shuffled); + + $this->assertEquals($input, $shuffled); + } + public function testEmptyShuffle() { $this->assertEquals([], Arr::shuffle([])); From 9dff01c10fe48b31bac0badc2b2927f7828e4e8e Mon Sep 17 00:00:00 2001 From: Stephen Rees-Carter Date: Wed, 10 Jan 2024 16:22:49 +0100 Subject: [PATCH 3/4] Remove unused $seed from shuffle --- src/Illuminate/Collections/Collection.php | 5 ++--- src/Illuminate/Collections/Enumerable.php | 3 +-- src/Illuminate/Collections/LazyCollection.php | 5 ++--- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index 7869e1ce65e0..5a718e5b08d2 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -1125,12 +1125,11 @@ public function shift($count = 1) /** * Shuffle the items in the collection. * - * @param int|null $seed * @return static */ - public function shuffle($seed = null) + public function shuffle() { - return new static(Arr::shuffle($this->items, $seed)); + return new static(Arr::shuffle($this->items)); } /** diff --git a/src/Illuminate/Collections/Enumerable.php b/src/Illuminate/Collections/Enumerable.php index 559e2ecbfec7..a92eec560e05 100644 --- a/src/Illuminate/Collections/Enumerable.php +++ b/src/Illuminate/Collections/Enumerable.php @@ -889,10 +889,9 @@ public function search($value, $strict = false); /** * Shuffle the items in the collection. * - * @param int|null $seed * @return static */ - public function shuffle($seed = null); + public function shuffle(); /** * Create chunks representing a "sliding window" view of the items in the collection. diff --git a/src/Illuminate/Collections/LazyCollection.php b/src/Illuminate/Collections/LazyCollection.php index 7d25e9d899d1..475e55448864 100644 --- a/src/Illuminate/Collections/LazyCollection.php +++ b/src/Illuminate/Collections/LazyCollection.php @@ -1058,12 +1058,11 @@ public function search($value, $strict = false) /** * Shuffle the items in the collection. * - * @param int|null $seed * @return static */ - public function shuffle($seed = null) + public function shuffle() { - return $this->passthru('shuffle', func_get_args()); + return $this->passthru('shuffle'); } /** From 9816e18ea78a9c57d11967c7f26014633685ff5f Mon Sep 17 00:00:00 2001 From: Stephen Rees-Carter Date: Wed, 10 Jan 2024 16:32:59 +0100 Subject: [PATCH 4/4] Fix failing shuffle tests --- src/Illuminate/Collections/LazyCollection.php | 2 +- tests/Support/SupportCollectionTest.php | 11 ----------- 2 files changed, 1 insertion(+), 12 deletions(-) diff --git a/src/Illuminate/Collections/LazyCollection.php b/src/Illuminate/Collections/LazyCollection.php index 475e55448864..ab82ed242b7b 100644 --- a/src/Illuminate/Collections/LazyCollection.php +++ b/src/Illuminate/Collections/LazyCollection.php @@ -1062,7 +1062,7 @@ public function search($value, $strict = false) */ public function shuffle() { - return $this->passthru('shuffle'); + return $this->passthru('shuffle', []); } /** diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 03f5551becaa..8904e0b3ec1d 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -442,17 +442,6 @@ public function testCollectionIsConstructed($collection) $this->assertEmpty($data->all()); } - #[DataProvider('collectionClassProvider')] - public function testCollectionShuffleWithSeed($collection) - { - $data = new $collection(range(0, 100, 10)); - - $firstRandom = $data->shuffle(1234); - $secondRandom = $data->shuffle(1234); - - $this->assertEquals($firstRandom, $secondRandom); - } - #[DataProvider('collectionClassProvider')] public function testSkipMethod($collection) {