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 \Ajax\semantic\html\content\table\HtmlTableContent |
||
34 | */ |
||
35 | public function setRowCount($rowCount, $colCount) { |
||
47 | |||
48 | public function getTdTagName($tagName) { |
||
51 | |||
52 | /** |
||
53 | * |
||
54 | * {@inheritDoc} |
||
55 | * |
||
56 | * @see \Ajax\common\html\HtmlCollection::createItem() |
||
57 | */ |
||
58 | protected function createItem($value) { |
||
68 | |||
69 | public function addRow($colCount) { |
||
72 | |||
73 | /** |
||
74 | * Returns the cell (HtmlTD) at position $row,$col |
||
75 | * @param int $row |
||
76 | * @param int $col |
||
77 | * @return \Ajax\semantic\html\content\HtmlTD |
||
78 | */ |
||
79 | public function getCell($row, $col) { |
||
86 | |||
87 | /** |
||
88 | * |
||
89 | * @param int $index |
||
90 | * @return \Ajax\semantic\html\content\HtmlTR |
||
91 | */ |
||
92 | public function getRow($index) { |
||
95 | |||
96 | /** |
||
97 | * |
||
98 | * @param int $row |
||
99 | * @param int $col |
||
100 | * @param mixed $value |
||
101 | * @return \Ajax\semantic\html\content\table\HtmlTableContent |
||
102 | */ |
||
103 | public function setCellValue($row, $col, $value="") { |
||
110 | |||
111 | /** |
||
112 | * Sets the cells values |
||
113 | * @param mixed $values |
||
114 | */ |
||
115 | public function setValues($values=array()) { |
||
133 | |||
134 | View Code Duplication | public function setColValues($colIndex, $values=array()) { |
|
145 | |||
146 | public function addColVariations($colIndex, $variations=array()) { |
||
153 | |||
154 | public function setRowValues($rowIndex, $values=array()) { |
||
162 | |||
163 | public function colCenter($colIndex) { |
||
170 | |||
171 | public function colRight($colIndex) { |
||
178 | |||
179 | /** |
||
180 | * Returns the number of rows (TR) |
||
181 | * @return int |
||
182 | */ |
||
183 | public function getRowCount() { |
||
186 | |||
187 | /** |
||
188 | * Returns the number of columns (TD) |
||
189 | * @return int |
||
190 | */ |
||
191 | public function getColCount() { |
||
197 | |||
198 | /** |
||
199 | * Removes the cell at position $rowIndex,$colIndex |
||
200 | * @param int $rowIndex |
||
201 | * @param int $colIndex |
||
202 | * @return \Ajax\semantic\html\content\table\HtmlTableContent |
||
203 | */ |
||
204 | public function delete($rowIndex, $colIndex=NULL) { |
||
215 | |||
216 | public function mergeCol($rowIndex=0, $colIndex=0) { |
||
219 | |||
220 | public function mergeRow($rowIndex=0, $colIndex=0) { |
||
223 | |||
224 | public function setFullWidth() { |
||
227 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.