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 moduleController 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 moduleController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class moduleController extends module |
||
9 | { |
||
10 | /** |
||
11 | * @brief Initialization |
||
12 | */ |
||
13 | function init() |
||
14 | { |
||
15 | } |
||
16 | |||
17 | /** |
||
18 | * @brief Add action forward |
||
19 | * Action forward finds and forwards if an action is not in the requested module |
||
20 | * This is used when installing a module |
||
21 | */ |
||
22 | function insertActionForward($module, $type, $act) |
||
23 | { |
||
24 | $args = new stdClass(); |
||
25 | $args->module = $module; |
||
26 | $args->type = $type; |
||
27 | $args->act = $act; |
||
28 | |||
29 | $output = executeQuery('module.insertActionForward', $args); |
||
30 | |||
31 | $oCacheHandler = CacheHandler::getInstance('object', NULL, TRUE); |
||
32 | if($oCacheHandler->isSupport()) |
||
33 | { |
||
34 | $cache_key = 'action_forward'; |
||
35 | $oCacheHandler->delete($cache_key); |
||
36 | } |
||
37 | |||
38 | return $output; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @brief Delete action forward |
||
43 | */ |
||
44 | function deleteActionForward($module, $type, $act) |
||
45 | { |
||
46 | $args = new stdClass(); |
||
47 | $args->module = $module; |
||
48 | $args->type = $type; |
||
49 | $args->act = $act; |
||
50 | |||
51 | $output = executeQuery('module.deleteActionForward', $args); |
||
52 | |||
53 | $oCacheHandler = CacheHandler::getInstance('object', NULL, TRUE); |
||
54 | if($oCacheHandler->isSupport()) |
||
55 | { |
||
56 | $cache_key = 'action_forward'; |
||
57 | $oCacheHandler->delete($cache_key); |
||
58 | } |
||
59 | |||
60 | return $output; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @brief Add module trigger |
||
65 | * module trigger is to call a trigger to a target module |
||
66 | * |
||
67 | */ |
||
68 | View Code Duplication | function insertTrigger($trigger_name, $module, $type, $called_method, $called_position) |
|
|
|||
69 | { |
||
70 | $args = new stdClass(); |
||
71 | $args->trigger_name = $trigger_name; |
||
72 | $args->module = $module; |
||
73 | $args->type = $type; |
||
74 | $args->called_method = $called_method; |
||
75 | $args->called_position = $called_position; |
||
76 | |||
77 | $output = executeQuery('module.insertTrigger', $args); |
||
78 | if($output->toBool()) |
||
79 | { |
||
80 | //remove from cache |
||
81 | $GLOBALS['__triggers__'] = NULL; |
||
82 | $oCacheHandler = CacheHandler::getInstance('object', NULL, TRUE); |
||
83 | if($oCacheHandler->isSupport()) |
||
84 | { |
||
85 | $cache_key = 'triggers'; |
||
86 | $oCacheHandler->delete($cache_key); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | return $output; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @brief Delete module trigger |
||
95 | * |
||
96 | */ |
||
97 | View Code Duplication | function deleteTrigger($trigger_name, $module, $type, $called_method, $called_position) |
|
98 | { |
||
99 | $args = new stdClass(); |
||
100 | $args->trigger_name = $trigger_name; |
||
101 | $args->module = $module; |
||
102 | $args->type = $type; |
||
103 | $args->called_method = $called_method; |
||
104 | $args->called_position = $called_position; |
||
105 | |||
106 | $output = executeQuery('module.deleteTrigger', $args); |
||
107 | if($output->toBool()) |
||
108 | { |
||
109 | //remove from cache |
||
110 | $GLOBALS['__triggers__'] = NULL; |
||
111 | $oCacheHandler = CacheHandler::getInstance('object', NULL, TRUE); |
||
112 | if($oCacheHandler->isSupport()) |
||
113 | { |
||
114 | $cache_key = 'triggers'; |
||
115 | $oCacheHandler->delete($cache_key); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | return $output; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @brief Delete module trigger |
||
124 | * |
||
125 | */ |
||
126 | View Code Duplication | function deleteModuleTriggers($module) |
|
127 | { |
||
128 | $args = new stdClass(); |
||
129 | $args->module = $module; |
||
130 | |||
131 | $output = executeQuery('module.deleteModuleTriggers', $args); |
||
132 | if($output->toBool()) |
||
133 | { |
||
134 | //remove from cache |
||
135 | $GLOBALS['__triggers__'] = NULL; |
||
136 | $oCacheHandler = CacheHandler::getInstance('object', NULL, TRUE); |
||
137 | if($oCacheHandler->isSupport()) |
||
138 | { |
||
139 | $cache_key = 'triggers'; |
||
140 | $oCacheHandler->delete($cache_key); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | return $output; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @brief Add module extend |
||
149 | * |
||
150 | */ |
||
151 | function insertModuleExtend($parent_module, $extend_module, $type, $kind='') |
||
152 | { |
||
153 | if($kind != 'admin') $kind = ''; |
||
154 | if(!in_array($type,array('model','controller','view','api','mobile'))) return false; |
||
155 | if(in_array($parent_module, array('module','addon','widget','layout'))) return false; |
||
156 | |||
157 | $cache_file = './files/config/module_extend.php'; |
||
158 | FileHandler::removeFile($cache_file); |
||
159 | |||
160 | $args = new stdClass; |
||
161 | $args->parent_module = $parent_module; |
||
162 | $args->extend_module = $extend_module; |
||
163 | $args->type = $type; |
||
164 | $args->kind = $kind; |
||
165 | |||
166 | $output = executeQuery('module.getModuleExtendCount', $args); |
||
167 | if($output->data->count>0) return false; |
||
168 | |||
169 | $output = executeQuery('module.insertModuleExtend', $args); |
||
170 | return $output; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @brief Delete module extend |
||
175 | * |
||
176 | */ |
||
177 | function deleteModuleExtend($parent_module, $extend_module, $type, $kind='') |
||
178 | { |
||
179 | $cache_file = './files/config/module_extend.php'; |
||
180 | FileHandler::removeFile($cache_file); |
||
181 | |||
182 | $args = new stdClass; |
||
183 | $args->parent_module = $parent_module; |
||
184 | $args->extend_module = $extend_module; |
||
185 | $args->type = $type; |
||
186 | $args->kind = $kind; |
||
187 | |||
188 | $output = executeQuery('module.deleteModuleExtend', $args); |
||
189 | |||
190 | return $output; |
||
191 | } |
||
192 | |||
193 | function updateModuleConfig($module, $config, $site_srl = 0) |
||
194 | { |
||
195 | $args = new stdClass(); |
||
196 | $args->module = $module; |
||
197 | $args->site_srl = $site_srl; |
||
198 | |||
199 | $oModuleModel = getModel('module'); |
||
200 | $origin_config = $oModuleModel->getModuleConfig($module, $site_srl); |
||
201 | |||
202 | if(!$origin_config) $origin_config = new stdClass; |
||
203 | |||
204 | foreach($config as $key => $val) |
||
205 | { |
||
206 | $origin_config->{$key} = $val; |
||
207 | } |
||
208 | |||
209 | return $this->insertModuleConfig($module, $origin_config, $site_srl); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * @brief Enter a specific set of modules |
||
214 | * In order to manage global configurations of modules such as board, member and so on |
||
215 | */ |
||
216 | View Code Duplication | function insertModuleConfig($module, $config, $site_srl = 0) |
|
217 | { |
||
218 | $args =new stdClass(); |
||
219 | $args->module = $module; |
||
220 | $args->config = serialize($config); |
||
221 | $args->site_srl = $site_srl; |
||
222 | |||
223 | $output = executeQuery('module.deleteModuleConfig', $args); |
||
224 | if(!$output->toBool()) return $output; |
||
225 | |||
226 | $output = executeQuery('module.insertModuleConfig', $args); |
||
227 | |||
228 | //remove from cache |
||
229 | $oCacheHandler = CacheHandler::getInstance('object', NULL, TRUE); |
||
230 | if($oCacheHandler->isSupport()) |
||
231 | { |
||
232 | $oCacheHandler->invalidateGroupKey('site_and_module'); |
||
233 | } |
||
234 | return $output; |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * @brief Save module configurations of the mid |
||
239 | * Manage mid configurations depending on module |
||
240 | */ |
||
241 | View Code Duplication | function insertModulePartConfig($module, $module_srl, $config) |
|
242 | { |
||
243 | $args = new stdClass(); |
||
244 | $args->module = $module; |
||
245 | $args->module_srl = $module_srl; |
||
246 | $args->config = serialize($config); |
||
247 | |||
248 | $output = executeQuery('module.deleteModulePartConfig', $args); |
||
249 | if(!$output->toBool()) return $output; |
||
250 | |||
251 | $output = executeQuery('module.insertModulePartConfig', $args); |
||
252 | |||
253 | //remove from cache |
||
254 | $oCacheHandler = CacheHandler::getInstance('object', NULL, TRUE); |
||
255 | if($oCacheHandler->isSupport()) |
||
256 | { |
||
257 | $oCacheHandler->invalidateGroupKey('site_and_module'); |
||
258 | } |
||
259 | |||
260 | return $output; |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * @brief create virtual site |
||
265 | */ |
||
266 | function insertSite($domain, $index_module_srl) |
||
267 | { |
||
268 | if(isSiteID($domain)) |
||
269 | { |
||
270 | $oModuleModel = getModel('module'); |
||
271 | if($oModuleModel->isIDExists($domain, 0)) return new Object(-1,'msg_already_registed_vid'); |
||
272 | } |
||
273 | else |
||
274 | { |
||
275 | $domain = strtolower($domain); |
||
276 | } |
||
277 | |||
278 | $args = new stdClass; |
||
279 | $args->site_srl = getNextSequence(); |
||
280 | $args->domain = (substr_compare($domain, '/', -1) === 0) ? substr($domain, 0, -1) : $domain; |
||
281 | $args->index_module_srl = $index_module_srl; |
||
282 | $args->default_language = Context::getLangType(); |
||
283 | |||
284 | $columnList = array('modules.site_srl'); |
||
285 | $oModuleModel = getModel('module'); |
||
286 | $output = $oModuleModel->getSiteInfoByDomain($args->domain, $columnList); |
||
287 | if($output) return new Object(-1,'msg_already_registed_vid'); |
||
288 | |||
289 | $output = executeQuery('module.insertSite', $args); |
||
290 | if(!$output->toBool()) return $output; |
||
291 | |||
292 | $output->add('site_srl', $args->site_srl); |
||
293 | return $output; |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * @brief modify virtual site |
||
298 | */ |
||
299 | function updateSite($args) |
||
300 | { |
||
301 | $oModuleModel = getModel('module'); |
||
302 | $columnList = array('sites.site_srl', 'sites.domain'); |
||
303 | $site_info = $oModuleModel->getSiteInfo($args->site_srl, $columnList); |
||
304 | |||
305 | if(!$args->domain && $site_info->site_srl == $args->site_srl) |
||
306 | { |
||
307 | $args->domain = $site_info->domain; |
||
308 | } |
||
309 | |||
310 | if($site_info->domain != $args->domain) |
||
311 | { |
||
312 | $info = $oModuleModel->getSiteInfoByDomain($args->domain, $columnList); |
||
313 | if($info->site_srl && $info->site_srl != $args->site_srl) return new Object(-1,'msg_already_registed_domain'); |
||
314 | if(isSiteID($args->domain) && $oModuleModel->isIDExists($args->domain)) return new Object(-1,'msg_already_registed_vid'); |
||
315 | |||
316 | if($args->domain && !isSiteID($args->domain)) |
||
317 | { |
||
318 | $args->domain = (strlen($args->domain) >= 1 && substr_compare($args->domain, '/', -1) === 0) ? substr($args->domain, 0, -1) : $args->domain; |
||
319 | } |
||
320 | } |
||
321 | $output = executeQuery('module.updateSite', $args); |
||
322 | //clear cache for default mid |
||
323 | if($args->site_srl == 0) $vid=''; |
||
324 | else $vid=$args->domain; |
||
325 | |||
326 | $module_info = $oModuleModel->getModuleInfoByModuleSrl($args->index_module_srl); |
||
327 | $mid = $module_info->mid; |
||
328 | |||
329 | $oCacheHandler = CacheHandler::getInstance('object', null, true); |
||
330 | if($oCacheHandler->isSupport()) |
||
331 | { |
||
332 | $oCacheHandler->invalidateGroupKey('site_and_module'); |
||
333 | } |
||
334 | |||
335 | return $output; |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * @brief Arrange module information |
||
340 | */ |
||
341 | function arrangeModuleInfo(&$args, &$extra_vars) |
||
342 | { |
||
343 | // Remove unnecessary information |
||
344 | unset($args->body); |
||
345 | unset($args->act); |
||
346 | unset($args->page); |
||
347 | // Test mid value |
||
348 | if(!preg_match("/^[a-z][a-z0-9_]+$/i", $args->mid)) return new Object(-1, 'msg_limit_mid'); |
||
349 | // Test variables (separate basic vars and other vars in modules) |
||
350 | $extra_vars = clone($args); |
||
351 | unset($extra_vars->module_srl); |
||
352 | unset($extra_vars->module); |
||
353 | unset($extra_vars->module_category_srl); |
||
354 | unset($extra_vars->layout_srl); |
||
355 | unset($extra_vars->mlayout_srl); |
||
356 | unset($extra_vars->use_mobile); |
||
357 | unset($extra_vars->menu_srl); |
||
358 | unset($extra_vars->site_srl); |
||
359 | unset($extra_vars->mid); |
||
360 | unset($extra_vars->is_skin_fix); |
||
361 | unset($extra_vars->skin); |
||
362 | unset($extra_vars->is_mskin_fix); |
||
363 | unset($extra_vars->mskin); |
||
364 | unset($extra_vars->browser_title); |
||
365 | unset($extra_vars->description); |
||
366 | unset($extra_vars->is_default); |
||
367 | unset($extra_vars->content); |
||
368 | unset($extra_vars->mcontent); |
||
369 | unset($extra_vars->open_rss); |
||
370 | unset($extra_vars->header_text); |
||
371 | unset($extra_vars->footer_text); |
||
372 | $args = delObjectVars($args, $extra_vars); |
||
373 | |||
374 | return new Object(); |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * @brief Insert module |
||
379 | */ |
||
380 | function insertModule($args) |
||
504 | |||
505 | /** |
||
506 | * @brief Modify module information |
||
507 | */ |
||
508 | function updateModule($args) |
||
616 | |||
617 | /** |
||
618 | * @brief Change the module's virtual site |
||
619 | */ |
||
620 | function updateModuleSite($module_srl, $site_srl, $layout_srl = 0) |
||
621 | { |
||
622 | $args = new stdClass; |
||
623 | $args->module_srl = $module_srl; |
||
624 | $args->site_srl = $site_srl; |
||
625 | $args->layout_srl = $layout_srl; |
||
626 | $output = executeQuery('module.updateModuleSite', $args); |
||
627 | if(!$output->toBool()) return $output; |
||
628 | |||
629 | //remove from cache |
||
630 | $oCacheHandler = CacheHandler::getInstance('object', null, true); |
||
638 | |||
639 | /** |
||
640 | * Delete module |
||
641 | * Attempt to delete all related information when deleting a module. |
||
642 | * Origin method is changed. because menu validation check is needed |
||
643 | */ |
||
644 | function deleteModule($module_srl, $site_srl = 0) |
||
702 | |||
703 | /** |
||
704 | * Delete module |
||
705 | * Attempt to delete all related information when deleting a module. |
||
706 | */ |
||
707 | public function onlyDeleteModule($module_srl) |
||
766 | |||
767 | /** |
||
768 | * @brief Change other information of the module |
||
769 | * @deprecated |
||
770 | */ |
||
771 | function updateModuleSkinVars($module_srl, $skin_vars) |
||
775 | |||
776 | /** |
||
777 | * @brief Set is_default as N in all modules(the default module is disabled) |
||
778 | */ |
||
779 | View Code Duplication | function clearDefaultModule() |
|
792 | |||
793 | /** |
||
794 | * @brief Update menu_srl of mid which belongs to menu_srl |
||
795 | */ |
||
796 | View Code Duplication | function updateModuleMenu($args) |
|
808 | |||
809 | /** |
||
810 | * @brief Update layout_srl of mid which belongs to menu_srl |
||
811 | */ |
||
812 | function updateModuleLayout($layout_srl, $menu_srl_list) |
||
829 | |||
830 | /** |
||
831 | * @brief Change the site administrator |
||
832 | */ |
||
833 | function insertSiteAdmin($site_srl, $arr_admins) |
||
875 | |||
876 | /** |
||
877 | * @brief Specify the admin ID to a module |
||
878 | */ |
||
879 | function insertAdminId($module_srl, $admin_id) |
||
895 | |||
896 | /** |
||
897 | * @brief Remove the admin ID from a module |
||
898 | */ |
||
899 | function deleteAdminId($module_srl, $admin_id = '') |
||
912 | |||
913 | /** |
||
914 | * Insert skin vars to a module |
||
915 | * @param $module_srl Sequence of module |
||
916 | * @param $obj Skin variables |
||
917 | */ |
||
918 | function insertModuleSkinVars($module_srl, $obj) |
||
922 | |||
923 | /** |
||
924 | * Insert mobile skin vars to a module |
||
925 | * @param $module_srl Sequence of module |
||
926 | * @param $obj Skin variables |
||
927 | */ |
||
928 | function insertModuleMobileSkinVars($module_srl, $obj) |
||
932 | |||
933 | |||
934 | /** |
||
935 | * @brief Insert skin vars to a module |
||
936 | */ |
||
937 | function _insertModuleSkinVars($module_srl, $obj, $mode) |
||
988 | |||
989 | /** |
||
990 | * Remove skin vars ofa module |
||
991 | * @param $module_srl seqence of module |
||
992 | */ |
||
993 | function deleteModuleSkinVars($module_srl) |
||
997 | |||
998 | /** |
||
999 | * Remove mobile skin vars ofa module |
||
1000 | * @param $module_srl seqence of module |
||
1001 | */ |
||
1002 | function deleteModuleMobileSkinVars($module_srl) |
||
1006 | |||
1007 | /** |
||
1008 | * @brief Remove skin vars of a module |
||
1009 | */ |
||
1010 | function _deleteModuleSkinVars($module_srl, $mode) |
||
1037 | |||
1038 | /** |
||
1039 | * @brief Register extra vars to the module |
||
1040 | */ |
||
1041 | function insertModuleExtraVars($module_srl, $obj) |
||
1067 | |||
1068 | /** |
||
1069 | * @brief Remove extra vars from the module |
||
1070 | */ |
||
1071 | View Code Duplication | function deleteModuleExtraVars($module_srl) |
|
1088 | |||
1089 | /** |
||
1090 | * @brief Grant permission to the module |
||
1091 | */ |
||
1092 | function insertModuleGrants($module_srl, $obj) |
||
1112 | |||
1113 | /** |
||
1114 | * @brief Remove permission from the module |
||
1115 | */ |
||
1116 | function deleteModuleGrants($module_srl) |
||
1122 | |||
1123 | /** |
||
1124 | * @brief Change user-defined language |
||
1125 | */ |
||
1126 | function replaceDefinedLangCode(&$output, $isReplaceLangCode = true) |
||
1133 | |||
1134 | function _replaceLangCode($matches) |
||
1171 | |||
1172 | |||
1173 | /** |
||
1174 | * @brief Add and update a file into the file box |
||
1175 | */ |
||
1176 | function procModuleFileBoxAdd() |
||
1242 | |||
1243 | /** |
||
1244 | * @brief Update a file into the file box |
||
1245 | */ |
||
1246 | function updateModuleFileBox($vars) |
||
1282 | |||
1283 | |||
1284 | /** |
||
1285 | * @brief Add a file into the file box |
||
1286 | */ |
||
1287 | function insertModuleFileBox($vars) |
||
1321 | |||
1322 | |||
1323 | /** |
||
1324 | * @brief Delete a file from the file box |
||
1325 | */ |
||
1326 | function procModuleFileBoxDelete() |
||
1338 | |||
1339 | function deleteModuleFileBox($vars) |
||
1350 | |||
1351 | /** |
||
1352 | * @brief function of locking (timeout is in seconds) |
||
1353 | */ |
||
1354 | function lock($lock_name, $timeout, $member_srl = null) |
||
1370 | |||
1371 | function unlockTimeoutPassed() |
||
1375 | |||
1376 | function unlock($lock_name, $deadline) |
||
1384 | |||
1385 | function updateModuleInSites($site_srls, $args) |
||
1399 | } |
||
1400 | /* End of file module.controller.php */ |
||
1402 |
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.