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 autoinstallModel 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 autoinstallModel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class autoinstallModel extends autoinstall |
||
9 | { |
||
10 | |||
11 | /** |
||
12 | * Get category information |
||
13 | * |
||
14 | * @param int $category_srl The sequence of category to get information |
||
15 | * @return object |
||
16 | */ |
||
17 | View Code Duplication | function getCategory($category_srl) |
|
28 | |||
29 | /** |
||
30 | * Get packages information |
||
31 | * |
||
32 | * @return array |
||
33 | */ |
||
34 | function getPackages() |
||
43 | |||
44 | /** |
||
45 | * Get installed packages information |
||
46 | * |
||
47 | * @param int $package_srl The sequence of package to get information |
||
48 | * @return object |
||
49 | */ |
||
50 | View Code Duplication | function getInstalledPackage($package_srl) |
|
61 | |||
62 | /** |
||
63 | * Get one package information |
||
64 | * |
||
65 | * @param int $package_srl The sequence of package to get information |
||
66 | * @return object |
||
67 | */ |
||
68 | View Code Duplication | function getPackage($package_srl) |
|
79 | |||
80 | /** |
||
81 | * Get category list |
||
82 | * |
||
83 | * @return array |
||
84 | */ |
||
85 | function getCategoryList() |
||
119 | |||
120 | /** |
||
121 | * Get pcakge count in category |
||
122 | * |
||
123 | * @param int $category_srl The sequence of category to get count |
||
124 | * @return int |
||
125 | */ |
||
126 | function getPackageCount($category_srl) |
||
137 | |||
138 | /** |
||
139 | * Get installed package count |
||
140 | * |
||
141 | * @return int |
||
142 | */ |
||
143 | function getInstalledPackageCount() |
||
152 | |||
153 | /** |
||
154 | * Set depth, children list and package count of category |
||
155 | * |
||
156 | * @param object $item Category information |
||
157 | * @param int $depth Depth of category |
||
158 | * @param array $list Category list |
||
159 | * @param array $resultList Final result list |
||
160 | * @return string $siblingList Comma seperated list |
||
161 | */ |
||
162 | function setDepth(&$item, $depth, &$list, &$resultList) |
||
178 | |||
179 | /** |
||
180 | * Get lastest package information |
||
181 | * |
||
182 | * @return object Returns lastest package information. If no result returns null. |
||
183 | */ |
||
184 | function getLatestPackage() |
||
193 | |||
194 | /** |
||
195 | * Get installed package informations |
||
196 | * |
||
197 | * @param array $package_list Package sequence list to get information |
||
198 | * @return array Returns array contains pacakge information. If no result returns empty array. |
||
199 | */ |
||
200 | function getInstalledPackages($package_list) |
||
216 | |||
217 | /** |
||
218 | * Get installed package list |
||
219 | * |
||
220 | * @param int $page |
||
221 | * @return Object |
||
222 | */ |
||
223 | function getInstalledPackageList($page) |
||
245 | |||
246 | /** |
||
247 | * Get type using path |
||
248 | * |
||
249 | * @param string $path Path to get type |
||
250 | * @return string |
||
251 | */ |
||
252 | function getTypeFromPath($path) |
||
273 | |||
274 | /** |
||
275 | * Get config file path by type |
||
276 | * |
||
277 | * @param string $type Type to get config file path |
||
278 | * @return string |
||
279 | */ |
||
280 | function getConfigFilePath($type) |
||
308 | |||
309 | /** |
||
310 | * Returns target is removable |
||
311 | * |
||
312 | * @param string $path Path |
||
313 | * @return bool |
||
314 | */ |
||
315 | function checkRemovable($path) |
||
333 | |||
334 | /** |
||
335 | * Get sequence of package by path |
||
336 | * |
||
337 | * @param string $path Path to get sequence |
||
338 | * @return int |
||
339 | */ |
||
340 | function getPackageSrlByPath($path) |
||
363 | |||
364 | /** |
||
365 | * Get remove url by package srl |
||
366 | * |
||
367 | * @param int $packageSrl Sequence of pakcage to get url |
||
368 | * @return string |
||
369 | */ |
||
370 | function getRemoveUrlByPackageSrl($packageSrl) |
||
385 | |||
386 | /** |
||
387 | * Get remove url by path |
||
388 | * |
||
389 | * @param string $path Path to get url |
||
390 | * @return string |
||
391 | */ |
||
392 | function getRemoveUrlByPath($path) |
||
413 | |||
414 | /** |
||
415 | * Get update url by package srl |
||
416 | * |
||
417 | * @param int $packageSrl Sequence to get url |
||
418 | * @return string |
||
419 | */ |
||
420 | function getUpdateUrlByPackageSrl($packageSrl) |
||
429 | |||
430 | /** |
||
431 | * Get update url by path |
||
432 | * |
||
433 | * @param string $path Path to get url |
||
434 | * @return string |
||
435 | */ |
||
436 | function getUpdateUrlByPath($path) |
||
451 | |||
452 | function getHaveInstance($columnList = array()) |
||
462 | |||
463 | } |
||
464 | /* End of file autoinstall.model.php */ |
||
466 |
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.