mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-30 20:16:45 +00:00
ce6698abd51b7d4430c7947adb7582f7cc654082
This PR was merged into the 3.x branch.
Discussion
----------
Fix cycle() with non-countable ArrayAccess+Traversable objects
When using `cycle()` with an object that implements `\ArrayAccess` and `\Traversable` but is not `\Countable`, the function currently returns the object instance immediately after triggering the deprecation notice.
This prevents the value from being converted to an array, causing the cycle logic to fail or return the object itself.
This PR removes the early return to ensure `self::toArray()` is called, allowing these objects to be cycled correctly as expected.
**How to test**
```php
$seq = new class implements \ArrayAccess, \IteratorAggregate {
public function offsetExists($offset): bool { return true; }
public function offsetGet($offset): mixed { return 'val'; }
public function offsetSet($offset, $value): void {}
public function offsetUnset($offset): void {}
public function getIterator(): \Traversable { yield 'odd'; yield 'even'; }
};
// Should return 'odd', currently returns the $seq object
$result = CoreExtension::cycle($seq, 0);
```
Related to #4241
Commits
-------
473653d19b [Core] Fix cycle() with non-countable ArrayAccess+Traversable objects
…
Twig, the flexible, fast, and secure template language for PHP
==============================================================
Twig is a template language for PHP.
Twig uses a syntax similar to the Django and Jinja template languages which
inspired the Twig runtime environment.
Sponsors
--------
.. raw:: html
<a href="https://docs.blackfire.io/introduction?utm_source=twig&utm_medium=github_readme&utm_campaign=logo">
<img src="https://static.blackfire.io/assets/intemporals/logo/png/blackfire-io_secondary_horizontal_transparent.png?1" width="255px" alt="Blackfire.io">
</a>
More Information
----------------
Read the `documentation`_ for more information.
.. _documentation: https://twig.symfony.com/documentation
Description
Languages
PHP
99.9%