Complex classes like HtmlTableContent 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 HtmlTableContent, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class HtmlTableContent extends HtmlSemCollection { |
||
14 | protected $_tdTagNames=[ "thead" => "th","tbody" => "td","tfoot" => "th" ]; |
||
15 | |||
16 | /** |
||
17 | * |
||
18 | * @param string $identifier |
||
19 | * @param string $tagName |
||
20 | * @param int $rowCount |
||
21 | * @param int $colCount |
||
22 | */ |
||
23 | public function __construct($identifier, $tagName="tbody", $rowCount=NULL, $colCount=NULL) { |
||
28 | |||
29 | /** |
||
30 | * |
||
31 | * @param int $rowCount |
||
32 | * @param int $colCount |
||
33 | * @return \Ajax\semantic\html\content\table\HtmlTableContent |
||
34 | */ |
||
35 | public function setRowCount($rowCount, $colCount) { |
||
49 | |||
50 | public function getTdTagName($tagName) { |
||
53 | |||
54 | /** |
||
55 | * |
||
56 | * {@inheritDoc} |
||
57 | * |
||
58 | * @see \Ajax\common\html\HtmlCollection::createItem() |
||
59 | */ |
||
60 | protected function createItem($value) { |
||
70 | |||
71 | public function newRow($value) { |
||
74 | |||
75 | public function addRow($colCount) { |
||
78 | |||
79 | public function _addRow($row) { |
||
82 | |||
83 | /** |
||
84 | * Returns the cell (HtmlTD) at position $row,$col |
||
85 | * @param int $row |
||
86 | * @param int $col |
||
87 | * @return \Ajax\semantic\html\content\HtmlTD |
||
88 | */ |
||
89 | public function getCell($row, $col) { |
||
96 | |||
97 | /** |
||
98 | * |
||
99 | * @param int $index |
||
100 | * @return \Ajax\semantic\html\content\HtmlTR |
||
101 | */ |
||
102 | public function getRow($index) { |
||
105 | |||
106 | /** |
||
107 | * |
||
108 | * @param int $row |
||
109 | * @param int $col |
||
110 | * @param mixed $value |
||
111 | * @return \Ajax\semantic\html\content\table\HtmlTableContent |
||
112 | */ |
||
113 | public function setCellValue($row, $col, $value="") { |
||
120 | |||
121 | /** |
||
122 | * Sets the cells values |
||
123 | * @param mixed $values |
||
124 | */ |
||
125 | public function setValues($values=array()) { |
||
143 | |||
144 | public function setColValues($colIndex, $values=array()) { |
||
155 | |||
156 | public function addColVariations($colIndex, $variations=array()) { |
||
163 | |||
164 | public function setRowValues($rowIndex, $values=array()) { |
||
172 | |||
173 | private function colAlign($colIndex, $function) { |
||
182 | |||
183 | public function colCenter($colIndex) { |
||
186 | |||
187 | public function colRight($colIndex) { |
||
190 | |||
191 | public function colLeft($colIndex) { |
||
194 | |||
195 | /** |
||
196 | * Returns the number of rows (TR) |
||
197 | * @return int |
||
198 | */ |
||
199 | public function getRowCount() { |
||
202 | |||
203 | /** |
||
204 | * Returns the number of columns (TD) |
||
205 | * @return int |
||
206 | */ |
||
207 | public function getColCount() { |
||
213 | |||
214 | /** |
||
215 | * Removes the cell at position $rowIndex,$colIndex |
||
216 | * @param int $rowIndex |
||
217 | * @param int $colIndex |
||
218 | * @return \Ajax\semantic\html\content\table\HtmlTableContent |
||
219 | */ |
||
220 | public function delete($rowIndex, $colIndex=NULL) { |
||
231 | |||
232 | public function mergeCol($rowIndex=0, $colIndex=0) { |
||
235 | |||
236 | public function mergeRow($rowIndex=0, $colIndex=0) { |
||
239 | |||
240 | public function setFullWidth() { |
||
243 | |||
244 | public function sort($colIndex) { |
||
247 | |||
248 | /** |
||
249 | * @param mixed $callback |
||
250 | * @param string $format |
||
251 | * @return \Ajax\semantic\html\content\table\HtmlTableContent |
||
252 | */ |
||
253 | public function conditionalCellFormat($callback, $format) { |
||
260 | |||
261 | /** |
||
262 | * @param mixed $callback |
||
263 | * @param string $format |
||
264 | * @return \Ajax\semantic\html\content\table\HtmlTableContent |
||
265 | */ |
||
266 | public function conditionalRowFormat($callback, $format) { |
||
273 | |||
274 | /** |
||
275 | * @param mixed $callback |
||
276 | * @return \Ajax\semantic\html\content\table\HtmlTableContent |
||
277 | */ |
||
278 | public function applyCells($callback) { |
||
285 | |||
286 | /** |
||
287 | * @param mixed $callback |
||
288 | * @return \Ajax\semantic\html\content\table\HtmlTableContent |
||
289 | */ |
||
290 | public function applyRows($callback) { |
||
297 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.