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 | protected $_merged=false; |
||
16 | |||
17 | /** |
||
18 | * |
||
19 | * @param string $identifier |
||
20 | * @param string $tagName |
||
21 | * @param int $rowCount |
||
22 | * @param int $colCount |
||
23 | */ |
||
24 | public function __construct($identifier, $tagName="tbody", $rowCount=NULL, $colCount=NULL) { |
||
29 | |||
30 | /** |
||
31 | * |
||
32 | * @param int $rowCount |
||
33 | * @param int $colCount |
||
34 | * @return HtmlTableContent |
||
35 | */ |
||
36 | public function setRowCount($rowCount, $colCount) { |
||
43 | |||
44 | public function getTdTagName($tagName) { |
||
47 | |||
48 | /** |
||
49 | * |
||
50 | * {@inheritDoc} |
||
51 | * |
||
52 | * @see \Ajax\common\html\HtmlCollection::createItem() |
||
53 | * @return HtmlTR |
||
54 | */ |
||
55 | protected function createItem($value) { |
||
65 | |||
66 | public function newRow($value) { |
||
69 | |||
70 | /** |
||
71 | * @param int $colCount |
||
72 | * @return HtmlTR |
||
73 | */ |
||
74 | public function addRow($colCount) { |
||
77 | |||
78 | /** |
||
79 | * @param mixed $row |
||
80 | * @return HtmlTR |
||
81 | */ |
||
82 | public function _addRow($row) { |
||
85 | |||
86 | /** |
||
87 | * Returns the cell (HtmlTD) at position $row,$col |
||
88 | * @param int $row |
||
89 | * @param int $col |
||
90 | * @return HtmlTD |
||
91 | */ |
||
92 | public function getCell($row, $col) { |
||
99 | |||
100 | /** |
||
101 | * |
||
102 | * @param int $index |
||
103 | * @return HtmlTR |
||
104 | */ |
||
105 | public function getRow($index) { |
||
108 | |||
109 | /** |
||
110 | * |
||
111 | * @param int $row |
||
112 | * @param int $col |
||
113 | * @param mixed $value |
||
114 | * @return HtmlTableContent |
||
115 | */ |
||
116 | public function setCellValue($row, $col, $value="") { |
||
123 | |||
124 | /** |
||
125 | * Sets the cells values |
||
126 | * @param mixed $values |
||
127 | */ |
||
128 | public function setValues($values=array()) { |
||
131 | |||
132 | /** |
||
133 | * Adds the cells values |
||
134 | * @param mixed $values |
||
135 | */ |
||
136 | public function addValues($values=array()) { |
||
139 | |||
140 | /** |
||
141 | * Adds or sets the cells values |
||
142 | * @param mixed $values |
||
143 | * @param callable $callback |
||
144 | */ |
||
145 | protected function _addOrSetValues($values,$callback) { |
||
163 | |||
164 | public function setColValues($colIndex, $values=array()) { |
||
175 | |||
176 | public function addColVariations($colIndex, $variations=array()) { |
||
183 | |||
184 | public function setRowValues($rowIndex, $values=array()) { |
||
192 | |||
193 | private function colAlign($colIndex, $function) { |
||
202 | |||
203 | public function colCenter($colIndex) { |
||
206 | |||
207 | public function colRight($colIndex) { |
||
210 | |||
211 | public function colLeft($colIndex) { |
||
214 | |||
215 | /** |
||
216 | * Returns the number of rows (TR) |
||
217 | * @return int |
||
218 | */ |
||
219 | public function getRowCount() { |
||
222 | |||
223 | /** |
||
224 | * Returns the number of columns (TD) |
||
225 | * @return int |
||
226 | */ |
||
227 | public function getColCount() { |
||
233 | |||
234 | /** |
||
235 | * Removes the cell at position $rowIndex,$colIndex |
||
236 | * @param int $rowIndex |
||
237 | * @param int $colIndex |
||
238 | * @return HtmlTableContent |
||
239 | */ |
||
240 | public function delete($rowIndex, $colIndex=NULL) { |
||
251 | |||
252 | public function toDelete($rowIndex, $colIndex){ |
||
258 | |||
259 | public function mergeCol($rowIndex=0, $colIndex=0) { |
||
262 | |||
263 | public function mergeRow($rowIndex=0, $colIndex=0) { |
||
266 | |||
267 | public function setFullWidth() { |
||
270 | |||
271 | public function sort($colIndex) { |
||
275 | |||
276 | /** |
||
277 | * @param mixed $callback |
||
278 | * @param string $format |
||
279 | * @return HtmlTableContent |
||
280 | */ |
||
281 | public function conditionalCellFormat($callback, $format) { |
||
288 | |||
289 | public function conditionalColFormat($colIndex,$callback,$format){ |
||
297 | |||
298 | /** |
||
299 | * @param mixed $callback |
||
300 | * @param string $format |
||
301 | * @return HtmlTableContent |
||
302 | */ |
||
303 | public function conditionalRowFormat($callback, $format) { |
||
310 | |||
311 | public function hideColumn($colIndex){ |
||
319 | |||
320 | /** |
||
321 | * @param mixed $callback |
||
322 | * @return HtmlTableContent |
||
323 | */ |
||
324 | public function applyCells($callback) { |
||
331 | |||
332 | /** |
||
333 | * @param mixed $callback |
||
334 | * @return HtmlTableContent |
||
335 | */ |
||
336 | public function applyRows($callback) { |
||
343 | |||
344 | public function mergeIdentiqualValues($colIndex,$function="strip_tags"){ |
||
370 | |||
371 | public function _isMerged(){ |
||
374 | |||
375 | public function _setMerged($value){ |
||
379 | } |
||
380 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.