From 3830e38e8323c56a423e506d429ee9df5312fe38 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Tue, 8 Sep 2020 21:41:41 +0200 Subject: [PATCH] Update annotations. --- composer.json | 2 +- src/Collection.php | 30 +++++++-- src/Operation/Append.php | 4 ++ src/Operation/Apply.php | 2 + src/Operation/Cache.php | 26 ++++---- src/Operation/Chunk.php | 2 +- src/Operation/Collapse.php | 2 +- src/Operation/Combine.php | 2 +- src/Operation/Compact.php | 3 + src/Operation/Compose.php | 3 + src/Operation/Contains.php | 5 ++ src/Operation/Diff.php | 2 + src/Operation/DiffKeys.php | 4 ++ src/Operation/Drop.php | 26 ++++---- src/Operation/Explode.php | 5 ++ src/Operation/Falsy.php | 1 + src/Operation/Filter.php | 3 + src/Operation/First.php | 21 +++++-- src/Operation/Flatten.php | 62 +++++++++--------- src/Operation/Flip.php | 2 +- src/Operation/FoldRight.php | 10 ++- src/Operation/Forget.php | 2 + src/Operation/Frequency.php | 3 + src/Operation/Group.php | 2 + src/Operation/Init.php | 7 ++- src/Operation/Intersect.php | 2 + src/Operation/IntersectKeys.php | 4 +- src/Operation/Intersperse.php | 57 ++++++++++------- src/Operation/Keys.php | 3 + src/Operation/Last.php | 2 +- src/Operation/Limit.php | 24 ++++--- src/Operation/Map.php | 5 +- src/Operation/Nth.php | 35 +++++++---- src/Operation/Pad.php | 59 +++++++++-------- src/Operation/Prepend.php | 4 ++ src/Operation/RSample.php | 2 +- src/Operation/Random.php | 1 - src/Operation/Sort.php | 108 ++++++++++++++++++-------------- src/Operation/Split.php | 2 +- src/Operation/Truthy.php | 1 + src/Operation/Unpack.php | 3 + src/Operation/Until.php | 2 + 42 files changed, 345 insertions(+), 200 deletions(-) diff --git a/composer.json b/composer.json index dfd20135b..785ce164e 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ "psr/cache": "^1.0", "symfony/cache": "^4.4 || ^5.1", "symfony/polyfill-mbstring": "^1.18", - "vimeo/psalm": "^3.14.2" + "vimeo/psalm": "dev-master" }, "suggest": { "symfony/polyfill-mbstring": "Use it if you do not have the PHP mbstring extension." diff --git a/src/Collection.php b/src/Collection.php index a71c94dad..b0a30e527 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -244,13 +244,31 @@ public function associate( ?callable $callbackForKeys = null, ?callable $callbackForValues = null ): CollectionInterface { - $callbackForKeys = $callbackForKeys ?? static function ($key, $value) { - return $key; - }; + $callbackForKeys = $callbackForKeys ?? + /** + * @psalm-param TKey $key + * @psalm-param T $value + * @psalm-return TKey + * + * @param mixed $key + * @param mixed $value + */ + static function ($key, $value) { + return $key; + }; - $callbackForValues = $callbackForValues ?? static function ($key, $value) { - return $value; - }; + $callbackForValues = $callbackForValues ?? + /** + * @psalm-param TKey $key + * @psalm-param T $value + * @psalm-return T + * + * @param mixed $key + * @param mixed $value + */ + static function ($key, $value) { + return $value; + }; return $this->run(Associate::of()($callbackForKeys)($callbackForValues)); } diff --git a/src/Operation/Append.php b/src/Operation/Append.php index e3b7d9ca5..bab6ed01d 100644 --- a/src/Operation/Append.php +++ b/src/Operation/Append.php @@ -23,11 +23,15 @@ public function __invoke(): Closure return /** * @psalm-param T ...$items + * + * @psalm-return Closure(Iterator): Generator */ static function (...$items): Closure { return /** * @psalm-param Iterator $iterator + * + * @psalm-return Generator */ static function (Iterator $iterator) use ($items): Generator { foreach ($iterator as $key => $value) { diff --git a/src/Operation/Apply.php b/src/Operation/Apply.php index 1740eb395..ca9143edd 100644 --- a/src/Operation/Apply.php +++ b/src/Operation/Apply.php @@ -23,6 +23,8 @@ public function __invoke(): Closure return /** * @psalm-param callable(T, TKey):(bool) ...$callbacks + * + * @psalm-return Closure(Iterator): Generator */ static function (callable ...$callbacks): Closure { return diff --git a/src/Operation/Cache.php b/src/Operation/Cache.php index eced017b4..b58bb8b53 100644 --- a/src/Operation/Cache.php +++ b/src/Operation/Cache.php @@ -22,16 +22,20 @@ final class Cache extends AbstractOperation */ public function __invoke(): Closure { - return static function (CacheItemPoolInterface $cache): Closure { - return - /** - * @psalm-param Iterator $iterator - * - * @psalm-return Generator - */ - static function (Iterator $iterator) use ($cache): Generator { - return yield from new CacheIterator($iterator, $cache); - }; - }; + return + /** + * @psalm-return Closure(Iterator): Generator + */ + static function (CacheItemPoolInterface $cache): Closure { + return + /** + * @psalm-param Iterator $iterator + * + * @psalm-return Generator + */ + static function (Iterator $iterator) use ($cache): Generator { + return yield from new CacheIterator($iterator, $cache); + }; + }; } } diff --git a/src/Operation/Chunk.php b/src/Operation/Chunk.php index a1440a401..99290edac 100644 --- a/src/Operation/Chunk.php +++ b/src/Operation/Chunk.php @@ -25,7 +25,7 @@ public function __invoke(): Closure { return /** - * @psalm-param int ...$sizes + * @psalm-return Closure(Iterator): Generator> */ static function (int ...$sizes): Closure { return diff --git a/src/Operation/Collapse.php b/src/Operation/Collapse.php index 4c590c327..270793f9e 100644 --- a/src/Operation/Collapse.php +++ b/src/Operation/Collapse.php @@ -16,7 +16,7 @@ final class Collapse extends AbstractOperation { /** - * @psalm-return Closure(Iterator): Generator + * @psalm-return Closure(Iterator>): Generator */ public function __invoke(): Closure { diff --git a/src/Operation/Combine.php b/src/Operation/Combine.php index 4b20c9c8d..47bd92f79 100644 --- a/src/Operation/Combine.php +++ b/src/Operation/Combine.php @@ -19,7 +19,7 @@ final class Combine extends AbstractOperation { /** - * @return Closure(T...): Closure(Iterator): Generator + * @psalm-return Closure(T...): Closure(Iterator): Generator */ public function __invoke(): Closure { diff --git a/src/Operation/Compact.php b/src/Operation/Compact.php index 44efe933b..e96b5da8b 100644 --- a/src/Operation/Compact.php +++ b/src/Operation/Compact.php @@ -25,11 +25,14 @@ public function __invoke(): Closure return /** * @psalm-param T ...$values + * + * @psalm-return Closure(Iterator): Generator */ static function (...$values): Closure { return /** * @psalm-param Iterator $iterator + * * @psalm-return Generator */ static function (Iterator $iterator) use ($values): Generator { diff --git a/src/Operation/Compose.php b/src/Operation/Compose.php index 943086c10..0e20e6334 100644 --- a/src/Operation/Compose.php +++ b/src/Operation/Compose.php @@ -25,11 +25,14 @@ public function __invoke(): Closure return /** * @psalm-param callable(Iterator): Generator ...$operations + * + * @psalm-return Closure(Iterator): Generator */ static function (callable ...$operations): Closure { return /** * @psalm-param Iterator $iterator + * * @psalm-return Generator */ static function (Iterator $iterator) use ($operations): Generator { diff --git a/src/Operation/Contains.php b/src/Operation/Contains.php index 555b5175a..1c1a859fe 100644 --- a/src/Operation/Contains.php +++ b/src/Operation/Contains.php @@ -15,11 +15,16 @@ */ final class Contains extends AbstractOperation { + /** + * @psalm-return Closure(T...): Closure(Iterator): Generator + */ public function __invoke(): Closure { return /** * @psalm-param T ...$values + * + * @psalm-return Closure(Iterator): Generator */ static function (...$values): Closure { return diff --git a/src/Operation/Diff.php b/src/Operation/Diff.php index e39001225..243f4763b 100644 --- a/src/Operation/Diff.php +++ b/src/Operation/Diff.php @@ -25,6 +25,8 @@ public function __invoke(): Closure return /** * @psalm-param T ...$values + * + * @psalm-return Closure(Iterator): Generator */ static function (...$values): Closure { return diff --git a/src/Operation/DiffKeys.php b/src/Operation/DiffKeys.php index 7b5f75bed..8e725965c 100644 --- a/src/Operation/DiffKeys.php +++ b/src/Operation/DiffKeys.php @@ -25,11 +25,15 @@ public function __invoke(): Closure return /** * @psalm-param T ...$values + * + * @psalm-return Closure(Iterator): Generator */ static function (...$values): Closure { return /** * @psalm-param Iterator $iterator + * + * @psalm-return Generator */ static function (Iterator $iterator) use ($values): Generator { $filterCallbackFactory = static function (array $values): Closure { diff --git a/src/Operation/Drop.php b/src/Operation/Drop.php index 140393b0f..c848f17c5 100644 --- a/src/Operation/Drop.php +++ b/src/Operation/Drop.php @@ -21,16 +21,20 @@ final class Drop extends AbstractOperation */ public function __invoke(): Closure { - return static function (int ...$offsets): Closure { - return - /** - * @psalm-param Iterator $iterator - * - * @psalm-return Generator - */ - static function (Iterator $iterator) use ($offsets): Generator { - return yield from new LimitIterator($iterator, (int) array_sum($offsets)); - }; - }; + return + /** + * @psalm-return Closure(Iterator): Generator + */ + static function (int ...$offsets): Closure { + return + /** + * @psalm-param Iterator $iterator + * + * @psalm-return Generator + */ + static function (Iterator $iterator) use ($offsets): Generator { + return yield from new LimitIterator($iterator, (int) array_sum($offsets)); + }; + }; } } diff --git a/src/Operation/Explode.php b/src/Operation/Explode.php index ad44e09cb..1a2abb7e2 100644 --- a/src/Operation/Explode.php +++ b/src/Operation/Explode.php @@ -15,11 +15,16 @@ */ final class Explode extends AbstractOperation { + /** + * @psalm-return Closure(T...): Closure(Iterator): Generator> + */ public function __invoke(): Closure { return /** * @psalm-param T ...$explodes + * + * @psalm-return Closure(Iterator): Generator> */ static function (...$explodes): Closure { return diff --git a/src/Operation/Falsy.php b/src/Operation/Falsy.php index 832ba6128..a84a69102 100644 --- a/src/Operation/Falsy.php +++ b/src/Operation/Falsy.php @@ -23,6 +23,7 @@ public function __invoke(): Closure return /** * @psalm-param Iterator $iterator + * * @psalm-return Generator $iterator */ static function (Iterator $iterator): Generator { diff --git a/src/Operation/Filter.php b/src/Operation/Filter.php index 96d2b542a..5124d695a 100644 --- a/src/Operation/Filter.php +++ b/src/Operation/Filter.php @@ -26,11 +26,14 @@ public function __invoke(): Closure return /** * @psalm-param callable(T, TKey, Iterator): bool ...$callbacks + * + * @psalm-return Closure(Iterator): Generator */ static function (callable ...$callbacks): Closure { return /** * @psalm-param Iterator $iterator + * * @psalm-return Generator */ static function (Iterator $iterator) use ($callbacks): Generator { diff --git a/src/Operation/First.php b/src/Operation/First.php index 5f6f117ea..12ace49c0 100644 --- a/src/Operation/First.php +++ b/src/Operation/First.php @@ -15,14 +15,23 @@ */ final class First extends AbstractOperation { + /** + * @psalm-return Closure(Iterator): Generator + */ public function __invoke(): Closure { - return static function (Iterator $iterator): Generator { - if (!$iterator->valid()) { - return yield from []; - } + return + /** + * @psalm-param Iterator $iterator + * + * @psalm-return Generator + */ + static function (Iterator $iterator): Generator { + if (!$iterator->valid()) { + return yield from []; + } - return yield $iterator->key() => $iterator->current(); - }; + return yield $iterator->key() => $iterator->current(); + }; } } diff --git a/src/Operation/Flatten.php b/src/Operation/Flatten.php index c7b2028ef..4bd53cc31 100644 --- a/src/Operation/Flatten.php +++ b/src/Operation/Flatten.php @@ -21,37 +21,41 @@ final class Flatten extends AbstractOperation */ public function __invoke(): Closure { - return static function (int $depth): Closure { - return - /** - * @psalm-param Iterator $iterator - * - * @psalm-return Generator - */ - static function (Iterator $iterator) use ($depth): Generator { - foreach ($iterator as $key => $value) { - if (false === is_iterable($value)) { - yield $key => $value; - } elseif (1 === $depth) { - /** @psalm-var TKey $subKey */ - /** @psalm-var T $subValue */ - foreach ($value as $subKey => $subValue) { - yield $subKey => $subValue; - } - } else { - /** @psalm-var callable(Iterator): Generator $flatten */ - $flatten = Flatten::of()($depth - 1); + return + /** + * @psalm-return Closure(Iterator): Generator + */ + static function (int $depth): Closure { + return + /** + * @psalm-param Iterator $iterator + * + * @psalm-return Generator + */ + static function (Iterator $iterator) use ($depth): Generator { + foreach ($iterator as $key => $value) { + if (false === is_iterable($value)) { + yield $key => $value; + } elseif (1 === $depth) { + /** @psalm-var TKey $subKey */ + /** @psalm-var T $subValue */ + foreach ($value as $subKey => $subValue) { + yield $subKey => $subValue; + } + } else { + /** @psalm-var callable(Iterator): Generator $flatten */ + $flatten = Flatten::of()($depth - 1); - /** - * @psalm-var TKey $subKey - * @psalm-var T $subValue - */ - foreach ($flatten(new IterableIterator($value)) as $subKey => $subValue) { - yield $subKey => $subValue; + /** + * @psalm-var TKey $subKey + * @psalm-var T $subValue + */ + foreach ($flatten(new IterableIterator($value)) as $subKey => $subValue) { + yield $subKey => $subValue; + } } } - } - }; - }; + }; + }; } } diff --git a/src/Operation/Flip.php b/src/Operation/Flip.php index 0384d05d3..e87d4ddd7 100644 --- a/src/Operation/Flip.php +++ b/src/Operation/Flip.php @@ -16,7 +16,7 @@ final class Flip extends AbstractOperation { /** - * @return Closure(Iterator): Generator + * @psalm-return Closure(Iterator): Generator */ public function __invoke(): Closure { diff --git a/src/Operation/FoldRight.php b/src/Operation/FoldRight.php index f4b876f7b..4704bef8f 100644 --- a/src/Operation/FoldRight.php +++ b/src/Operation/FoldRight.php @@ -18,26 +18,30 @@ final class FoldRight extends AbstractOperation { /** - * @psalm-return Closure(callable(T|null, T, TKey, Iterator): T): Closure(T|null): Closure(Iterator): T|null + * @psalm-return Closure(callable(T|null, T, TKey, Iterator):(T|null)): Closure(T): Closure(Iterator): Generator */ public function __invoke(): Closure { return /** - * @psalm-param callable(T|null, T, TKey, Iterator): T $callback + * @psalm-param callable(T|null, T, TKey, Iterator):(T|null) $callback + * + * @psalm-return Closure(T): Closure(Iterator): Generator */ static function (callable $callback): Closure { return /** * @param mixed|null $initial * @psalm-param T|null $initial + * + * @psalm-return Closure(Iterator): Generator */ static function ($initial = null) use ($callback): Closure { return /** * @psalm-param Iterator $iterator * - * @psalm-return Generator + * @psalm-return Generator */ static function (Iterator $iterator) use ($callback, $initial): Generator { /** @psalm-var callable(Iterator): Generator $foldRight */ diff --git a/src/Operation/Forget.php b/src/Operation/Forget.php index f444d9344..8f4950391 100644 --- a/src/Operation/Forget.php +++ b/src/Operation/Forget.php @@ -25,6 +25,8 @@ public function __invoke(): Closure return /** * @psalm-param TKey ...$keys + * + * @psalm-return Closure(Iterator): Generator */ static function (...$keys): Closure { return diff --git a/src/Operation/Frequency.php b/src/Operation/Frequency.php index b0b0be849..cf2c93d6f 100644 --- a/src/Operation/Frequency.php +++ b/src/Operation/Frequency.php @@ -15,6 +15,9 @@ */ final class Frequency extends AbstractOperation { + /** + * @psalm-return Closure(Iterator): Generator + */ public function __invoke(): Closure { return diff --git a/src/Operation/Group.php b/src/Operation/Group.php index 2c43fe3e1..457cb8062 100644 --- a/src/Operation/Group.php +++ b/src/Operation/Group.php @@ -23,6 +23,8 @@ public function __invoke(): Closure return /** * @psalm-param null|callable(TKey, T):(TKey|null) $callable + * + * @psalm-return Closure(Iterator): Generator> */ static function (?callable $callable = null): Closure { return diff --git a/src/Operation/Init.php b/src/Operation/Init.php index c26cf4247..5325aa7c7 100644 --- a/src/Operation/Init.php +++ b/src/Operation/Init.php @@ -32,7 +32,12 @@ static function (Iterator $iterator): Generator { $cacheIterator->next(); for (; $iterator->valid(); $cacheIterator->next()) { - yield $cacheIterator->key() => $cacheIterator->current(); + /** @psalm-var TKey $key */ + $key = $cacheIterator->key(); + /** @psalm-var T $current */ + $current = $cacheIterator->current(); + + yield $key => $current; } }; } diff --git a/src/Operation/Intersect.php b/src/Operation/Intersect.php index 6ac4a3a47..0995a94bf 100644 --- a/src/Operation/Intersect.php +++ b/src/Operation/Intersect.php @@ -25,6 +25,8 @@ public function __invoke(): Closure return /** * @psalm-param T ...$values + * + * @psalm-return Closure(Iterator): Generator */ static function (...$values): Closure { return diff --git a/src/Operation/IntersectKeys.php b/src/Operation/IntersectKeys.php index afd3286b1..4b04559f8 100644 --- a/src/Operation/IntersectKeys.php +++ b/src/Operation/IntersectKeys.php @@ -24,7 +24,9 @@ public function __invoke(): Closure { return /** - * @psalm-param TKey ...$values + * @psalm-param TKey ...$keys + * + * @psalm-return Closure(Iterator): Generator */ static function (...$keys): Closure { return diff --git a/src/Operation/Intersperse.php b/src/Operation/Intersperse.php index 4976ae255..25d592b1b 100644 --- a/src/Operation/Intersperse.php +++ b/src/Operation/Intersperse.php @@ -23,42 +23,51 @@ public function __invoke(): Closure { return /** + * @param mixed $element * @psalm-param T $element * - * @param mixed $element + * @psalm-return Closure(int): Closure(int): Closure(Iterator): Generator */ static function ($element): Closure { - return static function (int $atEvery) use ($element): Closure { - return static function (int $startAt) use ($element, $atEvery): Closure { + return + /** + * @psalm-return Closure(int): Closure(Iterator): Generator + */ + static function (int $atEvery) use ($element): Closure { return /** - * @psalm-param Iterator $iterator - * - * @psalm-return Generator + * @psalm-return Closure(Iterator): Generator */ - static function (Iterator $iterator) use ($element, $atEvery, $startAt): Generator { - if (0 > $atEvery) { - throw new InvalidArgumentException( - 'The second parameter must be a positive integer.' - ); - } + static function (int $startAt) use ($element, $atEvery): Closure { + return + /** + * @psalm-param Iterator $iterator + * + * @psalm-return Generator + */ + static function (Iterator $iterator) use ($element, $atEvery, $startAt): Generator { + 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.' - ); - } + if (0 > $startAt) { + throw new InvalidArgumentException( + 'The third parameter must be a positive integer.' + ); + } - foreach ($iterator as $key => $value) { - if (0 === $startAt++ % $atEvery) { - yield $element; - } + foreach ($iterator as $key => $value) { + if (0 === $startAt++ % $atEvery) { + yield $element; + } - yield $key => $value; - } + yield $key => $value; + } + }; }; }; - }; }; } } diff --git a/src/Operation/Keys.php b/src/Operation/Keys.php index 0adb08b57..08c0b769f 100644 --- a/src/Operation/Keys.php +++ b/src/Operation/Keys.php @@ -15,6 +15,9 @@ */ final class Keys extends AbstractOperation { + /** + * @psalm-return Closure(Iterator): Generator + */ public function __invoke(): Closure { return diff --git a/src/Operation/Last.php b/src/Operation/Last.php index 6383dc6bd..45bfb20c2 100644 --- a/src/Operation/Last.php +++ b/src/Operation/Last.php @@ -16,7 +16,7 @@ final class Last extends AbstractOperation { /** - * @return Closure(Iterator): Generator + * @psalm-return Closure(Iterator): Generator */ public function __invoke(): Closure { diff --git a/src/Operation/Limit.php b/src/Operation/Limit.php index ee46b3322..1d592c0c9 100644 --- a/src/Operation/Limit.php +++ b/src/Operation/Limit.php @@ -21,18 +21,26 @@ final class Limit extends AbstractOperation */ public function __invoke(): Closure { - return static function (int $count = -1): Closure { - return static function (int $offset = 0) use ($count): Closure { + return + /** + * @psalm-return Closure(int=): Closure(Iterator): Generator + */ + static function (int $count = -1): Closure { return /** - * @psalm-param Iterator $iterator - * - * @psalm-return Generator + * @psalm-return Closure(Iterator): Generator */ - static function (Iterator $iterator) use ($count, $offset): Generator { - return yield from new LimitIterator($iterator, $offset, $count); + static function (int $offset = 0) use ($count): Closure { + return + /** + * @psalm-param Iterator $iterator + * + * @psalm-return Generator + */ + static function (Iterator $iterator) use ($count, $offset): Generator { + return yield from new LimitIterator($iterator, $offset, $count); + }; }; }; - }; } } diff --git a/src/Operation/Map.php b/src/Operation/Map.php index 9f0d99dd9..b4d95c411 100644 --- a/src/Operation/Map.php +++ b/src/Operation/Map.php @@ -37,11 +37,10 @@ static function (callable ...$callbacks): Closure { static function (Iterator $iterator) use ($callbacks): Generator { $callbackFactory = /** + * @param mixed $key * @psalm-param TKey $key * - * @psalm-return Closure(T): Closure(T, callable(T, TKey): T): T - * - * @param mixed $key + * @psalm-return Closure(T, callable(T, TKey): T): T */ static function ($key): Closure { return diff --git a/src/Operation/Nth.php b/src/Operation/Nth.php index 91faf73f9..23f6028d8 100644 --- a/src/Operation/Nth.php +++ b/src/Operation/Nth.php @@ -20,25 +20,34 @@ final class Nth extends AbstractOperation */ public function __invoke(): Closure { - return static function (int $step): Closure { - return static function (int $offset) use ($step): Closure { + return + /** + * @psalm-return Closure(int): Closure(Iterator): Generator + */ + static function (int $step): Closure { return /** - * @psalm-param Iterator $iterator - * @psalm-return Generator + * @psalm-return Closure(Iterator): Generator */ - static function (Iterator $iterator) use ($step, $offset): Generator { - $position = 0; + static function (int $offset) use ($step): Closure { + return + /** + * @psalm-param Iterator $iterator + * + * @psalm-return Generator + */ + static function (Iterator $iterator) use ($step, $offset): Generator { + $position = 0; - foreach ($iterator as $key => $value) { - if ($position++ % $step !== $offset) { - continue; - } + foreach ($iterator as $key => $value) { + if ($position++ % $step !== $offset) { + continue; + } - yield $key => $value; - } + yield $key => $value; + } + }; }; }; - }; } } diff --git a/src/Operation/Pad.php b/src/Operation/Pad.php index 87bb2947f..3ccd050d8 100644 --- a/src/Operation/Pad.php +++ b/src/Operation/Pad.php @@ -16,38 +16,43 @@ final class Pad extends AbstractOperation { /** - * @return Closure(int): Closure(T): Closure(Iterator): Generator + * @psalm-return Closure(int): Closure(T): Closure(Iterator): Generator */ public function __invoke(): Closure { - return static function (int $size): Closure { - return - /** - * @psalm-param T $padValue - * - * @param mixed $padValue - */ - static function ($padValue) use ($size): Closure { - return - /** - * @psalm-param Iterator $iterator - * - * @psalm-return Generator - */ - static function (Iterator $iterator) use ($size, $padValue): Generator { - $y = 0; + return + /** + * @psalm-return Closure(T): Closure(Iterator): Generator + */ + static function (int $size): Closure { + return + /** + * @param mixed $padValue + * @psalm-param T $padValue + * + * @psalm-return Closure(Iterator): Generator + */ + static function ($padValue) use ($size): Closure { + return + /** + * @psalm-param Iterator $iterator + * + * @psalm-return Generator + */ + static function (Iterator $iterator) use ($size, $padValue): Generator { + $y = 0; - foreach ($iterator as $key => $value) { - ++$y; + foreach ($iterator as $key => $value) { + ++$y; - yield $key => $value; - } + yield $key => $value; + } - while ($y++ < $size) { - yield $padValue; - } - }; - }; - }; + while ($y++ < $size) { + yield $padValue; + } + }; + }; + }; } } diff --git a/src/Operation/Prepend.php b/src/Operation/Prepend.php index 139cfd315..1786e1af5 100644 --- a/src/Operation/Prepend.php +++ b/src/Operation/Prepend.php @@ -23,11 +23,15 @@ public function __invoke(): Closure return /** * @psalm-param T ...$items + * + * @psalm-return Closure(Iterator): Generator */ static function (...$items): Closure { return /** * @psalm-param Iterator $iterator + * + * @psalm-return Generator */ static function (Iterator $iterator) use ($items): Generator { foreach ($items as $key => $item) { diff --git a/src/Operation/RSample.php b/src/Operation/RSample.php index a674dcb05..dcd808909 100644 --- a/src/Operation/RSample.php +++ b/src/Operation/RSample.php @@ -16,7 +16,7 @@ final class RSample extends AbstractOperation { /** - * @return Closure(float): Closure(Iterator): Generator + * @psalm-return Closure(float): Closure(Iterator): Generator */ public function __invoke(): Closure { diff --git a/src/Operation/Random.php b/src/Operation/Random.php index 1d673ecca..1a71aaf07 100644 --- a/src/Operation/Random.php +++ b/src/Operation/Random.php @@ -15,7 +15,6 @@ */ final class Random extends AbstractOperation { - /** * @psalm-return Closure(int): Closure(Iterator): Generator */ diff --git a/src/Operation/Sort.php b/src/Operation/Sort.php index 7097b9607..863ee42ca 100644 --- a/src/Operation/Sort.php +++ b/src/Operation/Sort.php @@ -15,73 +15,85 @@ * @psalm-template TKey * @psalm-template TKey of array-key * @psalm-template T + * + * phpcs:disable Generic.Files.LineLength.TooLong */ final class Sort extends AbstractOperation { + /** + * @psalm-return Closure(int): Closure(callable(T|TKey, T|TKey): int): Closure(Iterator): Generator + */ public function __invoke(): Closure { - return static function (int $type = Operation\Sortable::BY_VALUES): Closure { - return static function (?callable $callback = null) use ($type): Closure { - $callback = $callback ?? - /** - * @param mixed $left - * @psalm-param T|TKey $left - * - * @param mixed $right - * @psalm-param T|TKey $right - */ - static function ($left, $right): int { - return $left <=> $right; - }; - + return + /** + * @psalm-return Closure(callable(T|TKey, T|TKey): int): Closure(Iterator): Generator + */ + static function (int $type = Operation\Sortable::BY_VALUES): Closure { return /** - * @psalm-param Iterator $iterator - * @psalm-param callable(T, T):(int) $callback - * - * @psalm-return Generator + * @psalm-return Closure(Iterator): Generator */ - static function (Iterator $iterator) use ($type, $callback): Generator { - if (Operation\Sortable::BY_VALUES !== $type && Operation\Sortable::BY_KEYS !== $type) { - throw new Exception('Invalid sort type.'); - } - - $operations = Operation\Sortable::BY_VALUES === $type ? - [ - 'before' => [Pack::of()], - 'after' => [Unpack::of()], - ] : - [ - 'before' => [Flip::of(), Pack::of()], - 'after' => [Unpack::of(), Flip::of()], - ]; + static function (?callable $callback = null) use ($type): Closure { + $callback = $callback ?? + /** + * @param mixed $left + * @psalm-param T|TKey $left + * + * @param mixed $right + * @psalm-param T|TKey $right + */ + static function ($left, $right): int { + return $left <=> $right; + }; - $sortCallback = + return /** - * @psalm-param callable(T|TKey, T|TKey): int $callback + * @psalm-param Iterator $iterator * - * @psalm-return Closure(array{0:TKey|T, 1:T|TKey}, array{0:TKey|T, 1:T|TKey}): int + * @psalm-return Generator */ - static function (callable $callback): Closure { - return + static function (Iterator $iterator) use ($type, $callback): Generator { + if (Operation\Sortable::BY_VALUES !== $type && Operation\Sortable::BY_KEYS !== $type) { + throw new Exception('Invalid sort type.'); + } + + $operations = Operation\Sortable::BY_VALUES === $type ? + [ + 'before' => [Pack::of()], + 'after' => [Unpack::of()], + ] : + [ + 'before' => [Flip::of(), Pack::of()], + 'after' => [Unpack::of(), Flip::of()], + ]; + + $sortCallback = /** - * @psalm-param array{0:TKey|T, 1:T|TKey} $left - * @psalm-param array{0:TKey|T, 1:T|TKey} $right + * @psalm-param callable(T|TKey, T|TKey): int $callback + * + * @psalm-return Closure(array{0:TKey|T, 1:T|TKey}, array{0:TKey|T, 1:T|TKey}): int */ - static function (array $left, array $right) use ($callback): int { - return $callback($left[1], $right[1]); + static function (callable $callback): Closure { + return + /** + * @psalm-param array{0:TKey|T, 1:T|TKey} $left + * @psalm-param array{0:TKey|T, 1:T|TKey} $right + */ + static function (array $left, array $right) use ($callback): int { + return $callback($left[1], $right[1]); + }; }; - }; - /** @psalm-var callable(Iterator): Generator | callable(Iterator): Generator $before */ - $before = Compose::of()(...$operations['before']); + /** @psalm-var callable(Iterator): Generator | callable(Iterator): Generator $before */ + $before = Compose::of()(...$operations['before']); - $arrayIterator = new ArrayIterator(iterator_to_array($before($iterator))); - $arrayIterator->uasort($sortCallback($callback)); + $arrayIterator = new ArrayIterator(iterator_to_array($before($iterator))); + $arrayIterator->uasort($sortCallback($callback)); - return yield from Compose::of()(...$operations['after'])($arrayIterator); + return yield from Compose::of()(...$operations['after'])($arrayIterator); + }; }; }; - }; } } diff --git a/src/Operation/Split.php b/src/Operation/Split.php index 4ebd5e59a..a24485a96 100644 --- a/src/Operation/Split.php +++ b/src/Operation/Split.php @@ -16,7 +16,7 @@ final class Split extends AbstractOperation { /** - * @return Closure((callable(T, TKey): bool)...): Closure(Iterator): Generator> + * @psalm-return Closure((callable(T, TKey): bool)...): Closure(Iterator): Generator> */ public function __invoke(): Closure { diff --git a/src/Operation/Truthy.php b/src/Operation/Truthy.php index 65b1757c3..dd6219808 100644 --- a/src/Operation/Truthy.php +++ b/src/Operation/Truthy.php @@ -23,6 +23,7 @@ public function __invoke(): Closure return /** * @psalm-param Iterator $iterator + * * @psalm-return Generator $iterator */ static function (Iterator $iterator): Generator { diff --git a/src/Operation/Unpack.php b/src/Operation/Unpack.php index f8330dbec..f885fdba7 100644 --- a/src/Operation/Unpack.php +++ b/src/Operation/Unpack.php @@ -18,6 +18,9 @@ */ final class Unpack extends AbstractOperation { + /** + * @psalm-return Closure(Iterator): Generator + */ public function __invoke(): Closure { return diff --git a/src/Operation/Until.php b/src/Operation/Until.php index 146c1ed36..6c0c972e1 100644 --- a/src/Operation/Until.php +++ b/src/Operation/Until.php @@ -26,6 +26,8 @@ public function __invoke(): Closure return /** * @psalm-param callable(T, TKey):bool ...$callbacks + * + * @psalm-return Closure(Iterator): Generator */ static function (callable ...$callbacks): Closure { return