diff --git a/src/Domain/Collection.php b/src/Domain/Collection.php index c5d738c..451450c 100644 --- a/src/Domain/Collection.php +++ b/src/Domain/Collection.php @@ -4,18 +4,13 @@ namespace GeekCell\Ddd\Domain; -use ArrayAccess; -use ArrayIterator; use Assert; -use Countable; -use IteratorAggregate; -use Traversable; -class Collection implements ArrayAccess, Countable, IteratorAggregate +class Collection implements \ArrayAccess, \Countable, \IteratorAggregate { /** * @template T of object - * @extends IteratorAggregate + * @extends \IteratorAggregate * * @param T[] $items * @param class-string $itemType @@ -63,7 +58,7 @@ public function add(mixed $item): static public function filter(callable $callback): static { return new static( - array_filter($this->items, $callback), + \array_values(\array_filter($this->items, $callback)), $this->itemType, ); } @@ -81,15 +76,15 @@ public function filter(callable $callback): static */ public function map(callable $callback, bool $inferTypes = true): static { - $mapResult = array_map($callback, $this->items); - $firstItem = reset($mapResult); + $mapResult = \array_map($callback, $this->items); + $firstItem = \reset($mapResult); if ($firstItem === false || !is_object($firstItem)) { return new static($mapResult); } if ($inferTypes && $this->itemType !== null) { - return new static($mapResult, get_class($firstItem)); + return new static($mapResult, \get_class($firstItem)); } return new static($mapResult); @@ -105,7 +100,7 @@ public function map(callable $callback, bool $inferTypes = true): static */ public function reduce(callable $callback, mixed $initial = null): mixed { - return array_reduce($this->items, $callback, $initial); + return \array_reduce($this->items, $callback, $initial); } /** @@ -113,7 +108,7 @@ public function reduce(callable $callback, mixed $initial = null): mixed */ public function offsetExists(mixed $offset): bool { - if (!is_int($offset)) { + if (!\is_int($offset)) { return false; } @@ -159,14 +154,14 @@ public function offsetUnset(mixed $offset): void */ public function count(): int { - return count($this->items); + return \count($this->items); } /** * @inheritDoc */ - public function getIterator(): Traversable + public function getIterator(): \Traversable { - return new ArrayIterator($this->items); + return new \ArrayIterator($this->items); } } diff --git a/tests/Domain/CollectionTest.php b/tests/Domain/CollectionTest.php index dcf22f2..86044d5 100644 --- a/tests/Domain/CollectionTest.php +++ b/tests/Domain/CollectionTest.php @@ -166,6 +166,21 @@ public function testFilter(): void $this->assertNotSame($collection, $newCollection); } + public function testFilterWithAdjustedIndices(): void + { + // Given + $items = [1, 2, 3, 4]; + $collection = new Collection($items); + + // When + $newCollection = $collection->filter(fn (int $i) => $i % 2 === 0); + + // Then + $this->assertCount(2, $newCollection); + $this->assertEquals(2, $newCollection[0]); + $this->assertEquals(4, $newCollection[1]); + } + public function testMap(): void { // Given