Skip to content

Commit 1d9fbd6

Browse files
committed
Add PHPDocs
Signed-off-by: Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
1 parent 6999187 commit 1d9fbd6

5 files changed

Lines changed: 72 additions & 9 deletions

src/CountableFilterIterator.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121
use Countable;
2222
use FilterIterator;
2323

24+
/**
25+
* Provides a filter iterator implementation that is also countable.
26+
*
27+
* This abstract class SHALL extend {@see FilterIterator} so that concrete
28+
* implementations MAY filter elements from an inner iterator while also
29+
* exposing count semantics. The counting behavior MUST be provided through
30+
* the composed trait and SHALL operate according to the characteristics of
31+
* the wrapped inner iterator. Implementations SHOULD ensure that their
32+
* filtering logic remains consistent with the sequence being counted.
33+
*/
2434
abstract class CountableFilterIterator extends FilterIterator implements Countable
2535
{
2636
use CountableIteratorIteratorTrait;

src/CountableIterator.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,27 @@
2121
use Countable;
2222
use Iterator;
2323

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+
*/
2433
abstract class CountableIterator implements Iterator, Countable
2534
{
2635
/**
27-
* Counts the number of elements in the iterable.
36+
* Counts the number of elements available in the iterator.
2837
*
29-
* If the inner iterator implements Countable, it uses that. Otherwise, it
30-
* counts the elements by iterating through them.
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.
3143
*
32-
* @return int the number of elements in the iterable
44+
* @return int the total number of elements exposed by the iterator
3345
*/
3446
public function count(): int
3547
{

src/CountableIteratorAggregate.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,29 @@
2222
use IteratorAggregate;
2323
use Traversable;
2424

25+
/**
26+
* Provides a base implementation for iterator aggregate objects that are also countable.
27+
*
28+
* This abstract class SHALL serve as a reusable foundation for concrete iterator aggregate
29+
* implementations that expose a traversable iterator and require count-related behavior.
30+
* Implementations MUST provide a valid iterator through {@see getIterator()}, and that
31+
* iterator SHOULD be suitable for reuse by the count-related logic provided through the
32+
* composed trait.
33+
*/
2534
abstract class CountableIteratorAggregate implements IteratorAggregate, Countable
2635
{
2736
use CountableIteratorIteratorTrait;
2837

2938
/**
30-
* @return Traversable
39+
* Returns the inner traversable iterator used by the aggregate.
40+
*
41+
* This method SHALL proxy the iterator returned by {@see getIterator()} without altering
42+
* its semantics or traversal behavior. Consumers of this method MUST receive the same
43+
* traversable instance or equivalent traversable representation exposed by the aggregate.
44+
* This method SHOULD remain private because it exists only to support the internal
45+
* collaboration between this class and its trait-based behavior.
46+
*
47+
* @return Traversable the traversable iterator exposed by the aggregate implementation
3148
*/
3249
private function getInnerIterator(): Traversable
3350
{

src/CountableIteratorIterator.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
use Countable;
2222
use IteratorIterator;
2323

24+
/**
25+
* Provides an iterator wrapper that is also countable.
26+
*
27+
* This class SHALL extend {@see IteratorIterator} to decorate an existing iterator while
28+
* exposing counting behavior through the composed trait. The wrapped iterator MUST be
29+
* compatible with the expectations of {@see IteratorIterator}, and consumers SHOULD rely
30+
* on this class when they need both traversal and count semantics from a single object.
31+
*/
2432
class CountableIteratorIterator extends IteratorIterator implements Countable
2533
{
2634
use CountableIteratorIteratorTrait;

src/CountableIteratorIteratorTrait.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,31 @@
2222
use Countable;
2323
use IteratorIterator;
2424

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+
*/
2535
trait CountableIteratorIteratorTrait
2636
{
2737
/**
28-
* Counts the number of elements in the iterable.
38+
* Counts the number of elements exposed by the inner iterator.
2939
*
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.
3248
*
33-
* @return int the number of elements in the iterable
49+
* @return int the total number of elements available from the inner iterator
3450
*/
3551
public function count(): int
3652
{

0 commit comments

Comments
 (0)