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() { |
|
| 39 | 8 | $this->init(); |
|
| 40 | 8 | } |
|
| 41 | |||
| 42 | /** |
||
| 43 | * Método que carga la configuración del sistema |
||
| 44 | * @return Config |
||
| 45 | */ |
||
| 46 | 8 | protected function init() { |
|
| 47 | 8 | if (file_exists(CONFIG_DIR.DIRECTORY_SEPARATOR."config.json")) |
|
| 48 | 8 | { |
|
| 49 | 7 | $this->loadConfigData(); |
|
| 50 | 7 | } else { |
|
| 51 | 1 | $this->debug = true; |
|
| 52 | } |
||
| 53 | 8 | return $this; |
|
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Método que guarda los datos de la configuración |
||
| 58 | * @param array $data |
||
| 59 | * @param array $extra |
||
| 60 | * @return array |
||
| 61 | */ |
||
| 62 | 2 | protected static function saveConfigParams(array $data, array $extra) { |
|
| 63 | //En caso de tener parámetros nuevos los guardamos |
||
| 64 | 2 | if (!empty($extra['label'])) { |
|
| 65 | 1 | foreach ($extra['label'] as $index => $field) { |
|
| 66 | 1 | if (array_key_exists($index, $extra['value']) && !empty($extra['value'][$index])) { |
|
| 67 | /** @var $data array */ |
||
| 68 | 1 | $data[$field] = $extra['value'][$index]; |
|
| 69 | 1 | } |
|
| 70 | 1 | } |
|
| 71 | 1 | } |
|
| 72 | 2 | return $data; |
|
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Método que guarda los parámetros adicionales de la configuración |
||
| 77 | * @param array $data |
||
| 78 | * @return array |
||
| 79 | */ |
||
| 80 | 2 | protected static function saveExtraParams(array $data) { |
|
| 81 | 2 | $final_data = array(); |
|
| 82 | 2 | if (count($data) > 0) { |
|
| 83 | 2 | foreach ($data as $key => $value) { |
|
| 84 | 2 | if (null !== $value || $value !== '') { |
|
| 85 | 2 | $final_data[$key] = $value; |
|
| 86 | 2 | } |
|
| 87 | 2 | } |
|
| 88 | 2 | } |
|
| 89 | 2 | return $final_data; |
|
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Método que devuelve si la plataforma está en modo debug |
||
| 94 | * @return boolean |
||
| 95 | */ |
||
| 96 | public function getDebugMode() { return $this->debug; } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Método que devuelve el path de cache |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public function getCachePath() { return CACHE_DIR; } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Método que devuelve el path general de templates de PSFS |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | 2 | public function getTemplatePath() { |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Método que indica si se ha configurado correctamente la plataforma |
||
| 115 | * @return boolean |
||
| 116 | */ |
||
| 117 | 3 | public function isConfigured() |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Method that check if the user is trying to save the config |
||
| 133 | * @return bool |
||
| 134 | */ |
||
| 135 | 3 | public function checkTryToSaveConfig() |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Método que guarda la configuración del framework |
||
| 144 | * |
||
| 145 | * @param array $data |
||
| 146 | * @param array|null $extra |
||
| 147 | * @return boolean |
||
| 148 | */ |
||
| 149 | 2 | public static function save(array $data, array $extra = null) { |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Método que devuelve un parámetro de configuración |
||
| 164 | * @param string $param |
||
| 165 | * |
||
| 166 | * @return mixed|null |
||
| 167 | */ |
||
| 168 | 2 | public function get($param) { |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Método que devuelve toda la configuración en un array |
||
| 174 | * @return array|null |
||
| 175 | */ |
||
| 176 | 2 | public function dumpConfig() { |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Servicio que devuelve los parámetros de configuración de Propel para las BD |
||
| 182 | * @return array|null |
||
| 183 | */ |
||
| 184 | public function getPropelParams() { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Método estático para la generación de directorios |
||
| 190 | * @param string $dir |
||
| 191 | * throws ConfigException |
||
| 192 | */ |
||
| 193 | 4 | public static function createDir($dir) { |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Método estático que elimina los directorio del document root |
||
| 207 | */ |
||
| 208 | 1 | public static function clearDocumentRoot() { |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Method that reloads config file |
||
| 223 | */ |
||
| 224 | 1 | public function loadConfigData() |
|
| 229 | } |
||
| 230 |
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.