From 274132fafebf6d5dad604b2a95bca1bf20223214 Mon Sep 17 00:00:00 2001 From: Camille Hodoul Date: Wed, 19 Jul 2023 07:21:44 +0200 Subject: [PATCH 1/8] Update dev dependencies, support php 8.1, fix phpstan errors --- .gitignore | 1 + composer.json | 29 +++++++------ phpbench.json | 2 +- phpunit.xml.dist | 23 +++++----- phpunit.xml.dist.bak | 13 ++++++ src/Exception/InvalidArgumentException.php | 20 ++++----- src/LazyIterator.php | 44 ++++++++++--------- src/LazyWorker.php | 50 +++++++++++----------- src/Transducer/Each.php | 15 ++++--- src/Transducer/Filter.php | 17 +++++--- src/Transducer/Flatten.php | 33 +++++++------- src/Transducer/Map.php | 15 ++++--- src/Transducer/PureTransducer.php | 8 ++-- src/Transducer/Reduce.php | 24 +++++------ src/Transducer/Take.php | 21 +++++---- src/Transducer/TransducerInterface.php | 10 ++--- src/Transducer/Until.php | 21 +++++---- src/each.php | 10 +++-- src/filter.php | 9 ++-- src/flatten.php | 14 +++--- src/isAssociativeArray.php | 2 +- src/iterate.php | 2 +- src/map.php | 10 +++-- src/pipe.php | 2 +- src/reduce.php | 12 +++--- src/take.php | 9 ++-- src/until.php | 7 +-- tests/EachTest.php | 2 +- tests/FilterTest.php | 2 +- tests/MapTest.php | 2 +- tests/PipeTest.php | 2 +- tests/ReduceTest.php | 2 +- tests/TakeTest.php | 2 +- tests/UntilTest.php | 2 +- 34 files changed, 242 insertions(+), 195 deletions(-) create mode 100644 phpunit.xml.dist.bak diff --git a/.gitignore b/.gitignore index dcd4b15..5a7020a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .phpunit.result.cache +.phpunit.cache /build/ /composer.lock /phpunit.xml diff --git a/composer.json b/composer.json index 54d64e2..0640609 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "authors": [ { "name": "Camille\u0020Hodoul", - "email": "camille.hodoul\u0040gmail.com", + "email": "camilleh\u0040hey.com", "homepage": "https\u003A\/\/camillehdl.dev" } ], @@ -19,16 +19,7 @@ "source": "https://github.com/camille\u002Dhdl/lazy\u002Dlists.git" }, "require": { - "php": "^7.2" - }, - "require-dev": { - "jakub-onderka/php-parallel-lint": "^1", - "mockery/mockery": "^1", - "phpbench/phpbench": "^0.17.1", - "phpstan/phpstan": "^0.11", - "phpstan/phpstan-mockery": "^0.11", - "phpunit/phpunit": "^8", - "squizlabs/php_codesniffer": "^3" + "php": "^8.1" }, "autoload": { "psr-4": { @@ -65,9 +56,10 @@ "phpbench run benchmarks/PipeFilterMapReduceBench.php --report=aggregate", "phpbench run benchmarks/PipeMapFilterTakeBench.php --report=aggregate" ], - "phpunit": "phpunit --verbose --colors=always", - "phpunit-ci": "phpunit --verbose --coverage-clover build/logs/clover.xml", - "phpunit-coverage": "phpunit --verbose --colors=always --coverage-html build/coverage", + "phpunit": "phpunit --colors=always", + "phpunit-migrate": "phpunit --migrate-configuration", + "phpunit-ci": "phpunit --coverage-clover build/logs/clover.xml", + "phpunit-coverage": "phpunit --colors=always --coverage-html build/coverage", "test": [ "@lint", "@phpcs", @@ -83,5 +75,14 @@ }, "config": { "sort-packages": true + }, + "require-dev": { + "mockery/mockery": "^1.6", + "php-parallel-lint/php-parallel-lint": "^1.3", + "phpbench/phpbench": "^1.2", + "phpstan/phpstan": "^1.10", + "phpstan/phpstan-mockery": "^1.1", + "phpunit/phpunit": "^10.2", + "squizlabs/php_codesniffer": "^3.7" } } diff --git a/phpbench.json b/phpbench.json index 466f9e5..baf584a 100644 --- a/phpbench.json +++ b/phpbench.json @@ -1,3 +1,3 @@ { - "bootstrap": "vendor/autoload.php" + "runner.bootstrap": "vendor/autoload.php" } \ No newline at end of file diff --git a/phpunit.xml.dist b/phpunit.xml.dist index c8a3dd6..6cf0308 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,13 +1,14 @@ - - - - ./tests - - - - - ./src - - + + + + + ./tests + + + + + ./src + + diff --git a/phpunit.xml.dist.bak b/phpunit.xml.dist.bak new file mode 100644 index 0000000..c8a3dd6 --- /dev/null +++ b/phpunit.xml.dist.bak @@ -0,0 +1,13 @@ + + + + + ./tests + + + + + ./src + + + diff --git a/src/Exception/InvalidArgumentException.php b/src/Exception/InvalidArgumentException.php index a5eaf4a..3720e46 100644 --- a/src/Exception/InvalidArgumentException.php +++ b/src/Exception/InvalidArgumentException.php @@ -8,9 +8,9 @@ namespace LazyLists\Exception; -class InvalidArgumentException extends \InvalidArgumentException +final class InvalidArgumentException extends \InvalidArgumentException { - public static function assertCollection($collection, $callee, $parameterPosition) + public static function assertCollection(mixed $collection, string $callee, int $parameterPosition = 1): void { self::assertCollectionAlike($collection, 'Traversable', $callee, $parameterPosition); } @@ -22,7 +22,7 @@ public static function assertCollection($collection, $callee, $parameterPosition * @param integer $parameterPosition * @throws InvalidArgumentException */ - private static function assertCollectionAlike($collection, $className, $callee, $parameterPosition) + private static function assertCollectionAlike(mixed $collection, string $className, string $callee, int $parameterPosition): void { if (!\is_array($collection) && !$collection instanceof $className) { throw new static( @@ -37,22 +37,22 @@ private static function assertCollectionAlike($collection, $className, $callee, } } - /** - * @param mixed $value - * @return string - */ - private static function getType($value): string + private static function getType(mixed $value): string { return \is_object($value) ? \get_class($value) : \gettype($value); } - public static function assertTransducers(array $candidates, $callee) + /** + * @param array $candidates + * @param string $callee + */ + public static function assertTransducers(array $candidates, string $callee): void { foreach ($candidates as $candidate) { self::assertTransducer($candidate, $callee); } } - public static function assertTransducer($candidate, $callee) + public static function assertTransducer(mixed $candidate, string $callee): void { if (!$candidate instanceof \LazyLists\Transducer\TransducerInterface) { throw new static( diff --git a/src/LazyIterator.php b/src/LazyIterator.php index 3e9e025..5038fda 100644 --- a/src/LazyIterator.php +++ b/src/LazyIterator.php @@ -19,19 +19,24 @@ * * This class can be instanciated by `LazyLists\iterate()` * @see \LazyLists\iterate + * @template InputType + * @implements \Iterator */ class LazyIterator extends LazyWorker implements \Iterator { - protected $key = 0; - protected $currentValueBuffer = []; - protected $currentValue = null; - protected $hasValue = false; - protected $canLoop = false; + protected int $key = 0; /** - * @param array|\Iterator $subject - * @param array $transducers + * @var array */ - public function __construct($subject, array $transducers) + protected array $currentValueBuffer = []; + protected mixed $currentValue = null; + protected bool $hasValue = false; + protected bool $canLoop = false; + /** + * @param array|\Iterator $subject + * @param \LazyLists\Transducer\TransducerInterface[] $transducers + */ + public function __construct(array|\Iterator $subject, array $transducers) { parent::__construct($subject, $transducers); $this->registerValueCallback(function ($value) { @@ -40,13 +45,13 @@ public function __construct($subject, array $transducers) }); } - public function iteratorInitialization() + public function iteratorInitialization(): void { $this->key = 0; $this->reset(); if ($this->iterator->valid()) { $this->currentWorkingValue = $this->iterator->current(); - $this->finalResultSoFar = $this->getLastTransducer()->getEmptyFinalResult(); + $this->finalResultSoFar = $this->getLastTransducer()?->getEmptyFinalResult(); } $this->computeFirstValue(); } @@ -63,10 +68,7 @@ protected function reset() $this->iterator->rewind(); } - /** - * @return void - */ - public function rewind() + public function rewind(): void { $this->iteratorInitialization(); } @@ -89,13 +91,13 @@ protected function computeFirstValue() $this->setCurrentValueFromBuffer(); } - protected function loopUntilNextValue() + protected function loopUntilNextValue(): void { $this->canLoop = true; $this->loop(); } - protected function setCurrentValueFromBuffer() + protected function setCurrentValueFromBuffer(): void { if (\count($this->currentValueBuffer) > 0) { $this->hasValue = true; @@ -103,7 +105,7 @@ protected function setCurrentValueFromBuffer() } } - protected function computeCurrentValue() + protected function computeCurrentValue(): void { $this->hasValue = false; $this->key++; @@ -113,22 +115,22 @@ protected function computeCurrentValue() $this->setCurrentValueFromBuffer(); } - public function valid() + public function valid(): bool { return $this->hasValue; } - public function next() + public function next(): void { $this->computeCurrentValue(); } - public function current() + public function current(): mixed { return $this->currentValue; } - public function key() + public function key(): mixed { return $this->key; } diff --git a/src/LazyWorker.php b/src/LazyWorker.php index 836f045..752d7fe 100644 --- a/src/LazyWorker.php +++ b/src/LazyWorker.php @@ -70,15 +70,15 @@ class LazyWorker * List of callables to be invoked whenever a new value * is obtained * - * @var array + * @var array */ protected $newValueCallbacks = []; /** - * @param array|\Iterator $subject - * @param array $transducers + * @param array|\Iterator $subject + * @param \LazyLists\Transducer\TransducerInterface[] $transducers */ - public function __construct($subject, array $transducers) + public function __construct(array|\Iterator $subject, array $transducers) { if (\count($transducers) <= 0) { throw new \LogicException( @@ -131,7 +131,7 @@ protected function run() return $this->finalResultSoFar; } $this->currentWorkingValue = $this->iterator->current(); - $this->finalResultSoFar = $this->getLastTransducer()->getEmptyFinalResult(); + $this->finalResultSoFar = $this->getLastTransducer()?->getEmptyFinalResult(); $this->loop(); return $this->finalResultSoFar; } @@ -170,9 +170,9 @@ protected function onNewValue($value) public function readNextItem() { $computedValuesInfo = $this->computedValuesToProcess(); - if (!\is_null($computedValuesInfo)) { + if (!\is_null($computedValuesInfo) && is_array($computedValuesInfo) && isset($computedValuesInfo["index"])) { $computedValue = $this->readComputedValueToProcessForIndex( - $computedValuesInfo["index"] + (int)$computedValuesInfo["index"] ); return $computedValue; } @@ -201,7 +201,7 @@ public function yieldToNextTransducer($newCurrentWorkingValue) * by the Transducers downstream, it can call this method. * For example : `Flatten` * - * @param array $futureValues + * @param array $futureValues * @see \LazyLists\Transducer\Flatten * @return void */ @@ -280,7 +280,7 @@ protected function nextTransducer() */ protected function hasComputedValuesForIndex(int $index) { - return isset($this->computedFutureValues[$index]) && + return isset($this->computedFutureValues[$index]) && \is_array($this->computedFutureValues[$index]) && \count($this->computedFutureValues[$index]) > 0; } @@ -292,7 +292,7 @@ protected function hasComputedValuesForIndex(int $index) protected function readComputedValueToProcessForIndex(int $index) { if ( - isset($this->computedFutureValues[$index]) && + isset($this->computedFutureValues[$index]) && \is_array($this->computedFutureValues[$index]) && \count($this->computedFutureValues[$index]) > 0 ) { return \array_shift($this->computedFutureValues[$index]); @@ -336,8 +336,10 @@ protected function resetTransducers() if (\is_null($computedValuesToProcess)) { $this->computedFutureValues = []; $this->currentTransducerIndex = 0; - } else { - $this->currentTransducerIndex = $computedValuesToProcess["index"]; + } elseif (\is_array($computedValuesToProcess) && isset($computedValuesToProcess["index"])) { + if (\is_numeric($computedValuesToProcess["index"])) { + $this->currentTransducerIndex = (int)$computedValuesToProcess["index"]; + } } } @@ -362,11 +364,10 @@ protected function resetLoop() * Typically: when `flatten()` is the last transducer in the pipeline. * * @param integer $fromIndex - * @return array + * @return array|\Traversable */ - protected function readAllFutureComputedValues(int $fromIndex): array + protected function readAllFutureComputedValues(int $fromIndex): array|\Traversable { - $output = []; $isFutureTransducerIndexWithComputedValues = function ($index) use ($fromIndex) { return $index > $fromIndex && $this->hasComputedValuesForIndex($index); }; @@ -396,6 +397,9 @@ protected function readAllFutureComputedValues(int $fromIndex): array protected function updateFinalResult() { $lastTransducer = $this->getLastTransducer(); + if (!$lastTransducer) { + return; + } $this->onNewValue($this->currentWorkingValue); $this->finalResultSoFar = $lastTransducer->computeFinalResult( $this->finalResultSoFar, @@ -436,10 +440,10 @@ protected function initializeTransducers() } /** - * @param array|\Iterator $subject - * @return \Iterator + * @param array|\Iterator $subject + * @return \Iterator */ - protected static function iteratorFromSubject($subject) + protected static function iteratorFromSubject(array|\Iterator $subject) { if (\is_array($subject)) { return new ArrayIterator($subject); @@ -447,16 +451,10 @@ protected static function iteratorFromSubject($subject) if ($subject instanceof \Iterator) { return $subject; } - throw new \InvalidArgumentException( - \sprintf("Could not create Iterator from subject") - ); } - /** - * @return \LazyLists\Transducer\TransducerInterface - */ - protected function getLastTransducer() + protected function getLastTransducer(): ?\LazyLists\Transducer\TransducerInterface { - return $this->transducers[\count($this->transducers) - 1]; + return $this->transducers[\count($this->transducers) - 1] ?? null; } } diff --git a/src/Transducer/Each.php b/src/Transducer/Each.php index f99df08..7b99e13 100644 --- a/src/Transducer/Each.php +++ b/src/Transducer/Each.php @@ -28,24 +28,27 @@ public function __construct(callable $sideEffect) $this->sideEffect = $sideEffect; } - public function computeNextResult($item) + public function computeNextResult(mixed $item): void { $sideEffect = $this->sideEffect; $sideEffect($item); - $this->worker->yieldToNextTransducer($item); + $this->worker?->yieldToNextTransducer($item); } - public function getEmptyFinalResult() + public function getEmptyFinalResult(): mixed { return []; } - public function computeFinalResult($previousResult, $lastValue) + public function computeFinalResult(mixed $previousResult, mixed $lastValue): mixed { if (\is_null($previousResult)) { return []; } - $previousResult[] = $lastValue; - return $previousResult; + if ($previousResult instanceof \ArrayAccess || \is_array($previousResult)) { + $previousResult[] = $lastValue; + return $previousResult; + } + throw new \LogicException('Cannot use Each transducer on a non-array, non-ArrayAccess result'); } } diff --git a/src/Transducer/Filter.php b/src/Transducer/Filter.php index ac84a97..11bed7b 100644 --- a/src/Transducer/Filter.php +++ b/src/Transducer/Filter.php @@ -31,27 +31,30 @@ public function __construct(callable $predicate) $this->predicate = $predicate; } - public function computeNextResult($item) + public function computeNextResult(mixed $item): void { $predicate = $this->predicate; if ($predicate($item)) { - $this->worker->yieldToNextTransducer($item); + $this->worker?->yieldToNextTransducer($item); } else { - $this->worker->skipToNextLoop(); + $this->worker?->skipToNextLoop(); } } - public function getEmptyFinalResult() + public function getEmptyFinalResult(): mixed { return []; } - public function computeFinalResult($previousResult, $lastValue) + public function computeFinalResult(mixed $previousResult, mixed $lastValue): mixed { if (\is_null($previousResult)) { return []; } - $previousResult[] = $lastValue; - return $previousResult; + if ($previousResult instanceof \ArrayAccess || \is_array($previousResult)) { + $previousResult[] = $lastValue; + return $previousResult; + } + throw new \LogicException('Cannot use Filter transducer on a non-array, non-ArrayAccess result'); } } diff --git a/src/Transducer/Flatten.php b/src/Transducer/Flatten.php index 97d6c2b..1eeeed0 100644 --- a/src/Transducer/Flatten.php +++ b/src/Transducer/Flatten.php @@ -16,9 +16,6 @@ use function LazyLists\isAssociativeArray; -/** - * - */ class Flatten extends PureTransducer implements TransducerInterface { /** @@ -36,6 +33,7 @@ public function __construct(int $levels) /** * @param mixed $subject * @return boolean + * @phpstan-assert array $subject */ protected static function isPlainArray($subject): bool { @@ -44,15 +42,15 @@ protected static function isPlainArray($subject): bool /** * @param integer $levels - * @param array|\Traversable $item - * @return array + * @param array|\Traversable $item + * @return array */ - public static function flattenItem(int $levels, $item): array + public static function flattenItem(int $levels, array|\Traversable $item): array { $output = []; foreach ($item as $child) { - if (self::isPlainArray($child) && $levels > 1) { - $output = array_merge($output, self::flattenItem($levels - 1, $child)); + if (((\is_array($child) && self::isPlainArray($child)) || $child instanceof \Traversable) && $levels > 1) { + $output = \array_merge($output, self::flattenItem($levels - 1, $child)); } else { $output[] = $child; } @@ -60,31 +58,34 @@ public static function flattenItem(int $levels, $item): array return $output; } - public function computeNextResult($item) + public function computeNextResult(mixed $item): void { if (!\is_array($item)) { - $this->worker->yieldToNextTransducer($item); + $this->worker?->yieldToNextTransducer($item); return; } if (isAssociativeArray($item)) { - $this->worker->yieldToNextTransducer($item); + $this->worker?->yieldToNextTransducer($item); return; } $flattened = self::flattenItem($this->levels, $item); - $this->worker->yieldToNextTransducerWithFutureValues($flattened); + $this->worker?->yieldToNextTransducerWithFutureValues($flattened); } - public function getEmptyFinalResult() + public function getEmptyFinalResult(): mixed { return []; } - public function computeFinalResult($previousResult, $lastValue) + public function computeFinalResult(mixed $previousResult, mixed $lastValue): mixed { if (\is_null($previousResult)) { return []; } - $previousResult[] = $lastValue; - return $previousResult; + if ($previousResult instanceof \ArrayAccess || \is_array($previousResult)) { + $previousResult[] = $lastValue; + return $previousResult; + } + throw new \LogicException('Cannot use Flatten transducer on a non-array, non-ArrayAccess result'); } } diff --git a/src/Transducer/Map.php b/src/Transducer/Map.php index 7074e18..1f2ed4d 100644 --- a/src/Transducer/Map.php +++ b/src/Transducer/Map.php @@ -32,23 +32,26 @@ public function __construct(callable $procedure) $this->procedure = $procedure; } - public function computeNextResult($item) + public function computeNextResult(mixed $item): void { $procedure = $this->procedure; - $this->worker->yieldToNextTransducer($procedure($item)); + $this->worker?->yieldToNextTransducer($procedure($item)); } - public function getEmptyFinalResult() + public function getEmptyFinalResult(): mixed { return []; } - public function computeFinalResult($previousResult, $lastValue) + public function computeFinalResult(mixed $previousResult, mixed $lastValue): mixed { if (\is_null($previousResult)) { return []; } - $previousResult[] = $lastValue; - return $previousResult; + if ($previousResult instanceof \ArrayAccess || \is_array($previousResult)) { + $previousResult[] = $lastValue; + return $previousResult; + } + throw new \LogicException('Cannot use Map transducer on a non-array, non-ArrayAccess result'); } } diff --git a/src/Transducer/PureTransducer.php b/src/Transducer/PureTransducer.php index d56c6c7..110cd3b 100644 --- a/src/Transducer/PureTransducer.php +++ b/src/Transducer/PureTransducer.php @@ -22,16 +22,16 @@ */ abstract class PureTransducer { - protected $worker; - public function __invoke($item) + protected ?LazyWorker $worker = null; + public function __invoke(mixed $item): void { $this->computeNextResult($item); } - public function initialize(LazyWorker $worker) + public function initialize(LazyWorker $worker): void { $this->worker = $worker; } - abstract public function computeNextResult($item); + abstract public function computeNextResult(mixed $item): void; } diff --git a/src/Transducer/Reduce.php b/src/Transducer/Reduce.php index da3a883..e958a94 100644 --- a/src/Transducer/Reduce.php +++ b/src/Transducer/Reduce.php @@ -16,15 +16,15 @@ use LazyLists\LazyWorker; -/** - * - */ class Reduce implements TransducerInterface { - protected $worker; + protected ?LazyWorker $worker = null; + /** + * @var callable + */ protected $accumulator; - protected $initialReduction; - protected $reduction; + protected mixed $initialReduction; + protected mixed $reduction; public function __invoke($item) { $this->computeNextResult($item); @@ -32,7 +32,7 @@ public function __invoke($item) public function __construct( callable $accumulator, - $initialReduction + mixed $initialReduction ) { $this->accumulator = $accumulator; $this->initialReduction = $initialReduction; @@ -41,23 +41,23 @@ public function __construct( public function initialize( LazyWorker $worker - ) { + ): void { $this->worker = $worker; } - public function getEmptyFinalResult() + public function getEmptyFinalResult(): mixed { return $this->initialReduction; } - public function computeNextResult($item) + public function computeNextResult(mixed $item): void { $accumulator = $this->accumulator; $this->reduction = $accumulator($this->reduction, $item); - $this->worker->yieldToNextTransducer($this->reduction); + $this->worker?->yieldToNextTransducer($this->reduction); } - public function computeFinalResult($previousResult, $lastValue) + public function computeFinalResult(mixed $previousResult, mixed $lastValue): mixed { return $lastValue; } diff --git a/src/Transducer/Take.php b/src/Transducer/Take.php index fff28eb..589882e 100644 --- a/src/Transducer/Take.php +++ b/src/Transducer/Take.php @@ -21,9 +21,12 @@ */ class Take implements TransducerInterface { - protected $worker; - protected $taken; - protected $numberOfItems; + protected ?LazyWorker $worker = null; + /** + * @var array + */ + protected array $taken = []; + protected int $numberOfItems; public function __invoke($item) { $this->computeNextResult($item); @@ -37,26 +40,26 @@ public function __construct( public function initialize( LazyWorker $worker - ) { + ): void { $this->taken = []; $this->worker = $worker; } - public function getEmptyFinalResult() + public function getEmptyFinalResult(): mixed { return $this->taken; } - public function computeNextResult($item) + public function computeNextResult(mixed $item): void { if (\count($this->taken) >= $this->numberOfItems - 1) { - $this->worker->completeEarly(); + $this->worker?->completeEarly(); } $this->taken[] = $item; - $this->worker->yieldToNextTransducer($item); + $this->worker?->yieldToNextTransducer($item); } - public function computeFinalResult($previousResult, $lastValue) + public function computeFinalResult(mixed $previousResult, mixed $lastValue): mixed { return $this->taken; } diff --git a/src/Transducer/TransducerInterface.php b/src/Transducer/TransducerInterface.php index c39e216..8caa322 100644 --- a/src/Transducer/TransducerInterface.php +++ b/src/Transducer/TransducerInterface.php @@ -28,17 +28,17 @@ interface TransducerInterface * @param mixed $item * @return void */ - public function __invoke($item); + public function __invoke(mixed $item); /** * @param LazyWorker $worker * @return void */ - public function initialize(LazyWorker $worker); + public function initialize(LazyWorker $worker): void; - public function computeNextResult($item); + public function computeNextResult(mixed $item): void; - public function getEmptyFinalResult(); + public function getEmptyFinalResult(): mixed; - public function computeFinalResult($previousResult, $lastValue); + public function computeFinalResult(mixed $previousResult, mixed $lastValue): mixed; } diff --git a/src/Transducer/Until.php b/src/Transducer/Until.php index 2cb80af..eb62bbd 100644 --- a/src/Transducer/Until.php +++ b/src/Transducer/Until.php @@ -14,6 +14,8 @@ namespace LazyLists\Transducer; +use ArrayAccess; + /** * @see \LazyLists\filter */ @@ -31,28 +33,31 @@ public function __construct(callable $condition) $this->condition = $condition; } - public function computeNextResult($item) + public function computeNextResult(mixed $item): void { $condition = $this->condition; if ($condition($item)) { - $this->worker->completeEarly(); - $this->worker->skipToNextLoop(); + $this->worker?->completeEarly(); + $this->worker?->skipToNextLoop(); } else { - $this->worker->yieldToNextTransducer($item); + $this->worker?->yieldToNextTransducer($item); } } - public function getEmptyFinalResult() + public function getEmptyFinalResult(): mixed { return []; } - public function computeFinalResult($previousResult, $lastValue) + public function computeFinalResult(mixed $previousResult, mixed $lastValue): mixed { if (\is_null($previousResult)) { return []; } - $previousResult[] = $lastValue; - return $previousResult; + if ($previousResult instanceof \ArrayAccess || \is_array($previousResult)) { + $previousResult[] = $lastValue; + return $previousResult; + } + throw new \LogicException('Cannot use Until transducer on a non-array, non-ArrayAccess result'); } } diff --git a/src/each.php b/src/each.php index ce7630d..779afd8 100644 --- a/src/each.php +++ b/src/each.php @@ -20,12 +20,13 @@ * Calls `$sideEffect` on each element in `$list`. * If $list is omitted, returns a Transducer to be used with `pipe()` instead. * - * @param callable $sideEffect - * @param array|\Traversable|null $list * @see \LazyLists\Transducer\Map - * @return mixed void or \LazyLists\Transducer\Map + * @template InputType + * @param callable(InputType, mixed|null): void $sideEffect + * @param array|\Traversable|null $list + * @return ($list is null ? \LazyLists\Transducer\Each : null) */ -function each(callable $sideEffect, $list = null) +function each(callable $sideEffect, array|\Traversable|null $list = null): null|\LazyLists\Transducer\Each { if (\is_null($list)) { return new \LazyLists\Transducer\Each($sideEffect); @@ -35,4 +36,5 @@ function each(callable $sideEffect, $list = null) foreach ($list as $key => $item) { $sideEffect($item, $key); } + return null; } diff --git a/src/filter.php b/src/filter.php index 016dcac..6f1673a 100644 --- a/src/filter.php +++ b/src/filter.php @@ -21,11 +21,12 @@ * `$predicate` returns truthy. * If $list is omitted, returns a Transducer to be used with `pipe()` instead. * - * @param callable $predicate - * @param array|\Traversable|null $list - * @return mixed array or \LazyLists\Transducer\Filter + * @template InputType + * @param callable(InputType, mixed|null): bool $predicate + * @param array|\Traversable|null $list + * @return ($list is null ? \LazyLists\Transducer\Filter : array) */ -function filter(callable $predicate, $list = null) +function filter(callable $predicate, array|\Traversable|null $list = null): array|\LazyLists\Transducer\Filter { if (\is_null($list)) { return new \LazyLists\Transducer\Filter($predicate); diff --git a/src/flatten.php b/src/flatten.php index bee5f0a..29e052b 100644 --- a/src/flatten.php +++ b/src/flatten.php @@ -20,12 +20,13 @@ * Flattens nested arrays $levels deep. Ignores associative arrays. * If $list is omitted, returns a Transducer to be used with `pipe()` instead. * - * @param integer $levels - * @param array|\Traversable|null $list * @see \LazyLists\Transducer\Flatten - * @return mixed + * @template InputType + * @param integer $levels + * @param array|\Traversable|null $list + * @return ($list is null ? \LazyLists\Transducer\Flatten : array|\Traversable) */ -function flatten(int $levels, $list = null) +function flatten(int $levels, array|\Traversable|null $list = null): array|\LazyLists\Transducer\Flatten|\Traversable { if (\is_null($list)) { return new \LazyLists\Transducer\Flatten($levels); @@ -37,7 +38,10 @@ function flatten(int $levels, $list = null) $flattenItem = static function ($item) use (&$output, $levels) { foreach ($item as $child) { if (\is_array($child) && $levels > 1) { - $output = array_merge($output, flatten($levels - 1, $child)); + $flattenedChildren = flatten($levels - 1, $child); + if (\is_array($flattenedChildren)) { + $output = array_merge($output, $flattenedChildren); + } } else { $output[] = $child; } diff --git a/src/isAssociativeArray.php b/src/isAssociativeArray.php index bccb472..a6b426f 100644 --- a/src/isAssociativeArray.php +++ b/src/isAssociativeArray.php @@ -14,7 +14,7 @@ namespace LazyLists; -function isAssociativeArray($subject): bool +function isAssociativeArray(mixed $subject): bool { if (!\is_array($subject)) { return false; diff --git a/src/iterate.php b/src/iterate.php index 6ab79b4..1679093 100644 --- a/src/iterate.php +++ b/src/iterate.php @@ -27,7 +27,7 @@ * @throws \LazyLists\Exception\InvalidArgumentException * @return callable */ -function iterate(...$transducers) +function iterate(\LazyLists\Transducer\TransducerInterface ...$transducers) { InvalidArgumentException::assertTransducers($transducers, __FUNCTION__); return function ($subject) use ($transducers) { diff --git a/src/map.php b/src/map.php index c0e6dae..146ba01 100644 --- a/src/map.php +++ b/src/map.php @@ -20,12 +20,14 @@ * Applies `$procedure` to each element in `$list` and returns an array of the result. * If $list is omitted, returns a Transducer to be used with `pipe()` instead. * - * @param callable $procedure - * @param array|\Traversable|null $list + * @template InputType + * @template OutputType * @see \LazyLists\Transducer\Map - * @return mixed array or \LazyLists\Transducer\Map + * @param callable(InputType, mixed|null): OutputType $procedure + * @param array|\Traversable|null $list + * @return ($list is null ? \LazyLists\Transducer\Map : array) */ -function map(callable $procedure, $list = null) +function map(callable $procedure, array|\Traversable|null $list = null): array|\LazyLists\Transducer\Map { if (\is_null($list)) { return new \LazyLists\Transducer\Map($procedure); diff --git a/src/pipe.php b/src/pipe.php index a18a2bb..da1d4df 100644 --- a/src/pipe.php +++ b/src/pipe.php @@ -27,7 +27,7 @@ * @throws \LazyLists\Exception\InvalidArgumentException * @return callable */ -function pipe(...$transducers) +function pipe(\LazyLists\Transducer\TransducerInterface ...$transducers) { InvalidArgumentException::assertTransducers($transducers, __FUNCTION__); return function ($subject) use ($transducers) { diff --git a/src/reduce.php b/src/reduce.php index 5664a65..7d50c26 100644 --- a/src/reduce.php +++ b/src/reduce.php @@ -20,13 +20,15 @@ * Accumulates each element of $list into a single value. * If $list is omitted, returns a Transducer to be used with `pipe()` instead. * - * @param callable $accumulator - * @param mixed $initialReduction - * @param array|\Traversable|null $list * @see \LazyLists\Transducer\Reduce - * @return mixed + * @template ReductionType + * @template InputType + * @param callable(ReductionType $reduction, InputType $item, mixed $key): ReductionType $accumulator + * @param ReductionType $initialReduction + * @param array|\Traversable|null $list + * @return ($list is null ? \LazyLists\Transducer\Reduce : ReductionType) */ -function reduce(callable $accumulator, $initialReduction, $list = null) +function reduce(callable $accumulator, mixed $initialReduction, array|\Traversable|null $list = null): mixed { if (\is_null($list)) { return new \LazyLists\Transducer\Reduce($accumulator, $initialReduction); diff --git a/src/take.php b/src/take.php index f86bd9f..7ce551b 100644 --- a/src/take.php +++ b/src/take.php @@ -20,12 +20,13 @@ * Returns an array with the $numberOfItems first elements from $list. * If $list is omitted, returns a Transducer to be used with `pipe()` instead. * - * @param integer $numberOfItems - * @param array|\Traversable|null $list * @see \LazyLists\Transducer\Take - * @return mixed array or \LazyLists\Transducer\Take + * @template InputType + * @param integer $numberOfItems + * @param array|\Traversable|null $list + * @return ($list is null ? \LazyLists\Transducer\Take : array) */ -function take(int $numberOfItems, $list = null) +function take(int $numberOfItems, array|\Traversable|null $list = null): array|\LazyLists\Transducer\Take { if (\is_null($list)) { return new \LazyLists\Transducer\Take($numberOfItems); diff --git a/src/until.php b/src/until.php index 86c6bdd..24dec3f 100644 --- a/src/until.php +++ b/src/until.php @@ -21,11 +21,12 @@ * an element for which $condition returns true (excluding this element). * If $list is omitted, returns a Transducer to be used with `pipe()` instead. * + * @template InputType * @param callable $condition - * @param array|\Traversable|null $list - * @return mixed array or \LazyLists\Transducer\Filter + * @param array|\Traversable|null $list + * @return ($list is null ? \LazyLists\Transducer\Until : array) */ -function until(callable $condition, $list = null) +function until(callable $condition, \Traversable|array|null $list = null): array|\LazyLists\Transducer\Until { if (\is_null($list)) { return new \LazyLists\Transducer\Until($condition); diff --git a/tests/EachTest.php b/tests/EachTest.php index ab2fbd4..3d6645f 100644 --- a/tests/EachTest.php +++ b/tests/EachTest.php @@ -22,7 +22,7 @@ public function test() public function testPassNoCollection() { - $this->expectException(\Exception::class); + $this->expectException(\TypeError::class); each(function () { return; }, 'invalidCollection'); diff --git a/tests/FilterTest.php b/tests/FilterTest.php index 86f1a58..3d0c8fd 100644 --- a/tests/FilterTest.php +++ b/tests/FilterTest.php @@ -22,7 +22,7 @@ public function test() public function testPassNoCollection() { - $this->expectException(\Exception::class); + $this->expectException(\TypeError::class); filter(function () { return ""; }, 'invalidCollection'); diff --git a/tests/MapTest.php b/tests/MapTest.php index 97ca7cf..cf32493 100644 --- a/tests/MapTest.php +++ b/tests/MapTest.php @@ -40,7 +40,7 @@ public function testNullValue() public function testPassNoCollection() { - $this->expectException(\Exception::class); + $this->expectException(\TypeError::class); map(function () { return ""; }, 'invalidCollection'); diff --git a/tests/PipeTest.php b/tests/PipeTest.php index 42aa1b5..c3537e3 100644 --- a/tests/PipeTest.php +++ b/tests/PipeTest.php @@ -138,7 +138,7 @@ public function testDirectoryIterator() public function testInvalidTransducers() { - $this->expectException(InvalidArgumentException::class); + $this->expectException(\TypeError::class); $pipe = pipe("notATransducer"); } diff --git a/tests/ReduceTest.php b/tests/ReduceTest.php index 7366882..922cd40 100644 --- a/tests/ReduceTest.php +++ b/tests/ReduceTest.php @@ -20,7 +20,7 @@ public function test() } public function testPassNoCollection() { - $this->expectException(\Exception::class); + $this->expectException(\TypeError::class); reduce(function () { return ""; }, "", 'invalidCollection'); diff --git a/tests/TakeTest.php b/tests/TakeTest.php index d08f6bb..300f713 100644 --- a/tests/TakeTest.php +++ b/tests/TakeTest.php @@ -21,7 +21,7 @@ public function test() public function testPassNoCollection() { - $this->expectException(\Exception::class); + $this->expectException(\TypeError::class); take(1, "invalidCollection"); } } diff --git a/tests/UntilTest.php b/tests/UntilTest.php index 8162e99..a97ad9d 100644 --- a/tests/UntilTest.php +++ b/tests/UntilTest.php @@ -22,7 +22,7 @@ public function test() public function testPassNoCollection() { - $this->expectException(\Exception::class); + $this->expectException(\TypeError::class); until(function () { return false; }, 'invalidCollection'); From 224614bc8ae508604c00eb82e9832ce4c9df16f6 Mon Sep 17 00:00:00 2001 From: Camille Hodoul Date: Wed, 19 Jul 2023 07:42:43 +0200 Subject: [PATCH 2/8] github action ci workflow --- .github/workflows/ci.yaml | 46 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 .github/workflows/ci.yaml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..4339ec3 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,46 @@ +name: Tests + +on: + push: + branches: [ master ] + tags: + - '!*' # Do not execute on tags + pull_request: + branches: [ master ] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + php-version: [8.1] + + steps: + - uses: actions/checkout@v3 + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '${{ matrix.php-versions }}' + extensions: dom, curl, libxml, mbstring, zip, pcntl, bcmath, intl, iconv + coverage: xdebug + tools: 'cs2pr, php-cs-fixer, phpstan, phpunit' + + - name: Create build dir + run: mkdir -p build/logs + + - name: Install php-coveralls + run: composer require -n --no-update php-coveralls/php-coveralls:^2.0 + + - name: Run composer install + run: composer install -n --prefer-dist --no-progress --no-suggest + + - name: Run tests + id: run-tests + run: composer run test-ci + + - name: Upload coverage to Coveralls + if: steps.run-tests.outcome == 'success' && steps.run-tests.conclusion == 'success' + run: php vendor/bin/php-coveralls -v + continue-on-error: true + env: + COVERALLS_RUN_LOCALLY: 1 \ No newline at end of file From e100bfb6d0a6c6e983336946d5844dd38dca988e Mon Sep 17 00:00:00 2001 From: Camille Hodoul Date: Wed, 19 Jul 2023 07:44:33 +0200 Subject: [PATCH 3/8] remove travis config --- .travis.yml | 30 ------------------------------ 1 file changed, 30 deletions(-) delete mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index c2bcbb0..0000000 --- a/.travis.yml +++ /dev/null @@ -1,30 +0,0 @@ -language: php - -php: - - 7.2 - - 7.3 - - 7.4 - - nightly - -matrix: - fast_finish: true - allow_failures: - - php: nightly - -sudo: false - -before_install: - - travis_retry composer self-update - -install: - - travis_retry composer require --no-update php-coveralls/php-coveralls:^2.0 - - travis_retry composer install --no-interaction --prefer-dist --no-progress --no-suggest - -before_script: - - mkdir -p build/logs - -script: - - composer run test-ci - -after_success: - - travis_retry php vendor/bin/php-coveralls -v From d91edf41486c850829d2261f4c5e69aff3cf1010 Mon Sep 17 00:00:00 2001 From: Camille Hodoul Date: Wed, 19 Jul 2023 07:48:57 +0200 Subject: [PATCH 4/8] fix style issue --- src/Exception/InvalidArgumentException.php | 8 ++++++-- tests/ArrayIteratorSpy.php | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Exception/InvalidArgumentException.php b/src/Exception/InvalidArgumentException.php index 3720e46..baf22cd 100644 --- a/src/Exception/InvalidArgumentException.php +++ b/src/Exception/InvalidArgumentException.php @@ -22,8 +22,12 @@ public static function assertCollection(mixed $collection, string $callee, int $ * @param integer $parameterPosition * @throws InvalidArgumentException */ - private static function assertCollectionAlike(mixed $collection, string $className, string $callee, int $parameterPosition): void - { + private static function assertCollectionAlike( + mixed $collection, + string $className, + string $callee, + int $parameterPosition + ): void { if (!\is_array($collection) && !$collection instanceof $className) { throw new static( \sprintf( diff --git a/tests/ArrayIteratorSpy.php b/tests/ArrayIteratorSpy.php index 2cd6bfa..66ba1d0 100644 --- a/tests/ArrayIteratorSpy.php +++ b/tests/ArrayIteratorSpy.php @@ -12,7 +12,7 @@ class ArrayIteratorSpy extends ArrayIterator { public $howManyNexts = 0; - public function next() + public function next(): void { $this->howManyNexts++; parent::next(); From 69894353a82a7f6b15735cabdcf2b23edafe354f Mon Sep 17 00:00:00 2001 From: Camille Hodoul Date: Wed, 19 Jul 2023 07:53:37 +0200 Subject: [PATCH 5/8] config coveralls --- .github/workflows/ci.yaml | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 4339ec3..39444f2 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -38,9 +38,6 @@ jobs: id: run-tests run: composer run test-ci - - name: Upload coverage to Coveralls + - name: Coveralls if: steps.run-tests.outcome == 'success' && steps.run-tests.conclusion == 'success' - run: php vendor/bin/php-coveralls -v - continue-on-error: true - env: - COVERALLS_RUN_LOCALLY: 1 \ No newline at end of file + uses: coverallsapp/github-action@v2 \ No newline at end of file From 3a7de6905df724bef998d54fee527ec0a33ba68f Mon Sep 17 00:00:00 2001 From: Camille Hodoul Date: Wed, 19 Jul 2023 07:56:51 +0200 Subject: [PATCH 6/8] config coveralls --- .github/workflows/ci.yaml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 39444f2..c83c4de 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -28,9 +28,6 @@ jobs: - name: Create build dir run: mkdir -p build/logs - - name: Install php-coveralls - run: composer require -n --no-update php-coveralls/php-coveralls:^2.0 - - name: Run composer install run: composer install -n --prefer-dist --no-progress --no-suggest @@ -38,6 +35,9 @@ jobs: id: run-tests run: composer run test-ci - - name: Coveralls - if: steps.run-tests.outcome == 'success' && steps.run-tests.conclusion == 'success' - uses: coverallsapp/github-action@v2 \ No newline at end of file + - name: Upload coverage results to Coveralls + env: + COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + composer global require php-coveralls/php-coveralls + php-coveralls --coverage_clover=build/logs/clover.xml -v \ No newline at end of file From 97b441e4076f509fd2254a98a1f71fb01924aa2f Mon Sep 17 00:00:00 2001 From: Camille Hodoul Date: Wed, 19 Jul 2023 08:07:19 +0200 Subject: [PATCH 7/8] Custom InvalidArgumentException isn't needed anymore, because of new type-hints --- src/Exception/InvalidArgumentException.php | 70 ---------------------- src/each.php | 4 -- src/filter.php | 4 -- src/flatten.php | 2 - src/iterate.php | 4 -- src/map.php | 4 -- src/pipe.php | 4 -- src/reduce.php | 4 -- src/take.php | 4 -- src/until.php | 4 -- tests/IterateTest.php | 2 - tests/PipeTest.php | 2 - 12 files changed, 108 deletions(-) delete mode 100644 src/Exception/InvalidArgumentException.php diff --git a/src/Exception/InvalidArgumentException.php b/src/Exception/InvalidArgumentException.php deleted file mode 100644 index baf22cd..0000000 --- a/src/Exception/InvalidArgumentException.php +++ /dev/null @@ -1,70 +0,0 @@ - $candidates - * @param string $callee - */ - public static function assertTransducers(array $candidates, string $callee): void - { - foreach ($candidates as $candidate) { - self::assertTransducer($candidate, $callee); - } - } - public static function assertTransducer(mixed $candidate, string $callee): void - { - if (!$candidate instanceof \LazyLists\Transducer\TransducerInterface) { - throw new static( - \sprintf( - '%s() expects LazyLists\Transducer\TransducerInterface', - $callee - ) - ); - } - } -} diff --git a/src/each.php b/src/each.php index 779afd8..de34af3 100644 --- a/src/each.php +++ b/src/each.php @@ -14,8 +14,6 @@ namespace LazyLists; -use LazyLists\Exception\InvalidArgumentException; - /** * Calls `$sideEffect` on each element in `$list`. * If $list is omitted, returns a Transducer to be used with `pipe()` instead. @@ -31,8 +29,6 @@ function each(callable $sideEffect, array|\Traversable|null $list = null): null| if (\is_null($list)) { return new \LazyLists\Transducer\Each($sideEffect); } - - InvalidArgumentException::assertCollection($list, __FUNCTION__, 2); foreach ($list as $key => $item) { $sideEffect($item, $key); } diff --git a/src/filter.php b/src/filter.php index 6f1673a..3cf9a10 100644 --- a/src/filter.php +++ b/src/filter.php @@ -14,8 +14,6 @@ namespace LazyLists; -use LazyLists\Exception\InvalidArgumentException; - /** * Returns an array containing the elements of `$list` for which * `$predicate` returns truthy. @@ -31,8 +29,6 @@ function filter(callable $predicate, array|\Traversable|null $list = null): arra if (\is_null($list)) { return new \LazyLists\Transducer\Filter($predicate); } - - InvalidArgumentException::assertCollection($list, __FUNCTION__, 2); $output = []; foreach ($list as $key => $item) { if ($predicate($item, $key)) { diff --git a/src/flatten.php b/src/flatten.php index 29e052b..9838a5a 100644 --- a/src/flatten.php +++ b/src/flatten.php @@ -14,8 +14,6 @@ namespace LazyLists; -use LazyLists\Exception\InvalidArgumentException; - /** * Flattens nested arrays $levels deep. Ignores associative arrays. * If $list is omitted, returns a Transducer to be used with `pipe()` instead. diff --git a/src/iterate.php b/src/iterate.php index 1679093..fd7aa9d 100644 --- a/src/iterate.php +++ b/src/iterate.php @@ -14,8 +14,6 @@ namespace LazyLists; -use LazyLists\Exception\InvalidArgumentException; - /** * Combines Transducers to create a single "pipeline" function. * ```php @@ -24,12 +22,10 @@ * ``` * * @param \LazyLists\Transducer\TransducerInterface[] ...$transducers - * @throws \LazyLists\Exception\InvalidArgumentException * @return callable */ function iterate(\LazyLists\Transducer\TransducerInterface ...$transducers) { - InvalidArgumentException::assertTransducers($transducers, __FUNCTION__); return function ($subject) use ($transducers) { return new LazyIterator($subject, $transducers); }; diff --git a/src/map.php b/src/map.php index 146ba01..ccae7b4 100644 --- a/src/map.php +++ b/src/map.php @@ -14,8 +14,6 @@ namespace LazyLists; -use LazyLists\Exception\InvalidArgumentException; - /** * Applies `$procedure` to each element in `$list` and returns an array of the result. * If $list is omitted, returns a Transducer to be used with `pipe()` instead. @@ -32,8 +30,6 @@ function map(callable $procedure, array|\Traversable|null $list = null): array|\ if (\is_null($list)) { return new \LazyLists\Transducer\Map($procedure); } - - InvalidArgumentException::assertCollection($list, __FUNCTION__, 2); $output = []; foreach ($list as $key => $item) { $output[] = $procedure($item, $key); diff --git a/src/pipe.php b/src/pipe.php index da1d4df..e68ca53 100644 --- a/src/pipe.php +++ b/src/pipe.php @@ -14,8 +14,6 @@ namespace LazyLists; -use LazyLists\Exception\InvalidArgumentException; - /** * Combines Transducers to create a single "pipeline" function. * ```php @@ -24,12 +22,10 @@ * ``` * * @param \LazyLists\Transducer\TransducerInterface[] ...$transducers - * @throws \LazyLists\Exception\InvalidArgumentException * @return callable */ function pipe(\LazyLists\Transducer\TransducerInterface ...$transducers) { - InvalidArgumentException::assertTransducers($transducers, __FUNCTION__); return function ($subject) use ($transducers) { $LazyWorker = new LazyWorker($subject, $transducers); return $LazyWorker(); diff --git a/src/reduce.php b/src/reduce.php index 7d50c26..9ce6ec9 100644 --- a/src/reduce.php +++ b/src/reduce.php @@ -14,8 +14,6 @@ namespace LazyLists; -use LazyLists\Exception\InvalidArgumentException; - /** * Accumulates each element of $list into a single value. * If $list is omitted, returns a Transducer to be used with `pipe()` instead. @@ -33,8 +31,6 @@ function reduce(callable $accumulator, mixed $initialReduction, array|\Traversab if (\is_null($list)) { return new \LazyLists\Transducer\Reduce($accumulator, $initialReduction); } - - InvalidArgumentException::assertCollection($list, __FUNCTION__, 3); $reduction = $initialReduction; foreach ($list as $key => $item) { $reduction = $accumulator($reduction, $item, $key); diff --git a/src/take.php b/src/take.php index 7ce551b..db810e7 100644 --- a/src/take.php +++ b/src/take.php @@ -14,8 +14,6 @@ namespace LazyLists; -use LazyLists\Exception\InvalidArgumentException; - /** * Returns an array with the $numberOfItems first elements from $list. * If $list is omitted, returns a Transducer to be used with `pipe()` instead. @@ -31,8 +29,6 @@ function take(int $numberOfItems, array|\Traversable|null $list = null): array|\ if (\is_null($list)) { return new \LazyLists\Transducer\Take($numberOfItems); } - - InvalidArgumentException::assertCollection($list, __FUNCTION__, 2); $output = []; foreach ($list as $item) { if (\count($output) < $numberOfItems) { diff --git a/src/until.php b/src/until.php index 24dec3f..9c0590c 100644 --- a/src/until.php +++ b/src/until.php @@ -14,8 +14,6 @@ namespace LazyLists; -use LazyLists\Exception\InvalidArgumentException; - /** * Returns an array containing the elements of `$list` up to * an element for which $condition returns true (excluding this element). @@ -31,8 +29,6 @@ function until(callable $condition, \Traversable|array|null $list = null): array if (\is_null($list)) { return new \LazyLists\Transducer\Until($condition); } - - InvalidArgumentException::assertCollection($list, __FUNCTION__, 2); $output = []; foreach ($list as $key => $item) { if ($condition($item, $key)) { diff --git a/tests/IterateTest.php b/tests/IterateTest.php index 4f451f3..b3c7ced 100644 --- a/tests/IterateTest.php +++ b/tests/IterateTest.php @@ -4,8 +4,6 @@ namespace LazyLists\Test; -use LazyLists\Exception\InvalidArgumentException; - use function LazyLists\map; use function LazyLists\filter; use function LazyLists\iterate; diff --git a/tests/PipeTest.php b/tests/PipeTest.php index c3537e3..eef64c3 100644 --- a/tests/PipeTest.php +++ b/tests/PipeTest.php @@ -4,8 +4,6 @@ namespace LazyLists\Test; -use LazyLists\Exception\InvalidArgumentException; - use function LazyLists\map; use function LazyLists\filter; use function LazyLists\pipe; From 40ef6db3b0c9297df0f37b486aad80f477173ebe Mon Sep 17 00:00:00 2001 From: Camille Hodoul Date: Wed, 19 Jul 2023 08:15:27 +0200 Subject: [PATCH 8/8] package description --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 0640609..7f88dfe 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "camille\u002Dhdl\/lazy\u002Dlists", - "description": "Lazy list processing helpers", + "description": "Lazy list processing and transducers", "type": "library", "keywords": ["filter", "map", "reduce", "lazy", "iteration", "array", "iterator", "transducers"], "homepage": "https://github.com/camille\u002Dhdl/lazy\u002Dlists",