Repository: twigphp/Twig (php)

Issue:
`cycle` function on an empty hash / array throws an exception
https://twigfiddle.com/2scom6

```twig
{{ cycle({}, 1) }}
```

> An exception has been thrown during the rendering of a template ("Modulo by zero").

Note that the same exception is thrown when passing an empty array:

```twig
{{ cycle([], 1) }}
```

I assume the output here should be a blank string - this is what Twing outputs organically.

Pull request description:
Throws proper Twig exception when using cycle on an empty array
Closes #3925


Patch:
diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php
index 2a5cf028..c4e0bdab 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)];
     }
 


Test patch:
diff --git a/tests/Fixtures/functions/cycle_empty.test b/tests/Fixtures/functions/cycle_empty.test
new file mode 100644
index 00000000..1eaffc8e
--- /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