Complex classes like Settings 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 Settings, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | abstract class Settings |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * The Settings array |
||
| 24 | * |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $settings = null; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The name of the Settings table |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $table = ''; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Global JaegerApp product settings |
||
| 38 | * |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $_global_defaults = array( |
||
| 42 | 'date_format' => 'M d, Y, h:i:sA', |
||
| 43 | 'relative_time' => '1', |
||
| 44 | 'enable_rest_api' => '0', |
||
| 45 | 'api_key' => '', |
||
| 46 | 'api_secret' => '', |
||
| 47 | 'api_debug' => 0, |
||
| 48 | 'license_number' => '', |
||
| 49 | 'license_check' => 0, |
||
| 50 | 'license_status' => '' |
||
| 51 | ); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The settings keys that should be serialized for storage |
||
| 55 | * |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | protected $serialized = array(); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The settings keys that have a custom option available |
||
| 62 | * |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | protected $custom_options = array(); |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The settings, if any, that should be encrypted for storage |
||
| 69 | * |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | protected $encrypted = array(); |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The default settings storage variable |
||
| 76 | * |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | protected $defaults = array(); |
||
| 80 | |||
| 81 | /** |
||
| 82 | * The settings, if any, that should be exploded into arrays on storage |
||
| 83 | * |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | protected $new_lines = array(); |
||
| 87 | |||
| 88 | /** |
||
| 89 | * The file system based overrides if any |
||
| 90 | * |
||
| 91 | * Used to set configuration and settings through file based configuration |
||
| 92 | * |
||
| 93 | * @var array |
||
| 94 | */ |
||
| 95 | protected $overrides = array(); |
||
| 96 | |||
| 97 | /** |
||
| 98 | * The encryption object to use for storage |
||
| 99 | * |
||
| 100 | * @var \JaegerApp\Encrypt |
||
| 101 | */ |
||
| 102 | protected $encrypt = null; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * The lanaguage instance |
||
| 106 | * |
||
| 107 | * @var \JaegerApp\Language |
||
| 108 | */ |
||
| 109 | protected $lang = null; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * The database instance |
||
| 113 | * |
||
| 114 | * @var \JaegerApp\Db |
||
| 115 | */ |
||
| 116 | protected $db = null; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Sets it up |
||
| 120 | * |
||
| 121 | * @param \JaegerApp\Db $db |
||
| 122 | * @param \JaegerApp\Language $lang |
||
| 123 | */ |
||
| 124 | public function __construct(\JaegerApp\Db $db, \JaegerApp\Language $lang) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Will validate the passed setting data for errors |
||
| 132 | * |
||
| 133 | * @param array $data The data to valiate |
||
| 134 | * @param array $extra Any extra data to provide context for the $data |
||
| 135 | */ |
||
| 136 | abstract public function validate(array $data, array $extra = array()); |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Sets the default settings values |
||
| 140 | * |
||
| 141 | * @param array $defaults |
||
| 142 | * @return \JaegerApp\Settings |
||
| 143 | */ |
||
| 144 | public function setDefaults(array $defaults) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Returns the default settings |
||
| 152 | * |
||
| 153 | * @return array |
||
| 154 | */ |
||
| 155 | public function getDefaults() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Sets the table we're using |
||
| 162 | * |
||
| 163 | * @param string $table |
||
| 164 | * @return \JaegerApp\Settings |
||
| 165 | */ |
||
| 166 | public function setTable($table) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Returns the Settings table name |
||
| 174 | * |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | public function getTable() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Returns the settings that have custom options |
||
| 184 | * |
||
| 185 | * @return array |
||
| 186 | */ |
||
| 187 | public function getCustomOptions() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Returns the available overrides |
||
| 194 | * |
||
| 195 | * @return array |
||
| 196 | */ |
||
| 197 | public function getOverrides() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Sets the overrides to use |
||
| 204 | * |
||
| 205 | * @param array $overrides |
||
| 206 | * @return \JaegerApp\Settings |
||
| 207 | */ |
||
| 208 | public function setOverrides(array $overrides = array()) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Retrns the encryption object |
||
| 216 | * |
||
| 217 | * @return \JaegerApp\Encrypt |
||
| 218 | */ |
||
| 219 | public function getEncrypt() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Sets the encryption object we're using |
||
| 226 | * |
||
| 227 | * @param \JaegerApp\Encrypt $encrypt |
||
| 228 | * @return \JaegerApp\Settings |
||
| 229 | */ |
||
| 230 | public function setEncrypt(\JaegerApp\Encrypt $encrypt) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Sets the encrypted setting keys |
||
| 238 | * |
||
| 239 | * @param array $encrypted |
||
| 240 | * @return \JaegerApp\Settings |
||
| 241 | */ |
||
| 242 | public function setEncrypted(array $encrypted = array()) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Returns the settings keys to be encrypted |
||
| 250 | * |
||
| 251 | * @return \JaegerApp\array |
||
| 252 | */ |
||
| 253 | public function getEncrypted() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Returns the serialized setting options |
||
| 260 | * |
||
| 261 | * @return array |
||
| 262 | */ |
||
| 263 | public function getSerialized() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Sets the serialized setting options |
||
| 270 | * |
||
| 271 | * @param array $serialized |
||
| 272 | * @return \JaegerApp\Settings |
||
| 273 | */ |
||
| 274 | public function setSerialized(array $serialized = array()) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Returns the new lined system settings |
||
| 282 | * |
||
| 283 | * @return array |
||
| 284 | */ |
||
| 285 | public function getNewLines() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Sets the new lined system settings |
||
| 292 | * |
||
| 293 | * @param array $nl |
||
| 294 | * @return \mithra62\Settings |
||
| 295 | */ |
||
| 296 | public function setNewLines(array $nl = array()) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Saves the settings to the database |
||
| 304 | * |
||
| 305 | * @param array $data |
||
| 306 | */ |
||
| 307 | public function update(array $data) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Updates the value of a setting |
||
| 336 | * |
||
| 337 | * @param string $key |
||
| 338 | * @param string $value |
||
| 339 | */ |
||
| 340 | public function updateSetting($key, $value) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Verifies that a submitted setting is valid and exists. |
||
| 360 | * If it's valid but doesn't exist it is created. |
||
| 361 | * |
||
| 362 | * @param string $setting |
||
| 363 | */ |
||
| 364 | private function checkSetting($setting) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Returns the system settings |
||
| 377 | * |
||
| 378 | * @return array |
||
| 379 | */ |
||
| 380 | public function get($force = false) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Checks the database for a setting key |
||
| 452 | * |
||
| 453 | * @param string $setting |
||
| 454 | */ |
||
| 455 | public function getSetting($setting) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Adds a setting to the databse |
||
| 467 | * |
||
| 468 | * @param string $setting |
||
| 469 | */ |
||
| 470 | public function addSetting($setting) |
||
| 482 | } |
If an expression can have both
false, andnullas possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.