@@ -8,10 +8,13 @@ class TextGrid
8
8
{
9
9
private bool $ isCli ;
10
10
11
+ /** @var mixed[][] */
11
12
protected array $ matrix ;
12
13
14
+ /** @var int[] */
13
15
protected array $ rows ;
14
16
17
+ /** @var string[] */
15
18
protected array $ columns ;
16
19
17
20
private string $ gridDisplay ;
@@ -22,6 +25,7 @@ class TextGrid
22
25
23
26
private bool $ columnHeaders = true ;
24
27
28
+ /** @param mixed[][] $matrix */
25
29
public function __construct (array $ matrix , bool $ isCli = true , bool $ rowDividers = false , bool $ rowHeaders = true , bool $ columnHeaders = true )
26
30
{
27
31
$ this ->rows = array_keys ($ matrix );
@@ -63,6 +67,7 @@ public function render(): string
63
67
return $ this ->gridDisplay ;
64
68
}
65
69
70
+ /** @param int[] $columnWidths */
66
71
private function renderRows (int $ maxRowLength , array $ columnWidths ): void
67
72
{
68
73
foreach ($ this ->matrix as $ row => $ rowData ) {
@@ -77,6 +82,10 @@ private function renderRows(int $maxRowLength, array $columnWidths): void
77
82
}
78
83
}
79
84
85
+ /**
86
+ * @param mixed[] $rowData
87
+ * @param int[] $columnWidths
88
+ */
80
89
private function renderCells (array $ rowData , array $ columnWidths ): void
81
90
{
82
91
foreach ($ rowData as $ column => $ cell ) {
@@ -87,6 +96,7 @@ private function renderCells(array $rowData, array $columnWidths): void
87
96
}
88
97
}
89
98
99
+ /** @param int[] $columnWidths */
90
100
private function renderColumnHeader (int $ maxRowLength , array &$ columnWidths ): void
91
101
{
92
102
if (!$ this ->columnHeaders ) {
@@ -95,6 +105,7 @@ private function renderColumnHeader(int $maxRowLength, array &$columnWidths): vo
95
105
return ;
96
106
}
97
107
foreach ($ this ->columns as $ column => $ reference ) {
108
+ /** @var string $reference */
98
109
$ columnWidths [$ column ] = max ($ columnWidths [$ column ], $ this ->strlen ($ reference ));
99
110
}
100
111
if ($ this ->rowHeaders ) {
@@ -109,13 +120,15 @@ private function renderColumnHeader(int $maxRowLength, array &$columnWidths): vo
109
120
$ this ->gridDisplay .= str_repeat (' ' , $ maxRowLength + 2 );
110
121
}
111
122
foreach ($ this ->columns as $ column => $ reference ) {
123
+ /** @var scalar $reference */
112
124
$ this ->gridDisplay .= '| ' . str_pad ((string ) $ reference , $ columnWidths [$ column ] + 1 , ' ' );
113
125
}
114
126
$ this ->gridDisplay .= '| ' . PHP_EOL ;
115
127
116
128
$ this ->renderFooter ($ maxRowLength , $ columnWidths );
117
129
}
118
130
131
+ /** @param int[] $columnWidths */
119
132
private function renderFooter (int $ maxRowLength , array $ columnWidths ): void
120
133
{
121
134
if ($ this ->rowHeaders ) {
@@ -128,6 +141,7 @@ private function renderFooter(int $maxRowLength, array $columnWidths): void
128
141
$ this ->gridDisplay .= '+ ' . PHP_EOL ;
129
142
}
130
143
144
+ /** @return int[] */
131
145
private function getColumnWidths (): array
132
146
{
133
147
$ columnCount = count ($ this ->matrix , COUNT_RECURSIVE ) / count ($ this ->matrix );
@@ -139,6 +153,7 @@ private function getColumnWidths(): array
139
153
return $ columnWidths ;
140
154
}
141
155
156
+ /** @param mixed[] $columnData */
142
157
private function getColumnWidth (array $ columnData ): int
143
158
{
144
159
$ columnWidth = 0 ;
0 commit comments