Passed
Push — main ( 4059b3...f7ff17 )
by ANDREY
02:27
created

Table::getHeightByContent()   B

Complexity

Conditions 9
Paths 30

Size

Total Lines 37
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 9.0041

Importance

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