Passed
Push — main ( 56908f...4059b3 )
by ANDREY
02:30
created

Table::getWidthByContent()   B

Complexity

Conditions 8
Paths 36

Size

Total Lines 33
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 25
c 1
b 0
f 0
nc 36
nop 1
dl 0
loc 33
ccs 26
cts 26
cp 1
crap 8
rs 8.4444
1
<?php
2
3
namespace VPA\Console\Glyphs;
4
5
6
7
class Table extends GlyphBlock
8
{
9 13
    public function addRow(array $config = []): Glyph
10
    {
11 13
        $row = new Row($this->globalConfig);
12
        // Colors are inherited from the parent
13 13
        $symbolConfig = [
14 13
            'color' => $this->__get('color'),
15 13
            'borderColor' => $this->__get('borderColor'),
16 13
            'backgroundColor' => $this->__get('backgroundColor'),
17
        ];
18 13
        $row->setConfig(array_merge($row->getConfig(), $symbolConfig, $config));
19 13
        $this->addChild($row);
20 13
        return $row;
21
    }
22
23 11
    public function getWidthByContent(int $endOfPreviousSibling = 0): int
24
    {
25 11
        $cellLengths = [];
26 11
        foreach ($this->children as $rowIndex => $row) {
27 9
            $cells = $row->getChildren();
28 9
            foreach ($cells as $cellIndex => $cell) {
29 9
                $cellLengths[$cellIndex][$rowIndex] = $cell->getWidthByContent();
30
            }
31
        }
32 11
        $this->width = $endOfPreviousSibling;
33 11
        $maxCellLengths = [];
34 11
        foreach ($cellLengths as $rowIndex => $cells) {
35 9
            $maxCellLength = !empty($cells) ? max($cells) : 0;
36 9
            $maxCellLengths[$rowIndex] = $maxCellLength;
37 9
            $this->width += $maxCellLength;
38
        }
39 11
        foreach ($this->children as $rowIndex => $row) {
40 9
            $row->setWidth($this->width);
41 9
            $cells = $row->getChildren();
42 9
            $offset = 0;
43 9
            foreach ($cells as $cellIndex => $cell) {
44 9
                $cell->setWidth($maxCellLengths[$cellIndex]);
45 9
                $cell->setX($offset);
46 9
                $offset += $maxCellLengths[$cellIndex];
47 9
                $nodes = $cell->getChildren();
48 9
                foreach ($nodes as $node) {
49 9
                    $node->setWidth($cell->width);
50
                }
51
            }
52
        }
53 11
        $this->renderedWidth = true;
54 11
        $this->appendWidth($this->width);
55 11
        return $this->width;
56
    }
57
58 11
    public function getHeightByContent(int $endOfPreviousSibling = 0): int
59
    {
60 11
        $countCells = [];
61 11
        $cellHeights = [];
62
        // Find the height of all cells
63 11
        foreach ($this->children as $rowIndex => $row) {
64 10
            $cells = $row->getChildren();
65 10
            $countCells[] = count($cells);
66 10
            foreach ($cells as $cellIndex => $cell) {
67 10
                $cellHeights[$rowIndex][$cellIndex] = $cell->getHeightByContent();
68
            }
69
        }
70 11
        if (!empty($countCells) && min($countCells) != max($countCells)) {
71
            throw new \RuntimeException("Number of cells in rows does not match");
72
        }
73 11
        $this->height = $endOfPreviousSibling;
74 11
        $maxCellHeights = [];
75
        // Find the max height of cell
76 11
        foreach ($cellHeights as $rowIndex => $cells) {
77 10
            $maxCellHeight = !empty($cells) ? max($cells) : 0;
78 10
            $maxCellHeights[$rowIndex] = $maxCellHeight;
79
        }
80 11
        $offset = 0;
81 11
        foreach ($this->children as $rowIndex => $row) {
82 10
            $maxHeight = $maxCellHeights[$rowIndex];
83 10
            $row->setHeight($maxHeight);
84 10
            $row->setY($offset);
85 10
            $this->height += $row->getHeight();
86 10
            $offset += $row->getHeight();
87 10
            $cells = $row->getChildren();
88 10
            foreach ($cells as $cellIndex => $cell) {
89 10
                $cell->setHeight($maxHeight);
90
            }
91
        }
92 11
        $this->renderedHeight = true;
93 11
        $this->appendHeight($this->height);
94 11
        return $this->height;
95
    }
96
}