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 ModuleObject 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 ModuleObject, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class ModuleObject extends BaseObject |
||
10 | { |
||
11 | |||
12 | var $mid = NULL; ///< string to represent run-time instance of Module (XE Module) |
||
13 | var $module = NULL; ///< Class name of Xe Module that is identified by mid |
||
14 | var $module_srl = NULL; ///< integer value to represent a run-time instance of Module (XE Module) |
||
15 | var $module_info = NULL; ///< an object containing the module information |
||
16 | var $origin_module_info = NULL; |
||
17 | var $xml_info = NULL; ///< an object containing the module description extracted from XML file |
||
18 | var $module_path = NULL; ///< a path to directory where module source code resides |
||
19 | var $act = NULL; ///< a string value to contain the action name |
||
20 | var $template_path = NULL; ///< a path of directory where template files reside |
||
21 | var $template_file = NULL; ///< name of template file |
||
22 | var $layout_path = ''; ///< a path of directory where layout files reside |
||
23 | var $layout_file = ''; ///< name of layout file |
||
24 | var $edited_layout_file = ''; ///< name of temporary layout files that is modified in an admin mode |
||
25 | var $stop_proc = FALSE; ///< a flag to indicating whether to stop the execution of code. |
||
26 | var $module_config = NULL; |
||
27 | var $ajaxRequestMethod = array('XMLRPC', 'JSON'); |
||
28 | var $gzhandler_enable = TRUE; |
||
29 | |||
30 | /** |
||
31 | * setter to set the name of module |
||
32 | * @param string $module name of module |
||
33 | * @return void |
||
34 | * */ |
||
35 | function setModule($module) |
||
39 | |||
40 | /** |
||
41 | * setter to set the name of module path |
||
42 | * @param string $path the directory path to a module directory |
||
43 | * @return void |
||
44 | * */ |
||
45 | function setModulePath($path) |
||
53 | |||
54 | /** |
||
55 | * setter to set an url for redirection |
||
56 | * @param string $url url for redirection |
||
57 | * @remark redirect_url is used only for ajax requests |
||
58 | * @return void |
||
59 | * */ |
||
60 | function setRedirectUrl($url = './', $output = NULL) |
||
73 | |||
74 | /** |
||
75 | * get url for redirection |
||
76 | * @return string redirect_url |
||
77 | * */ |
||
78 | function getRedirectUrl() |
||
82 | |||
83 | /** |
||
84 | * set message |
||
85 | * @param string $message a message string |
||
86 | * @param string $type type of message (error, info, update) |
||
87 | * @return void |
||
88 | * */ |
||
89 | function setMessage($message = 'success', $type = NULL) |
||
94 | |||
95 | /** |
||
96 | * set type of message |
||
97 | * @param string $type type of message (error, info, update) |
||
98 | * @return void |
||
99 | * */ |
||
100 | function setMessageType($type) |
||
104 | |||
105 | /** |
||
106 | * get type of message |
||
107 | * @return string $type |
||
108 | * */ |
||
109 | function getMessageType() |
||
119 | |||
120 | /** |
||
121 | * sett to set the template path for refresh.html |
||
122 | * refresh.html is executed as a result of method execution |
||
123 | * Tpl as the common run of the refresh.html .. |
||
124 | * @return void |
||
125 | * */ |
||
126 | function setRefreshPage() |
||
131 | |||
132 | /** |
||
133 | * sett to set the action name |
||
134 | * @param string $act |
||
135 | * @return void |
||
136 | * */ |
||
137 | function setAct($act) |
||
141 | |||
142 | /** |
||
143 | * sett to set module information |
||
144 | * @param object $module_info object containing module information |
||
145 | * @param object $xml_info object containing module description |
||
146 | * @return void |
||
147 | * */ |
||
148 | function setModuleInfo($module_info, $xml_info) |
||
221 | |||
222 | /** |
||
223 | * set the stop_proc and approprate message for msg_code |
||
224 | * @param string $msg_code an error code |
||
225 | * @return ModuleObject $this |
||
226 | * */ |
||
227 | function stop($msg_code) |
||
246 | |||
247 | /** |
||
248 | * set the file name of the template file |
||
249 | * @param string name of file |
||
250 | * @return void |
||
251 | * */ |
||
252 | function setTemplateFile($filename) |
||
260 | |||
261 | /** |
||
262 | * retrieve the directory path of the template directory |
||
263 | * @return string |
||
264 | * */ |
||
265 | function getTemplateFile() |
||
269 | |||
270 | /** |
||
271 | * set the directory path of the template directory |
||
272 | * @param string path of template directory. |
||
273 | * @return void |
||
274 | * */ |
||
275 | View Code Duplication | function setTemplatePath($path) |
|
290 | |||
291 | /** |
||
292 | * retrieve the directory path of the template directory |
||
293 | * @return string |
||
294 | * */ |
||
295 | function getTemplatePath() |
||
299 | |||
300 | /** |
||
301 | * set the file name of the temporarily modified by admin |
||
302 | * @param string name of file |
||
303 | * @return void |
||
304 | * */ |
||
305 | function setEditedLayoutFile($filename) |
||
315 | |||
316 | /** |
||
317 | * retreived the file name of edited_layout_file |
||
318 | * @return string |
||
319 | * */ |
||
320 | function getEditedLayoutFile() |
||
324 | |||
325 | /** |
||
326 | * set the file name of the layout file |
||
327 | * @param string name of file |
||
328 | * @return void |
||
329 | * */ |
||
330 | function setLayoutFile($filename) |
||
340 | |||
341 | /** |
||
342 | * get the file name of the layout file |
||
343 | * @return string |
||
344 | * */ |
||
345 | function getLayoutFile() |
||
349 | |||
350 | /** |
||
351 | * set the directory path of the layout directory |
||
352 | * @param string path of layout directory. |
||
353 | * */ |
||
354 | View Code Duplication | function setLayoutPath($path) |
|
368 | |||
369 | /** |
||
370 | * set the directory path of the layout directory |
||
371 | * @return string |
||
372 | * */ |
||
373 | function getLayoutPath($layout_name = "", $layout_type = "P") |
||
377 | |||
378 | /** |
||
379 | * excute the member method specified by $act variable |
||
380 | * @return boolean true : success false : fail |
||
381 | * */ |
||
382 | function proc() |
||
489 | |||
490 | } |
||
491 | /* End of file ModuleObject.class.php */ |
||
493 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: