99use Entropy \Attributes \RelatedTest ;
1010use Rector \Monitor \Entropy \Enum \ColumnAlign ;
1111use Rector \Monitor \Tests \Entropy \ConsoleTableTest ;
12- use Stringable ;
1312use Webmozart \Assert \Assert ;
1413
1514#[RelatedTest(testClass: ConsoleTableTest::class)]
1615final class ConsoleTable
1716{
18- /**
19- * @var list<string>
20- */
21- private readonly array $ headers ;
22-
23- /**
24- * @var list<list<string>>
25- */
26- private array $ rows = [];
27-
28- /**
29- * @var list<'left'|'right'|'center'>
30- */
31- private array $ align ;
32-
3317 /**
3418 * @param string[] $headers
35- * @param array<array<string|int|float| TableCell|null>> $rows
36- * @param array<'left'|'right'|'center' > $columnsAlign
19+ * @param array<array<string|int|TableCell|null>> $rows
20+ * @param array<int, ColumnAlign::* > $columnsAlign
3721 */
38- public function __construct (array $ headers , array $ rows , array $ columnsAlign = [])
39- {
22+ public function __construct (
23+ private array $ headers ,
24+ private array $ rows ,
25+ private array $ columnsAlign = []
26+ ) {
4027 Assert::notEmpty ($ headers );
4128 Assert::allString ($ headers );
4229
4330 // nested arrays
4431 Assert::notEmpty ($ rows );
4532 Assert::allIsArray ($ rows );
46-
47- $ this ->headers = array_values ($ headers );
48- foreach ($ rows as $ row ) {
49- $ this ->rows [] = array_values (
50- array_map (
51- static fn (float |int |TableCell |string |null $ value ): string => $ value === null ? '' : (string ) $ value ,
52- $ row
53- )
54- );
55- }
56-
57- $ this ->align = array_values ($ columnsAlign );
5833 }
5934
6035 public function render (): string
6136 {
62- $ cols = max (count ($ this ->headers ), $ this ->maxRowColumns ());
63- $ columnWidths = array_fill (0 , $ cols , 0 );
37+ $ columns = max (count ($ this ->headers ), $ this ->maxRowColumns ());
38+ $ columnWidths = array_fill (0 , $ columns , 0 );
6439
6540 // headers
6641 foreach ($ this ->headers as $ columId => $ header ) {
@@ -108,7 +83,7 @@ private function maxRowColumns(): int
10883 }
10984
11085 /**
111- * @param list< int> $widths
86+ * @param int[] $widths
11287 */
11388 private function line (array $ widths ): string
11489 {
@@ -124,8 +99,8 @@ private function line(array $widths): string
12499 /**
125100 * Paints full row line
126101 *
127- * @param list <string|TableCell> $row
128- * @param list< int> $widths
102+ * @param array <string|int|null |TableCell> $row
103+ * @param int[] $widths
129104 */
130105 private function rowLine (array $ row , array $ widths ): string
131106 {
@@ -134,14 +109,14 @@ private function rowLine(array $row, array $widths): string
134109 foreach ($ widths as $ columnKey => $ width ) {
135110 $ value = $ row [$ columnKey ] ?? '' ;
136111
137- $ align = $ this ->align [$ columnKey ] ?? ColumnAlign::LEFT ;
138- $ result .= ' ' . $ this ->padVisible ($ value , $ width , $ align ) . ' | ' ;
112+ $ columnAlign = $ this ->columnsAlign [$ columnKey ] ?? ColumnAlign::LEFT ;
113+ $ result .= ' ' . $ this ->padVisible ($ value , $ width , $ columnAlign ) . ' ' ;
139114 }
140115
141116 return $ result ;
142117 }
143118
144- private function strlenVisible (string |Stringable $ contents ): int
119+ private function strlenVisible (string |int | null | TableCell $ contents ): int
145120 {
146121 $ string = (string ) preg_replace ('#\e\[[0-9;]*[A-Za-z]# ' , '' , (string ) $ contents );
147122
@@ -155,7 +130,7 @@ private function strlenVisible(string|Stringable $contents): int
155130 /**
156131 * @param ColumnAlign::* $columnAllign
157132 */
158- private function padVisible (string |TableCell $ content , int $ width , string $ columnAllign ): string
133+ private function padVisible (string |int | null | TableCell $ content , int $ width , string $ columnAllign ): string
159134 {
160135 $ length = $ this ->strlenVisible ($ content );
161136
0 commit comments