Complex classes like HtmlTable 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 HtmlTable, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class HtmlTable extends HtmlSemDoubleElement { |
||
21 | use TableTrait; |
||
22 | private $_colCount; |
||
23 | private $_compileParts; |
||
24 | private $_footer; |
||
25 | private $_afterCompileEvents; |
||
26 | private $_activeRowSelector; |
||
27 | |||
28 | public function __construct($identifier, $rowCount, $colCount) { |
||
36 | |||
37 | /** |
||
38 | * {@inheritDoc} |
||
39 | * @see TableTrait::getTable() |
||
40 | */ |
||
41 | protected function getTable() { |
||
44 | |||
45 | /** |
||
46 | * Returns/create eventually a part of the table corresponding to the $key : thead, tbody or tfoot |
||
47 | * @param string $key |
||
48 | * @return HtmlTableContent |
||
49 | */ |
||
50 | public function getPart($key) { |
||
59 | |||
60 | protected function _getFirstPart(){ |
||
66 | |||
67 | |||
68 | /** |
||
69 | * Returns/create eventually the body of the table |
||
70 | * @return HtmlTableContent |
||
71 | */ |
||
72 | public function getBody() { |
||
75 | |||
76 | /** |
||
77 | * Returns the number of rows (TR) |
||
78 | * @return int |
||
79 | */ |
||
80 | public function getRowCount() { |
||
83 | |||
84 | /** |
||
85 | * Returns/create eventually the header of the table |
||
86 | * @return HtmlTableContent |
||
87 | */ |
||
88 | public function getHeader() { |
||
91 | |||
92 | /** |
||
93 | * Returns/create eventually the footer of the table |
||
94 | * @return \Ajax\semantic\html\content\table\HtmlTableContent |
||
95 | */ |
||
96 | public function getFooter() { |
||
99 | |||
100 | /** |
||
101 | * Checks if the part corresponding to $key exists |
||
102 | * @param string $key |
||
103 | * @return boolean |
||
104 | */ |
||
105 | public function hasPart($key) { |
||
108 | |||
109 | /** |
||
110 | * |
||
111 | * @param int $rowCount |
||
112 | * @param int $colCount |
||
113 | * @return HtmlTableContent |
||
114 | */ |
||
115 | public function setRowCount($rowCount, $colCount) { |
||
119 | |||
120 | /** |
||
121 | * Returns the cell (HtmlTD) at position $row,$col |
||
122 | * @param int $row |
||
123 | * @param int $col |
||
124 | * @return HtmlTD |
||
125 | */ |
||
126 | public function getCell($row, $col) { |
||
129 | |||
130 | /** |
||
131 | * Retuns the row at $rowIndex |
||
132 | * @param int $rowIndex |
||
133 | * @return HtmlTR |
||
134 | */ |
||
135 | public function getRow($rowIndex) { |
||
138 | |||
139 | /** |
||
140 | * Adds a new row and sets $values to his cols |
||
141 | * @param array $values |
||
142 | * @return HtmlTR |
||
143 | */ |
||
144 | public function addRow($values=array()) { |
||
149 | |||
150 | /** |
||
151 | * adds and returns a new row |
||
152 | * @return HtmlTR |
||
153 | */ |
||
154 | public function newRow() { |
||
157 | |||
158 | /** |
||
159 | * Sets the tbody values |
||
160 | * @param array $values values in an array of array |
||
161 | * @return HtmlTable |
||
162 | */ |
||
163 | public function setValues($values=array()) { |
||
167 | |||
168 | /** |
||
169 | * Sets the header values |
||
170 | * @param array $values |
||
171 | * @return HtmlTableContent |
||
172 | */ |
||
173 | public function setHeaderValues($values=array()) { |
||
176 | |||
177 | /** |
||
178 | * Sets the footer values |
||
179 | * @param array $values |
||
180 | * @return HtmlTableContent |
||
181 | */ |
||
182 | public function setFooterValues($values=array()) { |
||
185 | |||
186 | /** |
||
187 | * Sets values to the col at index $colIndex |
||
188 | * @param int $colIndex |
||
189 | * @param array $values |
||
190 | * @return HtmlTable |
||
191 | */ |
||
192 | public function setColValues($colIndex, $values=array()) { |
||
196 | |||
197 | /** |
||
198 | * Sets values to the row at index $rowIndex |
||
199 | * @param int $rowIndex |
||
200 | * @param array $values |
||
201 | * @return HtmlTable |
||
202 | */ |
||
203 | public function setRowValues($rowIndex, $values=array()) { |
||
207 | |||
208 | public function addColVariations($colIndex, $variations=array()) { |
||
211 | |||
212 | /** |
||
213 | * Sets the col alignment to center |
||
214 | * @param int $colIndex |
||
215 | * @return HtmlTable |
||
216 | */ |
||
217 | public function colCenter($colIndex) { |
||
220 | |||
221 | /** |
||
222 | * Sets the col alignment to right |
||
223 | * @param int $colIndex |
||
224 | * @return HtmlTable |
||
225 | */ |
||
226 | public function colRight($colIndex) { |
||
229 | |||
230 | /** |
||
231 | * Sets col alignment to left |
||
232 | * @param int $colIndex |
||
233 | * @return HtmlTable |
||
234 | */ |
||
235 | public function colLeft($colIndex) { |
||
238 | |||
239 | private function colAlign($colIndex, $function) { |
||
252 | |||
253 | /** |
||
254 | * Applies a format on each cell when $callback returns true |
||
255 | * @param callable $callback function with the cell as parameter, must return a boolean |
||
256 | * @param string $format css class to apply |
||
257 | * @return HtmlTable |
||
258 | */ |
||
259 | public function conditionalCellFormat($callback, $format) { |
||
263 | |||
264 | /** |
||
265 | * Applies a format on each row when $callback returns true |
||
266 | * @param callable $callback function with the row as parameter, must return a boolean |
||
267 | * @param string $format css class to apply |
||
268 | * @return HtmlTable |
||
269 | */ |
||
270 | public function conditionalRowFormat($callback, $format) { |
||
274 | |||
275 | /** |
||
276 | * Applies a callback function on each cell |
||
277 | * @param callable $callback |
||
278 | * @return HtmlTable |
||
279 | */ |
||
280 | public function applyCells($callback) { |
||
284 | |||
285 | /** |
||
286 | * Applies a callback function on each row |
||
287 | * @param callable $callback |
||
288 | * @return HtmlTable |
||
289 | */ |
||
290 | public function applyRows($callback) { |
||
294 | |||
295 | /** |
||
296 | * |
||
297 | * {@inheritDoc} |
||
298 | * |
||
299 | * @see HtmlSemDoubleElement::compile() |
||
300 | */ |
||
301 | public function compile(JsUtils $js=NULL, &$view=NULL) { |
||
309 | |||
310 | protected function compile_once(JsUtils $js=NULL, &$view=NULL) { |
||
318 | |||
319 | /** |
||
320 | * |
||
321 | * {@inheritDoc} |
||
322 | * |
||
323 | * @see BaseHtml::fromDatabaseObject() |
||
324 | */ |
||
325 | public function fromDatabaseObject($object, $function) { |
||
338 | |||
339 | /** |
||
340 | * Sets the parts of the Table to compile |
||
341 | * @param array $parts array of thead,tbody,tfoot |
||
342 | * @return HtmlTable |
||
343 | */ |
||
344 | public function setCompileParts($parts=["tbody"]) { |
||
348 | |||
349 | public function refresh(){ |
||
353 | |||
354 | public function run(JsUtils $js){ |
||
360 | |||
361 | /** |
||
362 | * The callback function called after the insertion of each row when fromDatabaseObjects is called |
||
363 | * callback function takes the parameters $row : the row inserted and $object: the instance of model used |
||
364 | * @param callable $callback |
||
365 | * @return HtmlTable |
||
366 | */ |
||
367 | public function onNewRow($callback) { |
||
371 | |||
372 | /** |
||
373 | * Defines how a row is selectable |
||
374 | * @param string $class |
||
375 | * @param string $event |
||
376 | * @param boolean $multiple |
||
377 | * @return HtmlTable |
||
378 | */ |
||
379 | public function setActiveRowSelector($class="active",$event="click",$multiple=false){ |
||
383 | |||
384 | public function hideColumn($colIndex){ |
||
394 | |||
395 | public function setColWidth($colIndex,$width){ |
||
401 | |||
402 | public function setColWidths($widths){ |
||
416 | } |