Skip to content

Commit

Permalink
refac: import functions in Collection
Browse files Browse the repository at this point in the history
  • Loading branch information
Bl00D4NGEL committed Jan 5, 2024
1 parent 74ce180 commit 52ced3c
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/Domain/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,28 @@

namespace GeekCell\Ddd\Domain;

use ArrayAccess;
use ArrayIterator;
use Assert;
use Countable;
use InvalidArgumentException;
use IteratorAggregate;
use Traversable;
use function array_filter;
use function array_map;
use function array_reduce;
use function array_values;
use function count;
use function get_class;
use function is_int;
use function reset;

/**
* @template T of object
* @implements \IteratorAggregate<T>
* @implements \ArrayAccess<mixed, T>
* @implements IteratorAggregate<T>
* @implements ArrayAccess<mixed, T>
*/
class Collection implements \ArrayAccess, \Countable, \IteratorAggregate
class Collection implements ArrayAccess, Countable, IteratorAggregate
{
/**
* @param T[] $items
Expand Down Expand Up @@ -249,7 +261,7 @@ public function add(mixed $item): static
public function filter(callable $callback): static
{
return new static(
\array_values(\array_filter($this->items, $callback)),
array_values(array_filter($this->items, $callback)),
$this->itemType,
);
}
Expand All @@ -267,15 +279,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 @@ -291,15 +303,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 @@ -345,14 +357,14 @@ public function offsetUnset(mixed $offset): void
*/
public function count(): int
{
return \count($this->items);
return count($this->items);
}

/**
* @inheritDoc
*/
public function getIterator(): Traversable
{
return new \ArrayIterator($this->items);
return new ArrayIterator($this->items);
}
}

0 comments on commit 52ced3c

Please sign in to comment.