Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 HtmlTableContent |
||
34 | */ |
||
35 | public function setRowCount($rowCount, $colCount) { |
||
42 | |||
43 | public function getTdTagName($tagName) { |
||
46 | |||
47 | /** |
||
48 | * |
||
49 | * {@inheritDoc} |
||
50 | * |
||
51 | * @see \Ajax\common\html\HtmlCollection::createItem() |
||
52 | * @return HtmlTR |
||
53 | */ |
||
54 | protected function createItem($value) { |
||
64 | |||
65 | public function newRow($value) { |
||
68 | |||
69 | /** |
||
70 | * @param int $colCount |
||
71 | * @return HtmlTR |
||
72 | */ |
||
73 | public function addRow($colCount) { |
||
76 | |||
77 | /** |
||
78 | * @param mixed $row |
||
79 | * @return HtmlTR |
||
80 | */ |
||
81 | public function _addRow($row) { |
||
84 | |||
85 | /** |
||
86 | * Returns the cell (HtmlTD) at position $row,$col |
||
87 | * @param int $row |
||
88 | * @param int $col |
||
89 | * @return HtmlTD |
||
90 | */ |
||
91 | public function getCell($row, $col) { |
||
98 | |||
99 | /** |
||
100 | * |
||
101 | * @param int $index |
||
102 | * @return HtmlTR |
||
103 | */ |
||
104 | public function getRow($index) { |
||
107 | |||
108 | /** |
||
109 | * |
||
110 | * @param int $row |
||
111 | * @param int $col |
||
112 | * @param mixed $value |
||
113 | * @return HtmlTableContent |
||
114 | */ |
||
115 | public function setCellValue($row, $col, $value="") { |
||
122 | |||
123 | /** |
||
124 | * Sets the cells values |
||
125 | * @param mixed $values |
||
126 | */ |
||
127 | public function setValues($values=array()) { |
||
130 | |||
131 | /** |
||
132 | * Adds the cells values |
||
133 | * @param mixed $values |
||
134 | */ |
||
135 | public function addValues($values=array()) { |
||
138 | |||
139 | /** |
||
140 | * Adds or sets the cells values |
||
141 | * @param mixed $values |
||
142 | * @param callable $callback |
||
143 | */ |
||
144 | protected function _addOrSetValues($values,$callback) { |
||
162 | |||
163 | public function setColValues($colIndex, $values=array()) { |
||
174 | |||
175 | public function addColVariations($colIndex, $variations=array()) { |
||
182 | |||
183 | public function setRowValues($rowIndex, $values=array()) { |
||
191 | |||
192 | private function colAlign($colIndex, $function) { |
||
201 | |||
202 | public function colCenter($colIndex) { |
||
205 | |||
206 | public function colRight($colIndex) { |
||
209 | |||
210 | public function colLeft($colIndex) { |
||
213 | |||
214 | /** |
||
215 | * Returns the number of rows (TR) |
||
216 | * @return int |
||
217 | */ |
||
218 | public function getRowCount() { |
||
221 | |||
222 | /** |
||
223 | * Returns the number of columns (TD) |
||
224 | * @return int |
||
225 | */ |
||
226 | public function getColCount() { |
||
232 | |||
233 | /** |
||
234 | * Removes the cell at position $rowIndex,$colIndex |
||
235 | * @param int $rowIndex |
||
236 | * @param int $colIndex |
||
237 | * @return HtmlTableContent |
||
238 | */ |
||
239 | public function delete($rowIndex, $colIndex=NULL) { |
||
250 | |||
251 | public function toDelete($rowIndex, $colIndex){ |
||
257 | |||
258 | public function mergeCol($rowIndex=0, $colIndex=0) { |
||
261 | |||
262 | public function mergeRow($rowIndex=0, $colIndex=0) { |
||
265 | |||
266 | public function setFullWidth() { |
||
269 | |||
270 | public function sort($colIndex) { |
||
274 | |||
275 | /** |
||
276 | * @param mixed $callback |
||
277 | * @param string $format |
||
278 | * @return HtmlTableContent |
||
279 | */ |
||
280 | public function conditionalCellFormat($callback, $format) { |
||
287 | |||
288 | public function conditionalColFormat($colIndex,$callback,$format){ |
||
296 | |||
297 | /** |
||
298 | * @param mixed $callback |
||
299 | * @param string $format |
||
300 | * @return HtmlTableContent |
||
301 | */ |
||
302 | public function conditionalRowFormat($callback, $format) { |
||
309 | |||
310 | public function hideColumn($colIndex){ |
||
318 | |||
319 | /** |
||
320 | * @param mixed $callback |
||
321 | * @return HtmlTableContent |
||
322 | */ |
||
323 | public function applyCells($callback) { |
||
330 | |||
331 | /** |
||
332 | * @param mixed $callback |
||
333 | * @return HtmlTableContent |
||
334 | */ |
||
335 | public function applyRows($callback) { |
||
342 | |||
343 | public function mergeIdentiqualValues($colIndex,$function="strip_tags"){ |
||
366 | } |
||
367 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.