Skip to content

Commit

Permalink
Various minor changes and optimizations.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Aug 12, 2020
1 parent b78a704 commit 6e09157
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 71 deletions.
29 changes: 29 additions & 0 deletions spec/loophp/collection/Iterator/CacheIteratorSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace spec\loophp\collection\Iterator;

use Iterator;
use loophp\collection\Iterator\CacheIterator;
use PhpSpec\ObjectBehavior;
use Psr\Cache\CacheItemPoolInterface;

class CacheIteratorSpec extends ObjectBehavior
{
public function it_can_get_the_inner_iterator(Iterator $iterator, CacheItemPoolInterface $cache)
{
$this
->beConstructedWith($iterator, $cache);

$this
->getInnerIterator()
->shouldReturn($iterator);
}

public function it_is_initializable(Iterator $iterator, CacheItemPoolInterface $cache)
{
$this->beConstructedWith($iterator, $cache);
$this->shouldHaveType(CacheIterator::class);
}
}
4 changes: 2 additions & 2 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,9 @@ public function last()
return $this->transform(new Last());
}

public function limit(int $limit): CollectionInterface
public function limit(int $limit, int $offset = 0): CollectionInterface
{
return $this->run(new Limit($limit));
return $this->run(new Limit($limit, $offset));
}

public function loop(): CollectionInterface
Expand Down
7 changes: 0 additions & 7 deletions src/Contract/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ interface Operation
{
public function __invoke(): Closure;

/**
* @param mixed|null $default
*
* @return mixed|null
*/
public function get(string $key, $default = null);

/**
* @return array<string, mixed>
*/
Expand Down
1 change: 0 additions & 1 deletion src/Iterator/CacheIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* @psalm-template T
*
* @implements \Iterator<TKey, T>
* @implements \OuterIterator<TKey, T>
*/
final class CacheIterator implements Iterator, OuterIterator
{
Expand Down
1 change: 0 additions & 1 deletion src/Iterator/ClosureIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*
* @extends ProxyIterator<TKey, T>
* @implements \Iterator<TKey, T>
* @implements \OuterIterator<TKey, T>
*/
final class ClosureIterator extends ProxyIterator implements Iterator, OuterIterator
{
Expand Down
1 change: 0 additions & 1 deletion src/Iterator/IterableIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*
* @extends ProxyIterator<TKey, T>
* @implements \Iterator<TKey, T>
* @implements \OuterIterator<TKey, T>
*/
final class IterableIterator extends ProxyIterator implements Iterator, OuterIterator
{
Expand Down
1 change: 0 additions & 1 deletion src/Iterator/ResourceIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*
* @extends ProxyIterator<TKey, T>
* @implements \Iterator<TKey, T>
* @implements \OuterIterator<TKey, T>
*/
final class ResourceIterator extends ProxyIterator implements Iterator, OuterIterator
{
Expand Down
1 change: 0 additions & 1 deletion src/Iterator/StringIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*
* @extends ProxyIterator<TKey, T>
* @implements \Iterator<TKey, T>
* @implements \OuterIterator<TKey, T>
*/
final class StringIterator extends ProxyIterator implements Iterator, OuterIterator
{
Expand Down
14 changes: 0 additions & 14 deletions src/Operation/AbstractOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,13 @@

namespace loophp\collection\Operation;

use function array_key_exists;

abstract class AbstractOperation
{
/**
* @var array<string, mixed>
*/
protected $storage = [];

/**
* @param mixed|null $default
*
* @return mixed|null
*/
public function get(string $key, $default = null)
{
return array_key_exists($key, $this->storage) ?
$this->storage[$key] :
$default;
}

/**
* @return array<string, mixed>
*/
Expand Down
23 changes: 9 additions & 14 deletions src/Operation/Intersperse.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,16 @@ final class Intersperse extends AbstractOperation implements Operation
* @param mixed $element
* @psalm-param T $element
*/
public function __construct($element, int $atEvery = 1, int $startAt = 0)
public function __construct($element, int $atEvery, int $startAt)
{
if (0 > $atEvery) {
throw new InvalidArgumentException('The second parameter must be a positive integer.');
}

if (0 > $startAt) {
throw new InvalidArgumentException('The third parameter must be a positive integer.');
}

$this->storage = [
'element' => $element,
'atEvery' => $atEvery,
Expand All @@ -37,19 +45,6 @@ public function __construct($element, int $atEvery = 1, int $startAt = 0)

public function __invoke(): Closure
{
/** @var int $every */
$every = $this->get('atEvery');
/** @var int $startAt */
$startAt = $this->get('startAt');

if (0 > $every) {
throw new InvalidArgumentException('The second parameter must be a positive integer.');
}

if (0 > $startAt) {
throw new InvalidArgumentException('The third parameter must be a positive integer.');
}

return
/**
* @psalm-param \Iterator<TKey, T> $iterator
Expand Down
4 changes: 2 additions & 2 deletions src/Operation/Random.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
final class Random extends AbstractOperation implements Operation
{
public function __construct(int $size = 1)
public function __construct(int $size)
{
$this->storage = [
'size' => $size,
Expand All @@ -33,7 +33,7 @@ public function __invoke(): Closure
* @psalm-return \Generator<TKey, T>
*/
static function (Iterator $iterator, int $size): Generator {
yield from (new Run(new Limit($size)))((new Run(new Shuffle()))($iterator));
yield from (new Run(new Limit($size), new Shuffle()))($iterator);
};
}
}
7 changes: 6 additions & 1 deletion src/Operation/Sort.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ static function (Iterator $iterator, int $type, callable $callback): Generator {
* @psalm-param array{TKey, T} $right
*/
static function (array $left, array $right) use ($callback): int {
return $callback(current($left), current($right));
/** @psalm-var T $left */
$left = current($left);
/** @psalm-var T $right */
$right = current($right);

return $callback($left, $right);
};

$arrayIterator = new ArrayIterator(iterator_to_array((new Run(...$operations['before']))($iterator)));
Expand Down
6 changes: 3 additions & 3 deletions src/Operation/Tail.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public function __invoke(): Closure
static function (Iterator $iterator, int $length): Generator {
return yield from (
new Run(
new Skip(iterator_count($iterator) - $length),
new Limit($length)
))(
(new Run(new Skip(iterator_count($iterator) - $length)))($iterator)
);
)
)($iterator);
};
}
}
4 changes: 1 addition & 3 deletions src/Operation/Window.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ static function (Iterator $iterator, ArrayIterator $length): Generator {

for ($i = 0; iterator_count($iterator) > $i; ++$i) {
/** @psalm-var list<T> $window */
$window = iterator_to_array((new Run(new Slice($i, $length->current())))($iterator));
yield iterator_to_array((new Run(new Slice($i, $length->current())))($iterator));

$length->next();

yield $window;
}
};
}
Expand Down
39 changes: 20 additions & 19 deletions src/Transformation/Contains.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ final class Contains implements Transformation
* @var ArrayIterator<int, mixed>
* @psalm-var \ArrayIterator<int, T>
*/
private $value;
private $values;

/**
* @param mixed ...$value
* @psalm-param T ...$value
* @param mixed ...$values
* @psalm-param T ...$values
*/
public function __construct(...$value)
public function __construct(...$values)
{
$this->value = new ArrayIterator($value);
$this->values = new ArrayIterator($values);
}

/**
Expand All @@ -39,19 +39,20 @@ public function __construct(...$value)
*/
public function __invoke(Iterator $collection)
{
$value = $this->value;

return (new FoldLeft(
static function (bool $carry, $item, $key) use ($collection) {
$hasCallback = static function ($k, $v) use ($item) {
return $item;
};

return ((new Transform(new Has($hasCallback)))($collection)) ?
$carry :
false;
},
true
))($value);
$values = $this->values;

foreach ($collection as $key => $value) {
foreach ($values as $k => $v) {
if ($v === $value) {
unset($values[$k]);
}

if (0 === $values->count()) {
return true;
}
}
}

return false;
}
}
2 changes: 1 addition & 1 deletion src/Transformation/Implode.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class Implode implements Transformation
*/
private $glue;

public function __construct(string $glue = '')
public function __construct(string $glue)
{
$this->glue = $glue;
}
Expand Down

0 comments on commit 6e09157

Please sign in to comment.