Lines 100.00% 37 / 37
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% 25 / 25 100.00% 1 / 1 8
 summary 100.00% 5 / 5 100.00% 1 / 1 1
 [Lorekeeper\Server\Event\NamedAfterClass] name 100.00% 6 / 6 100.00% 1 / 1 1
22final readonly class TemporaryItemsAddedEvent implements Event
23{
24    use NamedAfterClass;
25
26    /** @param non-empty-list<PlayerItem> $items */
27    public function __construct(
28        public array $items,
29    ) {}
30
31    /** @return Option\Option<static> */
32    #[Override]
33    public static function from(Message $message): Option\Option
34    {
35        if (!$message instanceof JsonMessage || ($message->data['cmd'] ?? null) !== 'addItems') {
36            return Option\none();
37        }
38
39        try {
40            $items = Type\dict(Type\string(), Type\shape([
41                'ItemID' => Type\positive_int(),
42                'sName' => Type\optional(Type\non_empty_string()),
43                'sType' => Type\optional(Type\string()),
44                'bTemp' => Type\optional(Type\bool()),
45                'iQty' => Type\positive_int(),
46            ], allowUnknownFields: true))->coerce($message->data['items'] ?? null);
47        } catch (CoercionException $exception) {
48            throw new MalformedEventException(static::class, 'items are missing or invalid', $exception);
49        }
50
51        $added = [];
52        foreach ($items as $entry) {
53            if (!($entry['bTemp'] ?? false) || !array_key_exists('sName', $entry)) {
54                continue;
55            }
56
57            $added[] = new PlayerItem(
58                ItemKinds::read(
59                    new ItemId($entry['ItemID']),
60                    new ItemName($entry['sName']),
61                    $entry['sType'] ?? '',
62                    static::class,
63                ),
64                new Quantity($entry['iQty']),
65            );
66        }
67
68        return $added === [] ? Option\none() : Option\some(new static($added));
69    }
70
71    #[Override]
72    public function summary(): string
73    {
74        return 'Got for now '
75        . implode(', ', array_map(
76            static fn(PlayerItem $held): string => "{$held->quantity->value} {$held->item->name->value}",
77            $this->items,
78        ));
79    }
80}

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}