|
22 | 22 | use Countable; |
23 | 23 | use IteratorIterator; |
24 | 24 |
|
| 25 | +/** |
| 26 | + * Provides counting behavior for iterators and iterator aggregates. |
| 27 | + * |
| 28 | + * This trait SHALL add a {@see count()} implementation that delegates to the |
| 29 | + * inner iterator when possible. When the inner iterator does not implement |
| 30 | + * {@see Countable}, the trait MUST determine whether the iterator can be safely |
| 31 | + * cloned before counting its elements through traversal. Implementing classes |
| 32 | + * MUST provide a compatible {@see getInnerIterator()} method that returns the |
| 33 | + * traversable object to be counted. |
| 34 | + */ |
25 | 35 | trait CountableIteratorIteratorTrait |
26 | 36 | { |
27 | 37 | /** |
28 | | - * Counts the number of elements in the iterable. |
| 38 | + * Counts the number of elements exposed by the inner iterator. |
29 | 39 | * |
30 | | - * If the inner iterator implements Countable, it uses that. Otherwise, it |
31 | | - * counts the elements by iterating through them. |
| 40 | + * If the inner iterator implements {@see Countable}, this method SHALL |
| 41 | + * return the value provided by that implementation. Otherwise, it MUST count |
| 42 | + * elements by iterating over the iterator. If the inner iterator is not |
| 43 | + * cloneable, this method SHALL wrap the current object in an |
| 44 | + * {@see IteratorIterator} instance and count through that wrapper to avoid |
| 45 | + * performing an invalid clone operation. If the inner iterator is cloneable, |
| 46 | + * this method SHOULD count over a clone so that the original iterator state |
| 47 | + * is preserved as much as possible. |
32 | 48 | * |
33 | | - * @return int the number of elements in the iterable |
| 49 | + * @return int the total number of elements available from the inner iterator |
34 | 50 | */ |
35 | 51 | public function count(): int |
36 | 52 | { |
|
0 commit comments