bug #3961 Throws proper Twig exception when using cycle on an empty array (fabpot)

This PR was merged into the 3.x branch.

Discussion
----------

Throws proper Twig exception when using cycle on an empty array

Closes #3925

Commits
-------

d21666f7 Throws proper Twig exception when using cycle on an empty array
This commit is contained in:
Fabien Potencier
2024-01-12 12:20:12 +01:00
2 changed files with 12 additions and 0 deletions
+4
View File
@@ -329,6 +329,10 @@ final class CoreExtension extends AbstractExtension
return $values;
}
if (!\count($values)) {
throw new RuntimeError('The "cycle" function does not work on empty arrays');
}
return $values[$position % \count($values)];
}
@@ -0,0 +1,8 @@
--TEST--
"cycle" function returns an error on empty arrays
--TEMPLATE--
{{ cycle([], 0) }}
--DATA--
return []
--EXCEPTION--
Twig\Error\RuntimeError: The "cycle" function does not work on empty arrays in "index.twig" at line 2