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 Config_File 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 Config_File, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | class Config_File { |
||
39 | /**#@+ |
||
40 | * Options |
||
41 | * @var boolean |
||
42 | */ |
||
43 | /** |
||
44 | * Controls whether variables with the same name overwrite each other. |
||
45 | */ |
||
46 | var $overwrite = true; |
||
47 | |||
48 | /** |
||
49 | * Controls whether config values of on/true/yes and off/false/no get |
||
50 | * converted to boolean values automatically. |
||
51 | */ |
||
52 | var $booleanize = true; |
||
53 | |||
54 | /** |
||
55 | * Controls whether hidden config sections/vars are read from the file. |
||
56 | */ |
||
57 | var $read_hidden = true; |
||
58 | |||
59 | /** |
||
60 | * Controls whether or not to fix mac or dos formatted newlines. |
||
61 | * If set to true, \r or \r\n will be changed to \n. |
||
62 | */ |
||
63 | var $fix_newlines = true; |
||
64 | /**#@-*/ |
||
65 | |||
66 | /** @access private */ |
||
67 | var $_config_path = ""; |
||
68 | var $_config_data = array(); |
||
69 | /**#@-*/ |
||
70 | |||
71 | /** |
||
72 | * Constructs a new config file class. |
||
73 | * |
||
74 | * @param string $config_path (optional) path to the config files |
||
75 | */ |
||
76 | function __construct($config_path = NULL) |
||
|
|||
77 | { |
||
78 | if (isset($config_path)) |
||
79 | $this->set_path($config_path); |
||
80 | } |
||
81 | |||
82 | |||
83 | /** |
||
84 | * Set the path where configuration files can be found. |
||
85 | * |
||
86 | * @param string $config_path path to the config files |
||
87 | */ |
||
88 | function set_path($config_path) |
||
102 | |||
103 | |||
104 | /** |
||
105 | * Retrieves config info based on the file, section, and variable name. |
||
106 | * |
||
107 | * @param string $file_name config file to get info for |
||
108 | * @param string $section_name (optional) section to get info for |
||
109 | * @param string $var_name (optional) variable to get info for |
||
110 | * @return string|array a value or array of values |
||
111 | */ |
||
112 | function get($file_name, $section_name = NULL, $var_name = NULL) |
||
143 | |||
144 | |||
145 | /** |
||
146 | * Retrieves config info based on the key. |
||
147 | * |
||
148 | * @param $file_name string config key (filename/section/var) |
||
149 | * @return string|array same as get() |
||
150 | * @uses get() retrieves information from config file and returns it |
||
151 | */ |
||
152 | function &get_key($config_key) |
||
158 | |||
159 | /** |
||
160 | * Get all loaded config file names. |
||
161 | * |
||
162 | * @return array an array of loaded config file names |
||
163 | */ |
||
164 | function get_file_names() |
||
168 | |||
169 | |||
170 | /** |
||
171 | * Get all section names from a loaded file. |
||
172 | * |
||
173 | * @param string $file_name config file to get section names from |
||
174 | * @return array an array of section names from the specified file |
||
175 | */ |
||
176 | function get_section_names($file_name) |
||
186 | |||
187 | |||
188 | /** |
||
189 | * Get all global or section variable names. |
||
190 | * |
||
191 | * @param string $file_name config file to get info for |
||
192 | * @param string $section_name (optional) section to get info for |
||
193 | * @return array an array of variables names from the specified file/section |
||
194 | */ |
||
195 | function get_var_names($file_name, $section = NULL) |
||
210 | |||
211 | |||
212 | /** |
||
213 | * Clear loaded config data for a certain file or all files. |
||
214 | * |
||
215 | * @param string $file_name file to clear config data for |
||
216 | */ |
||
217 | function clear($file_name = NULL) |
||
224 | |||
225 | |||
226 | /** |
||
227 | * Load a configuration file manually. |
||
228 | * |
||
229 | * @param string $file_name file name to load |
||
230 | * @param boolean $prepend_path whether current config path should be |
||
231 | * prepended to the filename |
||
232 | */ |
||
233 | function load_file($file_name, $prepend_path = true) |
||
253 | |||
254 | /** |
||
255 | * Store the contents of a file manually. |
||
256 | * |
||
257 | * @param string $config_file file name of the related contents |
||
258 | * @param string $contents the file-contents to parse |
||
259 | */ |
||
260 | function set_file_contents($config_file, $contents) |
||
265 | |||
266 | /** |
||
267 | * parse the source of a configuration file manually. |
||
268 | * |
||
269 | * @param string $contents the file-contents to parse |
||
270 | */ |
||
271 | function parse_contents($contents) |
||
343 | |||
344 | /**#@+ @access private */ |
||
345 | /** |
||
346 | * @param array &$container |
||
347 | * @param string $var_name |
||
348 | * @param mixed $var_value |
||
349 | * @param boolean $booleanize determines whether $var_value is converted to |
||
350 | * to true/false |
||
351 | */ |
||
352 | function _set_config_var(&$container, $var_name, $var_value, $booleanize) |
||
380 | |||
381 | /** |
||
382 | * @uses trigger_error() creates a PHP warning/error |
||
383 | * @param string $error_msg |
||
384 | * @param integer $error_type one of |
||
385 | */ |
||
386 | function _trigger_error_msg($error_msg, $error_type = E_USER_WARNING) |
||
390 | /**#@-*/ |
||
391 | } |
||
392 | |||
394 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.