Skip to content

Commit

Permalink
Remove one of the interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai committed Apr 14, 2020
1 parent 1832ccb commit 97db16e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 112 deletions.
67 changes: 0 additions & 67 deletions src/Interfaces/PrincipalPipeline.php

This file was deleted.

26 changes: 17 additions & 9 deletions src/Interfaces/StandardPipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
/**
* Interface definitions for the standard pipeline.
*/
interface StandardPipeline extends PrincipalPipeline
interface StandardPipeline extends \IteratorAggregate, \Countable
{
/**
* {@inheritdoc}
* Takes a callback that for each input value may return one or yield many. Also takes an initial generator, where it must not require any arguments.
*
* With no callback is a no-op (can safely take a null).
*
* @param ?callable $func {@inheritdoc}
* @param ?callable $func a callback must either return a value or yield values (return a generator)
*
* @return $this
*/
Expand All @@ -45,7 +45,7 @@ public function map(?callable $func = null);
public function unpack(?callable $func = null);

/**
* {@inheritdoc}
* Removes elements unless a callback returns true.
*
* With no callback drops all null and false values (not unlike array_filter does by default).
*
Expand All @@ -66,19 +66,27 @@ public function filter(?callable $func = null);
public function reduce(?callable $func = null, $initial = null);

/**
* {@inheritdoc}
* Reduces input values to a single value.
*
* Defaults to summation.
*
* @param ?mixed $initial {@inheritdoc}
* @param ?callable $func {@inheritdoc}
* @param mixed $initial initial value for a $carry
* @param ?callable $func function (mixed $carry, mixed $item) { must return updated $carry }
*
* @return ?mixed
*/
public function fold($initial, ?callable $func = null);

/**
* {@inheritdoc}
* Performs a lazy zip operation on iterables, not unlike that of
* array_map with first argument set to null. Also known as transposition.
*
* @return $this
*/
public function zip(iterable ...$inputs);

/**
* By default returns all values regardless of keys used, discarding all keys in the process. Has an option to keep the keys.
*/
public function toArray(): array;
public function toArray(bool $useKeys = false): array;
}
29 changes: 6 additions & 23 deletions src/Principal.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*
* @internal
*/
abstract class Principal implements Interfaces\PrincipalPipeline
abstract class Principal implements \IteratorAggregate, \Countable
{
/**
* Pre-primed pipeline. This is not a full `iterable` per se because we exclude IteratorAggregate before assigning a value.
Expand All @@ -50,13 +50,9 @@ final public function __construct(iterable $input = null)
}

/**
* {@inheritdoc}
*
* @param callable $func {@inheritdoc}
*
* @return $this
*/
public function map(callable $func)
protected function map(callable $func)
{
// That's the standard case for any next stages
if (\is_iterable($this->pipeline)) {
Expand Down Expand Up @@ -105,13 +101,9 @@ private static function apply(iterable $previous, callable $func): iterable
}

/**
* {@inheritdoc}
*
* @param callable $func {@inheritdoc}
*
* @return $this
*/
public function filter(callable $func)
protected function filter(callable $func)
{
if (null === $this->pipeline) {
// No-op: null.
Expand Down Expand Up @@ -139,9 +131,6 @@ public function filter(callable $func)
return $this;
}

/**
* {@inheritdoc}
*/
public function getIterator(): \Traversable
{
if ($this->pipeline instanceof \Traversable) {
Expand All @@ -155,9 +144,6 @@ public function getIterator(): \Traversable
return new \EmptyIterator();
}

/**
* {@inheritdoc}
*/
public function toArray(bool $useKeys = false): array
{
if (null === $this->pipeline) {
Expand Down Expand Up @@ -205,14 +191,11 @@ public function count(): int
}

/**
* {@inheritdoc}
*
* @param mixed $initial {@inheritdoc}
* @param callable $func {@inheritdoc}
* @param mixed $initial
*
* @return ?mixed
*/
public function fold($initial, callable $func)
protected function fold($initial, callable $func)
{
if (\is_array($this->pipeline)) {
return \array_reduce($this->pipeline, $func, $initial);
Expand All @@ -226,7 +209,7 @@ public function fold($initial, callable $func)
}

/**
* {@inheritdoc}
* @return $this
*/
public function zip(iterable ...$inputs)
{
Expand Down
13 changes: 0 additions & 13 deletions tests/InterfaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
namespace Tests\Pipeline;

use PHPUnit\Framework\TestCase;
use Pipeline\Interfaces\PrincipalPipeline;
use Pipeline\Interfaces\StandardPipeline;
use Pipeline\Standard;

Expand All @@ -44,16 +43,4 @@ public function testStandardInterface(): void

$this->assertInstanceOf(StandardPipeline::class, $pipeline);
}

private function takesPrincipalInterface(PrincipalPipeline $pipeline)
{
return $pipeline;
}

public function testPrincipalInterface(): void
{
$pipeline = new Standard();

$this->assertInstanceOf(PrincipalPipeline::class, $this->takesPrincipalInterface($pipeline));
}
}

0 comments on commit 97db16e

Please sign in to comment.