Skip to content

Conversation

@yoeunes
Copy link
Contributor

@yoeunes yoeunes commented Nov 23, 2025

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

$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

@yoeunes yoeunes force-pushed the cycle-non-countable-traversable branch from aa7666e to d7e493a Compare November 23, 2025 03:27
@yoeunes yoeunes force-pushed the cycle-non-countable-traversable branch from d7e493a to 473653d Compare November 23, 2025 12:20
@fabpot
Copy link
Contributor

fabpot commented Nov 25, 2025

Thank you @yoeunes.

@fabpot fabpot merged commit ce6698a into twigphp:3.x Nov 25, 2025
40 of 41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants