Lines 100.00% 20 / 20
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% 12 / 12 100.00% 1 / 1 6
 summary 100.00% 1 / 1 100.00% 1 / 1 1
 [Lorekeeper\Server\Event\NamedAfterClass] name 100.00% 6 / 6 100.00% 1 / 1 1
22final readonly class RoomJoinedEvent implements Event
23{
24    use NamedAfterClass;
25
26    /** @param list<PlayerName> $players */
27    public function __construct(
28        public array $players,
29    ) {}
30
31    /** @return Option\Option<static> */
32    #[Override]
33    public static function from(Message $message): Option\Option
34    {
35        if (!$message instanceof XmlMessage) {
36            return Option\none();
37        }
38
39        $body = $message->dom->getElementsByTagName('body')->item(0);
40        if (!$body instanceof DOMElement || $body->getAttribute('action') !== 'joinOK') {
41            return Option\none();
42        }
43
44        $players = [];
45        foreach ($body->getElementsByTagName('u') as $user) {
46            try {
47                $name = Type\non_empty_string()->coerce($user->getElementsByTagName('n')->item(0)?->textContent);
48            } catch (CoercionException $exception) {
49                throw new MalformedEventException(static::class, 'a player name is missing or invalid', $exception);
50            }
51
52            $players[] = new PlayerName($name);
53        }
54
55        return Option\some(new static($players));
56    }
57
58    #[Override]
59    public function summary(): string
60    {
61        return 'Joined a room with ' . count($this->players) . ' players';
62    }
63}

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}