1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This is just a simple helper class which displays the result set as a nice console table. |
5
|
|
|
* |
6
|
|
|
* Class ConsoleTable |
7
|
|
|
* @phpstan-type DataType array<int|string, string|int|array<string|int, array<mixed>|int|string>> |
8
|
|
|
*/ |
9
|
|
|
class ConsoleTable |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var array<string,int> |
13
|
|
|
*/ |
14
|
|
|
protected array $columns = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param DataType $data |
|
|
|
|
18
|
|
|
* |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
|
|
protected function detectColumnSize(array $data): void |
22
|
|
|
{ |
23
|
|
|
// Find the longest string in each column |
24
|
|
|
$columns = []; |
25
|
|
|
foreach ($data as $row) { |
26
|
|
|
$row = (array)$row; |
27
|
|
|
foreach ($row as $cellKey => $cell) { |
28
|
|
|
if (is_array($cell)) { |
29
|
|
|
$cell = implode(', ', $cell); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$valueLength = strlen(strval($cell)); |
33
|
|
|
$nameLength = strlen(strval($cellKey)); |
34
|
|
|
|
35
|
|
|
$length = max($nameLength, $valueLength); |
36
|
|
|
|
37
|
|
|
if (empty($columns[$cellKey]) || $columns[$cellKey] < $length) { |
38
|
|
|
$columns[strval($cellKey)] = $length; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->columns = $columns; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function buildRow(): string |
47
|
|
|
{ |
48
|
|
|
$table = '+'; |
49
|
|
|
foreach ($this->columns as $length) { |
50
|
|
|
$table .= str_repeat('-', $length + 1) . '-+'; |
51
|
|
|
} |
52
|
|
|
$table .= PHP_EOL; |
53
|
|
|
|
54
|
|
|
return $table; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param DataType $data |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
protected function buildHeader(array $data): string |
63
|
|
|
{ |
64
|
|
|
$table = $this->buildRow(); |
65
|
|
|
|
66
|
|
|
// Build the header |
67
|
|
|
foreach ($data as $row) { |
68
|
|
|
$table .= '| '; |
69
|
|
|
$row = (array)$row; |
70
|
|
|
foreach ($row as $cellKey => $cell) { |
71
|
|
|
$cellKey = strval($cellKey); |
72
|
|
|
$table .= str_pad($cellKey, $this->columns[$cellKey]) . ' | '; |
73
|
|
|
} |
74
|
|
|
$table .= PHP_EOL; |
75
|
|
|
|
76
|
|
|
break; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$table .= $this->buildRow(); |
80
|
|
|
|
81
|
|
|
return $table; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param DataType $data |
86
|
|
|
*/ |
87
|
|
|
public function __construct(array $data) |
88
|
|
|
{ |
89
|
|
|
$this->detectColumnSize($data); |
90
|
|
|
|
91
|
|
|
$table = $this->buildHeader($data); |
92
|
|
|
|
93
|
|
|
// Output table, padding columns |
94
|
|
|
foreach ($data as $row) { |
95
|
|
|
$table .= '| '; |
96
|
|
|
|
97
|
|
|
foreach ($this->columns as $column => $length) { |
98
|
|
|
$value = $row[$column] ?? ''; |
99
|
|
|
if (is_array($value)) { |
100
|
|
|
$value = implode(', ', $value); |
101
|
|
|
} |
102
|
|
|
$table .= str_pad((string)($value), $length) . ' | '; |
103
|
|
|
} |
104
|
|
|
$table .= PHP_EOL; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$table .= $this->buildRow(); |
108
|
|
|
|
109
|
|
|
echo $table; |
110
|
|
|
} |
111
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths