Complex classes like Config 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, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Config |
||
| 18 | { |
||
| 19 | use SingletonTrait; |
||
| 20 | const DEFAULT_LANGUAGE = "es"; |
||
| 21 | const DEFAULT_ENCODE = "UTF-8"; |
||
| 22 | const DEFAULT_CTYPE = "text/html"; |
||
| 23 | const DEFAULT_DATETIMEZONE = "Europe/Madrid"; |
||
| 24 | |||
| 25 | protected $config = array(); |
||
| 26 | static public $defaults = array( |
||
| 27 | "db_host" => "localhost", |
||
| 28 | "db_port" => "3306", |
||
| 29 | "default_language" => "es_ES", |
||
| 30 | ); |
||
| 31 | static public $required = array('db_host', 'db_port', 'db_name', 'db_user', 'db_password', 'home_action', 'default_language'); |
||
|
|
|||
| 32 | static public $encrypted = array('db_password'); |
||
| 33 | static public $optional = array('platform_name', 'debug', 'restricted', 'admin_login', 'logger.phpFire', 'logger.memory', 'poweredBy', 'author', 'author_email', 'version', 'front.version', 'cors.enabled', 'pagination.limit', 'api.secret', 'api.admin', 'log.level', 'admin_action'); |
||
| 34 | protected $debug = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Config Constructor |
||
| 38 | */ |
||
| 39 | 8 | public function __construct() |
|
| 43 | |||
| 44 | /** |
||
| 45 | * Method that load the configuration data into the system |
||
| 46 | * @return Config |
||
| 47 | */ |
||
| 48 | 8 | protected function init() |
|
| 55 | |||
| 56 | /** |
||
| 57 | * Method that saves the configuration |
||
| 58 | * @param array $data |
||
| 59 | * @param array $extra |
||
| 60 | * @return array |
||
| 61 | */ |
||
| 62 | 2 | protected static function saveConfigParams(array $data, array $extra) |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Method that saves the extra parameters into the configuration |
||
| 79 | * @param array $data |
||
| 80 | * @return array |
||
| 81 | */ |
||
| 82 | 2 | protected static function saveExtraParams(array $data) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Method that returns if the system is in debug mode |
||
| 98 | * @return boolean |
||
| 99 | */ |
||
| 100 | 6 | public function getDebugMode() |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Method that returns the cache path |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | 2 | public function getCachePath() |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Method that returns the templates path |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | 2 | public function getTemplatePath() |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Method that checks if the platform is proper configured |
||
| 126 | * @return boolean |
||
| 127 | */ |
||
| 128 | 2 | public function isConfigured() |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Method that check if the user is trying to save the config |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | 2 | public function checkTryToSaveConfig() |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Method that saves all the configuration in the system |
||
| 156 | * |
||
| 157 | * @param array $data |
||
| 158 | * @param array|null $extra |
||
| 159 | * @return boolean |
||
| 160 | */ |
||
| 161 | 2 | public static function save(array $data, array $extra = null) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Method that returns a config value |
||
| 178 | * @param string $param |
||
| 179 | * |
||
| 180 | * @return mixed|null |
||
| 181 | */ |
||
| 182 | 3 | public function get($param) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Method that returns all the configuration |
||
| 189 | * @return array |
||
| 190 | */ |
||
| 191 | 2 | public function dumpConfig() |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Method that returns the Propel ORM parameters to setup the models |
||
| 198 | * @return array|null |
||
| 199 | */ |
||
| 200 | 1 | public function getPropelParams() |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Method that creates any parametrized path |
||
| 207 | * @param string $dir |
||
| 208 | * throws ConfigException |
||
| 209 | */ |
||
| 210 | 8 | public static function createDir($dir) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Method that remove all data in the document root path |
||
| 226 | */ |
||
| 227 | 1 | public static function clearDocumentRoot() |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Method that reloads config file |
||
| 243 | */ |
||
| 244 | 2 | public function loadConfigData() |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Clear configuration set |
||
| 254 | */ |
||
| 255 | 1 | public function clearConfig() |
|
| 259 | } |
||
| 260 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.