Throws proper Twig exception when using cycle on an empty array

This commit is contained in:
Fabien Potencier
2024-01-11 21:45:07 +01:00
parent 433189bb5a
commit d21666f728
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