Skip to content

Commit

Permalink
Minor code updates and increase code coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Aug 20, 2019
1 parent 831850e commit 89aa7a1
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 32 deletions.
36 changes: 35 additions & 1 deletion spec/drupol/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,23 @@ public function it_can_apply(): void

$this
->apply(static function ($item) {
return (bool) ($item % 2);
// do what you want here.

return true;
})
->shouldIterateAs(\range(1, 10));

$this
->apply(static function ($item) {
// do what you want here.

return false;
})
->shouldIterateAs(\range(1, 10));

$this
->apply(static function ($item): void {
$item %= 2;
})
->shouldIterateAs([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

Expand Down Expand Up @@ -108,6 +124,24 @@ public function it_can_apply(): void
})
->all()
->shouldReturn($context);

$context = [];

$applyCallback1 = static function ($item, $key) use (&$context) {
$context[] = $item;

return true;
};

$walkCallback2 = static function ($item) {
return $item;
};

$this::with(\range(1, 20))
->apply($applyCallback1)
->walk($walkCallback2)
->all()
->shouldReturn($context);
}

public function it_can_be_constructed_from_array(): void
Expand Down
26 changes: 13 additions & 13 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ public function all(): array
*/
public function append(...$items): CollectionInterface
{
return $this->run(Append::with(...$items));
return $this->run(Append::with($items));
}

/**
* {@inheritdoc}
*/
public function apply(callable $callback): CollectionInterface
public function apply(callable ...$callables): CollectionInterface
{
return $this->run(Apply::with($callback));
return $this->run(Apply::with($callables));
}

/**
Expand All @@ -95,7 +95,7 @@ public function collapse(): CollectionInterface
*/
public function combine($keys): CollectionInterface
{
return $this->run(Combine::with(...$keys));
return $this->run(Combine::with($keys));
}

/**
Expand Down Expand Up @@ -159,7 +159,7 @@ public function flip(): CollectionInterface
*/
public function forget(...$keys): CollectionInterface
{
return $this->run(Forget::with(...$keys));
return $this->run(Forget::with($keys));
}

/**
Expand Down Expand Up @@ -199,15 +199,15 @@ public function limit(int $limit): CollectionInterface
*/
public function map(callable ...$callbacks): CollectionInterface
{
return $this->run(Walk::with(...$callbacks), Normalize::with());
return $this->run(Walk::with($callbacks), Normalize::with());
}

/**
* {@inheritdoc}
*/
public function merge(...$sources): CollectionInterface
{
return $this->run(Merge::with(...\array_map([$this, 'makeIterator'], $sources)));
return $this->run(Merge::with(\array_map([$this, 'makeIterator'], $sources)));
}

/**
Expand All @@ -231,7 +231,7 @@ public function nth(int $step, int $offset = 0): CollectionInterface
*/
public function only(...$keys): CollectionInterface
{
return $this->run(Only::with(...$keys));
return $this->run(Only::with($keys));
}

/**
Expand All @@ -255,7 +255,7 @@ public function pluck($pluck, $default = null): CollectionInterface
*/
public function prepend(...$items): CollectionInterface
{
return $this->run(Prepend::with(...$items));
return $this->run(Prepend::with($items));
}

/**
Expand Down Expand Up @@ -309,7 +309,7 @@ public function run(Operation ...$operations)
*/
public function skip(int ...$counts): CollectionInterface
{
return $this->run(Skip::with(...$counts));
return $this->run(Skip::with($counts));
}

/**
Expand Down Expand Up @@ -350,13 +350,13 @@ static function () use ($number) {
*/
public function walk(callable ...$callbacks): CollectionInterface
{
return $this->run(Walk::with(...$callbacks));
return $this->run(Walk::with($callbacks));
}

/**
* Create a new collection instance.
*
* @param null|array|callable|Closure|CollectionInterface $data
* @param null|array|callable|Closure|CollectionInterface|\Iterator $data
*
* @return \drupol\collection\Contract\Collection
*/
Expand All @@ -374,7 +374,7 @@ public static function with($data = []): CollectionInterface
*/
public function zip(...$items): CollectionInterface
{
return $this->run(Zip::with(...$items));
return $this->run(Zip::with($items));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Contract/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public function append(...$items): self;
/**
* Apply a callback to all the element of an array.
*
* @param callable $callable
* @param callable ...$callables
*
* @return \drupol\collection\Contract\Collection
*/
public function apply(callable $callable): self;
public function apply(callable ...$callables): self;

/**
* Chunk the collection into chunks of the given size.
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Append.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class Append extends Operation
*/
public function run(CollectionInterface $collection): CollectionInterface
{
$items = $this->parameters;
[$items] = $this->parameters;

return Collection::with(
static function () use ($items, $collection) {
Expand Down
12 changes: 6 additions & 6 deletions src/Operation/Apply.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ final class Apply extends Operation
*/
public function run(CollectionInterface $collection): CollectionInterface
{
[$callback] = $this->parameters;
[$callbacks] = $this->parameters;

$newCollection = Collection::with($collection);

foreach ($newCollection as $key => $item) {
if (false === $callback($item, $key)) {
break;
foreach ($callbacks as $callback) {
foreach (Collection::with($collection) as $key => $item) {
if (false === $callback($item, $key)) {
break;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Combine.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class Combine extends Operation
*/
public function run(CollectionInterface $collection): CollectionInterface
{
$keys = $this->parameters;
[$keys] = $this->parameters;

return Collection::with(
static function () use ($keys, $collection) {
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Forget.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class Forget extends Operation
*/
public function run(CollectionInterface $collection): CollectionInterface
{
$keys = $this->parameters;
[$keys] = $this->parameters;

return Collection::with(
static function () use ($keys, $collection) {
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Merge.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class Merge extends Operation
*/
public function run(CollectionInterface $collection): CollectionInterface
{
$sources = $this->parameters;
[$sources] = $this->parameters;

return Collection::with(
static function () use ($sources, $collection) {
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Only.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class Only extends Operation
*/
public function run(CollectionInterface $collection): CollectionInterface
{
$keys = $this->parameters;
[$keys] = $this->parameters;

return Collection::with(
static function () use ($keys, $collection) {
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Prepend.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class Prepend extends Operation
*/
public function run(CollectionInterface $collection): CollectionInterface
{
$items = $this->parameters;
[$items] = $this->parameters;

return Collection::with(
static function () use ($items, $collection) {
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Rebase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ final class Rebase extends Operation
*/
public function run(CollectionInterface $collection): CollectionInterface
{
return Collection::with($collection->all());
return Collection::with($collection->getIterator());
}
}
2 changes: 1 addition & 1 deletion src/Operation/Skip.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class Skip extends Operation
*/
public function run(CollectionInterface $collection): CollectionInterface
{
$counts = $this->parameters;
[$counts] = $this->parameters;

return Collection::with(
static function () use ($counts, $collection) {
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Walk.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class Walk extends Operation
*/
public function run(CollectionInterface $collection): CollectionInterface
{
$callbacks = $this->parameters;
[$callbacks] = $this->parameters;

return Collection::with(
static function () use ($callbacks, $collection) {
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Zip.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class Zip extends Operation
*/
public function run(CollectionInterface $collection): CollectionInterface
{
$iterables = $this->parameters;
[$iterables] = $this->parameters;

return Collection::with(
static function () use ($iterables, $collection) {
Expand Down

0 comments on commit 89aa7a1

Please sign in to comment.