Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| CountableIterator | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
| count | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * This file is part of php-fast-forward/iterators. |
| 7 | * |
| 8 | * This source file is subject to the license that is bundled |
| 9 | * with this source code in the file LICENSE. |
| 10 | * |
| 11 | * @copyright Copyright (c) 2025-2026 Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 12 | * @license https://opensource.org/licenses/MIT MIT License |
| 13 | * |
| 14 | * @see https://github.com/php-fast-forward/iterators |
| 15 | * @see https://github.com/php-fast-forward |
| 16 | * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 17 | */ |
| 18 | |
| 19 | namespace FastForward\Iterator; |
| 20 | |
| 21 | use Countable; |
| 22 | use Iterator; |
| 23 | |
| 24 | /** |
| 25 | * Provides a base iterator implementation that is also countable. |
| 26 | * |
| 27 | * This abstract class SHALL be used as a foundation for iterators that need |
| 28 | * to expose counting behavior while remaining compatible with the standard |
| 29 | * {@see Iterator} contract. Implementations MUST preserve iterator semantics, |
| 30 | * and consumers SHOULD expect the counting logic to operate over the same |
| 31 | * sequence that is exposed during normal iteration. |
| 32 | */ |
| 33 | abstract class CountableIterator implements Iterator, Countable |
| 34 | { |
| 35 | /** |
| 36 | * Counts the number of elements available in the iterator. |
| 37 | * |
| 38 | * This method MUST count the elements by iterating over a clone of the |
| 39 | * current iterator instance so that the active iterator state of the |
| 40 | * original object is not modified during the counting process. Concrete |
| 41 | * implementations SHOULD therefore remain safely cloneable whenever this |
| 42 | * behavior is expected to be used. |
| 43 | * |
| 44 | * @return int the total number of elements exposed by the iterator |
| 45 | */ |
| 46 | public function count(): int |
| 47 | { |
| 48 | return iterator_count(clone $this); |
| 49 | } |
| 50 | } |