From ed5613a4a02a144928caa17f88debda9c87854de Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Sat, 31 Jul 2021 10:19:26 +0200 Subject: [PATCH 1/5] SA: Update Partition operation. --- src/Collection.php | 16 ++++++++++------ src/Operation/Partition.php | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Collection.php b/src/Collection.php index 7b6dfb80c..a9fccf255 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -702,15 +702,19 @@ public function pair(): CollectionInterface public function partition(callable ...$callbacks): CollectionInterface { // TODO: Move this docblock above closure when https://github.com/phpstan/phpstan/issues/3770 lands. - $mapCallback = static function (array $partitionResult): CollectionInterface { + $mapCallback = /** - * @var Closure(Iterator): Generator $callback - * @var array{0: Iterator} $parameters + * @param array{0: (Closure(Iterator): Generator), 1: (array{0: Iterator})} $partitionResult */ - [$callback, $parameters] = $partitionResult; + static function (array $partitionResult): CollectionInterface { + /** + * @var Closure(Iterator): Generator $callback + * @var array{0: Iterator} $parameters + */ + [$callback, $parameters] = $partitionResult; - return self::fromCallable($callback, $parameters); - }; + return self::fromCallable($callback, $parameters); + }; return new self(Pipe::of()(Partition::of()(...$callbacks), Map::of()($mapCallback)), [$this->getIterator()]); } diff --git a/src/Operation/Partition.php b/src/Operation/Partition.php index 46f111470..550d1a7e4 100644 --- a/src/Operation/Partition.php +++ b/src/Operation/Partition.php @@ -49,7 +49,7 @@ static function (Iterator $iterator) use ($callbacks): Generator { /** @var array{0: Closure(Iterator): Generator, 1: array{0: Iterator}} $reject */ $reject = [Reject::of()(...$callbacks), [$iterator]]; - yield from [$filter, $reject]; + return yield from [$filter, $reject]; }; } } From a2190afacbec1e6d0d18fe3119e4685cd969ae6d Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Sat, 31 Jul 2021 10:19:34 +0200 Subject: [PATCH 2/5] SA: Update Span operation. --- src/Collection.php | 16 ++++++++++------ src/Operation/Span.php | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Collection.php b/src/Collection.php index a9fccf255..7a6e48b70 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -851,15 +851,19 @@ public function sort(int $type = Operation\Sortable::BY_VALUES, ?callable $callb public function span(callable ...$callbacks): CollectionInterface { // TODO: Move this docblock above closure when https://github.com/phpstan/phpstan/issues/3770 lands. - $mapCallback = static function (array $spanResult): CollectionInterface { + $mapCallback = /** - * @var Closure(Iterator): Generator $callback - * @var array{0: Iterator} $parameters + * @param array{0: (Closure(Iterator): Generator), 1: (array{0: Iterator})} $spanResult */ - [$callback, $parameters] = $spanResult; + static function (array $spanResult): CollectionInterface { + /** + * @var Closure(Iterator): Generator $callback + * @var array{0: Iterator} $parameters + */ + [$callback, $parameters] = $spanResult; - return self::fromCallable($callback, $parameters); - }; + return self::fromCallable($callback, $parameters); + }; return new self(Pipe::of()(Span::of()(...$callbacks), Map::of()($mapCallback)), [$this->getIterator()]); } diff --git a/src/Operation/Span.php b/src/Operation/Span.php index 71db9c3a5..4e82ed247 100644 --- a/src/Operation/Span.php +++ b/src/Operation/Span.php @@ -49,7 +49,7 @@ static function (Iterator $iterator) use ($callbacks): Generator { /** @var array{0: Closure(Iterator): Generator, 1: array{0: Iterator}} $dropWhile */ $dropWhile = [DropWhile::of()(...$callbacks), [$iterator]]; - yield from [$takeWhile, $dropWhile]; + return yield from [$takeWhile, $dropWhile]; }; } } From 56dd3f168e3fa6518da17dbb55a5d332a73ab950 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Sat, 31 Jul 2021 10:19:42 +0200 Subject: [PATCH 3/5] SA: Update Transpose operation. --- src/Operation/Transpose.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Operation/Transpose.php b/src/Operation/Transpose.php index 61980a2f1..e82d0e74a 100644 --- a/src/Operation/Transpose.php +++ b/src/Operation/Transpose.php @@ -46,7 +46,7 @@ static function (Iterator $iterator): Generator { $callbackForKeys = /** * @param array $carry - * @param array $key + * @param non-empty-array $key * * @return TKey */ From 74c701d636f5eb2ef8b2212b6fb31028690c601e Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Sat, 31 Jul 2021 10:19:51 +0200 Subject: [PATCH 4/5] SA: Update Unpair operation. --- src/Operation/Unpair.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Operation/Unpair.php b/src/Operation/Unpair.php index 077f3c6f3..a4d3edb5d 100644 --- a/src/Operation/Unpair.php +++ b/src/Operation/Unpair.php @@ -24,7 +24,7 @@ final class Unpair extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Generator + * @return Closure(Iterator): Generator> */ public function __invoke(): Closure { @@ -32,7 +32,7 @@ public function __invoke(): Closure /** * @param Iterator $iterator * - * @return Generator + * @return Generator> */ static function (Iterator $iterator): Generator { foreach ($iterator as $key => $value) { From 2f9d4aa17da23f925ca05eba22f855d84a7ee085 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Sat, 31 Jul 2021 19:00:03 +0200 Subject: [PATCH 5/5] Fix Psalm errors. --- tests/static-analysis/scanLeft.php | 5 +++-- tests/static-analysis/scanRight.php | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/static-analysis/scanLeft.php b/tests/static-analysis/scanLeft.php index f74d94d2c..9c30fe89a 100644 --- a/tests/static-analysis/scanLeft.php +++ b/tests/static-analysis/scanLeft.php @@ -13,7 +13,8 @@ use loophp\collection\Contract\Collection as CollectionInterface; $sum = static fn (int $carry, int $value): int => $carry + $value; -$concat = static fn (?string $carry, string $string): string => sprintf('%s%s', (string) $carry, $string); +$concat = static fn (string $carry, string $string): string => sprintf('%s%s', $carry, $string); +$concatWithNull = static fn (?string $carry, string $string): string => sprintf('%s%s', (string) $carry, $string); $toString = /** * @param bool|string $carry @@ -50,5 +51,5 @@ function scanLeft_checkListOfSize1String(CollectionInterface $collection): void scanLeft_checkListInt(Collection::fromIterable([1, 2, 3])->scanLeft($sum, 5)); scanLeft_checkListString(Collection::fromIterable(range('a', 'c'))->scanLeft($concat, '')); -scanLeft_checkListStringWithNull(Collection::fromIterable(range('a', 'c'))->scanLeft($concat)); +scanLeft_checkListStringWithNull(Collection::fromIterable(range('a', 'c'))->scanLeft($concatWithNull)); scanLeft_checkListOfSize1String(Collection::fromIterable([10])->scanLeft($toString, true)); diff --git a/tests/static-analysis/scanRight.php b/tests/static-analysis/scanRight.php index 7afbb4aa4..57edc0285 100644 --- a/tests/static-analysis/scanRight.php +++ b/tests/static-analysis/scanRight.php @@ -13,7 +13,8 @@ use loophp\collection\Contract\Collection as CollectionInterface; $sum = static fn (int $carry, int $value): int => $carry + $value; -$concat = static fn (?string $carry, string $string): string => sprintf('%s%s', (string) $carry, $string); +$concat = static fn (string $carry, string $string): string => sprintf('%s%s', $carry, $string); +$concatWithNull = static fn (?string $carry, string $string): string => sprintf('%s%s', (string) $carry, $string); $toString = /** * @param bool|string $carry @@ -50,5 +51,5 @@ function scanRight_checkListOfSize1String(CollectionInterface $collection): void scanRight_checkListInt(Collection::fromIterable([1, 2, 3])->scanRight($sum, 5)); scanRight_checkListString(Collection::fromIterable(range('a', 'c'))->scanRight($concat, '')); -scanRight_checkListStringWithNull(Collection::fromIterable(range('a', 'c'))->scanRight($concat)); +scanRight_checkListStringWithNull(Collection::fromIterable(range('a', 'c'))->scanRight($concatWithNull)); scanRight_checkListOfSize1String(Collection::fromIterable([10])->scanRight($toString, true));