Skip to content

Commit a434de4

Browse files
authored
Merge pull request #272 from boesing/1.3.x-merge-up-into-2.0.x_dFYQU2LB
Merge release 1.3.0 into 2.0.x
2 parents 1e3e8b7 + a295bff commit a434de4

9 files changed

+66
-69
lines changed

.github/dependabot.yml

-16
This file was deleted.

.github/workflows/auto-merge-dependabot.yml

-21
This file was deleted.

renovate.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
3+
"extends": [
4+
"local>boesing/.github:renovate-config"
5+
]
6+
}

src/Map.php

+15-14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use function array_values;
2121
use function asort;
2222
use function implode;
23+
use function is_array;
2324
use function sprintf;
2425
use function strcmp;
2526
use function uasort;
@@ -375,16 +376,10 @@ public function partition(callable $callback): array
375376
return [$instance1, $instance2];
376377
}
377378

378-
/**
379-
* @template TGroup of non-empty-string
380-
* @psalm-param callable(TValue):TGroup $callback
381-
*
382-
* @psalm-return MapInterface<TGroup,MapInterface<TKey,TValue>>
383-
*/
384379
public function group(callable $callback): MapInterface
385380
{
386381
/**
387-
* @psalm-var MapInterface<TGroup,MapInterface<TKey,TValue>> $groups
382+
* @psalm-var MapInterface<non-empty-string,MapInterface<TKey,TValue>> $groups
388383
*/
389384
$groups = new GenericMap([]);
390385

@@ -393,15 +388,21 @@ public function group(callable $callback): MapInterface
393388
* @psalm-suppress ImpureFunctionCall Upstream projects have to ensure that they do not manipulate the
394389
* value here.
395390
*/
396-
$groupIdentifier = $callback($value);
397-
try {
398-
$group = $groups->get($groupIdentifier);
399-
} catch (OutOfBoundsException) {
400-
$group = clone $this;
401-
$group->data = [];
391+
$groupIdentifiers = $callback($value);
392+
if (! is_array($groupIdentifiers)) {
393+
$groupIdentifiers = [$groupIdentifiers];
402394
}
403395

404-
$groups = $groups->put($groupIdentifier, $group->put($key, $value));
396+
foreach ($groupIdentifiers as $groupIdentifier) {
397+
try {
398+
$group = $groups->get($groupIdentifier);
399+
} catch (OutOfBoundsException) {
400+
$group = clone $this;
401+
$group->data = [];
402+
}
403+
404+
$groups = $groups->put($groupIdentifier, $group->put($key, $value));
405+
}
405406
}
406407

407408
return $groups;

src/MapInterface.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,10 @@ public function partition(callable $callback): array;
215215
/**
216216
* Groups the items of this object by using the callback.
217217
*
218-
* @template TGroup of non-empty-string
218+
* @template TGroup of non-empty-string|non-empty-list<non-empty-string>
219219
* @psalm-param callable(TValue):TGroup $callback
220220
*
221-
* @psalm-return MapInterface<TGroup,MapInterface<TKey,TValue>>
221+
* @psalm-return MapInterface<non-empty-string,MapInterface<TKey,TValue>>
222222
*/
223223
public function group(callable $callback): MapInterface;
224224

src/OrderedList.php

+15-14
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use function count;
2424
use function hash;
2525
use function implode;
26+
use function is_array;
2627
use function is_callable;
2728
use function serialize;
2829
use function shuffle;
@@ -369,30 +370,30 @@ public function partition(callable $callback): array
369370
return [$instance1, $instance2];
370371
}
371372

372-
/**
373-
* @template TGroup of non-empty-string
374-
* @psalm-param callable(TValue):TGroup $callback
375-
*
376-
* @psalm-return MapInterface<TGroup,OrderedListInterface<TValue>>
377-
*/
378373
public function group(callable $callback): MapInterface
379374
{
380-
/** @var MapInterface<TGroup,OrderedListInterface<TValue>> $groups */
375+
/** @var MapInterface<non-empty-string,OrderedListInterface<TValue>> $groups */
381376
$groups = new GenericMap([]);
382377
foreach ($this as $value) {
383378
/**
384379
* @psalm-suppress ImpureFunctionCall Upstream projects have to ensure that they do not manipulate the
385380
* value here.
386381
*/
387-
$groupName = $callback($value);
388-
if (! $groups->has($groupName)) {
389-
$groups = $groups->put($groupName, new GenericOrderedList([$value]));
390-
continue;
382+
$groupNames = $callback($value);
383+
if (! is_array($groupNames)) {
384+
$groupNames = [$groupNames];
391385
}
392386

393-
$existingGroup = $groups->get($groupName);
394-
$existingGroup = $existingGroup->add($value);
395-
$groups = $groups->put($groupName, $existingGroup);
387+
foreach ($groupNames as $groupName) {
388+
if (! $groups->has($groupName)) {
389+
$groups = $groups->put($groupName, new GenericOrderedList([$value]));
390+
continue;
391+
}
392+
393+
$existingGroup = $groups->get($groupName);
394+
$existingGroup = $existingGroup->add($value);
395+
$groups = $groups->put($groupName, $existingGroup);
396+
}
396397
}
397398

398399
return $groups;

src/OrderedListInterface.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,10 @@ public function partition(callable $callback): array;
182182
/**
183183
* Groups the items by using the callback.
184184
*
185-
* @template TGroup of non-empty-string
185+
* @template TGroup of non-empty-string|non-empty-list<non-empty-string>
186186
* @psalm-param callable(TValue):TGroup $callback
187187
*
188-
* @psalm-return MapInterface<TGroup,OrderedListInterface<TValue>>
188+
* @psalm-return MapInterface<non-empty-string,OrderedListInterface<TValue>>
189189
*/
190190
public function group(callable $callback): MapInterface;
191191

tests/GenericMapTest.php

+13
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,19 @@ public function testWillGroupValuesToNewInstancesOfInitialInstance(): void
736736
self::assertEquals($object2, $b->get('bar'));
737737
}
738738

739+
public function testWillGroupValueIntoMultipleGroups(): void
740+
{
741+
$map = new GenericMap([
742+
'foo' => $object1 = new GenericObject(1),
743+
]);
744+
745+
$grouped = $map->group(fn () => ['a', 'b']);
746+
self::assertTrue($grouped->has('a'));
747+
self::assertTrue($grouped->has('b'));
748+
self::assertTrue($grouped->get('a')->contains($object1));
749+
self::assertTrue($grouped->get('b')->contains($object1));
750+
}
751+
739752
/**
740753
* @template T
741754
* @psalm-param array<string,T> $elements

tests/GenericOrderedListTest.php

+13
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,19 @@ public function testWillGroupValuesToNewInstancesOfInitialInstance(): void
10491049
self::assertEquals($object2, $b->at(0));
10501050
}
10511051

1052+
public function testWillGroupValueIntoMultipleGroups(): void
1053+
{
1054+
$list = new GenericOrderedList([
1055+
$object1 = new GenericObject(1),
1056+
]);
1057+
1058+
$grouped = $list->group(fn () => ['a', 'b']);
1059+
self::assertTrue($grouped->has('a'));
1060+
self::assertTrue($grouped->has('b'));
1061+
self::assertTrue($grouped->get('a')->contains($object1));
1062+
self::assertTrue($grouped->get('b')->contains($object1));
1063+
}
1064+
10521065
/**
10531066
* @template T
10541067
* @psalm-param list<T> $data

0 commit comments

Comments
 (0)