Lines 85.71% 12 / 14
Methods 33.33% 1 / 3
Classes 0.00% 0 / 1
Covered by tests of size
Name Lines Methods CRAP
 isValidCategory 66.66% 2 / 3 0.00% 0 / 1 3.33
 isDirectChild 80.00% 4 / 5 0.00% 0 / 1 4.13
 collectDirectChildren 100.00% 6 / 6 100.00% 1 / 1 3
22final class CategoryValidator
23{
24    /**
25     * Validates if a category array has required fields.
26     */
27    public function isValidCategory(mixed $category): bool
28    {
29        if (!is_array($category)) {
30            return false;
31        }
32
33        return array_key_exists(key: 'parent_id', array: $category) && array_key_exists(key: 'id', array: $category);
34    }
35
36    /**
37     * Checks if a category is a direct child of a parent.
38     *
39     * @param array<string, mixed> $category
40     */
41    public function isDirectChild(array $category, mixed $categoryId, int $parentId): bool
42    {
43        if (!array_key_exists('parent_id', $category)) {
44            return false;
45        }
46
47        if ((int) $category['parent_id'] !== $parentId) {
48            return false;
49        }
50
51        return is_int($categoryId) && $categoryId > 0;
52    }
53
54    /**
55     * Collects direct children IDs for a parent.
56     *
57     * @param array<int, array<string, mixed>> $categories
58     * @return array<int>
59     */
60    public function collectDirectChildren(array $categories, int $parentId): array
61    {
62        $children = [];
63        foreach ($categories as $categoryId => $category) {
64            if (!$this->isDirectChild($category, $categoryId, $parentId)) {
65                continue;
66            }
67
68            $children[] = $categoryId;
69        }
70
71        return $children;
72    }
73}