diff --git a/composer.json b/composer.json index 71f40d970..5a3895478 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ } ], "require": { - "php": ">= 7.1.3" + "php": ">= 7.4" }, "require-dev": { "drupol/php-conventions": "^1.7.4 || ^1.8.19 || ^2", diff --git a/spec/loophp/collection/CollectionSpec.php b/spec/loophp/collection/CollectionSpec.php index 15177ec5f..6fd7804f8 100644 --- a/spec/loophp/collection/CollectionSpec.php +++ b/spec/loophp/collection/CollectionSpec.php @@ -1077,13 +1077,13 @@ public function it_can_group(): void ->group() ->shouldIterateAs([ 0 => [0 => 'M'], - 1 => [1 => 'i'], - 2 => [2 => 's', 3 => 's'], - 4 => [4 => 'i'], - 5 => [5 => 's', 6 => 's'], - 7 => [7 => 'i'], - 8 => [8 => 'p', 9 => 'p'], - 10 => [10 => 'i'], + 1 => [0 => 'i'], + 2 => [0 => 's', 1 => 's'], + 4 => [0 => 'i'], + 5 => [0 => 's', 1 => 's'], + 7 => [0 => 'i'], + 8 => [0 => 'p', 1 => 'p'], + 10 => [0 => 'i'], ]); } @@ -2227,10 +2227,10 @@ public function it_can_tails(): void ->tails() ->shouldIterateAs([ ['A', 'B', 'C', 'D', 'E'], - [1 => 'B', 2 => 'C', 3 => 'D', 4 => 'E'], - [2 => 'C', 3 => 'D', 4 => 'E'], - [3 => 'D', 4 => 'E'], - [4 => 'E'], + [0 => 'B', 1 => 'C', 2 => 'D', 3 => 'E'], + [0 => 'C', 1 => 'D', 2 => 'E'], + [0 => 'D', 1 => 'E'], + [0 => 'E'], [], ]); } diff --git a/src/Collection.php b/src/Collection.php index 250c646af..40aa46bcb 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -120,21 +120,22 @@ * @psalm-template TKey of array-key * @psalm-template T * + * phpcs:disable Generic.Files.LineLength.TooLong + * * @implements \loophp\collection\Contract\Collection */ final class Collection implements CollectionInterface { /** - * @var mixed[] + * @var array * @psalm-var list */ - private $parameters; + private array $parameters; /** - * @var Closure * @psalm-var callable(...mixed): Generator */ - private $source; + private Closure $source; /** * @param callable|Closure $callable @@ -151,7 +152,7 @@ public function __construct(callable $callable, ...$parameters) public function all(): array { - return iterator_to_array($this); + return iterator_to_array($this->getIterator()); } public function append(...$items): CollectionInterface @@ -181,12 +182,10 @@ public function associate( * * @psalm-return TKey|T */ - static function ($carry, $key, $value) { - return $carry; - }; + static fn ($carry, $key, $value) => $carry; - $callbackForKeys = $callbackForKeys ?? $defaultCallback; - $callbackForValues = $callbackForValues ?? $defaultCallback; + $callbackForKeys ??= $defaultCallback; + $callbackForValues ??= $defaultCallback; return $this->pipe(Associate::of()($callbackForKeys)($callbackForValues)); } @@ -346,7 +345,7 @@ public function frequency(): CollectionInterface return $this->pipe(Frequency::of()); } - public static function fromCallable(callable $callable, ...$parameters): CollectionInterface + public static function fromCallable(callable $callable, ...$parameters): self { return new self( /** @@ -355,25 +354,21 @@ public static function fromCallable(callable $callable, ...$parameters): Collect * * @psalm-return Generator */ - static function (callable $callable, array $parameters): Generator { - return yield from new ClosureIterator($callable, ...$parameters); - }, + static fn (callable $callable, array $parameters): Generator => yield from new ClosureIterator($callable, ...$parameters), $callable, $parameters ); } - public static function fromFile(string $filepath): CollectionInterface + public static function fromFile(string $filepath): self { return new self( - static function (string $filepath): Generator { - return yield from new ResourceIterator(fopen($filepath, 'rb')); - }, + static fn (string $filepath): Generator => yield from new ResourceIterator(fopen($filepath, 'rb')), $filepath ); } - public static function fromIterable(iterable $iterable): CollectionInterface + public static function fromIterable(iterable $iterable): self { return new self( /** @@ -381,14 +376,12 @@ public static function fromIterable(iterable $iterable): CollectionInterface * * @psalm-return Generator */ - static function (iterable $iterable): Generator { - return yield from new IterableIterator($iterable); - }, + static fn (iterable $iterable): Generator => yield from new IterableIterator($iterable), $iterable ); } - public static function fromResource($resource): CollectionInterface + public static function fromResource($resource): self { return new self( /** @@ -397,14 +390,12 @@ public static function fromResource($resource): CollectionInterface * * @psalm-return Generator */ - static function ($resource): Generator { - return yield from new ResourceIterator($resource); - }, + static fn ($resource): Generator => yield from new ResourceIterator($resource), $resource ); } - public static function fromString(string $string, string $delimiter = ''): CollectionInterface + public static function fromString(string $string, string $delimiter = ''): self { return new self( /** @@ -412,9 +403,7 @@ public static function fromString(string $string, string $delimiter = ''): Colle * * @psalm-return Generator */ - static function (string $string, string $delimiter): Generator { - return yield from new StringIterator($string, $delimiter); - }, + static fn (string $string, string $delimiter): Generator => yield from new StringIterator($string, $delimiter), $string, $delimiter ); @@ -452,19 +441,14 @@ public function head(): CollectionInterface public function ifThenElse(callable $condition, callable $then, ?callable $else = null): CollectionInterface { - $else = $else ?? + $else ??= /** * @psalm-param T $value * @psalm-param TKey $key * * @psalm-return T - * - * @param mixed $value - * @param mixed $key */ - static function ($value, $key) { - return $value; - }; + static fn ($value, $key) => $value; return $this->pipe(IfThenElse::of()($condition)($then)($else)); } @@ -577,7 +561,7 @@ public function permutate(): CollectionInterface return $this->pipe(Permutate::of()); } - public function pipe(callable ...$callables): CollectionInterface + public function pipe(callable ...$callables): self { return self::fromCallable( Pipe::of()(...$callables), diff --git a/src/Contract/Collection.php b/src/Contract/Collection.php index 2587d5437..9f45f0218 100644 --- a/src/Contract/Collection.php +++ b/src/Contract/Collection.php @@ -316,10 +316,8 @@ interface Collection extends * @psalm-template NewTKey * @psalm-template NewTKey of array-key * @psalm-template NewT - * - * @psalm-return \loophp\collection\Contract\Collection */ - public static function empty(): Collection; + public static function empty(): self; /** * @psalm-template NewTKey @@ -327,15 +325,10 @@ public static function empty(): Collection; * @psalm-template NewT * * @param mixed ...$parameters - * - * @psalm-return \loophp\collection\Contract\Collection */ - public static function fromCallable(callable $callable, ...$parameters): Collection; + public static function fromCallable(callable $callable, ...$parameters): self; - /** - * @psalm-return \loophp\collection\Contract\Collection - */ - public static function fromFile(string $filepath): Collection; + public static function fromFile(string $filepath): self; /** * @psalm-template NewTKey @@ -344,22 +337,15 @@ public static function fromFile(string $filepath): Collection; * * @param iterable $iterable * @psalm-param iterable $iterable - * - * @psalm-return \loophp\collection\Contract\Collection */ - public static function fromIterable(iterable $iterable): Collection; + public static function fromIterable(iterable $iterable): self; /** * @param resource $resource - * - * @psalm-return \loophp\collection\Contract\Collection */ - public static function fromResource($resource): Collection; + public static function fromResource($resource): self; - /** - * @psalm-return \loophp\collection\Contract\Collection - */ - public static function fromString(string $string, string $delimiter = ''): Collection; + public static function fromString(string $string, string $delimiter = ''): self; /** * @psalm-return \loophp\collection\Iterator\ClosureIterator diff --git a/src/Contract/Operation/Applyable.php b/src/Contract/Operation/Applyable.php index 7aebe6f53..fd6494010 100644 --- a/src/Contract/Operation/Applyable.php +++ b/src/Contract/Operation/Applyable.php @@ -17,7 +17,7 @@ interface Applyable * Execute a callback for each element of the collection. * * @param callable ...$callables - * @psalm-param callable(TKey, T):(bool) ...$callables + * @psalm-param callable(TKey, T):bool ...$callables * * @psalm-return \loophp\collection\Contract\Collection */ diff --git a/src/Contract/Operation/Associateable.php b/src/Contract/Operation/Associateable.php index 16f5e4853..36f07068b 100644 --- a/src/Contract/Operation/Associateable.php +++ b/src/Contract/Operation/Associateable.php @@ -14,8 +14,8 @@ interface Associateable { /** - * @psalm-param null|callable(TKey, T):(TKey) $callbackForKeys - * @psalm-param null|callable(TKey, T):(T) $callbackForValues + * @psalm-param null|callable(TKey, T):TKey $callbackForKeys + * @psalm-param null|callable(TKey, T):T $callbackForValues * * @psalm-return \loophp\collection\Contract\Collection */ diff --git a/src/Contract/Operation/Keyable.php b/src/Contract/Operation/Keyable.php index 3845854f0..4db4fee6c 100644 --- a/src/Contract/Operation/Keyable.php +++ b/src/Contract/Operation/Keyable.php @@ -12,7 +12,7 @@ interface Keyable { /** - * @psalm-return TKey + * @psalm-return T */ public function key(int $index = 0); } diff --git a/src/Contract/Operation/Rangeable.php b/src/Contract/Operation/Rangeable.php index 28a179b9c..c085dfc3c 100644 --- a/src/Contract/Operation/Rangeable.php +++ b/src/Contract/Operation/Rangeable.php @@ -16,8 +16,6 @@ interface Rangeable * @psalm-template TKey * @psalm-template TKey of array-key * @psalm-template T - * - * @psalm-return \loophp\collection\Contract\Collection */ public static function range(float $start = 0.0, float $end = INF, float $step = 1.0): Collection; } diff --git a/src/Contract/Operation/Sinceable.php b/src/Contract/Operation/Sinceable.php index 964a4fc3e..fc21ef687 100644 --- a/src/Contract/Operation/Sinceable.php +++ b/src/Contract/Operation/Sinceable.php @@ -15,7 +15,7 @@ interface Sinceable { /** * @param callable ...$callbacks - * @psalm-param callable(T, TKey):(bool) ...$callbacks + * @psalm-param callable(T, TKey):bool ...$callbacks * * @psalm-return \loophp\collection\Contract\Collection */ diff --git a/src/Contract/Operation/Timesable.php b/src/Contract/Operation/Timesable.php index dec4807b9..b3341bec3 100644 --- a/src/Contract/Operation/Timesable.php +++ b/src/Contract/Operation/Timesable.php @@ -16,8 +16,6 @@ interface Timesable * @psalm-template T * * @psalm-param null|callable(): T $callback - * - * @psalm-return \loophp\collection\Contract\Collection */ public static function times(int $number = 0, ?callable $callback = null): Collection; } diff --git a/src/Contract/Operation/Unfoldable.php b/src/Contract/Operation/Unfoldable.php index 359157db0..0aa30e15a 100644 --- a/src/Contract/Operation/Unfoldable.php +++ b/src/Contract/Operation/Unfoldable.php @@ -15,8 +15,6 @@ interface Unfoldable * * @psalm-param callable(T...): array $callback * @psalm-param T ...$parameters - * - * @psalm-return \loophp\collection\Contract\Collection */ public static function unfold(callable $callback, ...$parameters): Collection; } diff --git a/src/Contract/Operation/Untilable.php b/src/Contract/Operation/Untilable.php index 557c00d74..371607081 100644 --- a/src/Contract/Operation/Untilable.php +++ b/src/Contract/Operation/Untilable.php @@ -15,7 +15,7 @@ interface Untilable { /** * @param callable ...$callbacks - * @psalm-param callable(T, TKey):(bool) ...$callbacks + * @psalm-param callable(T, TKey):bool ...$callbacks * * @psalm-return \loophp\collection\Contract\Collection */ diff --git a/src/Iterator/CacheIterator.php b/src/Iterator/CacheIterator.php index c7df89c2e..fc57fbfbb 100644 --- a/src/Iterator/CacheIterator.php +++ b/src/Iterator/CacheIterator.php @@ -17,15 +17,9 @@ */ final class CacheIterator extends ProxyIterator { - /** - * @var CacheItemPoolInterface - */ - private $cache; + private CacheItemPoolInterface $cache; - /** - * @var int - */ - private $key; + private int $key; /** * @psalm-param Iterator $iterator diff --git a/src/Iterator/ClosureIterator.php b/src/Iterator/ClosureIterator.php index 99c328ff9..b9613574a 100644 --- a/src/Iterator/ClosureIterator.php +++ b/src/Iterator/ClosureIterator.php @@ -19,24 +19,24 @@ final class ClosureIterator extends ProxyIterator * @var array * @psalm-var list */ - private $arguments; + private array $arguments; /** * @var callable - * @psalm-var callable(mixed...):(Generator) + * @psalm-var callable(mixed ...):Generator */ private $callable; /** * @var Closure - * @psalm-var Closure(callable(mixed...):Generator, list):(Generator) + * @psalm-var Closure(callable(mixed ...): Generator, list):Generator */ private $generator; /** * @param mixed ...$arguments * @psalm-param mixed ...$arguments - * @psalm-param callable(mixed...):(Generator) $callable + * @psalm-param callable(mixed ...):Generator $callable */ public function __construct(callable $callable, ...$arguments) { @@ -47,9 +47,7 @@ public function __construct(callable $callable, ...$arguments) * @psalm-param callable(T...):Generator $callable * @psalm-param list $arguments */ - static function (callable $callable, array $arguments): Generator { - return yield from ($callable)(...$arguments); - }; + static fn (callable $callable, array $arguments): Generator => yield from ($callable)(...$arguments); $this->iterator = $this->getGenerator(); } diff --git a/src/Iterator/ProxyIterator.php b/src/Iterator/ProxyIterator.php index e88523751..77f9d08dd 100644 --- a/src/Iterator/ProxyIterator.php +++ b/src/Iterator/ProxyIterator.php @@ -19,7 +19,7 @@ abstract class ProxyIterator implements OuterIterator /** * @psalm-var Generator|Iterator */ - protected $iterator; + protected Iterator $iterator; /** * @return mixed diff --git a/src/Iterator/RandomIterator.php b/src/Iterator/RandomIterator.php index c981e6e56..416753046 100644 --- a/src/Iterator/RandomIterator.php +++ b/src/Iterator/RandomIterator.php @@ -17,26 +17,21 @@ final class RandomIterator extends ProxyIterator { /** - * @var Iterator * @psalm-var Iterator */ - protected $iterator; + protected Iterator $iterator; /** * @var array */ - private $indexes; + private array $indexes; - /** - * @var int - */ - private $key; + private int $key; /** - * @var ArrayIterator * @psalm-var ArrayIterator */ - private $wrappedIterator; + private ArrayIterator $wrappedIterator; /** * @psalm-param Iterator $iterator diff --git a/src/Iterator/ResourceIterator.php b/src/Iterator/ResourceIterator.php index cfa7a65aa..be3abdf35 100644 --- a/src/Iterator/ResourceIterator.php +++ b/src/Iterator/ResourceIterator.php @@ -25,7 +25,7 @@ public function __construct($resource) throw new InvalidArgumentException('Invalid resource type.'); } - $closure = + $this->iterator = new ClosureIterator( /** * @param resource $resource * @@ -35,10 +35,7 @@ static function ($resource): Generator { while (false !== $chunk = fgetc($resource)) { yield $chunk; } - }; - - $this->iterator = new ClosureIterator( - $closure, + }, $resource ); } diff --git a/src/Iterator/StringIterator.php b/src/Iterator/StringIterator.php index 5478d5d60..ead9e00d8 100644 --- a/src/Iterator/StringIterator.php +++ b/src/Iterator/StringIterator.php @@ -5,8 +5,6 @@ namespace loophp\collection\Iterator; use Generator; -use Iterator; -use OuterIterator; /** * @psalm-template TKey @@ -14,9 +12,8 @@ * @psalm-template T of string * * @extends ProxyIterator - * @implements Iterator */ -final class StringIterator extends ProxyIterator implements Iterator, OuterIterator +final class StringIterator extends ProxyIterator { public function __construct(string $data, string $delimiter = '') { diff --git a/src/Operation/Append.php b/src/Operation/Append.php index bab6ed01d..6ca9f5e86 100644 --- a/src/Operation/Append.php +++ b/src/Operation/Append.php @@ -26,22 +26,20 @@ public function __invoke(): Closure * * @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) { - yield $key => $value; - } + static fn (...$items): Closure => + /** + * @psalm-param Iterator $iterator + * + * @psalm-return Generator + */ + static function (Iterator $iterator) use ($items): Generator { + foreach ($iterator as $key => $value) { + yield $key => $value; + } - foreach ($items as $key => $item) { - yield $key => $item; - } - }; - }; + foreach ($items as $key => $item) { + yield $key => $item; + } + }; } } diff --git a/src/Operation/Apply.php b/src/Operation/Apply.php index ca9143edd..72afc2ca9 100644 --- a/src/Operation/Apply.php +++ b/src/Operation/Apply.php @@ -16,7 +16,7 @@ final class Apply extends AbstractOperation { /** - * @psalm-return Closure((callable(T, TKey):(bool))...): Closure(Iterator): Generator + * @psalm-return Closure(callable(T , TKey ): bool ...):Closure (Iterator): Generator */ public function __invoke(): Closure { @@ -26,26 +26,24 @@ public function __invoke(): Closure * * @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 { - foreach ($iterator as $key => $value) { - foreach ($callbacks as $callback) { - if (true === $callback($value, $key)) { - continue; - } - - break; + static fn (callable ...$callbacks): Closure => + /** + * @psalm-param Iterator $iterator + * + * @psalm-return Generator + */ + static function (Iterator $iterator) use ($callbacks): Generator { + foreach ($iterator as $key => $value) { + foreach ($callbacks as $callback) { + if (true === $callback($value, $key)) { + continue; } - yield $key => $value; + break; } - }; - }; + + yield $key => $value; + } + }; } } diff --git a/src/Operation/Associate.php b/src/Operation/Associate.php index b57aae466..3ec2b1892 100644 --- a/src/Operation/Associate.php +++ b/src/Operation/Associate.php @@ -20,7 +20,7 @@ final class Associate extends AbstractOperation { /** - * @psalm-return Closure((callable(T, TKey, T, Iterator): (T|TKey))...): Closure((callable(T, TKey, T, Iterator): (T|TKey))...): Closure(Iterator): Generator + * @psalm-return Closure(callable(T , TKey , T , Iterator ): (T | TKey) ...):Closure ((callable(T, TKey, T, Iterator): (T|TKey))...): Closure(Iterator): Generator */ public function __invoke(): Closure { @@ -30,63 +30,53 @@ public function __invoke(): Closure * * @psalm-return Closure((callable(T, TKey, T, Iterator): (T|TKey))...): Closure(Iterator): Generator */ - static function (callable ...$callbackForKeys): Closure { - return + static fn (callable ...$callbackForKeys): Closure => + /** + * @psalm-param callable(T, TKey, T, Iterator): (T|TKey) ...$callbackForValues + * + * @psalm-return Closure(Iterator): Generator + */ + static fn (callable ...$callbackForValues): Closure => /** - * @psalm-param callable(T, TKey, T, Iterator): (T|TKey) ...$callbackForValues + * @psalm-param Iterator $iterator * - * @psalm-return Closure(Iterator): Generator + * @psalm-return Generator */ - static function (callable ...$callbackForValues) use ($callbackForKeys): Closure { - return + static function (Iterator $iterator) use ($callbackForKeys, $callbackForValues): Generator { + $callbackFactory = /** - * @psalm-param Iterator $iterator + * @param mixed $key + * @psalm-param TKey $key * - * @psalm-return Generator + * @psalm-return Closure(T): Closure(T, callable(T, TKey, T, Iterator): (T|TKey), int, Iterator): (T|TKey) */ - static function (Iterator $iterator) use ($callbackForKeys, $callbackForValues): Generator { - $callbackFactory = + static fn ($key): Closure => + /** + * @param mixed $value + * @psalm-param T $value + * + * @psalm-return Closure(T, callable(T, TKey, T, Iterator): (T|TKey), int, Iterator): (T|TKey) + */ + static fn ($value): Closure => /** - * @param mixed $key - * @psalm-param TKey $key + * @param mixed $initial + * @psalm-param T $initial + * @psalm-param callable(T, TKey, T, Iterator): (T|TKey) $callback + * @psalm-param Iterator $iterator * - * @psalm-return Closure(T): Closure(T, callable(T, TKey, T, Iterator): (T|TKey), int, Iterator): (T|TKey) + * @psalm-return T|TKey */ - static function ($key): Closure { - return - /** - * @param mixed $value - * @psalm-param T $value - * - * @psalm-return Closure(T, callable(T, TKey, T, Iterator): (T|TKey), int, Iterator): (T|TKey) - */ - static function ($value) use ($key): Closure { - return - /** - * @param mixed $initial - * @psalm-param T $initial - * @psalm-param callable(T, TKey, T, Iterator): (T|TKey) $callback - * @psalm-param Iterator $iterator - * - * @psalm-return T|TKey - */ - static function ($initial, callable $callback, int $callbackId, Iterator $iterator) use ($key, $value) { - return $callback($initial, $key, $value, $iterator); - }; - }; - }; + static fn ($initial, callable $callback, int $callbackId, Iterator $iterator) => $callback($initial, $key, $value, $iterator); - foreach ($iterator as $key => $value) { - /** @psalm-var Generator $k */ - $k = FoldLeft::of()($callbackFactory($key)($value))($key)(new ArrayIterator($callbackForKeys)); + foreach ($iterator as $key => $value) { + /** @psalm-var Generator $k */ + $k = FoldLeft::of()($callbackFactory($key)($value))($key)(new ArrayIterator($callbackForKeys)); - /** @psalm-var Generator $c */ - $c = FoldLeft::of()($callbackFactory($key)($value))($value)(new ArrayIterator($callbackForValues)); + /** @psalm-var Generator $c */ + $c = FoldLeft::of()($callbackFactory($key)($value))($value)(new ArrayIterator($callbackForValues)); - yield $k->current() => $c->current(); - } - }; + yield $k->current() => $c->current(); + } }; - }; } } diff --git a/src/Operation/Cache.php b/src/Operation/Cache.php index b58bb8b53..cb0081b17 100644 --- a/src/Operation/Cache.php +++ b/src/Operation/Cache.php @@ -14,6 +14,8 @@ * @psalm-template TKey * @psalm-template TKey of array-key * @psalm-template T + * + * phpcs:disable Generic.Files.LineLength.TooLong */ final class Cache extends AbstractOperation { @@ -26,16 +28,12 @@ public function __invoke(): Closure /** * @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); - }; - }; + static fn (CacheItemPoolInterface $cache): Closure => + /** + * @psalm-param Iterator $iterator + * + * @psalm-return Generator + */ + static fn (Iterator $iterator): Generator => yield from new CacheIterator($iterator, $cache); } } diff --git a/src/Operation/Chunk.php b/src/Operation/Chunk.php index 99290edac..5fdb43b75 100644 --- a/src/Operation/Chunk.php +++ b/src/Operation/Chunk.php @@ -27,39 +27,37 @@ public function __invoke(): Closure /** * @psalm-return Closure(Iterator): Generator> */ - static function (int ...$sizes): Closure { - return - /** - * @psalm-param Iterator $iterator - * - * @psalm-return Generator> - */ - static function (Iterator $iterator) use ($sizes): Generator { - /** @psalm-var Iterator $sizesIterator */ - $sizesIterator = Cycle::of()(new ArrayIterator($sizes)); - - $values = []; - - foreach ($iterator as $value) { - if (0 >= $sizesIterator->current()) { - return yield from []; - } + static fn (int ...$sizes): Closure => + /** + * @psalm-param Iterator $iterator + * + * @psalm-return Generator> + */ + static function (Iterator $iterator) use ($sizes): Generator { + /** @psalm-var Iterator $sizesIterator */ + $sizesIterator = Cycle::of()(new ArrayIterator($sizes)); + + $values = []; + + foreach ($iterator as $value) { + if (0 >= $sizesIterator->current()) { + return yield from []; + } - if (count($values) !== $sizesIterator->current()) { - $values[] = $value; + if (count($values) !== $sizesIterator->current()) { + $values[] = $value; - continue; - } + continue; + } - $sizesIterator->next(); + $sizesIterator->next(); - yield $values; + yield $values; - $values = [$value]; - } + $values = [$value]; + } - return yield $values; - }; - }; + return yield $values; + }; } } diff --git a/src/Operation/Collapse.php b/src/Operation/Collapse.php index 270793f9e..c7367b6a1 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/Column.php b/src/Operation/Column.php index 9567c49b4..24fa926ba 100644 --- a/src/Operation/Column.php +++ b/src/Operation/Column.php @@ -30,24 +30,19 @@ public function __invoke(): Closure static function ($column): Closure { $filterCallbackBuilder = /** - * @psalm-param T $column - * * @param mixed $column + * @psalm-param T $column */ - static function ($column): Closure { - return - /** - * @psalm-param T $value - * @psalm-param TKey $key - * @psalm-param Iterator $iterator - * - * @param mixed $value - * @param mixed $key - */ - static function ($value, $key, Iterator $iterator) use ($column): bool { - return $key === $column; - }; - }; + static fn ($column): Closure => + /** + * @param mixed $value + * @psalm-param T $value + * + * @param mixed $key + * @psalm-param TKey $key + * @psalm-param Iterator $iterator + */ + static fn ($value, $key, Iterator $iterator): bool => $key === $column; /** @psalm-var Closure(Iterator): Generator> $pipe */ $pipe = Pipe::of()( diff --git a/src/Operation/Combinate.php b/src/Operation/Combinate.php index 33e9ae8da..fa665c063 100644 --- a/src/Operation/Combinate.php +++ b/src/Operation/Combinate.php @@ -53,7 +53,7 @@ static function (array $dataset, int $length) use (&$getCombinations): Generator * @psalm-return Generator> */ static function (Iterator $iterator) use ($length, $getCombinations): Generator { - $dataset = iterator_to_array($iterator); + $dataset = [...$iterator]; if (0 < $length) { return yield from $getCombinations($dataset, (int) $length); diff --git a/src/Operation/Combine.php b/src/Operation/Combine.php index 47bd92f79..d94bf4d6d 100644 --- a/src/Operation/Combine.php +++ b/src/Operation/Combine.php @@ -29,27 +29,19 @@ public function __invoke(): Closure * * @psalm-return Closure(Iterator): Generator */ - static function (...$keys): Closure { - return - /** - * @psalm-param Iterator $iterator - * - * @psalm-return Generator - */ - static function (Iterator $iterator) use ($keys): Generator { - $keys = new ArrayIterator($keys); - - while ($iterator->valid() && $keys->valid()) { - yield $keys->current() => $iterator->current(); - - $iterator->next(); - $keys->next(); - } - - if ($iterator->valid() !== $keys->valid()) { - trigger_error('Both keys and values must have the same amount of items.', E_USER_WARNING); - } - }; + static fn (...$keys): Closure => static function (Iterator $iterator) use ($keys): Generator { + $keys = new ArrayIterator($keys); + + while ($iterator->valid() && $keys->valid()) { + yield $keys->current() => $iterator->current(); + + $iterator->next(); + $keys->next(); + } + + if ($iterator->valid() !== $keys->valid()) { + trigger_error('Both keys and values must have the same amount of items.', E_USER_WARNING); + } }; } } diff --git a/src/Operation/Compact.php b/src/Operation/Compact.php index 3e7507d56..f79456c50 100644 --- a/src/Operation/Compact.php +++ b/src/Operation/Compact.php @@ -14,6 +14,8 @@ * @psalm-template TKey * @psalm-template TKey of array-key * @psalm-template T + * + * phpcs:disable Generic.Files.LineLength.TooLong */ final class Compact extends AbstractOperation { @@ -29,14 +31,16 @@ public function __invoke(): Closure * @psalm-return Closure(Iterator): Generator */ static function (...$values): Closure { - $filterCallback = static function (array $values): Closure { - return static function ($value) use ($values): bool { - return !in_array($value, $values, true); - }; - }; + $filterCallback = static fn (array $values): Closure => static fn ($value): bool => !in_array($value, $values, true); /** @psalm-var Closure(Iterator):Generator $filter */ - $filter = Filter::of()($filterCallback([] === $values ? [null, [], 0, false, ''] : $values)); + $filter = Filter::of()( + $filterCallback( + [] === $values ? + [null, [], 0, false, ''] : + $values + ) + ); // Point free style. return $filter; diff --git a/src/Operation/Contains.php b/src/Operation/Contains.php index 1c1a859fe..0d7e9b11e 100644 --- a/src/Operation/Contains.php +++ b/src/Operation/Contains.php @@ -26,28 +26,26 @@ public function __invoke(): Closure * * @psalm-return Closure(Iterator): Generator */ - static function (...$values): Closure { - return - /** - * @psalm-param Iterator $iterator - * - * @psalm-return Generator - */ - static function (Iterator $iterator) use ($values): Generator { - foreach ($iterator as $value) { - foreach ($values as $k => $v) { - if ($v === $value) { - unset($values[$k]); - } + static fn (...$values): Closure => + /** + * @psalm-param Iterator $iterator + * + * @psalm-return Generator + */ + static function (Iterator $iterator) use ($values): Generator { + foreach ($iterator as $value) { + foreach ($values as $k => $v) { + if ($v === $value) { + unset($values[$k]); + } - if ([] === $values) { - return yield true; - } + if ([] === $values) { + return yield true; } } + } - return yield false; - }; - }; + return yield false; + }; } } diff --git a/src/Operation/Cycle.php b/src/Operation/Cycle.php index 1e6445716..1a90ad5fa 100644 --- a/src/Operation/Cycle.php +++ b/src/Operation/Cycle.php @@ -27,8 +27,6 @@ public function __invoke(): Closure * * @psalm-return Generator */ - static function (Iterator $iterator): Generator { - return yield from new InfiniteIterator($iterator); - }; + static fn (Iterator $iterator): Generator => yield from new InfiniteIterator($iterator); } } diff --git a/src/Operation/Diff.php b/src/Operation/Diff.php index e91d53929..ce4ef512a 100644 --- a/src/Operation/Diff.php +++ b/src/Operation/Diff.php @@ -29,20 +29,16 @@ public function __invoke(): Closure * @psalm-return Closure(Iterator): Generator */ static function (...$values): Closure { - $filterCallbackFactory = static function (array $values): Closure { - return - /** - * @psalm-param T $value - * @psalm-param TKey $key - * @psalm-param Iterator $iterator - * - * @param mixed $value - * @param mixed $key - */ - static function ($value, $key, Iterator $iterator) use ($values): bool { - return false === in_array($value, $values, true); - }; - }; + $filterCallbackFactory = static fn (array $values): Closure => + /** + * @param mixed $value + * @psalm-param T $value + * + * @param mixed $key + * @psalm-param TKey $key + * @psalm-param Iterator $iterator + */ + static fn ($value, $key, Iterator $iterator): bool => false === in_array($value, $values, true); /** @psalm-var Closure(Iterator): Generator $filter */ $filter = Filter::of()($filterCallbackFactory($values)); diff --git a/src/Operation/DiffKeys.php b/src/Operation/DiffKeys.php index 9e9d5e5f3..7e2e25074 100644 --- a/src/Operation/DiffKeys.php +++ b/src/Operation/DiffKeys.php @@ -29,20 +29,16 @@ public function __invoke(): Closure * @psalm-return Closure(Iterator): Generator */ static function (...$values): Closure { - $filterCallbackFactory = static function (array $values): Closure { - return - /** - * @psalm-param T $value - * @psalm-param TKey $key - * @psalm-param Iterator $iterator - * - * @param mixed $value - * @param mixed $key - */ - static function ($value, $key, Iterator $iterator) use ($values): bool { - return false === in_array($key, $values, true); - }; - }; + $filterCallbackFactory = static fn (array $values): Closure => + /** + * @psalm-param T $value + * @psalm-param TKey $key + * @psalm-param Iterator $iterator + * + * @param mixed $value + * @param mixed $key + */ + static fn ($value, $key, Iterator $iterator): bool => false === in_array($key, $values, true); /** @psalm-var Closure(Iterator): Generator $filter */ $filter = Filter::of()($filterCallbackFactory($values)); diff --git a/src/Operation/Drop.php b/src/Operation/Drop.php index bada42ef6..7af3cc8b1 100644 --- a/src/Operation/Drop.php +++ b/src/Operation/Drop.php @@ -25,20 +25,12 @@ public function __invoke(): Closure /** * @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 { - if (!$iterator->valid()) { - return yield from []; - } + static fn (int ...$offsets): Closure => static function (Iterator $iterator) use ($offsets): Generator { + if (!$iterator->valid()) { + return yield from []; + } - return yield from new LimitIterator($iterator, (int) array_sum($offsets)); - }; + return yield from new LimitIterator($iterator, (int) array_sum($offsets)); }; } } diff --git a/src/Operation/DropWhile.php b/src/Operation/DropWhile.php index 9b726bee9..d89537887 100644 --- a/src/Operation/DropWhile.php +++ b/src/Operation/DropWhile.php @@ -18,7 +18,7 @@ final class DropWhile extends AbstractOperation { /** - * @psalm-return Closure((callable(T, TKey, Iterator): bool)): Closure(Iterator): Generator + * @psalm-return Closure(callable(T , TKey , Iterator ): bool):Closure (Iterator): Generator */ public function __invoke(): Closure { @@ -28,36 +28,26 @@ public function __invoke(): Closure * * @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 { - for (; $iterator->valid(); $iterator->next()) { - $reduced = array_reduce( - $callbacks, - static function (bool $carry, callable $callback) use ($iterator): bool { - return ($callback($iterator->current(), $iterator->key(), $iterator)) ? - $carry : - false; - }, - true - ); + static fn (callable ...$callbacks): Closure => static function (Iterator $iterator) use ($callbacks): Generator { + for (; $iterator->valid(); $iterator->next()) { + $reduced = array_reduce( + $callbacks, + static fn (bool $carry, callable $callback): bool => ($callback($iterator->current(), $iterator->key(), $iterator)) ? + $carry : + false, + true + ); - if (true === $reduced) { - continue; - } + if (true === $reduced) { + continue; + } - break; - } + break; + } - for (; $iterator->valid(); $iterator->next()) { - yield $iterator->key() => $iterator->current(); - } - }; + for (; $iterator->valid(); $iterator->next()) { + yield $iterator->key() => $iterator->current(); + } }; } } diff --git a/src/Operation/Every.php b/src/Operation/Every.php index 682169bcf..9decd3d1a 100644 --- a/src/Operation/Every.php +++ b/src/Operation/Every.php @@ -18,7 +18,7 @@ final class Every extends AbstractOperation { /** - * @psalm-return Closure((callable(T, TKey, Iterator): bool)): Closure(Iterator): Generator + * @psalm-return Closure(callable(T , TKey , Iterator ): bool):Closure (Iterator): Generator */ public function __invoke(): Closure { @@ -31,23 +31,20 @@ static function (callable $callback): Closure { /** * @psalm-param callable(T, TKey, Iterator): bool $callback */ - static function (callable $callback): Closure { - return - /** - * @param mixed $carry - * @psalm-param T $carry - * - * @param mixed $value - * @psalm-param T $value - * - * @param mixed $key - * @psalm-param TKey $key - * @psalm-param Iterator $iterator - */ - static function ($carry, $value, $key, Iterator $iterator) use ($callback): bool { - return $callback($value, $key, $iterator); - }; - }; + static fn (callable $callback): Closure => + /** + * @param mixed $carry + * @psalm-param T $carry + * + * @param mixed $value + * @psalm-param T $value + * + * @param mixed $key + * @psalm-param TKey $key + * + * @psalm-param Iterator $iterator + */ + static fn ($carry, $value, $key, Iterator $iterator): bool => $callback($value, $key, $iterator); /** @psalm-var Closure(Iterator): Generator $foldLeft */ $foldLeft = FoldLeft::of()($callbackBuilder($callback))(true); diff --git a/src/Operation/Explode.php b/src/Operation/Explode.php index 6838b420b..48a6f2264 100644 --- a/src/Operation/Explode.php +++ b/src/Operation/Explode.php @@ -35,16 +35,12 @@ static function (...$explodes): Closure { * @param mixed $explode * @psalm-param T $explode */ - static function ($explode): Closure { - return - /** - * @param mixed $value - * @psalm-param T $value - */ - static function ($value) use ($explode): bool { - return $value === $explode; - }; - }, + static fn ($explode): Closure => + /** + * @param mixed $value + * @psalm-param T $value + */ + static fn ($value): bool => $value === $explode, $explodes ) ); diff --git a/src/Operation/Falsy.php b/src/Operation/Falsy.php index 4f8d494a2..09b95b5b5 100644 --- a/src/Operation/Falsy.php +++ b/src/Operation/Falsy.php @@ -25,18 +25,14 @@ public function __invoke(): Closure * @param mixed $value * @psalm-param T $value */ - static function ($value): bool { - return !(bool) $value; - }; + static fn ($value): bool => !(bool) $value; $dropWhileCallback = /** * @param mixed $value * @psalm-param T $value */ - static function ($value): bool { - return true === $value; - }; + static fn ($value): bool => true === $value; /** @psalm-var Closure(Iterator): Generator $pipe */ $pipe = Pipe::of()( diff --git a/src/Operation/Filter.php b/src/Operation/Filter.php index 5124d695a..8fcf9b21b 100644 --- a/src/Operation/Filter.php +++ b/src/Operation/Filter.php @@ -19,7 +19,7 @@ final class Filter extends AbstractOperation { /** - * @psalm-return Closure((callable(T, TKey, Iterator): bool)...): Closure(Iterator): Generator + * @psalm-return Closure(callable(T , TKey , Iterator ): bool ...):Closure (Iterator): Generator */ public function __invoke(): Closure { @@ -29,38 +29,28 @@ public function __invoke(): Closure * * @psalm-return Closure(Iterator): Generator */ - static function (callable ...$callbacks): Closure { - return + static fn (callable ...$callbacks): Closure => static function (Iterator $iterator) use ($callbacks): Generator { + $defaultCallback = /** - * @psalm-param Iterator $iterator + * @param mixed $value + * @psalm-param T $value + * + * @param mixed $key + * @psalm-param TKey $key * - * @psalm-return Generator + * @psalm-param Iterator $iterator */ - static function (Iterator $iterator) use ($callbacks): Generator { - $defaultCallback = - /** - * @param mixed $value - * @param mixed $key - * @psalm-param T $value - * @psalm-param TKey $key - * @psalm-param Iterator $iterator - */ - static function ($value, $key, Iterator $iterator): bool { - return (bool) $value; - }; + static fn ($value, $key, Iterator $iterator): bool => (bool) $value; - $callbacks = [] === $callbacks ? - [$defaultCallback] : - $callbacks; + $callbacks = [] === $callbacks ? + [$defaultCallback] : + $callbacks; - return yield from array_reduce( - $callbacks, - static function (Iterator $carry, callable $callback): CallbackFilterIterator { - return new CallbackFilterIterator($carry, $callback); - }, - $iterator - ); - }; + return yield from array_reduce( + $callbacks, + static fn (Iterator $carry, callable $callback): CallbackFilterIterator => new CallbackFilterIterator($carry, $callback), + $iterator + ); }; } } diff --git a/src/Operation/Flatten.php b/src/Operation/Flatten.php index 10db0a799..36991c272 100644 --- a/src/Operation/Flatten.php +++ b/src/Operation/Flatten.php @@ -25,37 +25,33 @@ public function __invoke(): Closure /** * @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; - - continue; - } - - if (1 !== $depth) { - /** @psalm-var callable(Iterator): Generator $flatten */ - $flatten = Flatten::of()($depth - 1); - - $value = $flatten(new IterableIterator($value)); - } - - /** - * @psalm-var TKey $subKey - * @psalm-var T $subValue - */ - foreach ($value as $subKey => $subValue) { - yield $subKey => $subValue; - } + static fn (int $depth): Closure => + /** + * @psalm-param Iterator $iterator + */ + static function (Iterator $iterator) use ($depth): Generator { + foreach ($iterator as $key => $value) { + if (false === is_iterable($value)) { + yield $key => $value; + + continue; } - }; - }; + + if (1 !== $depth) { + /** @psalm-var callable(Iterator): Generator $flatten */ + $flatten = Flatten::of()($depth - 1); + + $value = $flatten(new IterableIterator($value)); + } + + /** + * @psalm-var TKey $subKey + * @psalm-var T $subValue + */ + foreach ($value as $subKey => $subValue) { + yield $subKey => $subValue; + } + } + }; } } diff --git a/src/Operation/FoldLeft.php b/src/Operation/FoldLeft.php index f802fc585..04de986ee 100644 --- a/src/Operation/FoldLeft.php +++ b/src/Operation/FoldLeft.php @@ -18,7 +18,7 @@ final class FoldLeft extends AbstractOperation { /** - * @psalm-return Closure(callable(T|null, T, TKey, Iterator):(T|null)): Closure(T): Closure(Iterator): Generator + * @psalm-return Closure(callable((T | null) , T , TKey , Iterator ): (T | null)):Closure (T): Closure(Iterator): Generator */ public function __invoke(): Closure { @@ -28,24 +28,15 @@ public function __invoke(): Closure * * @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 { - /** @psalm-var Closure(Iterator): Generator $pipe */ - $pipe = Pipe::of()( - ScanLeft::of()($callback)($initial), - Last::of() - ); + static fn (callable $callback): Closure => static function ($initial = null) use ($callback): Closure { + /** @psalm-var Closure(Iterator): Generator $pipe */ + $pipe = Pipe::of()( + ScanLeft::of()($callback)($initial), + Last::of() + ); - // Point free style. - return $pipe; - }; + // Point free style. + return $pipe; }; } } diff --git a/src/Operation/FoldLeft1.php b/src/Operation/FoldLeft1.php index 587dd3540..08fd17309 100644 --- a/src/Operation/FoldLeft1.php +++ b/src/Operation/FoldLeft1.php @@ -18,7 +18,7 @@ final class FoldLeft1 extends AbstractOperation { /** - * @psalm-return Closure(callable(T|null, T, TKey, Iterator):(T|null)): Closure(Iterator): Generator + * @psalm-return Closure(callable((T | null) , T , TKey , Iterator ): (T | null)):Closure (Iterator): Generator */ public function __invoke(): Closure { @@ -29,7 +29,7 @@ public function __invoke(): Closure * @psalm-return Closure(Iterator): Generator */ static function (callable $callback): Closure { - /** @psalm-var Closure(Iterator): Generator $pipe */ + /** @psalm-var Closure(Iterator):(Generator) $pipe */ $pipe = Pipe::of()( ScanLeft1::of()($callback), Last::of() diff --git a/src/Operation/FoldRight.php b/src/Operation/FoldRight.php index 7b4138512..a3a80de3d 100644 --- a/src/Operation/FoldRight.php +++ b/src/Operation/FoldRight.php @@ -18,7 +18,7 @@ final class FoldRight extends AbstractOperation { /** - * @psalm-return Closure(callable(T|null, T, TKey, Iterator):(T|null)): Closure(T): Closure(Iterator): Generator + * @psalm-return Closure(callable((T | null) , T , TKey , Iterator ): (T | null)):Closure (T): Closure(Iterator): Generator */ public function __invoke(): Closure { @@ -28,24 +28,15 @@ public function __invoke(): Closure * * @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 { - /** @psalm-var Closure(Iterator): Generator $pipe */ - $pipe = Pipe::of()( - ScanRight::of()($callback)($initial), - Head::of() - ); + static fn (callable $callback): Closure => static function ($initial = null) use ($callback): Closure { + /** @psalm-var Closure(Iterator): Generator $pipe */ + $pipe = Pipe::of()( + ScanRight::of()($callback)($initial), + Head::of() + ); - // Point free style. - return $pipe; - }; + // Point free style. + return $pipe; }; } } diff --git a/src/Operation/FoldRight1.php b/src/Operation/FoldRight1.php index 5cb908373..5cfa8e2be 100644 --- a/src/Operation/FoldRight1.php +++ b/src/Operation/FoldRight1.php @@ -18,7 +18,7 @@ final class FoldRight1 extends AbstractOperation { /** - * @psalm-return Closure(callable(T|null, T, TKey, Iterator):(T|null)): Closure(Iterator): Generator + * @psalm-return Closure(callable((T | null) , T , TKey , Iterator ): (T | null)):Closure (Iterator): Generator */ public function __invoke(): Closure { @@ -29,7 +29,7 @@ public function __invoke(): Closure * @psalm-return Closure(Iterator): Generator */ static function (callable $callback): Closure { - /** @psalm-var Closure(Iterator): Generator $pipe */ + /** @psalm-var Closure(Iterator):(Generator) $pipe */ $pipe = Pipe::of()( ScanRight1::of()($callback), Head::of() diff --git a/src/Operation/Forget.php b/src/Operation/Forget.php index cf159cd3a..9dfce676d 100644 --- a/src/Operation/Forget.php +++ b/src/Operation/Forget.php @@ -29,20 +29,17 @@ public function __invoke(): Closure * @psalm-return Closure(Iterator): Generator */ static function (...$keys): Closure { - $filterCallbackFactory = static function (array $keys): Closure { - return - /** - * @psalm-param T $value - * @psalm-param TKey $key - * @psalm-param Iterator $iterator - * - * @param mixed $value - * @param mixed $key - */ - static function ($value, $key, Iterator $iterator) use ($keys): bool { - return false === in_array($key, $keys, true); - }; - }; + $filterCallbackFactory = static fn (array $keys): Closure => + /** + * @param mixed $value + * @psalm-param T $value + * + * @param mixed $key + * @psalm-param TKey $key + * + * @psalm-param Iterator $iterator + */ + static fn ($value, $key, Iterator $iterator): bool => false === in_array($key, $keys, true); /** @psalm-var Closure(Iterator): Generator $filter */ $filter = Filter::of()($filterCallbackFactory($keys)); diff --git a/src/Operation/Get.php b/src/Operation/Get.php index a7c0d5693..341de8cc9 100644 --- a/src/Operation/Get.php +++ b/src/Operation/Get.php @@ -16,7 +16,7 @@ final class Get extends AbstractOperation { /** - * @psalm-return Closure(T|TKey): Closure(T): Closure(Iterator): Generator + * @psalm-return Closure((T | TKey)):Closure (T): Closure(Iterator): Generator */ public function __invoke(): Closure { @@ -27,37 +27,26 @@ public function __invoke(): Closure * * @psalm-return Closure(T): Closure(Iterator): Generator */ - static function ($keyToGet): Closure { - return + static fn ($keyToGet): Closure => static function ($default) use ($keyToGet): Closure { + $filterCallback = /** - * @param mixed $default - * @psalm-param T $default + * @param mixed $value + * @psalm-param T $value * - * @psalm-return Closure(Iterator): Generator + * @param mixed $key + * @psalm-param TKey $key */ - static function ($default) use ($keyToGet): Closure { - $filterCallback = - /** - * @param mixed $value - * @psalm-param T $value - * - * @param mixed $key - * @psalm-param TKey $key - */ - static function ($value, $key) use ($keyToGet): bool { - return $key === $keyToGet; - }; + static fn ($value, $key): bool => $key === $keyToGet; - /** @psalm-var Closure(Iterator): Generator $pipe */ - $pipe = Pipe::of()( - Filter::of()($filterCallback), - Append::of()($default), - Head::of() - ); + /** @psalm-var Closure(Iterator):(Generator) $pipe */ + $pipe = Pipe::of()( + Filter::of()($filterCallback), + Append::of()($default), + Head::of() + ); - // Point free style. - return $pipe; - }; + // Point free style. + return $pipe; }; } } diff --git a/src/Operation/Group.php b/src/Operation/Group.php index a43a5377b..397c9fbb8 100644 --- a/src/Operation/Group.php +++ b/src/Operation/Group.php @@ -31,23 +31,17 @@ public function __invoke(): Closure static function (Iterator $iterator): Generator { $spanCallback = /** + * @param mixed $current * @psalm-param T $current * * @psalm-return Closure(T): bool - * - * @param mixed $current */ - static function ($current): Closure { - return - /** - * @psalm-param T $value - * - * @param mixed $value - */ - static function ($value) use ($current): bool { - return $value === $current; - }; - }; + static fn ($current): Closure => + /** + * @param mixed $value + * @psalm-param T $value + */ + static fn ($value): bool => $value === $current; for (; $iterator->valid(); $span->next(), $iterator = $span->current()) { $key = $iterator->key(); @@ -56,7 +50,7 @@ static function ($value) use ($current): bool { /** @psalm-var Iterator> $span */ $span = Span::of()($spanCallback($current))($iterator); - yield $key => iterator_to_array($span->current()); + yield $key => [...$span->current()]; } }; } diff --git a/src/Operation/GroupBy.php b/src/Operation/GroupBy.php index 43f0a37bd..424e8f492 100644 --- a/src/Operation/GroupBy.php +++ b/src/Operation/GroupBy.php @@ -12,11 +12,13 @@ * @psalm-template TKey * @psalm-template TKey of array-key * @psalm-template T + * + * phpcs:disable Generic.Files.LineLength.TooLong */ final class GroupBy extends AbstractOperation { /** - * @psalm-return Closure(null|callable(TKey, T):(TKey|null)): Closure(Iterator): Generator> + * @psalm-return Closure((null | callable(TKey , T ): (TKey | null))):Closure (Iterator): Generator> */ public function __invoke(): Closure { @@ -39,9 +41,7 @@ static function (?callable $callable = null): Closure { * @return mixed * @psalm-return TKey */ - static function ($value, $key) { - return $key; - }; + static fn ($value, $key) => $key; $reducerFactory = /** @@ -49,29 +49,27 @@ static function ($value, $key) { * * @psalm-return Closure(array>, T, TKey): array> */ - static function (callable $callback): Closure { - return - /** - * @psalm-param array> $collect - * - * @param mixed $value - * @psalm-param T $value - * - * @param mixed $key - * @psalm-param TKey $key - * - * @psalm-return non-empty-array> - */ - static function (array $collect, $value, $key) use ($callback): array { - if (null !== $groupKey = $callback($value, $key)) { - $collect[$groupKey][] = $value; - } else { - $collect[$key] = $value; - } + static fn (callable $callback): Closure => + /** + * @psalm-param array> $collect + * + * @param mixed $value + * @psalm-param T $value + * + * @param mixed $key + * @psalm-param TKey $key + * + * @psalm-return non-empty-array> + */ + static function (array $collect, $value, $key) use ($callback): array { + if (null !== $groupKey = $callback($value, $key)) { + $collect[$groupKey][] = $value; + } else { + $collect[$key] = $value; + } - return $collect; - }; - }; + return $collect; + }; /** @psalm-var Closure(Iterator): Generator> $pipe */ $pipe = Pipe::of()( diff --git a/src/Operation/Has.php b/src/Operation/Has.php index 478f2ce9a..93d21e7f8 100644 --- a/src/Operation/Has.php +++ b/src/Operation/Has.php @@ -35,18 +35,14 @@ static function (callable $callback): Closure { * @param mixed $key * @psalm-param TKey $key */ - static function ($value, $key) use ($callback): bool { - return $callback($key, $value) === $value; - }; + static fn ($value, $key): bool => $callback($key, $value) === $value; $dropWhileCallback = /** * @param mixed $value * @psalm-param T $value */ - static function ($value): bool { - return false === $value; - }; + static fn ($value): bool => false === $value; /** @psalm-var Closure(Iterator): Generator $pipe */ $pipe = Pipe::of()( diff --git a/src/Operation/IfThenElse.php b/src/Operation/IfThenElse.php index fbd4b86f8..1587ab05e 100644 --- a/src/Operation/IfThenElse.php +++ b/src/Operation/IfThenElse.php @@ -28,43 +28,35 @@ public function __invoke(): Closure * * @psalm-return Closure(callable(T, TKey): (T)): Closure(callable(T, TKey): (T)): Closure(Iterator): Generator */ - static function (callable $condition): Closure { - return + static fn (callable $condition): Closure => + /** + * @psalm-param callable(T, TKey):T $then + * + * @psalm-return Closure(callable(T, TKey): (T)): Closure(Iterator): Generator + */ + static fn (callable $then): Closure => /** - * @psalm-param callable(T, TKey):T $then + * @psalm-param callable(T, TKey):T $else * - * @psalm-return Closure(callable(T, TKey): (T)): Closure(Iterator): Generator + * @psalm-return Closure(Iterator): Generator */ - static function (callable $then) use ($condition): Closure { - return + static function (callable $else) use ($condition, $then): Closure { + /** @psalm-var Closure(Iterator): Generator $map */ + $map = Map::of()( /** - * @psalm-param callable(T, TKey):T $else + * @param mixed $value + * @psalm-param T $value * - * @psalm-return Closure(Iterator): Generator + * @param mixed $key + * @psalm-param TKey $key + * + * @psalm-return T */ - static function (callable $else) use ($condition, $then): Closure { - /** @psalm-var Closure(Iterator): Generator $map */ - $map = Map::of()( - /** - * @param mixed $value - * @psalm-param T $value - * - * @param mixed $key - * @psalm-param TKey $key - * - * @psalm-return T - */ - static function ($value, $key) use ($condition, $then, $else) { - return $condition($value, $key) ? - $then($value, $key) : - $else($value, $key); - } - ); + static fn ($value, $key) => $condition($value, $key) ? $then($value, $key) : $else($value, $key) + ); - // Point free style. - return $map; - }; + // Point free style. + return $map; }; - }; } } diff --git a/src/Operation/Implode.php b/src/Operation/Implode.php index 003d52792..78341814e 100644 --- a/src/Operation/Implode.php +++ b/src/Operation/Implode.php @@ -27,20 +27,14 @@ public function __invoke(): Closure * @psalm-return Closure(Iterator): Generator */ static function (string $glue): Closure { - $reducerFactory = static function (string $glue): Closure { - return - /** - * @psalm-param TKey $key - * @psalm-param T $item - * - * @param mixed $item - */ - static function (string $carry, $item): string { - $carry .= $item; - - return $carry; - }; - }; + $reducerFactory = static fn (string $glue): Closure => + /** + * @psalm-param TKey $key + * @psalm-param T $item + * + * @param mixed $item + */ + static fn (string $carry, $item): string => $carry .= $item; /** @psalm-var Closure(Iterator): Generator $pipe */ $pipe = Pipe::of()( diff --git a/src/Operation/Intersect.php b/src/Operation/Intersect.php index 9b7e6686b..c80cbc088 100644 --- a/src/Operation/Intersect.php +++ b/src/Operation/Intersect.php @@ -29,20 +29,17 @@ public function __invoke(): Closure * @psalm-return Closure(Iterator): Generator */ static function (...$values): Closure { - $filterCallbackFactory = static function (array $values): Closure { - return - /** - * @psalm-param T $value - * @psalm-param TKey $key - * @psalm-param Iterator $iterator - * - * @param mixed $value - * @param mixed $key - */ - static function ($value, $key, Iterator $iterator) use ($values): bool { - return in_array($value, $values, true); - }; - }; + $filterCallbackFactory = static fn (array $values): Closure => + /** + * @param mixed $value + * @psalm-param T $value + * + * @param mixed $key + * @psalm-param TKey $key + * + * @psalm-param Iterator $iterator + */ + static fn ($value, $key, Iterator $iterator): bool => in_array($value, $values, true); /** @psalm-var Closure(Iterator): Generator $filter */ $filter = Filter::of()($filterCallbackFactory($values)); diff --git a/src/Operation/IntersectKeys.php b/src/Operation/IntersectKeys.php index ae25995eb..751fe9215 100644 --- a/src/Operation/IntersectKeys.php +++ b/src/Operation/IntersectKeys.php @@ -29,20 +29,16 @@ public function __invoke(): Closure * @psalm-return Closure(Iterator): Generator */ static function (...$keys): Closure { - $filterCallbackFactory = static function (array $keys): Closure { - return - /** - * @psalm-param T $value - * @psalm-param TKey $key - * @psalm-param Iterator $iterator - * - * @param mixed $value - * @param mixed $key - */ - static function ($value, $key, Iterator $iterator) use ($keys): bool { - return in_array($key, $keys, true); - }; - }; + $filterCallbackFactory = static fn (array $keys): Closure => + /** + * @psalm-param T $value + * @psalm-param TKey $key + * @psalm-param Iterator $iterator + * + * @param mixed $value + * @param mixed $key + */ + static fn ($value, $key, Iterator $iterator): bool => in_array($key, $keys, true); /** @psalm-var Closure(Iterator): Generator $filter */ $filter = Filter::of()($filterCallbackFactory($keys)); diff --git a/src/Operation/Intersperse.php b/src/Operation/Intersperse.php index 25d592b1b..d9c727446 100644 --- a/src/Operation/Intersperse.php +++ b/src/Operation/Intersperse.php @@ -28,46 +28,40 @@ public function __invoke(): Closure * * @psalm-return Closure(int): Closure(int): Closure(Iterator): Generator */ - static function ($element): Closure { - return + static fn ($element): Closure => + /** + * @psalm-return Closure(int): Closure(Iterator): Generator + */ + static fn (int $atEvery): Closure => /** - * @psalm-return Closure(int): Closure(Iterator): Generator + * @psalm-return Closure(Iterator): Generator */ - static function (int $atEvery) use ($element): Closure { - return - /** - * @psalm-return Closure(Iterator): Generator - */ - 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.' - ); - } + static fn (int $startAt): Closure => + /** + * @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/Limit.php b/src/Operation/Limit.php index 1d592c0c9..788a17b24 100644 --- a/src/Operation/Limit.php +++ b/src/Operation/Limit.php @@ -13,11 +13,13 @@ * @psalm-template TKey * @psalm-template TKey of array-key * @psalm-template T + * + * phpcs:disable Generic.Files.LineLength.TooLong */ final class Limit extends AbstractOperation { /** - * @psalm-return Closure(int=): Closure(int=): Closure(Iterator): Generator + * @psalm-return Closure(int = default):Closure (int=): Closure(Iterator): Generator */ public function __invoke(): Closure { @@ -25,22 +27,16 @@ public function __invoke(): Closure /** * @psalm-return Closure(int=): Closure(Iterator): Generator */ - static function (int $count = -1): Closure { - return + static fn (int $count = -1): Closure => + /** + * @psalm-return Closure(Iterator): Generator + */ + static fn (int $offset = 0): Closure => /** - * @psalm-return Closure(Iterator): Generator + * @psalm-param Iterator $iterator + * + * @psalm-return Generator */ - 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); - }; - }; - }; + static fn (Iterator $iterator): Generator => yield from new LimitIterator($iterator, $offset, $count); } } diff --git a/src/Operation/Lines.php b/src/Operation/Lines.php index 1554cd8a7..e99f54c57 100644 --- a/src/Operation/Lines.php +++ b/src/Operation/Lines.php @@ -20,15 +20,13 @@ final class Lines extends AbstractOperation { /** - * @psalm-return Closure(Iterator): Generator + * @psalm-return Closure(Iterator):Generator */ public function __invoke(): Closure { - $mapCallback = static function (array $value): string { - return implode('', $value); - }; + $mapCallback = static fn (array $value): string => implode('', $value); - /** @psalm-var Closure(Iterator): Generator $pipe */ + /** @psalm-var Closure(Iterator):Generator $pipe */ $pipe = Pipe::of()( Explode::of()(PHP_EOL, "\n", "\r\n"), Map::of()($mapCallback) diff --git a/src/Operation/Map.php b/src/Operation/Map.php index b4d95c411..04b4d2fc2 100644 --- a/src/Operation/Map.php +++ b/src/Operation/Map.php @@ -19,7 +19,7 @@ final class Map extends AbstractOperation { /** - * @psalm-return Closure((callable(T, TKey): T)...): Closure(Iterator): Generator + * @psalm-return Closure(callable(T , TKey ): T ...):Closure (Iterator): Generator */ public function __invoke(): Closure { @@ -27,40 +27,33 @@ public function __invoke(): Closure /** * @psalm-param callable(T, TKey): T ...$callbacks */ - static function (callable ...$callbacks): Closure { - return - /** - * @psalm-param Iterator $iterator - * - * @psalm-return Generator - */ - static function (Iterator $iterator) use ($callbacks): Generator { - $callbackFactory = + static fn (callable ...$callbacks): Closure => + /** + * @psalm-param Iterator $iterator + * + * @psalm-return Generator + */ + static function (Iterator $iterator) use ($callbacks): Generator { + $callbackFactory = + /** + * @param mixed $key + * @psalm-param TKey $key + * + * @psalm-return Closure(T, callable(T, TKey): T): T + */ + static fn ($key): Closure => /** - * @param mixed $key - * @psalm-param TKey $key + * @param mixed $carry + * @psalm-param T $carry + * @psalm-param callable(T, TKey): T $callback * - * @psalm-return Closure(T, callable(T, TKey): T): T + * @psalm-return T */ - static function ($key): Closure { - return - /** - * @psalm-param T $carry - * @psalm-param callable(T, TKey): T $callback - * - * @psalm-return T - * - * @param mixed $carry - */ - static function ($carry, callable $callback) use ($key) { - return $callback($carry, $key); - }; - }; + static fn ($carry, callable $callback) => $callback($carry, $key); - foreach ($iterator as $key => $value) { - yield $key => array_reduce($callbacks, $callbackFactory($key), $value); - } - }; - }; + foreach ($iterator as $key => $value) { + yield $key => array_reduce($callbacks, $callbackFactory($key), $value); + } + }; } } diff --git a/src/Operation/Merge.php b/src/Operation/Merge.php index b445514b1..053e8bdc3 100644 --- a/src/Operation/Merge.php +++ b/src/Operation/Merge.php @@ -24,10 +24,10 @@ public function __invoke(): Closure /** * @psalm-param iterable ...$sources */ - static function (iterable ...$sources): Closure { - return + static fn (iterable ...$sources): Closure => /** * @psalm-param Iterator $iterator + * * @psalm-return Generator */ static function (Iterator $iterator) use ($sources): Generator { @@ -41,6 +41,5 @@ static function (Iterator $iterator) use ($sources): Generator { } } }; - }; } } diff --git a/src/Operation/Nth.php b/src/Operation/Nth.php index 23f6028d8..daa88d6d7 100644 --- a/src/Operation/Nth.php +++ b/src/Operation/Nth.php @@ -24,30 +24,26 @@ public function __invoke(): Closure /** * @psalm-return Closure(int): Closure(Iterator): Generator */ - static function (int $step): Closure { - return + static fn (int $step): Closure => + /** + * @psalm-return Closure(Iterator): Generator + */ + static fn (int $offset): Closure => /** - * @psalm-return Closure(Iterator): Generator + * @psalm-param Iterator $iterator + * + * @psalm-return Generator */ - 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; + 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/Nullsy.php b/src/Operation/Nullsy.php index 28132aecb..924999bf8 100644 --- a/src/Operation/Nullsy.php +++ b/src/Operation/Nullsy.php @@ -27,18 +27,14 @@ public function __invoke(): Closure * @param mixed $value * @psalm-param T $value */ - static function ($value): bool { - return in_array($value, [null, [], 0, false, ''], true); - }; + static fn ($value): bool => in_array($value, [null, [], 0, false, ''], true); $dropWhileCallback = /** * @param mixed $value * @psalm-param T $value */ - static function ($value): bool { - return true === $value; - }; + static fn ($value): bool => true === $value; /** @psalm-var Closure(Iterator): Generator $pipe */ $pipe = Pipe::of()( diff --git a/src/Operation/Pack.php b/src/Operation/Pack.php index 7ab2ffd3e..fe69cadbc 100644 --- a/src/Operation/Pack.php +++ b/src/Operation/Pack.php @@ -30,9 +30,7 @@ public function __invoke(): Closure * * @psalm-return array{0: TKey, 1: T} */ - static function ($value, $key): array { - return [$key, $value]; - }; + static fn ($value, $key): array => [$key, $value]; /** @psalm-var Closure(Iterator): Generator $pipe */ $pipe = Pipe::of()( diff --git a/src/Operation/Pad.php b/src/Operation/Pad.php index 3ccd050d8..b78585120 100644 --- a/src/Operation/Pad.php +++ b/src/Operation/Pad.php @@ -24,35 +24,31 @@ public function __invoke(): Closure /** * @psalm-return Closure(T): Closure(Iterator): Generator */ - static function (int $size): Closure { - return + static fn (int $size): Closure => + /** + * @param mixed $padValue + * @psalm-param T $padValue + * + * @psalm-return Closure(Iterator): Generator + */ + static fn ($padValue): Closure => /** - * @param mixed $padValue - * @psalm-param T $padValue + * @psalm-param Iterator $iterator * - * @psalm-return Closure(Iterator): Generator + * @psalm-return 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; + 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/Pair.php b/src/Operation/Pair.php index b24d22398..dc47f2507 100644 --- a/src/Operation/Pair.php +++ b/src/Operation/Pair.php @@ -16,7 +16,7 @@ final class Pair extends AbstractOperation { /** - * @psalm-return Closure(Iterator): Generator + * @psalm-return Closure(Iterator):(Generator) */ public function __invoke(): Closure { @@ -31,9 +31,7 @@ public function __invoke(): Closure * @param mixed $initial * @param mixed $key */ - static function ($initial, $key, array $value) { - return $value[0]; - }; + static fn ($initial, $key, array $value) => $value[0]; $callbackForValues = /** @@ -46,17 +44,13 @@ static function ($initial, $key, array $value) { * @param mixed $initial * @param mixed $key */ - static function ($initial, $key, array $value) { - return $value[1]; - }; + static fn ($initial, $key, array $value) => $value[1]; - /** @psalm-var Closure(Iterator): Generator $pipe */ + /** @psalm-var Closure(Iterator):(Generator) $pipe */ $pipe = Pipe::of()( Chunk::of()(2), Map::of()( - static function (array $value): array { - return array_values($value); - } + static fn (array $value): array => array_values($value) ), Associate::of()($callbackForKeys)($callbackForValues) ); diff --git a/src/Operation/Permutate.php b/src/Operation/Permutate.php index 48c60818e..23fa68f22 100644 --- a/src/Operation/Permutate.php +++ b/src/Operation/Permutate.php @@ -24,9 +24,7 @@ public function __invoke(): Closure * * @psalm-return Generator> */ - function (array $dataset): Generator { - return $this->getPermutations($dataset); - }; + fn (array $dataset): Generator => $this->getPermutations($dataset); return /** @@ -34,9 +32,7 @@ function (array $dataset): Generator { * * @psalm-return Generator> */ - static function (Iterator $iterator) use ($getPermutations): Generator { - return yield from $getPermutations(iterator_to_array($iterator)); - }; + static fn (Iterator $iterator): Generator => yield from $getPermutations([...$iterator]); } /** diff --git a/src/Operation/Pipe.php b/src/Operation/Pipe.php index bc5c2c6e6..feaac4c6d 100644 --- a/src/Operation/Pipe.php +++ b/src/Operation/Pipe.php @@ -18,7 +18,7 @@ final class Pipe extends AbstractOperation { /** - * @psalm-return Closure((callable(Iterator):Generator)...): Closure(Iterator): Generator + * @psalm-return Closure(callable(Iterator ): Generator ...):Closure (Iterator): Generator */ public function __invoke(): Closure { @@ -28,27 +28,23 @@ public function __invoke(): Closure * * @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 { - $callback = - /** - * @psalm-param Iterator $iterator - * @psalm-param callable(Iterator): Generator $fn - * - * @psalm-return Generator - */ - static function (Iterator $iterator, callable $fn): Generator { - return $fn($iterator); - }; + static fn (callable ...$operations): Closure => + /** + * @psalm-param Iterator $iterator + * + * @psalm-return Generator + */ + static function (Iterator $iterator) use ($operations): Generator { + $callback = + /** + * @psalm-param Iterator $iterator + * @psalm-param callable(Iterator): Generator $fn + * + * @psalm-return Generator + */ + static fn (Iterator $iterator, callable $fn): Generator => $fn($iterator); - return yield from array_reduce($operations, $callback, $iterator); - }; - }; + return yield from array_reduce($operations, $callback, $iterator); + }; } } diff --git a/src/Operation/Pluck.php b/src/Operation/Pluck.php index 30915db28..ccaceb19c 100644 --- a/src/Operation/Pluck.php +++ b/src/Operation/Pluck.php @@ -28,95 +28,91 @@ final class Pluck extends AbstractOperation { /** - * @psalm-return Closure(T): Closure(T): Closure(Iterator): Generator + * @psalm-return Closure(T):Closure(T):Closure(Iterator):Generator, mixed, void> */ public function __invoke(): Closure { return /** + * @param mixed $key * @psalm-param T $key * * @psalm-return Closure(T): Closure(Iterator): Generator, mixed, void> - * - * @param mixed $key */ - static function ($key): Closure { - return + static fn ($key): Closure => + /** + * @param mixed $default + * @psalm-param T $default + * + * @psalm-return Closure(Iterator): Generator, mixed, void> + */ + static fn ($default): Closure => /** - * @psalm-param T $default - * - * @psalm-return Closure(Iterator): Generator, mixed, void> + * @psalm-param Iterator $iterator * - * @param mixed $default + * @psalm-return Generator, mixed, void> */ - static function ($default) use ($key): Closure { - return + static function (Iterator $iterator) use ($key, $default): Generator { + $pick = /** * @psalm-param Iterator $iterator * - * @psalm-return Generator, mixed, void> + * @param mixed $target + * @psalm-param T|iterable $target + * + * @psalm-param array $key + * + * @param mixed|null $default + * @psalm-param T $default + * + * @psalm-return T|iterable */ - static function (Iterator $iterator) use ($key, $default): Generator { - $pick = - /** - * @psalm-param Iterator $iterator - * @psalm-param T|iterable $target - * @psalm-param array $key - * @psalm-param T $default - * - * @psalm-return T|iterable - * - * @param mixed $target - * @param mixed|null $default - */ - static function (Iterator $iterator, $target, array $key, $default = null) use (&$pick) { - while (null !== $segment = array_shift($key)) { - if ('*' === $segment) { - if (false === is_iterable($target)) { - return $default; - } - - /** @psalm-var array $result */ - $result = []; - - foreach ($target as $item) { - $result[] = $pick($iterator, $item, $key); - } - - /** @psalm-var Generator $collapse */ - $collapse = Collapse::of()(new ArrayIterator($result)); + static function (Iterator $iterator, $target, array $key, $default = null) use (&$pick) { + while (null !== $segment = array_shift($key)) { + if ('*' === $segment) { + if (false === is_iterable($target)) { + return $default; + } - return in_array('*', $key, true) ? $collapse : $result; - } + /** @psalm-var array $result */ + $result = []; - if ((true === is_array($target)) && (true === array_key_exists($segment, $target))) { - /** @psalm-var T $target */ - $target = $target[$segment]; - } elseif (($target instanceof ArrayAccess) && (true === $target->offsetExists($segment))) { - /** @psalm-var T $target */ - $target = $target[$segment]; - } elseif ($target instanceof Collection) { - /** @psalm-var T $target */ - $target = (Get::of()($segment)($default)($target->getIterator()))->current(); - } elseif ((true === is_object($target)) && (true === property_exists($target, $segment))) { - /** @psalm-var T $target */ - $target = (new ReflectionClass($target))->getProperty($segment)->getValue($target); - } else { - /** @psalm-var T $target */ - $target = $default; - } + foreach ($target as $item) { + $result[] = $pick($iterator, $item, $key); } - return $target; - }; + /** @psalm-var Generator $collapse */ + $collapse = Collapse::of()(new ArrayIterator($result)); - $key = true === is_scalar($key) ? explode('.', trim((string) $key, '.')) : $key; + return in_array('*', $key, true) ? $collapse : $result; + } - foreach ($iterator as $value) { - yield $pick($iterator, $value, $key, $default); + if ((true === is_array($target)) && (true === array_key_exists($segment, $target))) { + /** @psalm-var T $target */ + $target = $target[$segment]; + } elseif (($target instanceof ArrayAccess) && (true === $target->offsetExists($segment))) { + /** @psalm-var T $target */ + $target = $target[$segment]; + } elseif ($target instanceof Collection) { + /** @psalm-var T $target */ + $target = (Get::of()($segment)($default)($target->getIterator()))->current(); + } elseif ((true === is_object($target)) && (true === property_exists($target, $segment))) { + /** @psalm-var T $target */ + $target = (new ReflectionClass($target))->getProperty($segment)->getValue($target); + } else { + /** @psalm-var T $target */ + $target = $default; + } } + + return $target; }; + + $key = true === is_scalar($key) ? explode('.', trim((string) $key, '.')) : $key; + + foreach ($iterator as $value) { + yield $pick($iterator, $value, $key, $default); + } }; - }; } } diff --git a/src/Operation/Prepend.php b/src/Operation/Prepend.php index 1786e1af5..8f0512c7b 100644 --- a/src/Operation/Prepend.php +++ b/src/Operation/Prepend.php @@ -26,22 +26,20 @@ public function __invoke(): Closure * * @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) { - yield $key => $item; - } + static fn (...$items): Closure => + /** + * @psalm-param Iterator $iterator + * + * @psalm-return Generator + */ + static function (Iterator $iterator) use ($items): Generator { + foreach ($items as $key => $item) { + yield $key => $item; + } - foreach ($iterator as $key => $value) { - yield $key => $value; - } - }; - }; + foreach ($iterator as $key => $value) { + yield $key => $value; + } + }; } } diff --git a/src/Operation/Product.php b/src/Operation/Product.php index 589062a4a..fb1c0b9d8 100644 --- a/src/Operation/Product.php +++ b/src/Operation/Product.php @@ -27,47 +27,45 @@ public function __invoke(): Closure * * @psalm-return Closure(Iterator): Generator> */ - static function (iterable ...$iterables): Closure { - return - /** - * @psalm-param Iterator $iterator - * - * @psalm-return Generator> - */ - static function (Iterator $iterator) use ($iterables): Generator { - /** @psalm-var Closure(iterable...): Generator> $cartesian */ - $cartesian = - /** - * @param array ...$iterables - * - * @psalm-param iterable ...$iterables - * - * @psalm-return Generator> - */ - static function (iterable ...$iterables) use (&$cartesian): Generator { - $iterable = array_pop($iterables); + static fn (iterable ...$iterables): Closure => + /** + * @psalm-param Iterator $iterator + * + * @psalm-return Generator> + */ + static function (Iterator $iterator) use ($iterables): Generator { + /** @psalm-var Closure(iterable...): Generator> $cartesian */ + $cartesian = + /** + * @param array ...$iterables + * + * @psalm-param iterable ...$iterables + * + * @psalm-return Generator> + */ + static function (iterable ...$iterables) use (&$cartesian): Generator { + $iterable = array_pop($iterables); - if (null === $iterable) { - return yield []; - } + if (null === $iterable) { + return yield []; + } - // @todo Find better algo, without recursion. - /** @psalm-var array $item */ - foreach ($cartesian(...$iterables) as $item) { - foreach ($iterable as $value) { - yield $item + [count($item) => $value]; - } + // @todo Find better algo, without recursion. + /** @psalm-var array $item */ + foreach ($cartesian(...$iterables) as $item) { + foreach ($iterable as $value) { + yield $item + [count($item) => $value]; } - }; + } + }; - $iterators = [$iterator]; + $iterators = [$iterator]; - foreach ($iterables as $iterable) { - $iterators[] = $iterable; - } + foreach ($iterables as $iterable) { + $iterators[] = $iterable; + } - return yield from $cartesian(...$iterators); - }; - }; + return yield from $cartesian(...$iterators); + }; } } diff --git a/src/Operation/RSample.php b/src/Operation/RSample.php index 5f0d73f80..039bee431 100644 --- a/src/Operation/RSample.php +++ b/src/Operation/RSample.php @@ -12,6 +12,8 @@ * @psalm-template TKey * @psalm-template TKey of array-key * @psalm-template T + * + * phpcs:disable Generic.Files.LineLength.TooLong */ final class RSample extends AbstractOperation { @@ -25,11 +27,7 @@ public function __invoke(): Closure * @psalm-return Closure(Iterator): Generator */ static function (float $probability): Closure { - $callback = static function (float $probability): Closure { - return static function () use ($probability): bool { - return (mt_rand() / mt_getrandmax()) < $probability; - }; - }; + $callback = static fn (float $probability): Closure => static fn (): bool => (mt_rand() / mt_getrandmax()) < $probability; /** @psalm-var Closure(Iterator): Generator $filter */ $filter = Filter::of()($callback($probability)); diff --git a/src/Operation/Range.php b/src/Operation/Range.php index fb2a747f7..feedac060 100644 --- a/src/Operation/Range.php +++ b/src/Operation/Range.php @@ -20,7 +20,7 @@ final class Range extends AbstractOperation { /** - * @psalm-return Closure(float=): Closure(float=): Closure(float=): Closure(null|Iterator): Generator + * @psalm-return Closure(float = default):Closure (float=): Closure(float=): Closure(null|Iterator): Generator */ public function __invoke(): Closure { @@ -28,29 +28,24 @@ public function __invoke(): Closure /** * @psalm-return Closure(float=): Closure(float=): Closure(null|Iterator): Generator */ - static function (float $start = 0.0): Closure { - return + static fn (float $start = 0.0): Closure => + /** + * @psalm-return Closure(float=): Closure(null|Iterator): Generator + */ + static fn (float $end = INF): Closure => /** - * @psalm-return Closure(float=): Closure(null|Iterator): Generator + * @psalm-return Closure(null|Iterator): Generator */ - static function (float $end = INF) use ($start): Closure { - return - /** - * @psalm-return Closure(null|Iterator): Generator - */ - static function (float $step = 1.0) use ($start, $end): Closure { - return - /** - * @psalm-param null|Iterator $iterator - * @psalm-return Generator - */ - static function (?Iterator $iterator = null) use ($start, $end, $step): Generator { - for ($current = $start; $current < $end; $current += $step) { - yield $current; - } - }; - }; - }; - }; + static fn (float $step = 1.0): Closure => + /** + * @psalm-param null|Iterator $iterator + * + * @psalm-return Generator + */ + static function (?Iterator $iterator = null) use ($start, $end, $step): Generator { + for ($current = $start; $current < $end; $current += $step) { + yield $current; + } + }; } } diff --git a/src/Operation/Reduction.php b/src/Operation/Reduction.php index d7df3a626..62561447d 100644 --- a/src/Operation/Reduction.php +++ b/src/Operation/Reduction.php @@ -18,7 +18,7 @@ final class Reduction extends AbstractOperation { /** - * @psalm-return Closure(callable(T|null, T, TKey, Iterator):(T|null)): Closure(T|null): Closure(Iterator): Generator + * @psalm-return Closure(callable((T | null) , T , TKey , Iterator ): (T | null)):Closure (T|null): Closure(Iterator): Generator */ public function __invoke(): Closure { @@ -28,27 +28,23 @@ public function __invoke(): Closure * * @psalm-return Closure(T|null): Closure(Iterator): Generator */ - static function (callable $callback): Closure { - return + static fn (callable $callback): Closure => + /** + * @param mixed|null $initial + * @psalm-param T|null $initial + * + * @psalm-return Closure(Iterator): Generator + */ + static fn ($initial = null): Closure => /** - * @param mixed|null $initial - * @psalm-param T|null $initial + * @psalm-param Iterator $iterator * - * @psalm-return Closure(Iterator): Generator + * @psalm-return Generator */ - static function ($initial = null) use ($callback): Closure { - return - /** - * @psalm-param Iterator $iterator - * - * @psalm-return Generator - */ - static function (Iterator $iterator) use ($callback, $initial): Generator { - foreach ($iterator as $key => $value) { - yield $key => ($initial = $callback($initial, $value, $key, $iterator)); - } - }; + static function (Iterator $iterator) use ($callback, $initial): Generator { + foreach ($iterator as $key => $value) { + yield $key => ($initial = $callback($initial, $value, $key, $iterator)); + } }; - }; } } diff --git a/src/Operation/Reverse.php b/src/Operation/Reverse.php index 41d6b7e6a..1640b0065 100644 --- a/src/Operation/Reverse.php +++ b/src/Operation/Reverse.php @@ -31,11 +31,7 @@ public function __invoke(): Closure * * @psalm-return array */ - $callback = static function (array $carry, array $value): array { - array_unshift($carry, ...$value); - - return $carry; - }; + $callback = static fn (array $carry, array $value): array => [...$value, ...$carry]; /** @psalm-var Closure(Iterator): Generator $pipe */ $pipe = Pipe::of()( diff --git a/src/Operation/Scale.php b/src/Operation/Scale.php index d13e4f7ac..874393a34 100644 --- a/src/Operation/Scale.php +++ b/src/Operation/Scale.php @@ -28,72 +28,59 @@ public function __invoke(): Closure /** * @psalm-return Closure(float): Closure(float): Closure(float): Closure(float): Closure(Iterator): Generator */ - static function (float $lowerBound): Closure { - return + static fn (float $lowerBound): Closure => + /** + * @psalm-return Closure(float): Closure(float): Closure(float): Closure(Iterator): Generator + */ + static fn (float $upperBound): Closure => /** - * @psalm-return Closure(float): Closure(float): Closure(float): Closure(Iterator): Generator + * @psalm-return Closure(float): Closure(float): Closure(Iterator): Generator */ - static function (float $upperBound) use ($lowerBound): Closure { - return + static fn (float $wantedLowerBound = 0.0): Closure => + /** + * @psalm-return Closure(float): Closure(Iterator): Generator + */ + static fn (float $wantedUpperBound = 1.0): Closure => /** - * @psalm-return Closure(float): Closure(float): Closure(Iterator): Generator + * @psalm-return Closure(Iterator): Generator */ - static function (float $wantedLowerBound = 0.0) use ($lowerBound, $upperBound): Closure { - return + static function (float $base = 0.0) use ($lowerBound, $upperBound, $wantedLowerBound, $wantedUpperBound): Closure { + $wantedLowerBound = (0.0 === $wantedLowerBound) ? (0.0 === $base ? 0.0 : 1.0) : $wantedLowerBound; + $wantedUpperBound = (1.0 === $wantedUpperBound) ? (0.0 === $base ? 1.0 : $base) : $wantedUpperBound; + /** @psalm-var callable(Generator):Generator $mapper */ + $mapper = Map::of()( /** - * @psalm-return Closure(float): Closure(Iterator): Generator + * @param mixed $v + * @psalm-param float|int $v */ - static function (float $wantedUpperBound = 1.0) use ($lowerBound, $upperBound, $wantedLowerBound): Closure { // phpcs:ignore - return - /** - * @psalm-return Closure(Iterator): Generator - */ - static function (float $base = 0.0) use ($lowerBound, $upperBound, $wantedLowerBound, $wantedUpperBound): Closure { // phpcs:ignore - $wantedLowerBound = (0.0 === $wantedLowerBound) ? (0.0 === $base ? 0.0 : 1.0) : $wantedLowerBound; // phpcs:ignore - $wantedUpperBound = (1.0 === $wantedUpperBound) ? (0.0 === $base ? 1.0 : $base) : $wantedUpperBound; // phpcs:ignore + static function ($v) use ($lowerBound, $upperBound, $wantedLowerBound, $wantedUpperBound, $base): float { + $mx = 0.0 === $base ? + ($v - $lowerBound) / ($upperBound - $lowerBound) : + log($v - $lowerBound, $base) / log($upperBound - $lowerBound, $base); - /** @psalm-var callable(Generator): Generator $mapper */ - $mapper = Map::of()( - /** - * @param mixed $v - * @psalm-param float|int $v - */ - static function ($v) use ($lowerBound, $upperBound, $wantedLowerBound, $wantedUpperBound, $base): float { // phpcs:ignore - $mx = 0.0 === $base ? - ($v - $lowerBound) / ($upperBound - $lowerBound) : - log($v - $lowerBound, $base) / log($upperBound - $lowerBound, $base); + $mx = $mx === -INF ? 0 : $mx; - $mx = $mx === -INF ? 0 : $mx; + return $wantedLowerBound + $mx * ($wantedUpperBound - $wantedLowerBound); + } + ); - return $wantedLowerBound + $mx * ($wantedUpperBound - $wantedLowerBound); - } - ); - - /** @psalm-var callable(Iterator): Generator $filter */ - $filter = Filter::of()( - /** - * @param float|int $item - */ - static function ($item) use ($lowerBound): bool { - return $item >= $lowerBound; - }, - /** - * @param float|int $item - */ - static function ($item) use ($upperBound): bool { - return $item <= $upperBound; - } - ); + /** @psalm-var callable(Iterator):(Generator) $filter */ + $filter = Filter::of()( + /** + * @param float|int $item + */ + static fn ($item): bool => $item >= $lowerBound, + /** + * @param float|int $item + */ + static fn ($item): bool => $item <= $upperBound + ); - /** @psalm-var Closure(Iterator): Generator $pipe */ - $pipe = Pipe::of()($filter, $mapper); + /** @psalm-var Closure(Iterator):(Generator) $pipe */ + $pipe = Pipe::of()($filter, $mapper); - // Point free style. - return $pipe; - }; - }; + // Point free style. + return $pipe; }; - }; - }; } } diff --git a/src/Operation/ScanLeft.php b/src/Operation/ScanLeft.php index f52045ad1..8307abbf9 100644 --- a/src/Operation/ScanLeft.php +++ b/src/Operation/ScanLeft.php @@ -18,7 +18,7 @@ final class ScanLeft extends AbstractOperation { /** - * @psalm-return Closure(callable(T|null, T, TKey, Iterator):(T|null)): Closure(T|null): Closure(Iterator): Generator + * @psalm-return Closure(callable((T | null) , T , TKey , Iterator ): (T | null)):Closure (T|null): Closure(Iterator): Generator */ public function __invoke(): Closure { @@ -28,24 +28,22 @@ public function __invoke(): Closure * * @psalm-return Closure(T|null): 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 { - /** @psalm-var Closure(Iterator): Generator $pipe */ - $pipe = Pipe::of()( - Reduction::of()($callback)($initial), - Prepend::of()($initial) - ); + static fn (callable $callback): Closure => + /** + * @param mixed|null $initial + * @psalm-param T|null $initial + * + * @psalm-return Closure(Iterator): Generator + */ + static function ($initial = null) use ($callback): Closure { + /** @psalm-var Closure(Iterator):(Generator) $pipe */ + $pipe = Pipe::of()( + Reduction::of()($callback)($initial), + Prepend::of()($initial) + ); - // Point free style. - return $pipe; - }; - }; + // Point free style. + return $pipe; + }; } } diff --git a/src/Operation/ScanLeft1.php b/src/Operation/ScanLeft1.php index 1ef84406b..16948fcac 100644 --- a/src/Operation/ScanLeft1.php +++ b/src/Operation/ScanLeft1.php @@ -18,7 +18,7 @@ final class ScanLeft1 extends AbstractOperation { /** - * @psalm-return Closure(callable(T|null, T, TKey, Iterator):(T|null)): Closure(Iterator): Generator + * @psalm-return Closure(callable((T | null) , T , TKey , Iterator ): (T | null)):Closure (Iterator): Generator */ public function __invoke(): Closure { @@ -28,25 +28,23 @@ public function __invoke(): Closure * * @psalm-return Closure(Iterator): Generator */ - static function (callable $callback): Closure { - return - /** - * @psalm-param Iterator $iterator - * - * @psalm-return Generator - */ - static function (Iterator $iterator) use ($callback): Generator { - $initial = $iterator->current(); + static fn (callable $callback): Closure => + /** + * @psalm-param Iterator $iterator + * + * @psalm-return Generator + */ + static function (Iterator $iterator) use ($callback): Generator { + $initial = $iterator->current(); - /** @psalm-var Closure(Iterator): Generator $pipe */ - $pipe = Pipe::of()( - Tail::of(), - Reduction::of()($callback)($initial), - Prepend::of()($initial) - ); + /** @psalm-var Closure(Iterator):(Generator) $pipe */ + $pipe = Pipe::of()( + Tail::of(), + Reduction::of()($callback)($initial), + Prepend::of()($initial) + ); - return yield from $pipe($iterator); - }; - }; + return yield from $pipe($iterator); + }; } } diff --git a/src/Operation/ScanRight.php b/src/Operation/ScanRight.php index c97cd66c3..6ea8bcfb1 100644 --- a/src/Operation/ScanRight.php +++ b/src/Operation/ScanRight.php @@ -18,7 +18,7 @@ final class ScanRight extends AbstractOperation { /** - * @psalm-return Closure(callable(T|null, T, TKey, Iterator):(T|null)): Closure(T|null): Closure(Iterator): Generator + * @psalm-return Closure(callable((T | null) , T , TKey , Iterator ): (T | null)):Closure (T|null): Closure(Iterator): Generator */ public function __invoke(): Closure { @@ -28,26 +28,24 @@ public function __invoke(): Closure * * @psalm-return Closure(T|null): 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 { - /** @psalm-var Closure(Iterator): Generator $pipe */ - $pipe = Pipe::of()( - Reverse::of(), - Reduction::of()($callback)($initial), - Reverse::of(), - Append::of()($initial) - ); + static fn (callable $callback): Closure => + /** + * @param mixed|null $initial + * @psalm-param T|null $initial + * + * @psalm-return Closure(Iterator): Generator + */ + static function ($initial = null) use ($callback): Closure { + /** @psalm-var Closure(Iterator):(Generator) $pipe */ + $pipe = Pipe::of()( + Reverse::of(), + Reduction::of()($callback)($initial), + Reverse::of(), + Append::of()($initial) + ); - // Point free style. - return $pipe; - }; - }; + // Point free style. + return $pipe; + }; } } diff --git a/src/Operation/ScanRight1.php b/src/Operation/ScanRight1.php index 3f2da88b7..bbda75cce 100644 --- a/src/Operation/ScanRight1.php +++ b/src/Operation/ScanRight1.php @@ -18,7 +18,7 @@ final class ScanRight1 extends AbstractOperation { /** - * @psalm-return Closure(callable(T|null, T, TKey, Iterator):(T|null)): Closure(Iterator): Generator + * @psalm-return Closure(callable((T | null) , T , TKey , Iterator ): (T | null)):Closure (Iterator): Generator */ public function __invoke(): Closure { diff --git a/src/Operation/Shuffle.php b/src/Operation/Shuffle.php index 056b9c9bf..b5ea78b75 100644 --- a/src/Operation/Shuffle.php +++ b/src/Operation/Shuffle.php @@ -27,8 +27,6 @@ public function __invoke(): Closure * * @psalm-return Generator */ - static function (Iterator $iterator): Generator { - return yield from new RandomIterator($iterator); - }; + static fn (Iterator $iterator): Generator => yield from new RandomIterator($iterator); } } diff --git a/src/Operation/Since.php b/src/Operation/Since.php index 2639a6f9e..7e65ef945 100644 --- a/src/Operation/Since.php +++ b/src/Operation/Since.php @@ -13,12 +13,13 @@ * @psalm-template TKey of array-key * @psalm-template T * + * phpcs:disable Generic.Files.LineLength.TooLong * phpcs:disable Generic.WhiteSpace.ScopeIndent.IncorrectExact */ final class Since extends AbstractOperation { /** - * @psalm-return Closure((callable(T, TKey):bool)...): Closure(Iterator): Generator + * @psalm-return Closure(callable(T , TKey ): bool ...):Closure (Iterator): Generator */ public function __invoke(): Closure { @@ -28,47 +29,30 @@ public function __invoke(): Closure * * @psalm-return Closure(Iterator): Generator */ - static function (callable ...$callbacks): Closure { - return + static fn (callable ...$callbacks): Closure => static function (Iterator $iterator) use ($callbacks): Generator { + $reducer = /** * @psalm-param Iterator $iterator * - * @psalm-return Generator + * @psalm-return Closure(bool, callable(T, TKey): bool): bool */ - static function (Iterator $iterator) use ($callbacks): Generator { - $reducer = - /** - * @psalm-param Iterator $iterator - * - * @psalm-return Closure(bool, callable(T, TKey): bool): bool - */ - static function (Iterator $iterator): Closure { - return - /** - * @psalm-param bool $carry - * @psalm-param callable(T, TKey): bool $callback - */ - static function (bool $carry, callable $callback) use ($iterator): bool { - return ($callback($iterator->current(), $iterator->key())) ? - $carry : - false; - }; - }; + static fn (Iterator $iterator): Closure => static fn (bool $carry, callable $callback): bool => ($callback($iterator->current(), $iterator->key())) ? + $carry : + false; - while ($iterator->valid()) { - $result = array_reduce($callbacks, $reducer($iterator), true); + while ($iterator->valid()) { + $result = array_reduce($callbacks, $reducer($iterator), true); - if (false !== $result) { - break; - } + if (false !== $result) { + break; + } - $iterator->next(); - } + $iterator->next(); + } - for (; $iterator->valid(); $iterator->next()) { - yield $iterator->key() => $iterator->current(); - } - }; + for (; $iterator->valid(); $iterator->next()) { + yield $iterator->key() => $iterator->current(); + } }; } } diff --git a/src/Operation/Slice.php b/src/Operation/Slice.php index 9a25439fa..61e062ff5 100644 --- a/src/Operation/Slice.php +++ b/src/Operation/Slice.php @@ -24,30 +24,28 @@ public function __invoke(): Closure /** * @psalm-return Closure(int=): Closure(Iterator): Generator */ - static function (int $offset): Closure { - return - /** - * @psalm-param int $length - * - * @psalm-return Closure(Iterator): Generator - */ - static function (int $length = -1) use ($offset): Closure { - /** @psalm-var Closure(Iterator): Generator $skip */ - $skip = Drop::of()($offset); + static fn (int $offset): Closure => + /** + * @psalm-param int $length + * + * @psalm-return Closure(Iterator): Generator + */ + static function (int $length = -1) use ($offset): Closure { + /** @psalm-var Closure(Iterator): Generator $skip */ + $skip = Drop::of()($offset); - if (-1 === $length) { - return $skip; - } + if (-1 === $length) { + return $skip; + } - /** @psalm-var Closure(Iterator): Generator $pipe */ - $pipe = Pipe::of()( - $skip, - Limit::of()($length)(0) - ); + /** @psalm-var Closure(Iterator): Generator $pipe */ + $pipe = Pipe::of()( + $skip, + Limit::of()($length)(0) + ); - // Point free style. - return $pipe; - }; - }; + // Point free style. + return $pipe; + }; } } diff --git a/src/Operation/Sort.php b/src/Operation/Sort.php index a9e8c99a3..ad9f27133 100644 --- a/src/Operation/Sort.php +++ b/src/Operation/Sort.php @@ -29,71 +29,63 @@ public function __invoke(): Closure /** * @psalm-return Closure(callable(T|TKey, T|TKey): int): Closure(Iterator): Generator */ - static function (int $type = Operation\Sortable::BY_VALUES): Closure { - return + static fn (int $type = Operation\Sortable::BY_VALUES): Closure => + /** + * @psalm-return Closure(Iterator): Generator + */ + static function (?callable $callback = null) use ($type): Closure { + $callback ??= /** - * @psalm-return Closure(Iterator): Generator + * @param mixed $left + * @psalm-param T|TKey $left + * + * @param mixed $right + * @psalm-param T|TKey $right */ - 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; - }; + static fn ($left, $right): int => $left <=> $right; - return + return + /** + * @psalm-param Iterator $iterator + * + * @psalm-return 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()], + ]; + + $sortCallback = /** - * @psalm-param Iterator $iterator + * @psalm-param callable(T|TKey, T|TKey): int $callback * - * @psalm-return Generator + * @psalm-return Closure(array{0:TKey|T, 1:T|TKey}, array{0:TKey|T, 1:T|TKey}): int */ - 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 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 (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]); - }; - }; + static fn (callable $callback): Closure => + /** + * @psalm-param array{0:TKey|T, 1:T|TKey} $left + * @psalm-param array{0:TKey|T, 1:T|TKey} $right + */ + static fn (array $left, array $right): int => $callback($left[1], $right[1]); - /** @psalm-var callable(Iterator): Generator | callable(Iterator): Generator $before */ - $before = Pipe::of()(...$operations['before']); + /** @psalm-var callable(Iterator): Generator | callable(Iterator): Generator $before */ + $before = Pipe::of()(...$operations['before']); - $arrayIterator = new ArrayIterator(iterator_to_array($before($iterator))); - $arrayIterator->uasort($sortCallback($callback)); + $arrayIterator = new ArrayIterator([...$before($iterator)]); + $arrayIterator->uasort($sortCallback($callback)); - return yield from Pipe::of()(...$operations['after'])($arrayIterator); - }; + return yield from Pipe::of()(...$operations['after'])($arrayIterator); }; - }; + }; } } diff --git a/src/Operation/Span.php b/src/Operation/Span.php index 1f66c32c3..e12f9de05 100644 --- a/src/Operation/Span.php +++ b/src/Operation/Span.php @@ -18,7 +18,7 @@ final class Span extends AbstractOperation { /** - * @psalm-return Closure((callable(T, TKey, Iterator): bool)): Closure(Iterator): Generator> + * @psalm-return Closure(callable(T , TKey , Iterator ): bool):Closure (Iterator): Generator> */ public function __invoke(): Closure { @@ -28,21 +28,13 @@ public function __invoke(): Closure * * @psalm-return Closure(Iterator): Generator> */ - static function (callable $callback): Closure { - return - /** - * @psalm-param Iterator $iterator - * - * @psalm-return Generator> - */ - static function (Iterator $iterator) use ($callback): Generator { - /** @psalm-var Iterator $takeWhile */ - $takeWhile = TakeWhile::of()($callback)($iterator); - /** @psalm-var Iterator $dropWhile */ - $dropWhile = DropWhile::of()($callback)($iterator); + static fn (callable $callback): Closure => static function (Iterator $iterator) use ($callback): Generator { + /** @psalm-var Iterator $takeWhile */ + $takeWhile = TakeWhile::of()($callback)($iterator); + /** @psalm-var Iterator $dropWhile */ + $dropWhile = DropWhile::of()($callback)($iterator); - return yield from [$takeWhile, $dropWhile]; - }; + return yield from [$takeWhile, $dropWhile]; }; } } diff --git a/src/Operation/Split.php b/src/Operation/Split.php index 00719243d..c4c22f71a 100644 --- a/src/Operation/Split.php +++ b/src/Operation/Split.php @@ -28,82 +28,70 @@ public function __invoke(): Closure /** * @psalm-return Closure((callable(T, TKey): bool)...): Closure(Iterator): Generator> */ - static function (int $type = Splitable::BEFORE): Closure { - return + static fn (int $type = Splitable::BEFORE): Closure => + /** + * @psalm-param callable(T, TKey): bool ...$callbacks + * + * @psalm-return Closure(Iterator): Generator> + */ + static fn (callable ...$callbacks): Closure => /** - * @psalm-param callable(T, TKey): bool ...$callbacks + * @psalm-param Iterator $iterator * - * @psalm-return Closure(Iterator): Generator> + * @psalm-return Generator> */ - static function (callable ...$callbacks) use ($type): Closure { - return + static function (Iterator $iterator) use ($type, $callbacks): Generator { + $carry = []; + + $reducer = /** - * @psalm-param Iterator $iterator + * @param mixed $key + * @psalm-param TKey $key * - * @psalm-return Generator> + * @psalm-return Closure(T): Closure(bool, callable(T, TKey): bool): bool */ - static function (Iterator $iterator) use ($type, $callbacks): Generator { - $carry = []; - - $reducer = + static fn ($key): Closure => + /** + * @param mixed $value + * @psalm-param T $value + * + * @psalm-return Closure(bool, callable(T, TKey): bool): bool + */ + static fn ($value): Closure => /** - * @psalm-param TKey $key - * - * @psalm-return Closure(T): Closure(bool, callable(T, TKey): bool): bool - * - * @param mixed $key + * @psalm-param callable(T, TKey): bool $callback */ - static function ($key): Closure { - return - /** - * @psalm-param T $value - * - * @psalm-return Closure(bool, callable(T, TKey): bool): bool - * - * @param mixed $value - */ - static function ($value) use ($key): Closure { - return - /** - * @psalm-param callable(T, TKey): bool $callback - */ - static function (bool $carry, callable $callback) use ($key, $value): bool { - return $callback($value, $key) || $carry; - }; - }; - }; + static fn (bool $carry, callable $callback): bool => $callback($value, $key) || $carry; - foreach ($iterator as $key => $value) { - $callbackReturn = array_reduce($callbacks, $reducer($key)($value), false); + foreach ($iterator as $key => $value) { + $callbackReturn = array_reduce($callbacks, $reducer($key)($value), false); - if (Splitable::AFTER === $type) { - $carry[] = $value; - } + if (Splitable::AFTER === $type) { + $carry[] = $value; + } - if (Splitable::REMOVE === $type && true === $callbackReturn) { - yield $carry; + if (Splitable::REMOVE === $type && true === $callbackReturn) { + yield $carry; - $carry = []; + $carry = []; - continue; - } + continue; + } - if (true === $callbackReturn && [] !== $carry) { - yield $carry; + if (true === $callbackReturn && [] !== $carry) { + yield $carry; - $carry = []; - } + $carry = []; + } - if (Splitable::BEFORE === $type || Splitable::REMOVE === $type) { - $carry[] = $value; - } - } + if (Splitable::BEFORE === $type || Splitable::REMOVE === $type) { + $carry[] = $value; + } + } - if ([] !== $carry) { - yield $carry; - } - }; + if ([] !== $carry) { + yield $carry; + } }; - }; } } diff --git a/src/Operation/Tails.php b/src/Operation/Tails.php index a62984120..6b06f1f80 100644 --- a/src/Operation/Tails.php +++ b/src/Operation/Tails.php @@ -17,7 +17,7 @@ final class Tails extends AbstractOperation { /** - * @psalm-return Closure(Iterator): Generator, mixed, void> + * @psalm-return Closure(Iterator): Generator, mixed, void> */ public function __invoke(): Closure { @@ -25,18 +25,18 @@ public function __invoke(): Closure /** * @psalm-param Iterator $iterator * - * @psalm-return Generator, mixed, void> + * @psalm-return Generator, mixed, void> */ static function (Iterator $iterator): Generator { /** @psalm-var Iterator $iterator */ $iterator = Pack::of()($iterator); - $data = iterator_to_array($iterator); + $data = [...$iterator]; while ([] !== $data) { /** @psalm-var Iterator $unpack */ $unpack = Unpack::of()(new ArrayIterator($data)); - yield iterator_to_array($unpack); + yield [...$unpack]; array_shift($data); } diff --git a/src/Operation/TakeWhile.php b/src/Operation/TakeWhile.php index 97c10c4c9..01cd280c2 100644 --- a/src/Operation/TakeWhile.php +++ b/src/Operation/TakeWhile.php @@ -19,7 +19,7 @@ final class TakeWhile extends AbstractOperation { /** - * @psalm-return Closure((callable(T, TKey, Iterator): bool)): Closure(Iterator): Generator + * @psalm-return Closure(callable(T , TKey , Iterator ): bool):Closure (Iterator): Generator */ public function __invoke(): Closure { @@ -29,70 +29,56 @@ public function __invoke(): Closure * * @psalm-return Closure(Iterator): Generator */ - static function (callable ...$callbacks): Closure { - return + static fn (callable ...$callbacks): Closure => + /** + * @psalm-param Iterator $iterator + * + * @psalm-return Generator + */ + static function (Iterator $iterator) use ($callbacks): Generator { + $reducerCallback = /** - * @psalm-param Iterator $iterator + * @param mixed $key + * @psalm-param TKey $key * - * @psalm-return Generator + * @psalm-return Closure(T): Closure(Iterator): Closure(bool, callable(T, TKey, Iterator): bool): bool */ - static function (Iterator $iterator) use ($callbacks): Generator { - $reducerCallback = + static fn ($key): Closure => + /** + * @param mixed $current + * @psalm-param T $current + * + * @psalm-return Closure(Iterator): Closure(bool, callable(T, TKey, Iterator): bool): bool + */ + static fn ($current): Closure => /** - * @psalm-param TKey $key - * - * @psalm-return Closure(T): Closure(Iterator): Closure(bool, callable(T, TKey, Iterator): bool): bool + * @psalm-param Iterator $iterator * - * @param mixed $key + * @psalm-return Closure(bool, callable(T, TKey, Iterator): bool): bool */ - static function ($key): Closure { - return - /** - * @psalm-param T $current - * - * @psalm-return Closure(Iterator): Closure(bool, callable(T, TKey, Iterator): bool): bool - * - * @param mixed $current - */ - static function ($current) use ($key): Closure { - return - /** - * @psalm-param Iterator $iterator - * - * @psalm-return Closure(bool, callable(T, TKey, Iterator): bool): bool - */ - static function (Iterator $iterator) use ($key, $current): Closure { - return - /** - * @psalm-param bool $carry - * @psalm-param callable(T, TKey, Iterator): bool $callable - */ - static function (bool $carry, callable $callable) use ($key, $current, $iterator): bool { - return ($callable($current, $key, $iterator)) ? - $carry : - false; - }; - }; - }; - }; + static fn (Iterator $iterator): Closure => + /** + * @psalm-param bool $carry + * @psalm-param callable(T, TKey, Iterator): bool $callable + */ + static fn (bool $carry, callable $callable): bool => ($callable($current, $key, $iterator)) ? $carry : false; - for (; $iterator->valid(); $iterator->next()) { - $key = $iterator->key(); - $current = $iterator->current(); + for (; $iterator->valid(); $iterator->next()) { + $key = $iterator->key(); + $current = $iterator->current(); - $result = array_reduce( - $callbacks, - $reducerCallback($key)($current)($iterator), - true - ); + $result = array_reduce( + $callbacks, + $reducerCallback($key)($current)($iterator), + true + ); - if (false === $result) { - break; - } + if (false === $result) { + break; + } - yield $key => $current; - } - }; + yield $key => $current; + } }; } } diff --git a/src/Operation/Times.php b/src/Operation/Times.php index 2f6d276a1..d3f56b93c 100644 --- a/src/Operation/Times.php +++ b/src/Operation/Times.php @@ -27,32 +27,26 @@ public function __invoke(): Closure /** * @psalm-return Closure(null|callable(int): int|T): Closure(null|Iterator): Generator */ - static function (int $number = 0): Closure { - return + static fn (int $number = 0): Closure => + /** + * @psalm-return Closure(null|Iterator): Generator + */ + static fn (?callable $callback = null): Closure => /** - * @psalm-return Closure(null|Iterator): Generator + * @psalm-param null|Iterator $iterator + * + * @psalm-return Generator */ - static function (?callable $callback = null) use ($number): Closure { - return - /** - * @psalm-param null|Iterator $iterator - * - * @psalm-return Generator - */ - static function (?Iterator $iterator = null) use ($number, $callback): Generator { - if (1 > $number) { - yield from []; - } + static function (?Iterator $iterator = null) use ($number, $callback): Generator { + if (1 > $number) { + yield from []; + } - $callback = $callback ?? static function (int $value): int { - return $value; - }; + $callback ??= static fn (int $value): int => $value; - for ($current = 1; $current <= $number; ++$current) { - yield $callback($current); - } - }; + for ($current = 1; $current <= $number; ++$current) { + yield $callback($current); + } }; - }; } } diff --git a/src/Operation/Truthy.php b/src/Operation/Truthy.php index bb6b6f864..d0e8f2459 100644 --- a/src/Operation/Truthy.php +++ b/src/Operation/Truthy.php @@ -25,18 +25,14 @@ public function __invoke(): Closure * @param mixed $value * @psalm-param T $value */ - static function ($value): bool { - return (bool) $value; - }; + static fn ($value): bool => (bool) $value; $dropWhileCallback = /** * @param mixed $value * @psalm-param T $value */ - static function ($value): bool { - return true === $value; - }; + static fn ($value): bool => true === $value; /** @psalm-var Closure(Iterator): Generator $pipe */ $pipe = Pipe::of()( diff --git a/src/Operation/Unfold.php b/src/Operation/Unfold.php index 4ac6a2aa3..62883e99e 100644 --- a/src/Operation/Unfold.php +++ b/src/Operation/Unfold.php @@ -29,26 +29,22 @@ public function __invoke(): Closure * * @psalm-return Closure(callable(T...): (array)): Closure(null|Iterator): Generator */ - static function (...$parameters): Closure { - return + static fn (...$parameters): Closure => + /** + * @psalm-param callable(T...): (array) $callback + * + * @psalm-return Closure(null|Iterator): Generator + */ + static fn (callable $callback): Closure => /** - * @psalm-param callable(T...): (array) $callback + * @psalm-param null|Iterator $iterator * - * @psalm-return Closure(null|Iterator): Generator + * @psalm-return Generator */ - static function (callable $callback) use ($parameters): Closure { - return - /** - * @psalm-param null|Iterator $iterator - * - * @psalm-return Generator - */ - static function (?Iterator $iterator = null) use ($parameters, $callback): Generator { - while (true) { - yield $parameters = $callback(...array_values((array) $parameters)); - } - }; + static function (?Iterator $iterator = null) use ($parameters, $callback): Generator { + while (true) { + yield $parameters = $callback(...array_values((array) $parameters)); + } }; - }; } } diff --git a/src/Operation/Unlines.php b/src/Operation/Unlines.php index 523f4f64d..3a30bc6e8 100644 --- a/src/Operation/Unlines.php +++ b/src/Operation/Unlines.php @@ -18,11 +18,11 @@ final class Unlines extends AbstractOperation { /** - * @psalm-return Closure(Iterator): Generator + * @psalm-return Closure(Iterator):(Generator) */ public function __invoke(): Closure { - /** @psalm-var Closure(Iterator): Generator $implode */ + /** @psalm-var Closure(Iterator):Generator $implode */ $implode = Implode::of()("\n"); // Point free style. diff --git a/src/Operation/Unpack.php b/src/Operation/Unpack.php index 905ac0c4b..2c95e580c 100644 --- a/src/Operation/Unpack.php +++ b/src/Operation/Unpack.php @@ -29,13 +29,9 @@ public function __invoke(): Closure * * @param mixed $value */ - static function ($value): bool { - return is_iterable($value); - }; + static fn ($value): bool => is_iterable($value); - $toIterableIterator = static function (iterable $value): IterableIterator { - return new IterableIterator($value); - }; + $toIterableIterator = static fn (iterable $value): IterableIterator => new IterableIterator($value); $callbackForKeys = /** @@ -46,9 +42,7 @@ static function ($value): bool { * * @param mixed $initial */ - static function ($initial, int $key, array $value) { - return $value[0]; - }; + static fn ($initial, int $key, array $value) => $value[0]; $callbackForValues = /** @@ -59,9 +53,7 @@ static function ($initial, int $key, array $value) { * * @param mixed $initial */ - static function ($initial, int $key, array $value) { - return $value[1]; - }; + static fn ($initial, int $key, array $value) => $value[1]; /** @psalm-var Closure(Iterator): Generator $pipe */ $pipe = Pipe::of()( diff --git a/src/Operation/Until.php b/src/Operation/Until.php index 087fe68e1..9315e6e0c 100644 --- a/src/Operation/Until.php +++ b/src/Operation/Until.php @@ -19,7 +19,7 @@ final class Until extends AbstractOperation { /** - * @psalm-return Closure((callable(T, TKey):bool)...): Closure(Iterator): Generator + * @psalm-return Closure(callable(T , TKey ): bool ...):Closure (Iterator): Generator */ public function __invoke(): Closure { @@ -29,67 +29,53 @@ public function __invoke(): Closure * * @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 { - $reducerCallback = + static fn (callable ...$callbacks): Closure => + /** + * @psalm-param Iterator $iterator + * + * @psalm-return Generator + */ + static function (Iterator $iterator) use ($callbacks): Generator { + $reducerCallback = + /** + * @param mixed $key + * @psalm-param TKey $key + * + * @psalm-return Closure(T): Closure(Iterator): Closure(bool, callable(T, TKey, Iterator): bool): bool + */ + static fn ($key): Closure => /** - * @psalm-param TKey $key + * @param mixed $current + * @psalm-param T $current * - * @psalm-return Closure(T): Closure(Iterator): Closure(bool, callable(T, TKey, Iterator): bool): bool - * - * @param mixed $key + * @psalm-return Closure(Iterator): Closure(bool, callable(T, TKey, Iterator): bool): bool */ - static function ($key): Closure { - return + static fn ($current): Closure => + /** + * @psalm-param Iterator $iterator + * + * @psalm-return Closure(bool, callable(T, TKey, Iterator): bool): bool + */ + static fn (Iterator $iterator): Closure => /** - * @psalm-param T $current - * - * @psalm-return Closure(Iterator): Closure(bool, callable(T, TKey, Iterator): bool): bool - * - * @param mixed $current + * @psalm-param bool $carry + * @psalm-param callable(T, TKey, Iterator): bool $callable */ - static function ($current) use ($key): Closure { - return - /** - * @psalm-param Iterator $iterator - * - * @psalm-return Closure(bool, callable(T, TKey, Iterator): bool): bool - */ - static function (Iterator $iterator) use ($key, $current): Closure { - return - /** - * @psalm-param bool $carry - * @psalm-param callable(T, TKey, Iterator): bool $callable - */ - static function (bool $carry, callable $callable) use ($key, $current, $iterator): bool { - return ($callable($current, $key, $iterator)) ? - $carry : - false; - }; - }; - }; - }; + static fn (bool $carry, callable $callable): bool => ($callable($current, $key, $iterator)) ? $carry : false; - foreach ($iterator as $key => $current) { - yield $key => $current; + foreach ($iterator as $key => $current) { + yield $key => $current; - $result = array_reduce( - $callbacks, - $reducerCallback($key)($current)($iterator), - true - ); + $result = array_reduce( + $callbacks, + $reducerCallback($key)($current)($iterator), + true + ); - if (false !== $result) { - break; - } + if (false !== $result) { + break; } - }; - }; + } + }; } } diff --git a/src/Operation/Unwindow.php b/src/Operation/Unwindow.php index e038f7038..46da9b285 100644 --- a/src/Operation/Unwindow.php +++ b/src/Operation/Unwindow.php @@ -27,9 +27,7 @@ public function __invoke(): Closure /** * @psalm-param iterable> $value */ - static function (iterable $iterable): IterableIterator { - return new IterableIterator($iterable); - } + static fn (iterable $iterable): IterableIterator => new IterableIterator($iterable) ), Map::of()( /** @@ -50,9 +48,7 @@ static function (IterableIterator $iterator): Iterator { * * @psalm-return T */ - static function (Generator $value) { - return $value->current(); - } + static fn (Generator $value) => $value->current() ) ); diff --git a/src/Operation/Unwords.php b/src/Operation/Unwords.php index 58cf40558..39d274714 100644 --- a/src/Operation/Unwords.php +++ b/src/Operation/Unwords.php @@ -18,11 +18,11 @@ final class Unwords extends AbstractOperation { /** - * @psalm-return Closure(Iterator): Generator + * @psalm-return Closure(Iterator):(Generator) */ public function __invoke(): Closure { - /** @psalm-var Closure(Iterator): Generator $implode */ + /** @psalm-var Closure(Iterator):Generator $implode */ $implode = Implode::of()(' '); // Point free style. diff --git a/src/Operation/Window.php b/src/Operation/Window.php index 041bf40b7..80e710e87 100644 --- a/src/Operation/Window.php +++ b/src/Operation/Window.php @@ -26,31 +26,29 @@ public function __invoke(): Closure /** * @psalm-return Closure(Iterator): Generator> */ - static function (int $size): Closure { - return - /** - * @psalm-param Iterator $iterator - * - * @psalm-return Generator> - */ - static function (Iterator $iterator) use ($size): Generator { - if (0 === $size) { - return yield from $iterator; - } - - ++$size; - $size *= -1; - - /** @psalm-var list $stack */ - $stack = []; - - for (; $iterator->valid(); $iterator->next()) { - // @todo Should we use Pack ? - $stack[$iterator->key()] = $iterator->current(); - - yield $iterator->key() => array_slice($stack, $size); - } - }; - }; + static fn (int $size): Closure => + /** + * @psalm-param Iterator $iterator + * + * @psalm-return Generator> + */ + static function (Iterator $iterator) use ($size): Generator { + if (0 === $size) { + return yield from $iterator; + } + + ++$size; + $size *= -1; + + /** @psalm-var list $stack */ + $stack = []; + + for (; $iterator->valid(); $iterator->next()) { + // @todo Should we use Pack ? + $stack[$iterator->key()] = $iterator->current(); + + yield $iterator->key() => array_slice($stack, $size); + } + }; } } diff --git a/src/Operation/Words.php b/src/Operation/Words.php index 7b817e755..3294fd366 100644 --- a/src/Operation/Words.php +++ b/src/Operation/Words.php @@ -22,9 +22,7 @@ final class Words extends AbstractOperation */ public function __invoke(): Closure { - $mapCallback = static function (array $value): string { - return implode('', $value); - }; + $mapCallback = static fn (array $value): string => implode('', $value); /** @psalm-var Closure(Iterator): Generator $pipe */ $pipe = Pipe::of()( diff --git a/src/Operation/Wrap.php b/src/Operation/Wrap.php index 22073fefd..912517417 100644 --- a/src/Operation/Wrap.php +++ b/src/Operation/Wrap.php @@ -30,9 +30,7 @@ public function __invoke(): Closure * * @psalm-return array */ - static function ($value, $key): array { - return [$key => $value]; - }; + static fn ($value, $key): array => [$key => $value]; /** @psalm-var Closure(Iterator): Generator> $pipe */ $pipe = Pipe::of()( diff --git a/src/Operation/Zip.php b/src/Operation/Zip.php index 61d27869e..17fe43ee3 100644 --- a/src/Operation/Zip.php +++ b/src/Operation/Zip.php @@ -14,6 +14,8 @@ * @psalm-template TKey * @psalm-template TKey of array-key * @psalm-template T + * + * phpcs:disable Generic.Files.LineLength.TooLong */ final class Zip extends AbstractOperation { @@ -28,25 +30,17 @@ public function __invoke(): Closure * * @psalm-return Closure(Iterator): Generator> */ - static function (iterable ...$iterables): Closure { - return - /** - * @psalm-param Iterator $iterator - * - * @psalm-return Generator> - */ - static function (Iterator $iterator) use ($iterables): Generator { - $mit = new MultipleIterator(MultipleIterator::MIT_NEED_ANY); - $mit->attachIterator($iterator); + static fn (iterable ...$iterables): Closure => static function (Iterator $iterator) use ($iterables): Generator { + $mit = new MultipleIterator(MultipleIterator::MIT_NEED_ANY); + $mit->attachIterator($iterator); - foreach ($iterables as $iterableIterator) { - $mit->attachIterator(new IterableIterator($iterableIterator)); - } + foreach ($iterables as $iterableIterator) { + $mit->attachIterator(new IterableIterator($iterableIterator)); + } - foreach ($mit as $values) { - yield $values; - } - }; + foreach ($mit as $values) { + yield $values; + } }; } }