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 Table 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 Table, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Table extends Element |
||
26 | { |
||
27 | /** @var \UIComponents\Template\Template $template */ |
||
28 | public $template = null; |
||
29 | |||
30 | /** @var array $data */ |
||
31 | public $data = array(); |
||
32 | |||
33 | /** @var array $options */ |
||
34 | public $options = array(); |
||
35 | |||
36 | /** @var array $tags */ |
||
37 | public $tags = array( |
||
38 | "container" => "table", |
||
39 | "row" => "tr", |
||
40 | "cell" => "td", |
||
41 | "headcell" => "th" |
||
42 | ); |
||
43 | |||
44 | /** |
||
45 | * View helper entry point: |
||
46 | * Retrieves helper and optionally sets component options to operate on |
||
47 | * |
||
48 | * @param array|StdClass $config table configuration to operate on |
||
49 | * @param array|StdClass $options [optional] component options to operate on |
||
50 | * @return self |
||
51 | */ |
||
52 | public function __invoke($config = array(), $options = array()) |
||
101 | |||
102 | /** |
||
103 | * generate framework classnames collection as zend-config object |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | public function render() |
||
111 | |||
112 | /** |
||
113 | * generate table header HTML |
||
114 | * @param mixed $aHTMLTableColumns |
||
115 | * @return self |
||
116 | */ |
||
117 | public function buildHeaderCells ($aHTMLTableColumns) { |
||
135 | |||
136 | /** |
||
137 | * generate table footer HTML |
||
138 | * @param mixed $aHTMLTableColumns |
||
139 | * @return self |
||
140 | */ |
||
141 | public function buildFooterCells ($aHTMLTableColumns) { |
||
155 | |||
156 | /** |
||
157 | * generate table body HTML |
||
158 | * @param array $aRowData |
||
159 | * @param mixed $aHTMLTableColumns |
||
160 | * @return array |
||
161 | */ |
||
162 | public function buildBodyCells ($aRowData, $aHTMLTableColumns) { |
||
196 | |||
197 | /** |
||
198 | * generate mini table mark-up template |
||
199 | * @return string |
||
200 | */ |
||
201 | public function buildMarkupTemplate () { |
||
220 | |||
221 | /** |
||
222 | * generate table mark-up |
||
223 | * @return string |
||
224 | */ |
||
225 | public function buildMarkup () { |
||
246 | |||
247 | /** |
||
248 | * generate table JSON data |
||
249 | * @return string |
||
250 | */ |
||
251 | public function buildData () { |
||
258 | |||
259 | /** |
||
260 | * return template object |
||
261 | * @return Template |
||
262 | */ |
||
263 | public function getTemplate() { |
||
269 | |||
270 | /** |
||
271 | * generate template object |
||
272 | * @param Template $template |
||
273 | * @return ProductTable |
||
274 | */ |
||
275 | public function setTemplate( $template = null ) { |
||
283 | |||
284 | /** |
||
285 | * return table data |
||
286 | * @return mixed |
||
287 | */ |
||
288 | public function getData() { |
||
291 | |||
292 | /** |
||
293 | * set new table data |
||
294 | * @param mixed $data |
||
295 | * @return ProductTable |
||
296 | */ |
||
297 | public function setData( $data = null) { |
||
303 | |||
304 | /** |
||
305 | * return option by key or complete option set |
||
306 | * @param string $key |
||
307 | * @return mixed |
||
308 | */ |
||
309 | public function getOptions( $key = "" ) { |
||
319 | |||
320 | /** |
||
321 | * @param object|array $options |
||
322 | * @return ProductTable |
||
323 | */ |
||
324 | public function setOptions($options) { |
||
338 | |||
339 | |||
340 | } |
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.