Skip to content

Commit

Permalink
Fix: Prevent warning when using odd amount of items. (#154)
Browse files Browse the repository at this point in the history
* Fix: Prevent warning when using odd amount of item.

* Add tests.
  • Loading branch information
drupol authored Jul 18, 2021
1 parent 98b90fe commit cc52f3d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 12 additions & 0 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2254,6 +2254,18 @@ public function it_can_pair(): void
->unwrap()
->pair()
->shouldIterateAs($gen());

$input = ['a', 'b', 'c'];

$gen = static function () {
yield 'a' => 'b';

yield 'c' => null;
};

$this::fromIterable($input)
->pair()
->shouldIterateAs($gen());
}

public function it_can_partition(): void
Expand Down
8 changes: 4 additions & 4 deletions src/Operation/Pair.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ public function __invoke(): Closure
* @param TKey $key
* @param array{0: TKey, 1: T} $value
*
* @return T|TKey
* @return T|TKey|null
*/
static fn ($initial, $key, array $value) => $value[0];
static fn ($initial, $key, array $value) => $value[0] ?? null;

$callbackForValues =
/**
* @param T $initial
* @param TKey $key
* @param array{0: TKey, 1: T} $value
*
* @return T|TKey
* @return T|TKey|null
*/
static fn ($initial, $key, array $value) => $value[1];
static fn ($initial, $key, array $value) => $value[1] ?? null;

/** @var Closure(Iterator<TKey, T>): Generator<T|TKey, T> $pipe */
$pipe = Pipe::of()(
Expand Down

0 comments on commit cc52f3d

Please sign in to comment.