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 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 |
||
19 | class HtmlTable extends HtmlSemDoubleElement { |
||
20 | use TableTrait; |
||
21 | private $_colCount; |
||
22 | private $_compileParts; |
||
23 | private $_footer; |
||
24 | private $_afterCompileEvents; |
||
25 | |||
26 | View Code Duplication | public function __construct($identifier, $rowCount, $colCount) { |
|
34 | |||
35 | /** |
||
36 | * {@inheritDoc} |
||
37 | * @see \Ajax\semantic\html\collections\table\TableTrait::getTable() |
||
38 | */ |
||
39 | protected function getTable() { |
||
42 | |||
43 | /** |
||
44 | * Returns/create eventually a part of the table corresponding to the $key : thead, tbody or tfoot |
||
45 | * @param string $key |
||
46 | * @return HtmlTableContent |
||
47 | */ |
||
48 | View Code Duplication | public function getPart($key) { |
|
57 | |||
58 | /** |
||
59 | * Returns/create eventually the body of the table |
||
60 | * @return \Ajax\semantic\html\content\table\HtmlTableContent |
||
61 | */ |
||
62 | public function getBody() { |
||
65 | |||
66 | /** |
||
67 | * Returns/create eventually the header of the table |
||
68 | * @return \Ajax\semantic\html\content\table\HtmlTableContent |
||
69 | */ |
||
70 | public function getHeader() { |
||
73 | |||
74 | /** |
||
75 | * Returns/create eventually the footer of the table |
||
76 | * @return \Ajax\semantic\html\content\table\HtmlTableContent |
||
77 | */ |
||
78 | public function getFooter() { |
||
81 | |||
82 | /** |
||
83 | * Checks if the part corresponding to $key exists |
||
84 | * @param string $key |
||
85 | * @return boolean |
||
86 | */ |
||
87 | public function hasPart($key) { |
||
90 | |||
91 | /** |
||
92 | * |
||
93 | * @param int $rowCount |
||
94 | * @param int $colCount |
||
95 | * @return \Ajax\semantic\html\content\table\HtmlTableContent |
||
96 | */ |
||
97 | public function setRowCount($rowCount, $colCount) { |
||
101 | |||
102 | /** |
||
103 | * Returns the cell (HtmlTD) at position $row,$col |
||
104 | * @param int $row |
||
105 | * @param int $col |
||
106 | * @return \Ajax\semantic\html\content\HtmlTD |
||
107 | */ |
||
108 | public function getCell($row, $col) { |
||
111 | |||
112 | /** |
||
113 | * Retuns the row at $rowIndex |
||
114 | * @param int $rowIndex |
||
115 | * @return \Ajax\semantic\html\content\HtmlTR |
||
116 | */ |
||
117 | public function getRow($rowIndex) { |
||
120 | |||
121 | /** |
||
122 | * Adds a new row and sets $values to his cols |
||
123 | * @param array $values |
||
124 | * @return HtmlTR |
||
125 | */ |
||
126 | public function addRow($values=array()) { |
||
131 | |||
132 | /** |
||
133 | * adds and returns a new row |
||
134 | * @return \Ajax\semantic\html\content\table\HtmlTR |
||
135 | */ |
||
136 | public function newRow() { |
||
139 | |||
140 | public function setValues($values=array()) { |
||
144 | |||
145 | public function setHeaderValues($values=array()) { |
||
148 | |||
149 | public function setFooterValues($values=array()) { |
||
152 | |||
153 | /** |
||
154 | * Sets values to the col at index $colIndex |
||
155 | * @param int $colIndex |
||
156 | * @param array $values |
||
157 | * @return \Ajax\semantic\html\collections\HtmlTable |
||
158 | */ |
||
159 | public function setColValues($colIndex, $values=array()) { |
||
163 | |||
164 | /** |
||
165 | * Sets values to the row at index $rowIndex |
||
166 | * @param int $rowIndex |
||
167 | * @param array $values |
||
168 | * @return \Ajax\semantic\html\collections\HtmlTable |
||
169 | */ |
||
170 | public function setRowValues($rowIndex, $values=array()) { |
||
174 | |||
175 | public function addColVariations($colIndex, $variations=array()) { |
||
178 | |||
179 | public function colCenter($colIndex) { |
||
182 | |||
183 | public function colRight($colIndex) { |
||
186 | |||
187 | public function colLeft($colIndex) { |
||
190 | |||
191 | View Code Duplication | private function colAlign($colIndex, $function) { |
|
204 | |||
205 | public function conditionalCellFormat($callback, $format) { |
||
209 | |||
210 | public function conditionalRowFormat($callback, $format) { |
||
214 | |||
215 | public function applyCells($callback) { |
||
219 | |||
220 | public function applyRows($callback) { |
||
224 | |||
225 | /** |
||
226 | * |
||
227 | * {@inheritDoc} |
||
228 | * |
||
229 | * @see \Ajax\semantic\html\base\HtmlSemDoubleElement::compile() |
||
230 | */ |
||
231 | View Code Duplication | public function compile(JsUtils $js=NULL, &$view=NULL) { |
|
243 | |||
244 | /** |
||
245 | * |
||
246 | * {@inheritDoc} |
||
247 | * |
||
248 | * @see \Ajax\common\html\BaseHtml::fromDatabaseObject() |
||
249 | */ |
||
250 | View Code Duplication | public function fromDatabaseObject($object, $function) { |
|
263 | |||
264 | /** |
||
265 | * @param array $parts |
||
266 | * @return \Ajax\semantic\html\collections\HtmlTable |
||
267 | */ |
||
268 | public function setCompileParts($parts=["tbody"]) { |
||
272 | |||
273 | public function refresh(){ |
||
277 | |||
278 | public function run(JsUtils $js){ |
||
284 | |||
285 | /** |
||
286 | * The callback function called after the insertion of each row when fromDatabaseObjects is called |
||
287 | * callback function takes the parameters $row : the row inserted and $object: the instance of model used |
||
288 | * @param callable $callback |
||
289 | * @return \Ajax\semantic\html\collections\HtmlTable |
||
290 | */ |
||
291 | public function onNewRow($callback) { |
||
295 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.