Lines 100.00% 22 / 22
Methods 100.00% 3 / 3
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 read n/a 0 / 0 n/a 0 / 0 0
 from 100.00% 15 / 15 100.00% 1 / 1 4
 [Lorekeeper\Server\Event\NamedAfterClass] name 100.00% 6 / 6 100.00% 1 / 1 1
23abstract readonly class RewardedEvent implements Event
24{
25    use NamedAfterClass;
26
27    public function __construct(
28        public Gold $gold,
29        public Experience $experience,
30        public ClassPoints $classPoints,
31    ) {}
32
33    /**
34     * @param array<array-key, mixed> $data
35     *
36     * @throws MalformedEventException
37     *
38     * @return Option\Option<static>
39     */
40    abstract protected static function read(
41        array $data,
42        Gold $gold,
43        Experience $experience,
44        ClassPoints $classPoints,
45    ): Option\Option;
46
47    /** @return Option\Option<static> */
48    #[Override]
49    final public static function from(Message $message): Option\Option
50    {
51        if (!$message instanceof JsonMessage || ($message->data['cmd'] ?? null) !== 'addGoldExp') {
52            return Option\none();
53        }
54
55        try {
56            $reward = Type\shape([
57                'intGold' => Type\optional(Type\uint()),
58                'intExp' => Type\optional(Type\uint()),
59                'iCP' => Type\optional(Type\uint()),
60            ], allowUnknownFields: true)->coerce($message->data);
61        } catch (CoercionException $exception) {
62            throw new MalformedEventException(static::class, 'reward is invalid', $exception);
63        }
64
65        return static::read(
66            $message->data,
67            new Gold($reward['intGold'] ?? 0),
68            new Experience($reward['intExp'] ?? 0),
69            new ClassPoints($reward['iCP'] ?? 0),
70        );
71    }
72}

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}