Lines 100.00% 30 / 30
Methods 100.00% 4 / 4
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 from 100.00% 18 / 18 100.00% 1 / 1 6
 summary 100.00% 5 / 5 100.00% 1 / 1 1
 [Lorekeeper\Server\Event\NamedAfterClass] name 100.00% 6 / 6 100.00% 1 / 1 1
23final readonly class ItemDroppedEvent implements Event
24{
25    use NamedAfterClass;
26
27    /** @param non-empty-list<HeldItem> $items */
28    public function __construct(
29        public array $items,
30    ) {}
31
32    /** @return Option\Option<static> */
33    #[Override]
34    public static function from(Message $message): Option\Option
35    {
36        if (!$message instanceof JsonMessage || ($message->data['cmd'] ?? null) !== 'dropItem') {
37            return Option\none();
38        }
39
40        try {
41            $items = Type\non_empty_dict(Type\string(), Type\shape([
42                'ItemID' => Type\positive_int(),
43                'sName' => Type\non_empty_string(),
44                'sType' => Type\string(),
45                'iQty' => Type\positive_int(),
46                'bUpg' => Type\optional(Type\bool()),
47            ], allowUnknownFields: true))->coerce($message->data['items'] ?? null);
48        } catch (CoercionException $exception) {
49            throw new MalformedEventException(static::class, 'dropped items are missing or invalid', $exception);
50        }
51
52        $dropped = [];
53        foreach ($items as $entry) {
54            $item = ItemKinds::read($entry, static::class);
55            if ($entry['bUpg'] ?? false) {
56                $item->tags->add(ItemTag::MemberOnly);
57            }
58
59            $dropped[] = new HeldItem($item, new Quantity($entry['iQty']));
60        }
61
62        return Option\some(new static($dropped));
63    }
64
65    #[Override]
66    public function summary(): string
67    {
68        return 'Dropped '
69        . implode(', ', array_map(
70            static fn(HeldItem $held): string => "{$held->quantity->value} {$held->item->name->value}",
71            $this->items,
72        ));
73    }
74}

From Lorekeeper\Server\Event\NamedAfterClass

7trait NamedAfterClass
8{
9    public function name(): string
10    {
11        $class = static::class;
12
13        return (string) preg_replace(
14            '/Event$/',
15            replacement: '',
16            subject: substr($class, (int) strrpos($class, needle: '\\') + 1),
17        );
18    }
19}