Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improve Collection::filter method to not retain indices. #13

Merged
merged 1 commit into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions src/Domain/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>
* @extends \IteratorAggregate<T>
*
* @param T[] $items
* @param class-string<T> $itemType
Expand Down Expand Up @@ -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,
);
}
Expand All @@ -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);
Expand All @@ -105,15 +100,15 @@ 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);
}

/**
* @inheritDoc
*/
public function offsetExists(mixed $offset): bool
{
if (!is_int($offset)) {
if (!\is_int($offset)) {
return false;
}

Expand Down Expand Up @@ -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);
}
}
15 changes: 15 additions & 0 deletions tests/Domain/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down