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 Module 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 Module, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Module |
||
12 | { |
||
13 | /** |
||
14 | * Storage of cur requested module |
||
15 | * |
||
16 | * @var \Module |
||
17 | */ |
||
18 | public static $cur = null; |
||
19 | |||
20 | /** |
||
21 | * Module name |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | public $moduleName = ''; |
||
26 | |||
27 | /** |
||
28 | * Module config |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | public $config = []; |
||
33 | |||
34 | /** |
||
35 | * Module info |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | public $info = []; |
||
40 | |||
41 | /** |
||
42 | * Requested module params |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | public $params = []; |
||
47 | |||
48 | /** |
||
49 | * Module directory path |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | public $path = ''; |
||
54 | |||
55 | /** |
||
56 | * Module app |
||
57 | * |
||
58 | * @var \App |
||
59 | */ |
||
60 | public $app = null; |
||
61 | |||
62 | /** |
||
63 | * Parse cur module |
||
64 | * |
||
65 | * @param \App $app |
||
66 | */ |
||
67 | public function __construct($app) |
||
79 | |||
80 | /** |
||
81 | * Get all posible directorys for module files |
||
82 | * |
||
83 | * @param string $moduleName |
||
84 | * @return array |
||
85 | */ |
||
86 | public static function getModulePaths($moduleName) |
||
97 | |||
98 | /** |
||
99 | * Return directory where places module file |
||
100 | * |
||
101 | * @param string $moduleName |
||
102 | * @return string |
||
103 | */ |
||
104 | public static function getModulePath($moduleName) |
||
114 | |||
115 | /** |
||
116 | * Check module for installed |
||
117 | * |
||
118 | * @param string $moduleName |
||
119 | * @param \App $app |
||
120 | * @return boolean |
||
121 | */ |
||
122 | public static function installed($moduleName, $app) |
||
129 | |||
130 | /** |
||
131 | * Get installed modules for app |
||
132 | * |
||
133 | * @param \App $app |
||
134 | * @param boolean|\App $primary |
||
135 | * @return array |
||
136 | */ |
||
137 | public static function getInstalled($app, $primary = false) |
||
148 | |||
149 | /** |
||
150 | * Find module controllers |
||
151 | * |
||
152 | * @param string $moduleName |
||
153 | * @return array |
||
154 | */ |
||
155 | public static function getModuleControllers($moduleName) |
||
176 | |||
177 | /** |
||
178 | * Find module by request |
||
179 | * |
||
180 | * @param \App $app |
||
181 | * @return \Module |
||
182 | */ |
||
183 | public static function resolveModule($app) |
||
203 | |||
204 | /** |
||
205 | * Get posible path for controller |
||
206 | * |
||
207 | * @return array |
||
208 | */ |
||
209 | public function getControllerPaths() |
||
242 | |||
243 | /** |
||
244 | * Find controller by request |
||
245 | * |
||
246 | * @return \Controller |
||
247 | */ |
||
248 | public function findController() |
||
271 | |||
272 | /** |
||
273 | * Return module info |
||
274 | * |
||
275 | * @param string $moduleName |
||
276 | * @return array |
||
277 | */ |
||
278 | public static function getInfo($moduleName = '') |
||
293 | |||
294 | /** |
||
295 | * Return snippets by name |
||
296 | * |
||
297 | * @param string $snippetsPath |
||
298 | * @param boolean $extensions |
||
299 | * @param string $dir |
||
300 | * @param string $moduleName |
||
301 | * @return array |
||
302 | */ |
||
303 | public function getSnippets($snippetsPath, $extensions = true, $dir = '/snippets', $moduleName = '') |
||
326 | |||
327 | /** |
||
328 | * Return extensions for type |
||
329 | * |
||
330 | * @param string $extensionType |
||
331 | * @param string $request |
||
332 | * @return array |
||
333 | */ |
||
334 | public function getExtensions($extensionType, $request) |
||
344 | |||
345 | } |
||
346 |
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.