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 |
||
| 16 | class Config |
||
| 17 | { |
||
| 18 | use SingletonTrait; |
||
| 19 | const DEFAULT_LANGUAGE = "es"; |
||
| 20 | const DEFAULT_ENCODE = "UTF-8"; |
||
| 21 | const DEFAULT_CTYPE = "text/html"; |
||
| 22 | const DEFAULT_DATETIMEZONE = "Europe/Madrid"; |
||
| 23 | |||
| 24 | protected $config = array(); |
||
| 25 | static public $defaults = array( |
||
| 26 | "db_host" => "localhost", |
||
| 27 | "db_port" => "3306", |
||
| 28 | "default_language" => "es_ES", |
||
| 29 | ); |
||
| 30 | static public $required = array('db_host', 'db_port', 'db_name', 'db_user', 'db_password', 'home_action', 'default_language'); |
||
|
|
|||
| 31 | static public $encrypted = array('db_password'); |
||
| 32 | 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'); |
||
| 33 | protected $debug = false; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Config Constructor |
||
| 37 | */ |
||
| 38 | 8 | public function __construct() |
|
| 42 | |||
| 43 | /** |
||
| 44 | * Method that load the configuration data into the system |
||
| 45 | * @return Config |
||
| 46 | */ |
||
| 47 | 8 | protected function init() |
|
| 54 | |||
| 55 | /** |
||
| 56 | * Method that saves the configuration |
||
| 57 | * @param array $data |
||
| 58 | * @param array $extra |
||
| 59 | * @return array |
||
| 60 | */ |
||
| 61 | 1 | protected static function saveConfigParams(array $data, array $extra) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Method that saves the extra parameters into the configuration |
||
| 78 | * @param array $data |
||
| 79 | * @return array |
||
| 80 | */ |
||
| 81 | 1 | protected static function saveExtraParams(array $data) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Method that returns if the system is in debug mode |
||
| 97 | * @return boolean |
||
| 98 | */ |
||
| 99 | 3 | public function getDebugMode() |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Method that returns the cache path |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | 1 | public function getCachePath() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Method that returns the templates path |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | 1 | public function getTemplatePath() |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Method that checks if the platform is proper configured |
||
| 125 | * @return boolean |
||
| 126 | */ |
||
| 127 | 2 | public function isConfigured() |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Method that check if the user is trying to save the config |
||
| 144 | * @return bool |
||
| 145 | */ |
||
| 146 | 2 | public function checkTryToSaveConfig() |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Method that saves all the configuration in the system |
||
| 155 | * |
||
| 156 | * @param array $data |
||
| 157 | * @param array|null $extra |
||
| 158 | * @return boolean |
||
| 159 | */ |
||
| 160 | 1 | public static function save(array $data, array $extra = null) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Method that returns a config value |
||
| 177 | * @param string $param |
||
| 178 | * |
||
| 179 | * @return mixed|null |
||
| 180 | */ |
||
| 181 | 2 | public function get($param) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Method that returns all the configuration |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | 1 | public function dumpConfig() |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Method that returns the Propel ORM parameters to setup the models |
||
| 197 | * @return array|null |
||
| 198 | */ |
||
| 199 | 1 | public function getPropelParams() |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Method that creates any parametrized path |
||
| 206 | * @param string $dir |
||
| 207 | * throws ConfigException |
||
| 208 | */ |
||
| 209 | 3 | public static function createDir($dir) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Method that remove all data in the document root path |
||
| 225 | */ |
||
| 226 | 1 | public static function clearDocumentRoot() |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Method that reloads config file |
||
| 242 | */ |
||
| 243 | 1 | public function loadConfigData() |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Clear configuration set |
||
| 251 | */ |
||
| 252 | 1 | public function clearConfig() |
|
| 256 | } |
||
| 257 |
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.