Skip to content

Commit

Permalink
Update Collection.php
Browse files Browse the repository at this point in the history
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
  • Loading branch information
ghostwriter committed Aug 23, 2023
1 parent aa8d4d4 commit 489a8a8
Showing 1 changed file with 38 additions and 35 deletions.
73 changes: 38 additions & 35 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
namespace Ghostwriter\Collection;

use Closure;
use Countable;
use Generator;
use Ghostwriter\Collection\Exception\FirstValueNotFoundException;
use Ghostwriter\Collection\Exception\LengthMustBePositiveIntegerException;
use Ghostwriter\Collection\Exception\OffsetMustBePositiveIntegerException;
use IteratorAggregate;
use SplFixedArray;
use function iterator_to_array;

/**
* @template TValue
Expand All @@ -17,13 +21,13 @@
*
* @see \Ghostwriter\Collection\Tests\Unit\CollectionTest
*/
final class Collection implements \Countable, \IteratorAggregate
final class Collection implements Countable, IteratorAggregate
{
/**
* @param \SplFixedArray<TValue> $storage
* @param SplFixedArray<TValue> $storage
*/
private function __construct(
private readonly \SplFixedArray $storage
private readonly SplFixedArray $storage
) {
}

Expand All @@ -38,7 +42,7 @@ public function append(iterable $iterable = []): self
return $this;
}

return self::fromGenerator(function () use ($iterable): \Generator {
return self::fromGenerator(function () use ($iterable): Generator {
foreach ($this->storage as $value) {
yield $value;
}
Expand All @@ -52,16 +56,15 @@ public function append(iterable $iterable = []): self
/**
* @template TContains
*
* @param (\Closure(TValue):bool)|TContains $functionOrValue
* @param Closure(TValue):bool|TContains $functionOrValue
*/
public function contains(mixed $functionOrValue): bool
{
$function = $functionOrValue;
if (!$function instanceof \Closure) {
$function = static fn (mixed $value): bool => $value === $functionOrValue;
}
/** @var Closure(TValue):bool $function */
$function = $functionOrValue instanceof Closure ?
$functionOrValue :
static fn (mixed $value): bool => $value === $functionOrValue;

/* @var Closure(TValue):bool $function */
return (bool) $this->filter($function)->count();
}

Expand All @@ -76,31 +79,31 @@ public function count(): int
* @return self<TValue>
*
* @throws LengthMustBePositiveIntegerException
* @throws OffsetMustBePositiveIntegerException
*/
public function drop(int $length): self
{
/* @psalm-suppress MissingThrowsDocblock OffsetMustBePositiveIntegerException */
return $this->slice($length);
}

/**
* @param \Closure(TValue):void $function
* @param Closure(TValue):void $function
*/
public function each(\Closure $function): void
public function each(Closure $function): void
{
foreach ($this->storage as $value) {
$function($value);
}
}

/**
* @param \Closure(TValue):bool $function
* @param Closure(TValue):bool $function
*
* @return self<TValue>
*/
public function filter(\Closure $function): self
public function filter(Closure $function): self
{
return self::fromGenerator(function () use ($function): \Generator {
return self::fromGenerator(function () use ($function): Generator {
foreach ($this->storage as $value) {
if (!$function($value)) {
continue;
Expand All @@ -112,13 +115,13 @@ public function filter(\Closure $function): self
}

/**
* @param ?\Closure(TValue):bool $function
* @param ?Closure(TValue):bool $function
*
* @return ?TValue
*
* @throws FirstValueNotFoundException If no value is found
*/
public function first(\Closure $function = null): mixed
public function first(Closure $function = null): mixed
{
$function ??= static fn (mixed $value): bool => null !== $value;

Expand All @@ -130,19 +133,19 @@ public function first(\Closure $function = null): mixed
}

/**
* @param \Closure():\Generator $generator
* @param Closure():Generator $generator
*
* @return self<TValue>
*/
public static function fromGenerator(\Closure $generator): self
public static function fromGenerator(Closure $generator): self
{
/** @var \Closure():\Generator<TValue> $generator */
/** @var Closure():Generator<TValue> $generator */
$collection = $generator();

/** @var array<int,TValue> $asArray */
$asArray = \iterator_to_array($collection);
$asArray = iterator_to_array($collection);

return new self(\SplFixedArray::fromArray($asArray, false));
return new self(SplFixedArray::fromArray($asArray, false));
}

/**
Expand All @@ -155,19 +158,19 @@ public static function fromIterable(iterable $iterable = []): self
}

/**
* @return \Generator<TValue>
* @return Generator<TValue>
*/
public function getIterator(): \Generator
public function getIterator(): Generator
{
yield from $this->storage;
}

/**
* @param ?\Closure(TValue):bool $function
* @param ?Closure(TValue):bool $function
*
* @return TValue|null
*/
public function last(\Closure $function = null): mixed
public function last(Closure $function = null): mixed
{
$last = null;

Expand All @@ -183,13 +186,13 @@ public function last(\Closure $function = null): mixed
/**
* @template TMap
*
* @param \Closure(TValue):TMap $function
* @param Closure(TValue):TMap $function
*
* @return self<TMap>
*/
public function map(\Closure $function): self
public function map(Closure $function): self
{
return self::fromGenerator(function () use ($function): \Generator {
return self::fromGenerator(function () use ($function): Generator {
foreach ($this->storage as $value) {
yield $function($value);
}
Expand All @@ -200,11 +203,11 @@ public function map(\Closure $function): self
* @template TAccumulator
*
* @param ?TAccumulator $accumulator
* @param \Closure(TAccumulator|null,TValue):TAccumulator $function
* @param Closure(TAccumulator|null,TValue):TAccumulator $function
*
* @return ?TAccumulator
*/
public function reduce(\Closure $function, mixed $accumulator = null): mixed
public function reduce(Closure $function, mixed $accumulator = null): mixed
{
foreach ($this->storage as $value) {
$accumulator = $function($accumulator, $value);
Expand All @@ -224,7 +227,7 @@ public function reduce(\Closure $function, mixed $accumulator = null): mixed
* @throws OffsetMustBePositiveIntegerException
* @throws LengthMustBePositiveIntegerException
*/
public function slice(int $offset, int $length = \PHP_INT_MAX): self
public function slice(int $offset, int $length = PHP_INT_MAX): self
{
if ($offset < 0) {
throw new OffsetMustBePositiveIntegerException();
Expand All @@ -235,7 +238,7 @@ public function slice(int $offset, int $length = \PHP_INT_MAX): self
}

return self::fromGenerator(
function () use ($offset, $length): \Generator {
function () use ($offset, $length): Generator {
$total = 0;

if ($total !== $length) {
Expand All @@ -262,11 +265,11 @@ function () use ($offset, $length): \Generator {
*
* @return self<TValue>
*
* @throws OffsetMustBePositiveIntegerException
* @throws LengthMustBePositiveIntegerException
*/
public function take(int $length): self
{
/* @psalm-suppress MissingThrowsDocblock OffsetMustBePositiveIntegerException */
return $this->slice(0, $length);
}

Expand Down

0 comments on commit 489a8a8

Please sign in to comment.