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 | ]; |
||
| 56 | protected $debug = false; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Config Constructor |
||
| 60 | */ |
||
| 61 | 8 | public function __construct() |
|
| 65 | |||
| 66 | /** |
||
| 67 | * Method that load the configuration data into the system |
||
| 68 | * @return Config |
||
| 69 | */ |
||
| 70 | 8 | protected function init() |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Method that saves the configuration |
||
| 80 | * @param array $data |
||
| 81 | * @param array $extra |
||
| 82 | * @return array |
||
| 83 | */ |
||
| 84 | 2 | protected static function saveConfigParams(array $data, array $extra) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Method that saves the extra parameters into the configuration |
||
| 101 | * @param array $data |
||
| 102 | * @return array |
||
| 103 | */ |
||
| 104 | 2 | protected static function saveExtraParams(array $data) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Method that returns if the system is in debug mode |
||
| 120 | * @return boolean |
||
| 121 | */ |
||
| 122 | 6 | public function getDebugMode() |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Method that returns the cache path |
||
| 129 | * @return string |
||
| 130 | */ |
||
| 131 | 2 | public function getCachePath() |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Method that returns the templates path |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | 2 | public function getTemplatePath() |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Method that checks if the platform is proper configured |
||
| 148 | * @return boolean |
||
| 149 | */ |
||
| 150 | 2 | public function isConfigured() |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Method that check if the user is trying to save the config |
||
| 167 | * @return bool |
||
| 168 | */ |
||
| 169 | 2 | public function checkTryToSaveConfig() |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Method that saves all the configuration in the system |
||
| 178 | * |
||
| 179 | * @param array $data |
||
| 180 | * @param array|null $extra |
||
| 181 | * @return boolean |
||
| 182 | */ |
||
| 183 | 2 | public static function save(array $data, array $extra = null) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Method that returns a config value |
||
| 200 | * @param string $param |
||
| 201 | * |
||
| 202 | * @return mixed|null |
||
| 203 | */ |
||
| 204 | 4 | public function get($param) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Method that returns all the configuration |
||
| 211 | * @return array |
||
| 212 | */ |
||
| 213 | 2 | public function dumpConfig() |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Method that returns the Propel ORM parameters to setup the models |
||
| 220 | * @return array|null |
||
| 221 | */ |
||
| 222 | 1 | public function getPropelParams() |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Method that creates any parametrized path |
||
| 229 | * @param string $dir |
||
| 230 | * throws ConfigException |
||
| 231 | */ |
||
| 232 | 7 | public static function createDir($dir) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Method that remove all data in the document root path |
||
| 248 | */ |
||
| 249 | 1 | public static function clearDocumentRoot() |
|
| 262 | |||
| 263 | /** |
||
| 264 | * Method that reloads config file |
||
| 265 | */ |
||
| 266 | 2 | public function loadConfigData() |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Clear configuration set |
||
| 276 | */ |
||
| 277 | 1 | public function clearConfig() |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Static wrapper for extracting params |
||
| 284 | * @param string $key |
||
| 285 | * @param mixed|null $defaultValue |
||
| 286 | * @return mixed|null |
||
| 287 | */ |
||
| 288 | 1 | public static function getParam($key, $defaultValue = null) { |
|
| 292 | } |
||
| 293 |
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.