Complex classes like Configuration 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 Configuration, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Configuration { |
||
15 | |||
16 | const KEYMARKER = '____'; |
||
17 | |||
18 | protected $_name = 'conf'; // name of the config variable found in the files (overridden by $config['varname']) |
||
19 | protected $_format = 'php'; // format of the config file, supported formats - php (overridden by $config['format']) |
||
20 | protected $_heading = ''; // heading string written at top of config file - don't include comment indicators |
||
21 | protected $_loaded = false; // set to true after configuration files are loaded |
||
22 | protected $_metadata = array();// holds metadata describing the settings |
||
23 | /** @var Setting[] */ |
||
24 | public $setting = array(); // array of setting objects |
||
25 | public $locked = false; // configuration is considered locked if it can't be updated |
||
26 | public $show_disabled_plugins = false; |
||
27 | |||
28 | // configuration filenames |
||
29 | protected $_default_files = array(); |
||
30 | protected $_local_files = array(); // updated configuration is written to the first file |
||
31 | protected $_protected_files = array(); |
||
32 | |||
33 | protected $_plugin_list = null; |
||
34 | |||
35 | /** |
||
36 | * constructor |
||
37 | * |
||
38 | * @param string $datafile path to config metadata file |
||
39 | */ |
||
40 | public function __construct($datafile) { |
||
63 | |||
64 | /** |
||
65 | * Retrieve and stores settings in setting[] attribute |
||
66 | */ |
||
67 | public function retrieve_settings() { |
||
124 | |||
125 | /** |
||
126 | * Stores setting[] array to file |
||
127 | * |
||
128 | * @param string $id Name of plugin, which saves the settings |
||
129 | * @param string $header Text at the top of the rewritten settings file |
||
130 | * @param bool $backup backup current file? (remove any existing backup) |
||
131 | * @return bool succesful? |
||
132 | */ |
||
133 | public function save_settings($id, $header = '', $backup = true) { |
||
167 | |||
168 | /** |
||
169 | * Update last modified time stamp of the config file |
||
170 | * |
||
171 | * @return bool |
||
172 | */ |
||
173 | public function touch_settings() { |
||
178 | |||
179 | /** |
||
180 | * Read and merge given config files |
||
181 | * |
||
182 | * @param array $files file paths |
||
183 | * @return array config settings |
||
184 | */ |
||
185 | protected function _read_config_group($files) { |
||
193 | |||
194 | /** |
||
195 | * Return an array of config settings |
||
196 | * |
||
197 | * @param string $file file path |
||
198 | * @return array config settings |
||
199 | */ |
||
200 | protected function _read_config($file) { |
||
246 | |||
247 | /** |
||
248 | * Convert php string into value |
||
249 | * |
||
250 | * @param string $value |
||
251 | * @return bool|string |
||
252 | */ |
||
253 | protected function _readValue($value) { |
||
272 | |||
273 | /** |
||
274 | * Returns header of rewritten settings file |
||
275 | * |
||
276 | * @param string $id plugin name of which generated this output |
||
277 | * @param string $header additional text for at top of the file |
||
278 | * @return string text of header |
||
279 | */ |
||
280 | protected function _out_header($id, $header) { |
||
294 | |||
295 | /** |
||
296 | * Returns footer of rewritten settings file |
||
297 | * |
||
298 | * @return string text of footer |
||
299 | */ |
||
300 | protected function _out_footer() { |
||
308 | |||
309 | /** |
||
310 | * Configuration is considered locked if there is no local settings filename |
||
311 | * or the directory its in is not writable or the file exists and is not writable |
||
312 | * |
||
313 | * @return bool true: locked, false: writable |
||
314 | */ |
||
315 | protected function _is_locked() { |
||
325 | |||
326 | /** |
||
327 | * not used ... conf's contents are an array! |
||
328 | * reduce any multidimensional settings to one dimension using Configuration::KEYMARKER |
||
329 | * |
||
330 | * @param $conf |
||
331 | * @param string $prefix |
||
332 | * @return array |
||
333 | */ |
||
334 | protected function _flatten($conf, $prefix = '') { |
||
350 | |||
351 | /** |
||
352 | * Returns array of plugin names |
||
353 | * |
||
354 | * @return array plugin names |
||
355 | * @triggers PLUGIN_CONFIG_PLUGINLIST event |
||
356 | */ |
||
357 | protected function get_plugin_list() { |
||
371 | |||
372 | /** |
||
373 | * load metadata for plugin and template settings |
||
374 | * |
||
375 | * @param string $tpl name of active template |
||
376 | * @return array metadata of settings |
||
377 | */ |
||
378 | protected function get_plugintpl_metadata($tpl) { |
||
419 | |||
420 | /** |
||
421 | * Load default settings for plugins and templates |
||
422 | * |
||
423 | * @param string $tpl name of active template |
||
424 | * @return array default settings |
||
425 | */ |
||
426 | protected function get_plugintpl_default($tpl) { |
||
450 | |||
451 | } |
||
452 | |||
453 |
This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.
The variable may have been renamed without also renaming all references.