1 | <?php |
||
49 | class Table extends Widget |
||
50 | { |
||
51 | const DEFAULT_CONSOLE_SCREEN_WIDTH = 120; |
||
52 | const CONSOLE_SCROLLBAR_OFFSET = 3; |
||
53 | |||
54 | const CHAR_TOP = 'top'; |
||
55 | const CHAR_TOP_MID = 'top-mid'; |
||
56 | const CHAR_TOP_LEFT = 'top-left'; |
||
57 | const CHAR_TOP_RIGHT = 'top-right'; |
||
58 | const CHAR_BOTTOM = 'bottom'; |
||
59 | const CHAR_BOTTOM_MID = 'bottom-mid'; |
||
60 | const CHAR_BOTTOM_LEFT = 'bottom-left'; |
||
61 | const CHAR_BOTTOM_RIGHT = 'bottom-right'; |
||
62 | const CHAR_LEFT = 'left'; |
||
63 | const CHAR_LEFT_MID = 'left-mid'; |
||
64 | const CHAR_MID = 'mid'; |
||
65 | const CHAR_MID_MID = 'mid-mid'; |
||
66 | const CHAR_RIGHT = 'right'; |
||
67 | const CHAR_RIGHT_MID = 'right-mid'; |
||
68 | const CHAR_MIDDLE = 'middle'; |
||
69 | |||
70 | /** |
||
71 | * @var array table headers |
||
72 | */ |
||
73 | private $_headers = []; |
||
74 | /** |
||
75 | * @var array table rows |
||
76 | */ |
||
77 | private $_rows = []; |
||
78 | /** |
||
79 | * @var array table chars |
||
80 | */ |
||
81 | private $_chars = [ |
||
82 | self::CHAR_TOP => '═', |
||
83 | self::CHAR_TOP_MID => '╤', |
||
84 | self::CHAR_TOP_LEFT => '╔', |
||
85 | self::CHAR_TOP_RIGHT => '╗', |
||
86 | self::CHAR_BOTTOM => '═', |
||
87 | self::CHAR_BOTTOM_MID => '╧', |
||
88 | self::CHAR_BOTTOM_LEFT => '╚', |
||
89 | self::CHAR_BOTTOM_RIGHT => '╝', |
||
90 | self::CHAR_LEFT => '║', |
||
91 | self::CHAR_LEFT_MID => '╟', |
||
92 | self::CHAR_MID => '─', |
||
93 | self::CHAR_MID_MID => '┼', |
||
94 | self::CHAR_RIGHT => '║', |
||
95 | self::CHAR_RIGHT_MID => '╢', |
||
96 | self::CHAR_MIDDLE => '│', |
||
97 | ]; |
||
98 | /** |
||
99 | * @var array table column widths |
||
100 | */ |
||
101 | private $_columnWidths = []; |
||
102 | /** |
||
103 | * @var int screen width |
||
104 | */ |
||
105 | private $_screenWidth; |
||
106 | /** |
||
107 | * @var string list prefix |
||
108 | */ |
||
109 | private $_listPrefix = '• '; |
||
110 | |||
111 | |||
112 | /** |
||
113 | * Set table headers. |
||
114 | * |
||
115 | * @param array $headers table headers |
||
116 | * @return $this |
||
117 | */ |
||
118 | 9 | public function setHeaders(array $headers) |
|
123 | |||
124 | /** |
||
125 | * Set table rows. |
||
126 | * |
||
127 | * @param array $rows table rows |
||
128 | * @return $this |
||
129 | */ |
||
130 | 9 | public function setRows(array $rows) |
|
135 | |||
136 | /** |
||
137 | * Set table chars. |
||
138 | * |
||
139 | * @param array $chars table chars |
||
140 | * @return $this |
||
141 | */ |
||
142 | 1 | public function setChars(array $chars) |
|
147 | |||
148 | /** |
||
149 | * Set screen width. |
||
150 | * |
||
151 | * @param int $width screen width |
||
152 | * @return $this |
||
153 | */ |
||
154 | 9 | public function setScreenWidth($width) |
|
159 | |||
160 | /** |
||
161 | * Set list prefix. |
||
162 | * |
||
163 | * @param string $listPrefix list prefix |
||
164 | * @return $this |
||
165 | */ |
||
166 | 1 | public function setListPrefix($listPrefix) |
|
171 | |||
172 | /** |
||
173 | * @return string the rendered table |
||
174 | */ |
||
175 | 9 | public function run() |
|
217 | |||
218 | /** |
||
219 | * Renders a row of data into a string. |
||
220 | * |
||
221 | * @param array $row row of data |
||
222 | * @param string $spanLeft character for left border |
||
223 | * @param string $spanMiddle character for middle border |
||
224 | * @param string $spanRight character for right border |
||
225 | * @return string |
||
226 | * @see \yii\console\widgets\Table::render() |
||
227 | */ |
||
228 | 9 | protected function renderRow(array $row, $spanLeft, $spanMiddle, $spanRight) |
|
275 | |||
276 | /** |
||
277 | * Renders separator. |
||
278 | * |
||
279 | * @param string $spanLeft character for left border |
||
280 | * @param string $spanMid character for middle border |
||
281 | * @param string $spanMidMid character for middle-middle border |
||
282 | * @param string $spanRight character for right border |
||
283 | * @return string the generated separator row |
||
284 | * @see \yii\console\widgets\Table::render() |
||
285 | */ |
||
286 | 9 | protected function renderSeparator($spanLeft, $spanMid, $spanMidMid, $spanRight) |
|
298 | |||
299 | /** |
||
300 | * Calculate the size of rows to draw anchor of columns in console. |
||
301 | * |
||
302 | * @see \yii\console\widgets\Table::render() |
||
303 | */ |
||
304 | 9 | protected function calculateRowsSize() |
|
340 | |||
341 | /** |
||
342 | * Calculate the height of a row. |
||
343 | * |
||
344 | * @param array $row |
||
345 | * @return int maximum row per cell |
||
346 | * @see \yii\console\widgets\Table::render() |
||
347 | */ |
||
348 | 9 | protected function calculateRowHeight($row) |
|
376 | |||
377 | /** |
||
378 | * Getting screen width. |
||
379 | * If it is not able to determine screen width, default value `123` will be set. |
||
380 | * |
||
381 | * @return int screen width |
||
382 | */ |
||
383 | 9 | protected function getScreenWidth() |
|
393 | } |
||
394 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.