Lines 90.00% 9 / 10
Methods 50.00% 1 / 2
Classes 0.00% 0 / 1
Covered by tests of size
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 buildTree 88.88% 8 / 9 0.00% 0 / 1 4.02
22final readonly class TreeVisualizer
23{
24    public function __construct(
25        private TreePathResolver $treePathResolver,
26    ) {
27    }
28
29    /**
30     * Builds branch visuals (vertical/space) for a category path.
31     *
32     * @param array<int, array<string, mixed>> $categoryName
33     * @param array<int, array<int, array<string, mixed>>> $childrenMap
34     * @return array<int, string>
35     */
36    public function buildTree(array $categoryName, array $childrenMap, int $categoryId): array
37    {
38        $ascendants = $this->treePathResolver->getNodes($categoryName, $categoryId);
39        $tree = [];
40        foreach ($ascendants as $i => $ascendantId) {
41            if ($ascendantId === 0) {
42                break;
43            }
44
45            $brothers = $this->treePathResolver->getBrothers($categoryName, $childrenMap, (int) $ascendantId);
46            $last = end($brothers);
47            $tree[(int) $i] = $ascendantId === $last ? 'space' : 'vertical';
48        }
49
50        return $tree;
51    }
52}