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 | |||
| 22 | /** @var ConfigParser */ |
||
| 23 | protected $parser; |
||
| 24 | |||
| 25 | |||
| 26 | protected $_loaded = false; // set to true after configuration files are loaded |
||
| 27 | protected $_metadata = array();// holds metadata describing the settings |
||
| 28 | /** @var Setting[] */ |
||
| 29 | public $setting = array(); // array of setting objects |
||
| 30 | public $locked = false; // configuration is considered locked if it can't be updated |
||
| 31 | public $show_disabled_plugins = false; |
||
| 32 | |||
| 33 | // configuration filenames |
||
| 34 | protected $_default_files = array(); |
||
| 35 | protected $_local_files = array(); // updated configuration is written to the first file |
||
| 36 | protected $_protected_files = array(); |
||
| 37 | |||
| 38 | protected $_plugin_list = null; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * constructor |
||
| 42 | * |
||
| 43 | * @param string $datafile path to config metadata file |
||
| 44 | */ |
||
| 45 | public function __construct($datafile) { |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Retrieve and stores settings in setting[] attribute |
||
| 73 | */ |
||
| 74 | public function retrieve_settings() { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Stores setting[] array to file |
||
| 134 | * |
||
| 135 | * @param string $id Name of plugin, which saves the settings |
||
| 136 | * @param string $header Text at the top of the rewritten settings file |
||
| 137 | * @param bool $backup backup current file? (remove any existing backup) |
||
| 138 | * @return bool succesful? |
||
| 139 | */ |
||
| 140 | public function save_settings($id, $header = '', $backup = true) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Update last modified time stamp of the config file |
||
| 177 | * |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | public function touch_settings() { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Read and merge given config files |
||
| 188 | * |
||
| 189 | * @param array $files file paths |
||
| 190 | * @return array config settings |
||
| 191 | */ |
||
| 192 | protected function _read_config_group($files) { |
||
| 200 | |||
| 201 | |||
| 202 | /** |
||
| 203 | * Returns header of rewritten settings file |
||
| 204 | * |
||
| 205 | * @param string $id plugin name of which generated this output |
||
| 206 | * @param string $header additional text for at top of the file |
||
| 207 | * @return string text of header |
||
| 208 | */ |
||
| 209 | protected function _out_header($id, $header) { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Returns footer of rewritten settings file |
||
| 226 | * |
||
| 227 | * @return string text of footer |
||
| 228 | */ |
||
| 229 | protected function _out_footer() { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Configuration is considered locked if there is no local settings filename |
||
| 240 | * or the directory its in is not writable or the file exists and is not writable |
||
| 241 | * |
||
| 242 | * @return bool true: locked, false: writable |
||
| 243 | */ |
||
| 244 | protected function _is_locked() { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * not used ... conf's contents are an array! |
||
| 257 | * reduce any multidimensional settings to one dimension using Configuration::KEYMARKER |
||
| 258 | * |
||
| 259 | * @param $conf |
||
| 260 | * @param string $prefix |
||
| 261 | * @return array |
||
| 262 | */ |
||
| 263 | protected function _flatten($conf, $prefix = '') { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Returns array of plugin names |
||
| 282 | * |
||
| 283 | * @return array plugin names |
||
| 284 | * @triggers PLUGIN_CONFIG_PLUGINLIST event |
||
| 285 | */ |
||
| 286 | protected function get_plugin_list() { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * load metadata for plugin and template settings |
||
| 303 | * |
||
| 304 | * @param string $tpl name of active template |
||
| 305 | * @return array metadata of settings |
||
| 306 | */ |
||
| 307 | protected function get_plugintpl_metadata($tpl) { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Load default settings for plugins and templates |
||
| 351 | * |
||
| 352 | * @param string $tpl name of active template |
||
| 353 | * @return array default settings |
||
| 354 | */ |
||
| 355 | protected function get_plugintpl_default($tpl) { |
||
| 379 | |||
| 380 | } |
||
| 381 | |||
| 382 |
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.