Skip to content

Commit

Permalink
Merge pull request #272 from boesing/1.3.x-merge-up-into-2.0.x_dFYQU2LB
Browse files Browse the repository at this point in the history
Merge release 1.3.0 into 2.0.x
  • Loading branch information
boesing authored Oct 22, 2024
2 parents 1e3e8b7 + a295bff commit a434de4
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 69 deletions.
16 changes: 0 additions & 16 deletions .github/dependabot.yml

This file was deleted.

21 changes: 0 additions & 21 deletions .github/workflows/auto-merge-dependabot.yml

This file was deleted.

6 changes: 6 additions & 0 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"local>boesing/.github:renovate-config"
]
}
29 changes: 15 additions & 14 deletions src/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use function array_values;
use function asort;
use function implode;
use function is_array;
use function sprintf;
use function strcmp;
use function uasort;
Expand Down Expand Up @@ -375,16 +376,10 @@ public function partition(callable $callback): array
return [$instance1, $instance2];
}

/**
* @template TGroup of non-empty-string
* @psalm-param callable(TValue):TGroup $callback
*
* @psalm-return MapInterface<TGroup,MapInterface<TKey,TValue>>
*/
public function group(callable $callback): MapInterface
{
/**
* @psalm-var MapInterface<TGroup,MapInterface<TKey,TValue>> $groups
* @psalm-var MapInterface<non-empty-string,MapInterface<TKey,TValue>> $groups
*/
$groups = new GenericMap([]);

Expand All @@ -393,15 +388,21 @@ public function group(callable $callback): MapInterface
* @psalm-suppress ImpureFunctionCall Upstream projects have to ensure that they do not manipulate the
* value here.
*/
$groupIdentifier = $callback($value);
try {
$group = $groups->get($groupIdentifier);
} catch (OutOfBoundsException) {
$group = clone $this;
$group->data = [];
$groupIdentifiers = $callback($value);
if (! is_array($groupIdentifiers)) {
$groupIdentifiers = [$groupIdentifiers];
}

$groups = $groups->put($groupIdentifier, $group->put($key, $value));
foreach ($groupIdentifiers as $groupIdentifier) {
try {
$group = $groups->get($groupIdentifier);
} catch (OutOfBoundsException) {
$group = clone $this;
$group->data = [];
}

$groups = $groups->put($groupIdentifier, $group->put($key, $value));
}
}

return $groups;
Expand Down
4 changes: 2 additions & 2 deletions src/MapInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,10 @@ public function partition(callable $callback): array;
/**
* Groups the items of this object by using the callback.
*
* @template TGroup of non-empty-string
* @template TGroup of non-empty-string|non-empty-list<non-empty-string>
* @psalm-param callable(TValue):TGroup $callback
*
* @psalm-return MapInterface<TGroup,MapInterface<TKey,TValue>>
* @psalm-return MapInterface<non-empty-string,MapInterface<TKey,TValue>>
*/
public function group(callable $callback): MapInterface;

Expand Down
29 changes: 15 additions & 14 deletions src/OrderedList.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use function count;
use function hash;
use function implode;
use function is_array;
use function is_callable;
use function serialize;
use function shuffle;
Expand Down Expand Up @@ -369,30 +370,30 @@ public function partition(callable $callback): array
return [$instance1, $instance2];
}

/**
* @template TGroup of non-empty-string
* @psalm-param callable(TValue):TGroup $callback
*
* @psalm-return MapInterface<TGroup,OrderedListInterface<TValue>>
*/
public function group(callable $callback): MapInterface
{
/** @var MapInterface<TGroup,OrderedListInterface<TValue>> $groups */
/** @var MapInterface<non-empty-string,OrderedListInterface<TValue>> $groups */
$groups = new GenericMap([]);
foreach ($this as $value) {
/**
* @psalm-suppress ImpureFunctionCall Upstream projects have to ensure that they do not manipulate the
* value here.
*/
$groupName = $callback($value);
if (! $groups->has($groupName)) {
$groups = $groups->put($groupName, new GenericOrderedList([$value]));
continue;
$groupNames = $callback($value);
if (! is_array($groupNames)) {
$groupNames = [$groupNames];
}

$existingGroup = $groups->get($groupName);
$existingGroup = $existingGroup->add($value);
$groups = $groups->put($groupName, $existingGroup);
foreach ($groupNames as $groupName) {
if (! $groups->has($groupName)) {
$groups = $groups->put($groupName, new GenericOrderedList([$value]));
continue;
}

$existingGroup = $groups->get($groupName);
$existingGroup = $existingGroup->add($value);
$groups = $groups->put($groupName, $existingGroup);
}
}

return $groups;
Expand Down
4 changes: 2 additions & 2 deletions src/OrderedListInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ public function partition(callable $callback): array;
/**
* Groups the items by using the callback.
*
* @template TGroup of non-empty-string
* @template TGroup of non-empty-string|non-empty-list<non-empty-string>
* @psalm-param callable(TValue):TGroup $callback
*
* @psalm-return MapInterface<TGroup,OrderedListInterface<TValue>>
* @psalm-return MapInterface<non-empty-string,OrderedListInterface<TValue>>
*/
public function group(callable $callback): MapInterface;

Expand Down
13 changes: 13 additions & 0 deletions tests/GenericMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,19 @@ public function testWillGroupValuesToNewInstancesOfInitialInstance(): void
self::assertEquals($object2, $b->get('bar'));
}

public function testWillGroupValueIntoMultipleGroups(): void
{
$map = new GenericMap([
'foo' => $object1 = new GenericObject(1),
]);

$grouped = $map->group(fn () => ['a', 'b']);
self::assertTrue($grouped->has('a'));
self::assertTrue($grouped->has('b'));
self::assertTrue($grouped->get('a')->contains($object1));
self::assertTrue($grouped->get('b')->contains($object1));
}

/**
* @template T
* @psalm-param array<string,T> $elements
Expand Down
13 changes: 13 additions & 0 deletions tests/GenericOrderedListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,19 @@ public function testWillGroupValuesToNewInstancesOfInitialInstance(): void
self::assertEquals($object2, $b->at(0));
}

public function testWillGroupValueIntoMultipleGroups(): void
{
$list = new GenericOrderedList([
$object1 = new GenericObject(1),
]);

$grouped = $list->group(fn () => ['a', 'b']);
self::assertTrue($grouped->has('a'));
self::assertTrue($grouped->has('b'));
self::assertTrue($grouped->get('a')->contains($object1));
self::assertTrue($grouped->get('b')->contains($object1));
}

/**
* @template T
* @psalm-param list<T> $data
Expand Down

0 comments on commit a434de4

Please sign in to comment.