Skip to content

Commit 3ba0be6

Browse files
authored
Merge pull request #79 from boesing/feature/typed-merge-stack
feature: introduce typed merge stack
2 parents d2ed907 + 2d9967c commit 3ba0be6

6 files changed

+36
-12
lines changed

src/Map.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,16 @@ public function __construct(array $data = [])
4646
parent::__construct($data);
4747
}
4848

49-
public function merge(...$stack): MapInterface
49+
public function merge(MapInterface ...$stack): MapInterface
5050
{
5151
$instance = clone $this;
52-
foreach ($stack as $list) {
53-
$instance->data = array_merge($instance->data, $list->toNativeArray());
52+
$merges = [];
53+
foreach ($stack as $map) {
54+
$merges[] = $map->toNativeArray();
5455
}
5556

57+
$instance->data = array_merge($instance->data, ...$merges);
58+
5659
return $instance;
5760
}
5861

src/MapInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function sort(?callable $callback = null): MapInterface;
3232
* @psalm-param list<MapInterface<TKey,TValue>> $stack
3333
* @psalm-return MapInterface<TKey,TValue>
3434
*/
35-
public function merge(...$stack): MapInterface;
35+
public function merge(MapInterface ...$stack): MapInterface;
3636

3737
/**
3838
* @psalm-param MapInterface<TKey,TValue> $other

src/OrderedList.php

+6-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
use function array_key_exists;
1313
use function array_keys;
14-
use function array_map;
1514
use function array_merge;
1615
use function array_replace;
1716
use function array_reverse;
@@ -46,14 +45,14 @@ final public function __construct(array $data = [])
4645
parent::__construct($data);
4746
}
4847

49-
public function merge(...$stack): OrderedListInterface
48+
public function merge(OrderedListInterface ...$stack): OrderedListInterface
5049
{
5150
$instance = clone $this;
52-
$values = array_map(static function (OrderedListInterface $list): array {
53-
return $list->toNativeArray();
54-
}, $stack);
55-
56-
$instance->data = array_values(array_merge($instance->data, ...$values));
51+
foreach ($stack as $list) {
52+
foreach ($list->toNativeArray() as $value) {
53+
$instance->data[] = $value;
54+
}
55+
}
5756

5857
return $instance;
5958
}

src/OrderedListInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function sort(?callable $callback = null): OrderedListInterface;
4444
* @psalm-param list<OrderedListInterface<TValue>> $stack
4545
* @psalm-return OrderedListInterface<TValue>
4646
*/
47-
public function merge(...$stack): OrderedListInterface;
47+
public function merge(OrderedListInterface ...$stack): OrderedListInterface;
4848

4949
/**
5050
* @template TNewValue

tests/GenericMapTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,14 @@ public function mergeStacks(): Generator
185185
['baz' => 'bar', 'qoo' => 'ooq'],
186186
],
187187
];
188+
189+
yield 'none' => [
190+
['foo' => 'bar'],
191+
['foo' => 'bar'],
192+
[
193+
[],
194+
],
195+
];
188196
}
189197

190198
/**

tests/GenericOrderedListTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,20 @@ public function mergeStacks(): Generator
164164
['baz', 'foo'],
165165
],
166166
];
167+
168+
yield 'none' => [
169+
[
170+
'foo',
171+
'bar',
172+
],
173+
[
174+
'foo',
175+
'bar',
176+
],
177+
[
178+
[],
179+
],
180+
];
167181
}
168182

169183
public function testWillMapByUsingCallable(): void

0 commit comments

Comments
 (0)