Skip to content

Commit 1848c02

Browse files
authored
fix: Improve Collection::filter method to not retain indices. (#13)
refs: #12
1 parent 857d0f4 commit 1848c02

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

src/Domain/Collection.php

+11-16
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,13 @@
44

55
namespace GeekCell\Ddd\Domain;
66

7-
use ArrayAccess;
8-
use ArrayIterator;
97
use Assert;
10-
use Countable;
11-
use IteratorAggregate;
12-
use Traversable;
138

14-
class Collection implements ArrayAccess, Countable, IteratorAggregate
9+
class Collection implements \ArrayAccess, \Countable, \IteratorAggregate
1510
{
1611
/**
1712
* @template T of object
18-
* @extends IteratorAggregate<T>
13+
* @extends \IteratorAggregate<T>
1914
*
2015
* @param T[] $items
2116
* @param class-string<T> $itemType
@@ -63,7 +58,7 @@ public function add(mixed $item): static
6358
public function filter(callable $callback): static
6459
{
6560
return new static(
66-
array_filter($this->items, $callback),
61+
\array_values(\array_filter($this->items, $callback)),
6762
$this->itemType,
6863
);
6964
}
@@ -81,15 +76,15 @@ public function filter(callable $callback): static
8176
*/
8277
public function map(callable $callback, bool $inferTypes = true): static
8378
{
84-
$mapResult = array_map($callback, $this->items);
85-
$firstItem = reset($mapResult);
79+
$mapResult = \array_map($callback, $this->items);
80+
$firstItem = \reset($mapResult);
8681

8782
if ($firstItem === false || !is_object($firstItem)) {
8883
return new static($mapResult);
8984
}
9085

9186
if ($inferTypes && $this->itemType !== null) {
92-
return new static($mapResult, get_class($firstItem));
87+
return new static($mapResult, \get_class($firstItem));
9388
}
9489

9590
return new static($mapResult);
@@ -105,15 +100,15 @@ public function map(callable $callback, bool $inferTypes = true): static
105100
*/
106101
public function reduce(callable $callback, mixed $initial = null): mixed
107102
{
108-
return array_reduce($this->items, $callback, $initial);
103+
return \array_reduce($this->items, $callback, $initial);
109104
}
110105

111106
/**
112107
* @inheritDoc
113108
*/
114109
public function offsetExists(mixed $offset): bool
115110
{
116-
if (!is_int($offset)) {
111+
if (!\is_int($offset)) {
117112
return false;
118113
}
119114

@@ -159,14 +154,14 @@ public function offsetUnset(mixed $offset): void
159154
*/
160155
public function count(): int
161156
{
162-
return count($this->items);
157+
return \count($this->items);
163158
}
164159

165160
/**
166161
* @inheritDoc
167162
*/
168-
public function getIterator(): Traversable
163+
public function getIterator(): \Traversable
169164
{
170-
return new ArrayIterator($this->items);
165+
return new \ArrayIterator($this->items);
171166
}
172167
}

tests/Domain/CollectionTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,21 @@ public function testFilter(): void
166166
$this->assertNotSame($collection, $newCollection);
167167
}
168168

169+
public function testFilterWithAdjustedIndices(): void
170+
{
171+
// Given
172+
$items = [1, 2, 3, 4];
173+
$collection = new Collection($items);
174+
175+
// When
176+
$newCollection = $collection->filter(fn (int $i) => $i % 2 === 0);
177+
178+
// Then
179+
$this->assertCount(2, $newCollection);
180+
$this->assertEquals(2, $newCollection[0]);
181+
$this->assertEquals(4, $newCollection[1]);
182+
}
183+
169184
public function testMap(): void
170185
{
171186
// Given

0 commit comments

Comments
 (0)