Skip to content

Commit

Permalink
refactor: Simplify static constructors.
Browse files Browse the repository at this point in the history
Signed-off-by: Pol Dellaiera <pol.dellaiera@protonmail.com>
  • Loading branch information
drupol committed Oct 11, 2020
1 parent 464ff15 commit 958dc79
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 47 deletions.
19 changes: 14 additions & 5 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Closure;
use Exception;
use Generator;
use InvalidArgumentException;
use Iterator;
use JsonSerializable;
use loophp\collection\Collection;
Expand Down Expand Up @@ -169,6 +168,12 @@ public function it_can_be_constructed_from_a_stream(): void
$this::fromResource($stream)
->implode()
->shouldIterateAs([55 => $string]);

$stream = imagecreate(100, 100);

$this::with($stream)
->count()
->shouldReturn(1);
}

public function it_can_be_constructed_from_array(): void
Expand Down Expand Up @@ -2629,8 +2634,7 @@ public function it_can_use_times_with_a_callback(): void
->shouldIterateAs($a);

$this::times(-1, 'count')
->shouldThrow(InvalidArgumentException::class)
->during('all');
->shouldIterateAs([]);
}

public function it_can_use_times_without_a_callback(): void
Expand All @@ -2639,11 +2643,16 @@ public function it_can_use_times_without_a_callback(): void
->shouldIterateAs(range(1, 10));

$this::times(-5)
->shouldThrow(InvalidArgumentException::class)
->during('all');
->shouldIterateAs([]);

$this::times(1)
->shouldIterateAs([1]);

$this::times(0)
->shouldIterateAs([]);

$this::times()
->shouldIterateAs([]);
}

public function it_can_use_with(): void
Expand Down
95 changes: 53 additions & 42 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,15 @@ public function __construct($data = [], ...$parameters)
case is_resource($data) && 'stream' === get_resource_type($data):
$this->source =
/**
* @param mixed $data
* @psalm-param resource $data
*
* @param mixed $data
* @psalm-return Generator<int, string>
*/
static function ($data): Generator {
while (false !== $chunk = fgetc($data)) {
yield $chunk;
}
return yield from new ResourceIterator($data);
};

$this->parameters = [
$data,
];
Expand All @@ -167,73 +167,68 @@ static function ($data): Generator {
case is_callable($data):
$this->source =
/**
* @psalm-var callable(mixed...):(T)
* @psalm-var array<int, mixed>
* @psalm-param callable $data
* @psalm-param list<T> $parameters
*
* @psalm-return Generator<TKey, T>
*/
static function (callable $data, array $parameters): Generator {
return yield from $data(...$parameters);
return yield from new ClosureIterator($data, ...$parameters);
};

$this->parameters = [
$data,
$parameters,
];

break;
case is_iterable($data):
$this->source = static function (iterable $data): Generator {
foreach ($data as $key => $value) {
yield $key => $value;
}
};
$this->source =
/**
* @psalm-param iterable<TKey, T> $data
*
* @psalm-return Generator<TKey, T>
*/
static function (iterable $data): Generator {
return yield from new IterableIterator($data);
};

$this->parameters = [
$data,
];

break;
case is_string($data):
/** @psalm-var array{scalar} $parameters */
$parameters += [0 => null];
$separator = (string) $parameters[0];

$this->source = static function (string $data, string $separator): Generator {
$offset = 0;

$nextOffset = '' !== $separator ?
mb_strpos($data, $separator, $offset) :
1;

while (mb_strlen($data) > $offset && false !== $nextOffset) {
yield mb_substr($data, $offset, $nextOffset - $offset);
$offset = $nextOffset + mb_strlen($separator);

$nextOffset = '' !== $separator ?
mb_strpos($data, $separator, $offset) :
$nextOffset + 1;
}

if ('' !== $separator) {
yield mb_substr($data, $offset);
}
};
$this->source =
/**
* @param mixed $parameters
* @psalm-param list<string> $parameters
*
* @psalm-return Generator<int, string>
*/
static function (string $data, $parameters): Generator {
return yield from new StringIterator($data, ...$parameters);
};

$this->parameters = [
$data,
$separator,
$parameters,
];

break;

default:
$this->source =
/**
* @psalm-param mixed|scalar $data
*
* @param mixed $data
* @psalm-param mixed|scalar $data
*/
static function ($data): Generator {
foreach ((array) $data as $key => $value) {
yield $key => $value;
}
};

$this->parameters = [
$data,
];
Expand Down Expand Up @@ -440,7 +435,15 @@ public function frequency(): CollectionInterface
public static function fromCallable(callable $callable, ...$parameters): CollectionInterface
{
return new self(
static function (callable $callable, array $parameters): Generator {
/**
* @psalm-param callable $data
* @psalm-param list<T> $parameters
*
* @psalm-return Generator<TKey, T>
*
* @param mixed $parameters
*/
static function (callable $callable, $parameters): Generator {
return yield from new ClosureIterator($callable, ...$parameters);
},
$callable,
Expand All @@ -461,6 +464,11 @@ static function (string $filepath): Generator {
public static function fromIterable(iterable $iterable): CollectionInterface
{
return new self(
/**
* @psalm-param iterable<TKey, T> $data
*
* @psalm-return Generator<TKey, T>
*/
static function (iterable $iterable): Generator {
return yield from new IterableIterator($iterable);
},
Expand All @@ -472,9 +480,10 @@ public static function fromResource($resource): CollectionInterface
{
return new self(
/**
* @psalm-param resource $resource
*
* @param mixed $resource
* @psalm-param resource $data
*
* @psalm-return Generator<int, string>
*/
static function ($resource): Generator {
return yield from new ResourceIterator($resource);
Expand All @@ -487,6 +496,8 @@ public static function fromString(string $string, string $delimiter = ''): Colle
{
return new self(
/**
* @psalm-param list<string> $parameters
*
* @psalm-return Generator<int, string>
*/
static function (string $string, string $delimiter): Generator {
Expand Down

0 comments on commit 958dc79

Please sign in to comment.