Lines 100.00% 13 / 13
Methods 100.00% 2 / 2
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 follow 100.00% 6 / 6 100.00% 1 / 1 5
 dropped 100.00% 7 / 7 100.00% 1 / 1 3
14final readonly class DropTracking
15{
16    public static function follow(Drops $drops, Event $event): Drops
17    {
18        return match (true) {
19            $event instanceof ItemDroppedEvent => self::dropped($drops, $event->items),
20            $event instanceof DropPickedUpEvent && $drops->find($event->item) !== null => new Drops(array_diff_key(
21                $drops->items,
22                [$event->item->value => true],
23            )),
24            default => $drops,
25        };
26    }
27
28    /** @param non-empty-list<HeldItem> $dropped */
29    private static function dropped(Drops $drops, array $dropped): Drops
30    {
31        $items = $drops->items;
32        foreach ($dropped as $item) {
33            $before = $items[$item->item->id->value] ?? null;
34            $items[$item->item->id->value] = $before === null
35                ? $item
36                : new HeldItem($before->item, new Quantity($before->quantity->value + $item->quantity->value));
37        }
38
39        return new Drops($items);
40    }
41}