Complex classes like Table often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Table, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | class Table extends Widget |
||
50 | { |
||
51 | const DEFAULT_CONSOLE_SCREEN_WIDTH = 120; |
||
52 | const CONSOLE_SCROLLBAR_OFFSET = 3; |
||
53 | const CHAR_TOP = 'top'; |
||
54 | const CHAR_TOP_MID = 'top-mid'; |
||
55 | const CHAR_TOP_LEFT = 'top-left'; |
||
56 | const CHAR_TOP_RIGHT = 'top-right'; |
||
57 | const CHAR_BOTTOM = 'bottom'; |
||
58 | const CHAR_BOTTOM_MID = 'bottom-mid'; |
||
59 | const CHAR_BOTTOM_LEFT = 'bottom-left'; |
||
60 | const CHAR_BOTTOM_RIGHT = 'bottom-right'; |
||
61 | const CHAR_LEFT = 'left'; |
||
62 | const CHAR_LEFT_MID = 'left-mid'; |
||
63 | const CHAR_MID = 'mid'; |
||
64 | const CHAR_MID_MID = 'mid-mid'; |
||
65 | const CHAR_RIGHT = 'right'; |
||
66 | const CHAR_RIGHT_MID = 'right-mid'; |
||
67 | const CHAR_MIDDLE = 'middle'; |
||
68 | |||
69 | /** |
||
70 | * @var array table headers |
||
71 | */ |
||
72 | private $_headers = []; |
||
73 | /** |
||
74 | * @var array table rows |
||
75 | */ |
||
76 | private $_rows = []; |
||
77 | /** |
||
78 | * @var array table chars |
||
79 | */ |
||
80 | private $_chars = [ |
||
81 | self::CHAR_TOP => '═', |
||
82 | self::CHAR_TOP_MID => '╤', |
||
83 | self::CHAR_TOP_LEFT => '╔', |
||
84 | self::CHAR_TOP_RIGHT => '╗', |
||
85 | self::CHAR_BOTTOM => '═', |
||
86 | self::CHAR_BOTTOM_MID => '╧', |
||
87 | self::CHAR_BOTTOM_LEFT => '╚', |
||
88 | self::CHAR_BOTTOM_RIGHT => '╝', |
||
89 | self::CHAR_LEFT => '║', |
||
90 | self::CHAR_LEFT_MID => '╟', |
||
91 | self::CHAR_MID => '─', |
||
92 | self::CHAR_MID_MID => '┼', |
||
93 | self::CHAR_RIGHT => '║', |
||
94 | self::CHAR_RIGHT_MID => '╢', |
||
95 | self::CHAR_MIDDLE => '│', |
||
96 | ]; |
||
97 | /** |
||
98 | * @var array table column widths |
||
99 | */ |
||
100 | private $_columnWidths = []; |
||
101 | /** |
||
102 | * @var int screen width |
||
103 | */ |
||
104 | private $_screenWidth; |
||
105 | /** |
||
106 | * @var string list prefix |
||
107 | */ |
||
108 | private $_listPrefix = '• '; |
||
109 | |||
110 | |||
111 | /** |
||
112 | * Set table headers. |
||
113 | * |
||
114 | * @param array $headers table headers |
||
115 | * @return $this |
||
116 | */ |
||
117 | 10 | public function setHeaders(array $headers) |
|
122 | |||
123 | /** |
||
124 | * Set table rows. |
||
125 | * |
||
126 | * @param array $rows table rows |
||
127 | * @return $this |
||
128 | */ |
||
129 | 11 | public function setRows(array $rows) |
|
134 | |||
135 | /** |
||
136 | * Set table chars. |
||
137 | * |
||
138 | * @param array $chars table chars |
||
139 | * @return $this |
||
140 | */ |
||
141 | 1 | public function setChars(array $chars) |
|
146 | |||
147 | /** |
||
148 | * Set screen width. |
||
149 | * |
||
150 | * @param int $width screen width |
||
151 | * @return $this |
||
152 | */ |
||
153 | 11 | public function setScreenWidth($width) |
|
158 | |||
159 | /** |
||
160 | * Set list prefix. |
||
161 | * |
||
162 | * @param string $listPrefix list prefix |
||
163 | * @return $this |
||
164 | */ |
||
165 | 1 | public function setListPrefix($listPrefix) |
|
170 | |||
171 | /** |
||
172 | * @return string the rendered table |
||
173 | */ |
||
174 | 11 | public function run() |
|
219 | |||
220 | /** |
||
221 | * Renders a row of data into a string. |
||
222 | * |
||
223 | * @param array $row row of data |
||
224 | * @param string $spanLeft character for left border |
||
225 | * @param string $spanMiddle character for middle border |
||
226 | * @param string $spanRight character for right border |
||
227 | * @return string |
||
228 | * @see \yii\console\widgets\Table::render() |
||
229 | */ |
||
230 | 11 | protected function renderRow(array $row, $spanLeft, $spanMiddle, $spanRight) |
|
277 | |||
278 | /** |
||
279 | * Renders separator. |
||
280 | * |
||
281 | * @param string $spanLeft character for left border |
||
282 | * @param string $spanMid character for middle border |
||
283 | * @param string $spanMidMid character for middle-middle border |
||
284 | * @param string $spanRight character for right border |
||
285 | * @return string the generated separator row |
||
286 | * @see \yii\console\widgets\Table::render() |
||
287 | */ |
||
288 | 11 | protected function renderSeparator($spanLeft, $spanMid, $spanMidMid, $spanRight) |
|
300 | |||
301 | /** |
||
302 | * Calculate the size of rows to draw anchor of columns in console. |
||
303 | * |
||
304 | * @see \yii\console\widgets\Table::render() |
||
305 | */ |
||
306 | 11 | protected function calculateRowsSize() |
|
351 | |||
352 | /** |
||
353 | * Calculate the height of a row. |
||
354 | * |
||
355 | * @param array $row |
||
356 | * @return int maximum row per cell |
||
357 | * @see \yii\console\widgets\Table::render() |
||
358 | */ |
||
359 | 11 | protected function calculateRowHeight($row) |
|
384 | |||
385 | /** |
||
386 | * Getting screen width. |
||
387 | * If it is not able to determine screen width, default value `123` will be set. |
||
388 | * |
||
389 | * @return int screen width |
||
390 | */ |
||
391 | 11 | protected function getScreenWidth() |
|
401 | } |
||
402 |
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.