From acc
Generates Iterator pattern for PHP 8.4 collections: iterator interface, concrete iterator, iterable collection, value objects, and unit tests. Use for sequential traversal without exposing internals.
How this skill is triggered — by the user, by Claude, or both
Slash command
/acc:create-iteratorThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Creates Iterator pattern infrastructure for sequential access to collection elements.
Creates Iterator pattern infrastructure for sequential access to collection elements.
| Scenario | Example |
|---|---|
| Traverse collections | Order items, user lists, product catalog |
| Multiple traversal algorithms | Forward, backward, filtered iteration |
| Unified collection interface | Standardize iteration across different types |
| Hide collection structure | Encapsulate internal representation |
Path: src/Domain/{BoundedContext}/Iterator/
{Name}IteratorInterface.php — Custom iterator contract (extends \Iterator)Path: src/Domain/{BoundedContext}/Iterator/
{Name}Iterator.php — Iterator implementation with traversal logicFiltered{Name}Iterator.php — Filtered variant (optional)Path: src/Domain/{BoundedContext}/Collection/
{Name}Collection.php — Collection implementing \IteratorAggregatePath: src/Domain/{BoundedContext}/ValueObject/
{Element}.php — Collection element type{Name}IteratorTest.php — Iterator behavior tests{Name}CollectionTest.php — Collection tests| Component | Path |
|---|---|
| Iterator Interface | src/Domain/{BoundedContext}/Iterator/ |
| Concrete Iterator | src/Domain/{BoundedContext}/Iterator/ |
| Collection | src/Domain/{BoundedContext}/Collection/ |
| Value Objects | src/Domain/{BoundedContext}/ValueObject/ |
| Unit Tests | tests/Unit/Domain/{BoundedContext}/ |
| Component | Pattern | Example |
|---|---|---|
| Iterator Interface | {Name}IteratorInterface | OrderIteratorInterface |
| Concrete Iterator | {Name}Iterator | OrderIterator |
| Filtered Iterator | Filtered{Name}Iterator | FilteredUserIterator |
| Collection | {Name}Collection | OrderCollection |
| Test | {ClassName}Test | OrderIteratorTest |
final class {Name}Iterator implements \Iterator
{
private int $position = 0;
public function __construct(
private readonly array $items
) {}
public function current(): {ElementType}
{
return $this->items[$this->position];
}
public function next(): void
{
++$this->position;
}
public function key(): int
{
return $this->position;
}
public function valid(): bool
{
return isset($this->items[$this->position]);
}
public function rewind(): void
{
$this->position = 0;
}
}
final readonly class {Name}Collection implements \IteratorAggregate, \Countable
{
/**
* @param array<{ElementType}> $items
*/
public function __construct(
private array $items = []
) {}
public function getIterator(): \Traversable
{
return new {Name}Iterator($this->items);
}
public function count(): int
{
return count($this->items);
}
}
// Create collection
$orders = new OrderCollection([
new Order(id: '1', total: 100),
new Order(id: '2', total: 200),
new Order(id: '3', total: 50),
]);
// Iterate with foreach
foreach ($orders as $order) {
echo $order->total();
}
// Filtered iteration
$filtered = new FilteredOrderIterator(
orders: $orders,
filter: fn(Order $o) => $o->total() > 100
);
foreach ($filtered as $order) {
// Only orders with total > 100
}
| Domain | Iterators |
|---|---|
| Collections | OrderCollection, UserCollection, ProductCollection |
| Filtering | ActiveUserIterator, PendingOrderIterator |
| Pagination | PaginatedResultIterator, PageIterator |
| Tree Structures | DepthFirstIterator, BreadthFirstIterator |
| Composite | RecursiveIterator, FlatteningIterator |
| Anti-pattern | Problem | Solution |
|---|---|---|
| Mutable iterator state leak | External modification | Use readonly collections |
| Breaking foreach contract | Invalid implementation | Implement all \Iterator methods |
| Expensive current() | Performance issues | Cache current element |
| Missing rewind() | Can't iterate twice | Implement proper rewind() |
| Violating LSP | Inconsistent behavior | Follow SPL iterator contracts |
For complete PHP templates and examples, see:
references/templates.md — Iterator, IteratorAggregate, Filtered Iterator templatesreferences/examples.md — OrderCollection, UserIterator, Pagination with testsnpx claudepluginhub dykyi-roman/awesome-claude-code --plugin accImplements the Iterator design pattern to sequentially access collection elements without exposing underlying structure. Useful when decoupling traversal logic from data structures.
Generates Composite pattern for PHP 8.4 with interface, leaf, composite classes, and unit tests. For tree structures like menus, file systems, org charts.
Implements the Iterator protocol and generators for traversing custom data structures with for...of. Useful for lazy sequences, data pipelines, and tree/graph iteration.