44
55namespace DragonCode \Benchmark \View ;
66
7+ use function array_first ;
78use function array_keys ;
89use function array_map ;
910use function array_values ;
@@ -22,23 +23,24 @@ class TableView extends View
2223 public function show (array $ data ): void
2324 {
2425 $ headers = $ this ->headers ($ data );
25- $ widths = $ this ->columnWidths ($ headers , $ data );
26+ $ widths = $ this ->widths ($ headers , $ data );
2627
2728 $ this ->writeLine ($ this ->separator ($ widths ));
28- $ this ->writeLine ($ this ->formatRow ($ headers , $ widths ));
29+ $ this ->writeLine ($ this ->row ($ headers , $ widths ));
2930 $ this ->writeLine ($ this ->separator ($ widths ));
3031
31- foreach ($ data as $ row ) {
32- if ($ row === [null ]) {
33- $ this ->writeLine ($ this ->separator ($ widths ));
32+ $ this ->writeData ($ data , $ widths );
3433
35- continue ;
36- }
34+ $ this -> writeLine ( $ this -> separator ( $ widths )) ;
35+ }
3736
38- $ this ->writeLine ($ this ->formatRow (array_values ($ row ), $ widths ));
37+ protected function writeData (array $ data , array $ widths ): void
38+ {
39+ foreach ($ data as $ row ) {
40+ $ row === [null ]
41+ ? $ this ->writeLine ($ this ->separator ($ widths ))
42+ : $ this ->writeLine ($ this ->row ($ row , $ widths ));
3943 }
40-
41- $ this ->writeLine ($ this ->separator ($ widths ));
4244 }
4345
4446 /**
@@ -48,7 +50,7 @@ public function show(array $data): void
4850 */
4951 protected function headers (array $ data ): array
5052 {
51- return array_keys (array_values ($ data )[ 0 ] );
53+ return array_keys (array_first ($ data ));
5254 }
5355
5456 /**
@@ -57,7 +59,7 @@ protected function headers(array $data): array
5759 * @param array $headers The column headers.
5860 * @param array $data An array of table rows.
5961 */
60- protected function columnWidths (array $ headers , array $ data ): array
62+ protected function widths (array $ headers , array $ data ): array
6163 {
6264 $ widths = array_map (static fn ($ header ) => mb_strlen ((string ) $ header ), $ headers );
6365
@@ -77,7 +79,7 @@ protected function columnWidths(array $headers, array $data): array
7779 */
7880 protected function separator (array $ widths ): string
7981 {
80- $ parts = array_map (fn (int $ w ) => str_repeat ('- ' , $ w + 2 ), $ widths );
82+ $ parts = array_map (static fn (int $ w ) => str_repeat ('- ' , $ w + 2 ), $ widths );
8183
8284 return '+ ' . implode ('+ ' , $ parts ) . '+ ' ;
8385 }
@@ -88,11 +90,11 @@ protected function separator(array $widths): string
8890 * @param array $values The cell values of the row.
8991 * @param array $widths An array of column widths.
9092 */
91- protected function formatRow (array $ values , array $ widths ): string
93+ protected function row (array $ values , array $ widths ): string
9294 {
9395 $ cells = [];
9496
95- foreach ($ values as $ i => $ value ) {
97+ foreach (array_values ( $ values) as $ i => $ value ) {
9698 $ cells [] = ' ' . mb_str_pad ((string ) $ value , $ widths [$ i ]) . ' ' ;
9799 }
98100
0 commit comments