diff --git a/doc/filters/shuffle.rst b/doc/filters/shuffle.rst index 32821c58a..1908b7671 100644 --- a/doc/filters/shuffle.rst +++ b/doc/filters/shuffle.rst @@ -1,5 +1,5 @@ ``shuffle`` -======== +=========== The ``shuffle`` filter shuffles an array: @@ -11,9 +11,9 @@ The ``shuffle`` filter shuffles an array: .. caution:: - Internally, Twig uses the PHP `shuffle`_ function. - This function assigns new keys to the elements in array. It will remove - any existing keys that may have been assigned, rather than just reordering the keys. + The shuffled array does not preserve keys. So if the input had not sequential keys + but indexed keys (using the user id for instance), + it is not the case anymore after shuffling it. Example 1: @@ -72,5 +72,3 @@ The above example will be rendered as: Note, results can also be : "d, e, f" or "e, d, f" or "e, f, d" or "f, d, e" or "f, e, d". - -.. _`shuffle`: https://www.php.net/shuffle diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index 50ba5e700..361add3c6 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -953,18 +953,10 @@ public static function sortFilter(Environment $env, $array, $arrow = null) * Shuffle an array. * The function does not preserve keys. * - * @param array|\Traversable $array - * - * @return array - * * @internal */ - public function shuffle($array) + public function shuffle(iterable $array): array { - if (!is_iterable($array)) { - throw new RuntimeError(sprintf('The shuffle filter only works with array or "Traversable", got "%s" as argument.', \gettype($array))); - } - $array = self::toArray($array, false); shuffle($array); diff --git a/tests/Fixtures/filters/shuffle_with_alphanumerics_characters.test b/tests/Fixtures/filters/shuffle_with_alphanumerics_characters.test deleted file mode 100644 index 4bb049909..000000000 --- a/tests/Fixtures/filters/shuffle_with_alphanumerics_characters.test +++ /dev/null @@ -1,10 +0,0 @@ ---TEST-- -Exception for invalid argument type in "shuffle" filter call ---TEMPLATE-- -{{ array1|shuffle }} -{{ array2|shuffle }} ---DATA-- -return ['array1' => 'Hello World', 'array2' => 1234] ---EXCEPTION-- -The shuffle filter only works with array or "Traversable", got "string" as argument. -The shuffle filter only works with array or "Traversable", got "integer" as argument. diff --git a/tests/Fixtures/filters/shuffle_with_boolean.test b/tests/Fixtures/filters/shuffle_with_boolean.test deleted file mode 100644 index e14a0977c..000000000 --- a/tests/Fixtures/filters/shuffle_with_boolean.test +++ /dev/null @@ -1,8 +0,0 @@ ---TEST-- -Exception for invalid argument type in "shuffle" filter call ---TEMPLATE-- -{{ array|shuffle }} ---DATA-- -return ['array' => false] ---EXCEPTION-- -The shuffle filter only works with array or "Traversable", got "boolean" as argument. diff --git a/tests/Fixtures/filters/shuffle_with_empty_string.test b/tests/Fixtures/filters/shuffle_with_empty_string.test deleted file mode 100644 index d724decd1..000000000 --- a/tests/Fixtures/filters/shuffle_with_empty_string.test +++ /dev/null @@ -1,8 +0,0 @@ ---TEST-- -Exception for invalid argument type in "shuffle" filter call ---TEMPLATE-- -{{ array|shuffle }} ---DATA-- -return ['array' => ''] ---EXCEPTION-- -The shuffle filter only works with array or "Traversable", got "string" as argument. diff --git a/tests/Fixtures/filters/shuffle_with_null_value.test b/tests/Fixtures/filters/shuffle_with_null_value.test deleted file mode 100644 index ff1eed3f7..000000000 --- a/tests/Fixtures/filters/shuffle_with_null_value.test +++ /dev/null @@ -1,8 +0,0 @@ ---TEST-- -Exception for invalid argument type in "shuffle" filter call ---TEMPLATE-- -{{ array|shuffle }} ---DATA-- -return ['array' => null] ---EXCEPTION-- -The shuffle filter only works with array or "Traversable", got "NULL" as argument.