From d21666f728749a91be3a020ead1bc781bc637dd2 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 11 Jan 2024 21:45:07 +0100 Subject: [PATCH] Throws proper Twig exception when using cycle on an empty array --- src/Extension/CoreExtension.php | 4 ++++ tests/Fixtures/functions/cycle_empty.test | 8 ++++++++ 2 files changed, 12 insertions(+) create mode 100644 tests/Fixtures/functions/cycle_empty.test diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index 2a5cf0289..c4e0bdab2 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -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)]; } diff --git a/tests/Fixtures/functions/cycle_empty.test b/tests/Fixtures/functions/cycle_empty.test new file mode 100644 index 000000000..1eaffc8e5 --- /dev/null +++ b/tests/Fixtures/functions/cycle_empty.test @@ -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