Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SA: Update operations. #165

Merged
merged 5 commits into from
Jul 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<TKey, T>): Generator<TKey, T> $callback
* @var array{0: Iterator<TKey, T>} $parameters
* @param array{0: (Closure(Iterator<TKey, T>): Generator<TKey, T>), 1: (array{0: Iterator<TKey, T>})} $partitionResult
*/
[$callback, $parameters] = $partitionResult;
static function (array $partitionResult): CollectionInterface {
/**
* @var Closure(Iterator<TKey, T>): Generator<TKey, T> $callback
* @var array{0: Iterator<TKey, T>} $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()]);
}
Expand Down Expand Up @@ -847,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<TKey, T>): Generator<TKey, T> $callback
* @var array{0: Iterator<TKey, T>} $parameters
* @param array{0: (Closure(Iterator<TKey, T>): Generator<TKey, T>), 1: (array{0: Iterator<TKey, T>})} $spanResult
*/
[$callback, $parameters] = $spanResult;
static function (array $spanResult): CollectionInterface {
/**
* @var Closure(Iterator<TKey, T>): Generator<TKey, T> $callback
* @var array{0: Iterator<TKey, T>} $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()]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Partition.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static function (Iterator $iterator) use ($callbacks): Generator {
/** @var array{0: Closure(Iterator<TKey, T>): Generator<TKey, T>, 1: array{0: Iterator<TKey, T>}} $reject */
$reject = [Reject::of()(...$callbacks), [$iterator]];

yield from [$filter, $reject];
return yield from [$filter, $reject];
};
}
}
2 changes: 1 addition & 1 deletion src/Operation/Span.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static function (Iterator $iterator) use ($callbacks): Generator {
/** @var array{0: Closure(Iterator<TKey, T>): Generator<TKey, T>, 1: array{0: Iterator<TKey, T>}} $dropWhile */
$dropWhile = [DropWhile::of()(...$callbacks), [$iterator]];

yield from [$takeWhile, $dropWhile];
return yield from [$takeWhile, $dropWhile];
};
}
}
2 changes: 1 addition & 1 deletion src/Operation/Transpose.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static function (Iterator $iterator): Generator {
$callbackForKeys =
/**
* @param array $carry
* @param array<int, TKey> $key
* @param non-empty-array<int, TKey> $key
*
* @return TKey
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Operation/Unpair.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ final class Unpair extends AbstractOperation
/**
* @pure
*
* @return Closure(Iterator<TKey, T>): Generator<int, array{TKey, T}>
* @return Closure(Iterator<TKey, T>): Generator<int, array<TKey, T>>
*/
public function __invoke(): Closure
{
return
/**
* @param Iterator<TKey, T> $iterator
*
* @return Generator<int, array{TKey, T}>
* @return Generator<int, array<TKey, T>>
*/
static function (Iterator $iterator): Generator {
foreach ($iterator as $key => $value) {
Expand Down
5 changes: 3 additions & 2 deletions tests/static-analysis/scanLeft.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
5 changes: 3 additions & 2 deletions tests/static-analysis/scanRight.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));