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
return $values;
}

$values = self::toArray($values, false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this line be inside the !is_countable block (in the place that had the broken return). AFAICT, the intent of the is_countable check was to allow using the ArrayAccess&Countable object without converting it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @stof, thanks for the feedback! The conversion to array is now only performed inside the !is_countable block as suggested.

@yoeunes yoeunes force-pushed the cycle-non-countable-traversable branch from d7e493a to 473653d Compare November 23, 2025 12:20
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.

2 participants