Lines 96.96% 32 / 33
Methods 80.00% 4 / 5
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 from 100.00% 21 / 21 100.00% 1 / 1 5
 category 100.00% 4 / 4 100.00% 1 / 1 4
 summary 0.00% 0 / 1 0.00% 0 / 1 2
 [Lorekeeper\Server\Event\NamedAfterClass] name 100.00% 6 / 6 100.00% 1 / 1 1
22final readonly class ClassChangedEvent implements Event
23{
24    use NamedAfterClass;
25
26    /** @param Option\Option<ClassDescription> $description */
27    public function __construct(
28        public SocketId $player,
29        public ClassName $className,
30        public ClassCategory $category,
31        public ClassPoints $points,
32        public Option\Option $description,
33    ) {}
34
35    /** @return Option\Option<static> */
36    #[Override]
37    public static function from(Message $message): Option\Option
38    {
39        if (!$message instanceof JsonMessage || ($message->data['cmd'] ?? null) !== 'updateClass') {
40            return Option\none();
41        }
42
43        try {
44            $data = Type\shape([
45                'uid' => Type\positive_int(),
46                'sClassName' => Type\non_empty_string(),
47                'sClassCat' => Type\string(),
48                'iCP' => Type\uint(),
49                'sDesc' => Type\optional(Type\string()),
50            ], allowUnknownFields: true)->coerce($message->data);
51        } catch (CoercionException $exception) {
52            throw new MalformedEventException(static::class, 'class is missing or invalid', $exception);
53        }
54
55        $description = trim($data['sDesc'] ?? '');
56
57        return Option\some(
58            new static(
59                new SocketId($data['uid']),
60                new ClassName($data['sClassName']),
61                self::category($data['sClassCat']),
62                new ClassPoints($data['iCP']),
63                $description === '' ? Option\none() : Option\some(new ClassDescription($description)),
64            ),
65        );
66    }
67
68    /** @throws MalformedEventException */
69    private static function category(string $code): ClassCategory
70    {
71        return match (substr($code, offset: 0, length: 1)) {
72            'M' => ClassCategory::Melee,
73            'C' => ClassCategory::Caster,
74            default => throw new MalformedEventException(static::class, "unknown class category '{$code}'"),
75        };
76    }
77
78    #[Override]
79    public function summary(): string
80    {
81        return "Socket {$this->player->value} is {$this->className->value} ({$this->category->name}, {$this->points->value} CP)";
82    }
83}

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}