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 $_activeClass="warning"; |
||
27 | private $_activeRowEvent="click"; |
||
28 | |||
29 | public function __construct($identifier, $rowCount, $colCount) { |
||
37 | |||
38 | /** |
||
39 | * {@inheritDoc} |
||
40 | * @see \Ajax\semantic\html\collections\table\TableTrait::getTable() |
||
41 | */ |
||
42 | protected function getTable() { |
||
45 | |||
46 | /** |
||
47 | * Returns/create eventually a part of the table corresponding to the $key : thead, tbody or tfoot |
||
48 | * @param string $key |
||
49 | * @return HtmlTableContent |
||
50 | */ |
||
51 | public function getPart($key) { |
||
60 | |||
61 | /** |
||
62 | * Returns/create eventually the body of the table |
||
63 | * @return HtmlTableContent |
||
64 | */ |
||
65 | public function getBody() { |
||
68 | |||
69 | /** |
||
70 | * Returns the number of rows (TR) |
||
71 | * @return int |
||
72 | */ |
||
73 | public function getRowCount() { |
||
76 | |||
77 | /** |
||
78 | * Returns/create eventually the header of the table |
||
79 | * @return HtmlTableContent |
||
80 | */ |
||
81 | public function getHeader() { |
||
84 | |||
85 | /** |
||
86 | * Returns/create eventually the footer of the table |
||
87 | * @return \Ajax\semantic\html\content\table\HtmlTableContent |
||
88 | */ |
||
89 | public function getFooter() { |
||
92 | |||
93 | /** |
||
94 | * Checks if the part corresponding to $key exists |
||
95 | * @param string $key |
||
96 | * @return boolean |
||
97 | */ |
||
98 | public function hasPart($key) { |
||
101 | |||
102 | /** |
||
103 | * |
||
104 | * @param int $rowCount |
||
105 | * @param int $colCount |
||
106 | * @return HtmlTableContent |
||
107 | */ |
||
108 | public function setRowCount($rowCount, $colCount) { |
||
112 | |||
113 | /** |
||
114 | * Returns the cell (HtmlTD) at position $row,$col |
||
115 | * @param int $row |
||
116 | * @param int $col |
||
117 | * @return HtmlTD |
||
118 | */ |
||
119 | public function getCell($row, $col) { |
||
122 | |||
123 | /** |
||
124 | * Retuns the row at $rowIndex |
||
125 | * @param int $rowIndex |
||
126 | * @return HtmlTR |
||
127 | */ |
||
128 | public function getRow($rowIndex) { |
||
131 | |||
132 | /** |
||
133 | * Adds a new row and sets $values to his cols |
||
134 | * @param array $values |
||
135 | * @return HtmlTR |
||
136 | */ |
||
137 | public function addRow($values=array()) { |
||
142 | |||
143 | /** |
||
144 | * adds and returns a new row |
||
145 | * @return HtmlTR |
||
146 | */ |
||
147 | public function newRow() { |
||
150 | |||
151 | public function setValues($values=array()) { |
||
155 | |||
156 | public function setHeaderValues($values=array()) { |
||
159 | |||
160 | public function setFooterValues($values=array()) { |
||
163 | |||
164 | /** |
||
165 | * Sets values to the col at index $colIndex |
||
166 | * @param int $colIndex |
||
167 | * @param array $values |
||
168 | * @return HtmlTable |
||
169 | */ |
||
170 | public function setColValues($colIndex, $values=array()) { |
||
174 | |||
175 | /** |
||
176 | * Sets values to the row at index $rowIndex |
||
177 | * @param int $rowIndex |
||
178 | * @param array $values |
||
179 | * @return HtmlTable |
||
180 | */ |
||
181 | public function setRowValues($rowIndex, $values=array()) { |
||
185 | |||
186 | public function addColVariations($colIndex, $variations=array()) { |
||
189 | |||
190 | public function colCenter($colIndex) { |
||
193 | |||
194 | public function colRight($colIndex) { |
||
197 | |||
198 | public function colLeft($colIndex) { |
||
201 | |||
202 | private function colAlign($colIndex, $function) { |
||
215 | |||
216 | public function conditionalCellFormat($callback, $format) { |
||
220 | |||
221 | public function conditionalRowFormat($callback, $format) { |
||
225 | |||
226 | public function applyCells($callback) { |
||
230 | |||
231 | public function applyRows($callback) { |
||
235 | |||
236 | /** |
||
237 | * |
||
238 | * {@inheritDoc} |
||
239 | * |
||
240 | * @see \Ajax\semantic\html\base\HtmlSemDoubleElement::compile() |
||
241 | */ |
||
242 | public function compile(JsUtils $js=NULL, &$view=NULL) { |
||
250 | |||
251 | protected function compile_once(JsUtils $js=NULL, &$view=NULL) { |
||
259 | |||
260 | /** |
||
261 | * |
||
262 | * {@inheritDoc} |
||
263 | * |
||
264 | * @see \Ajax\common\html\BaseHtml::fromDatabaseObject() |
||
265 | */ |
||
266 | public function fromDatabaseObject($object, $function) { |
||
279 | |||
280 | /** |
||
281 | * @param array $parts |
||
282 | * @return HtmlTable |
||
283 | */ |
||
284 | public function setCompileParts($parts=["tbody"]) { |
||
288 | |||
289 | public function refresh(){ |
||
293 | |||
294 | public function run(JsUtils $js){ |
||
300 | |||
301 | /** |
||
302 | * The callback function called after the insertion of each row when fromDatabaseObjects is called |
||
303 | * callback function takes the parameters $row : the row inserted and $object: the instance of model used |
||
304 | * @param callable $callback |
||
305 | * @return HtmlTable |
||
306 | */ |
||
307 | public function onNewRow($callback) { |
||
311 | |||
312 | public function setActiveClass($_activeClass) { |
||
316 | |||
317 | public function setActiveRowEvent($_activeRowEvent) { |
||
321 | |||
322 | |||
323 | } |