Lines 100.00% 20 / 20
Methods 100.00% 3 / 3
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 of 100.00% 7 / 7 100.00% 1 / 1 5
 held 100.00% 8 / 8 100.00% 1 / 1 4
 group 100.00% 5 / 5 100.00% 1 / 1 5
14final readonly class QuestGroups
15{
16    private const array FIELDS = ['id0', 'id1', 'id2', 'id3', 'id4', 'id5', 'id6', 'id7', 'iw0', 'im0'];
17
18    private const int BITS_PER_FIELD = 32;
19
20    /** @return Option\Option<QuestGroupId> */
21    public static function of(string $field, int $index): Option\Option
22    {
23        $group = self::group($field);
24        $number = substr($field, offset: 2);
25        if ($group === null || $index < 0 || $index >= self::BITS_PER_FIELD || !ctype_digit($number)) {
26            return Option\none();
27        }
28
29        return Option\some(
30            new QuestGroupId($group, new QuestGroupNumber(((int) $number * self::BITS_PER_FIELD) + $index)),
31        );
32    }
33
34    /**
35     * @param array<array-key, mixed> $data
36     *
37     * @throws CoercionException
38     *
39     * @return list<QuestGroupId>
40     */
41    public static function held(array $data): array
42    {
43        $held = [];
44        foreach (self::FIELDS as $field) {
45            $flags = Type\uint()->coerce($data[$field] ?? 0);
46            for ($index = 0; $index < self::BITS_PER_FIELD; $index++) {
47                if ((($flags >> $index) & 1) !== 1) {
48                    continue;
49                }
50
51                $held[] = self::of($field, $index)->unwrap();
52            }
53        }
54
55        return $held;
56    }
57
58    private static function group(string $field): ?QuestGroup
59    {
60        return match (substr($field, offset: 0, length: 2)) {
61            'id' => QuestGroup::Daily,
62            'iw' => QuestGroup::Weekly,
63            'im' => QuestGroup::Monthly,
64            default => null,
65        };
66    }
67}