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 | "debug" => true, |
||
| 31 | "front.version" => "v1", |
||
| 32 | "version" => "v1", |
||
| 33 | ); |
||
| 34 | static public $required = array('db_host', 'db_port', 'db_name', 'db_user', 'db_password', 'home_action', 'default_language', 'debug'); |
||
|
|
|||
| 35 | static public $encrypted = array('db_password'); |
||
| 36 | static public $optional = [ |
||
| 37 | 'platform_name', // Platform name |
||
| 38 | 'restricted', // Restrict the web access |
||
| 39 | 'admin_login', // Enable web login for admin |
||
| 40 | 'logger.phpFire', // Enable phpFire to trace the logs in the browser |
||
| 41 | 'logger.memory', // Enable log memory usage un traces |
||
| 42 | 'poweredBy', // Show PoweredBy header customized |
||
| 43 | 'author', // Author for auto generated files |
||
| 44 | 'author_email', // Author email for auto generated files |
||
| 45 | 'version', // Platform version(for cache purposes) |
||
| 46 | 'front.version', // Static resources version |
||
| 47 | 'cors.enabled', // Enable CORS (regex with the domains, * for all) |
||
| 48 | 'pagination.limit', // Pagination limit for autogenerated api admin |
||
| 49 | 'api.secret', // Secret passphrase to securize the api |
||
| 50 | 'api.admin', // Enable de autogenerated api admin(wok) |
||
| 51 | 'log.level', // Max log level(default INFO) |
||
| 52 | 'admin_action', // Default admin url when access to /admin |
||
| 53 | 'cache.var', // Static cache var |
||
| 54 | 'twig.auto_reload', // Enable or disable auto reload templates for twig |
||
| 55 | 'modules.extend', // Variable for extending the current functionality |
||
| 56 | ]; |
||
| 57 | protected $debug = false; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Config Constructor |
||
| 61 | */ |
||
| 62 | 8 | public function __construct() |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Method that load the configuration data into the system |
||
| 69 | * @return Config |
||
| 70 | */ |
||
| 71 | 8 | protected function init() |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Method that saves the configuration |
||
| 81 | * @param array $data |
||
| 82 | * @param array $extra |
||
| 83 | * @return array |
||
| 84 | */ |
||
| 85 | 2 | protected static function saveConfigParams(array $data, array $extra) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Method that saves the extra parameters into the configuration |
||
| 102 | * @param array $data |
||
| 103 | * @return array |
||
| 104 | */ |
||
| 105 | 2 | protected static function saveExtraParams(array $data) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Method that returns if the system is in debug mode |
||
| 121 | * @return boolean |
||
| 122 | */ |
||
| 123 | 6 | public function getDebugMode() |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Method that returns the cache path |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | 2 | public function getCachePath() |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Method that returns the templates path |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | 2 | public function getTemplatePath() |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Method that checks if the platform is proper configured |
||
| 149 | * @return boolean |
||
| 150 | */ |
||
| 151 | 2 | public function isConfigured() |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Method that check if the user is trying to save the config |
||
| 168 | * @return bool |
||
| 169 | */ |
||
| 170 | 2 | public function checkTryToSaveConfig() |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Method that saves all the configuration in the system |
||
| 179 | * |
||
| 180 | * @param array $data |
||
| 181 | * @param array|null $extra |
||
| 182 | * @return boolean |
||
| 183 | */ |
||
| 184 | 2 | public static function save(array $data, array $extra = null) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Method that returns a config value |
||
| 201 | * @param string $param |
||
| 202 | * @param mixed $defaultValue |
||
| 203 | * |
||
| 204 | * @return mixed|null |
||
| 205 | */ |
||
| 206 | 4 | public function get($param, $defaultValue = null) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Method that returns all the configuration |
||
| 213 | * @return array |
||
| 214 | */ |
||
| 215 | 2 | public function dumpConfig() |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Method that returns the Propel ORM parameters to setup the models |
||
| 222 | * @return array|null |
||
| 223 | */ |
||
| 224 | 1 | public function getPropelParams() |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Method that creates any parametrized path |
||
| 231 | * @param string $dir |
||
| 232 | * throws ConfigException |
||
| 233 | */ |
||
| 234 | 7 | public static function createDir($dir) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Method that remove all data in the document root path |
||
| 250 | */ |
||
| 251 | 1 | public static function clearDocumentRoot() |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Method that reloads config file |
||
| 267 | */ |
||
| 268 | 2 | public function loadConfigData() |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Clear configuration set |
||
| 278 | */ |
||
| 279 | 1 | public function clearConfig() |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Static wrapper for extracting params |
||
| 286 | * @param string $key |
||
| 287 | * @param mixed|null $defaultValue |
||
| 288 | * @return mixed|null |
||
| 289 | */ |
||
| 290 | 2 | public static function getParam($key, $defaultValue = null) { |
|
| 294 | } |
||
| 295 |
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.