-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Closed
Description
I have a couple questions about the "cycle" function...
I'm not sure what is really expected in some scenarios (mainly "what to do with mappings"), and the documentation uses "mapping" "sequence" and "list" terms.
I'll gladly open a PR, but i'm not sure what to do here :)
Current code of the cycle function here
/**
* Cycles over a value.
*
* @param \ArrayAccess|array $values
* @param int $position The cycle position
*
* @return string The next value in the cycle
*
* @internal
*/
public static function cycle($values, $position): string
{
if (!\is_array($values) && !$values instanceof \ArrayAccess) {
return $values;
}
if (!\count($values)) {
throw new RuntimeError('The "cycle" function does not work on empty sequences/mappings.');
}
return $values[$position % \count($values)];
}ArrayAccess ?
If $values implements ArrayAccess but not Countable, depending on the PHP version the effects can be a simple warning or ... a DivisionByZeroError
- Genuine cursiosity: what is the use case with
ArrayAccesshere ? - Should we check is_countable first ?
- Or should this be added in the docblock ?
- Should we deprecate passing a not countable
ArrayAccess?
Sequences / Mappings
There is nothing explicit in the docblock, and the exception is about "sequences/mappings" but .. in reality this method does only works with lists (sequences) of strings.
- Should this be checked before accessing the result / counting items ?
- Or inversely, if $values is a mapping, should we convert it to a sequence first ?
- Should the "position" value be tested ? (a negative value will not work here)
- Should the 'string' return type be enforced / checked somewhere ?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels