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

Merge release 1.3.0 into 2.0.x #272

Merged
merged 4 commits into from
Oct 22, 2024
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
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 @@ -22,6 +22,7 @@
use function assert;
use function hash;
use function implode;
use function is_array;
use function is_callable;
use function serialize;
use function sort;
Expand Down Expand Up @@ -367,30 +368,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 @@ -1048,6 +1048,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