Complex classes like Configuration 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 Configuration, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class Configuration { |
||
| 30 | |||
| 31 | protected $configPrefix = null; |
||
| 32 | protected $configRead = false; |
||
| 33 | |||
| 34 | //settings |
||
| 35 | protected $config = array( |
||
| 36 | 'ldapHost' => null, |
||
| 37 | 'ldapPort' => null, |
||
| 38 | 'ldapBackupHost' => null, |
||
| 39 | 'ldapBackupPort' => null, |
||
| 40 | 'ldapBase' => null, |
||
| 41 | 'ldapBaseUsers' => null, |
||
| 42 | 'ldapBaseGroups' => null, |
||
| 43 | 'ldapAgentName' => null, |
||
| 44 | 'ldapAgentPassword' => null, |
||
| 45 | 'ldapTLS' => null, |
||
| 46 | 'turnOffCertCheck' => null, |
||
| 47 | 'ldapIgnoreNamingRules' => null, |
||
| 48 | 'ldapUserDisplayName' => null, |
||
| 49 | 'ldapUserDisplayName2' => null, |
||
| 50 | 'ldapUserFilterObjectclass' => null, |
||
| 51 | 'ldapUserFilterGroups' => null, |
||
| 52 | 'ldapUserFilter' => null, |
||
| 53 | 'ldapUserFilterMode' => null, |
||
| 54 | 'ldapGroupFilter' => null, |
||
| 55 | 'ldapGroupFilterMode' => null, |
||
| 56 | 'ldapGroupFilterObjectclass' => null, |
||
| 57 | 'ldapGroupFilterGroups' => null, |
||
| 58 | 'ldapGroupDisplayName' => null, |
||
| 59 | 'ldapGroupMemberAssocAttr' => null, |
||
| 60 | 'ldapLoginFilter' => null, |
||
| 61 | 'ldapLoginFilterMode' => null, |
||
| 62 | 'ldapLoginFilterEmail' => null, |
||
| 63 | 'ldapLoginFilterUsername' => null, |
||
| 64 | 'ldapLoginFilterAttributes' => null, |
||
| 65 | 'ldapQuotaAttribute' => null, |
||
| 66 | 'ldapQuotaDefault' => null, |
||
| 67 | 'ldapEmailAttribute' => null, |
||
| 68 | 'ldapCacheTTL' => null, |
||
| 69 | 'ldapUuidUserAttribute' => 'auto', |
||
| 70 | 'ldapUuidGroupAttribute' => 'auto', |
||
| 71 | 'ldapOverrideMainServer' => false, |
||
| 72 | 'ldapConfigurationActive' => false, |
||
| 73 | 'ldapAttributesForUserSearch' => null, |
||
| 74 | 'ldapAttributesForGroupSearch' => null, |
||
| 75 | 'ldapExperiencedAdmin' => false, |
||
| 76 | 'homeFolderNamingRule' => null, |
||
| 77 | 'hasPagedResultSupport' => false, |
||
| 78 | 'hasMemberOfFilterSupport' => false, |
||
| 79 | 'useMemberOfToDetectMembership' => true, |
||
| 80 | 'ldapExpertUsernameAttr' => null, |
||
| 81 | 'ldapExpertUUIDUserAttr' => null, |
||
| 82 | 'ldapExpertUUIDGroupAttr' => null, |
||
| 83 | 'lastJpegPhotoLookup' => null, |
||
| 84 | 'ldapNestedGroups' => false, |
||
| 85 | 'ldapPagingSize' => null, |
||
| 86 | ); |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param string $configPrefix |
||
| 90 | * @param bool $autoRead |
||
| 91 | */ |
||
| 92 | 115 | public function __construct($configPrefix, $autoRead = true) { |
|
| 93 | 115 | $this->configPrefix = $configPrefix; |
|
| 94 | 115 | if($autoRead) { |
|
| 95 | $this->readConfiguration(); |
||
| 96 | } |
||
| 97 | 115 | } |
|
| 98 | |||
| 99 | /** |
||
| 100 | * @param string $name |
||
| 101 | * @return mixed|void |
||
| 102 | */ |
||
| 103 | 18 | public function __get($name) { |
|
| 104 | 18 | if(isset($this->config[$name])) { |
|
| 105 | 17 | return $this->config[$name]; |
|
| 106 | } |
||
| 107 | 3 | } |
|
| 108 | |||
| 109 | /** |
||
| 110 | * @param string $name |
||
| 111 | * @param mixed $value |
||
| 112 | */ |
||
| 113 | 2 | public function __set($name, $value) { |
|
| 116 | |||
| 117 | /** |
||
| 118 | * @return array |
||
| 119 | */ |
||
| 120 | public function getConfiguration() { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * set LDAP configuration with values delivered by an array, not read |
||
| 126 | * from configuration. It does not save the configuration! To do so, you |
||
| 127 | * must call saveConfiguration afterwards. |
||
| 128 | * @param array $config array that holds the config parameters in an associated |
||
| 129 | * array |
||
| 130 | * @param array &$applied optional; array where the set fields will be given to |
||
| 131 | * @return false|null |
||
| 132 | */ |
||
| 133 | 17 | public function setConfiguration($config, &$applied = null) { |
|
| 179 | |||
| 180 | public function readConfiguration() { |
||
| 181 | if(!$this->configRead && !is_null($this->configPrefix)) { |
||
| 182 | $cta = array_flip($this->getConfigTranslationArray()); |
||
| 183 | foreach($this->config as $key => $val) { |
||
| 184 | if(!isset($cta[$key])) { |
||
| 185 | //some are determined |
||
| 186 | continue; |
||
| 187 | } |
||
| 188 | $dbKey = $cta[$key]; |
||
| 189 | switch($key) { |
||
| 190 | case 'ldapBase': |
||
| 191 | case 'ldapBaseUsers': |
||
| 192 | case 'ldapBaseGroups': |
||
| 193 | case 'ldapAttributesForUserSearch': |
||
| 194 | case 'ldapAttributesForGroupSearch': |
||
| 195 | case 'ldapUserFilterObjectclass': |
||
| 196 | case 'ldapUserFilterGroups': |
||
| 197 | case 'ldapGroupFilterObjectclass': |
||
| 198 | case 'ldapGroupFilterGroups': |
||
| 199 | case 'ldapLoginFilterAttributes': |
||
| 200 | $readMethod = 'getMultiLine'; |
||
| 201 | break; |
||
| 202 | case 'ldapIgnoreNamingRules': |
||
| 203 | $readMethod = 'getSystemValue'; |
||
| 204 | $dbKey = $key; |
||
| 205 | break; |
||
| 206 | case 'ldapAgentPassword': |
||
| 207 | $readMethod = 'getPwd'; |
||
| 208 | break; |
||
| 209 | case 'ldapUserDisplayName2': |
||
| 210 | case 'ldapGroupDisplayName': |
||
| 211 | $readMethod = 'getLcValue'; |
||
| 212 | break; |
||
| 213 | case 'ldapUserDisplayName': |
||
| 214 | default: |
||
| 215 | // user display name does not lower case because |
||
| 216 | // we rely on an upper case N as indicator whether to |
||
| 217 | // auto-detect it or not. FIXME |
||
| 218 | $readMethod = 'getValue'; |
||
| 219 | break; |
||
| 220 | } |
||
| 221 | $this->config[$key] = $this->$readMethod($dbKey); |
||
| 222 | } |
||
| 223 | $this->configRead = true; |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * saves the current Configuration in the database |
||
| 229 | */ |
||
| 230 | public function saveConfiguration() { |
||
| 231 | $cta = array_flip($this->getConfigTranslationArray()); |
||
| 232 | foreach($this->config as $key => $value) { |
||
| 233 | switch ($key) { |
||
| 234 | case 'ldapAgentPassword': |
||
| 235 | $value = base64_encode($value); |
||
| 236 | break; |
||
| 237 | case 'ldapBase': |
||
| 238 | case 'ldapBaseUsers': |
||
| 239 | case 'ldapBaseGroups': |
||
| 240 | case 'ldapAttributesForUserSearch': |
||
| 241 | case 'ldapAttributesForGroupSearch': |
||
| 242 | case 'ldapUserFilterObjectclass': |
||
| 243 | case 'ldapUserFilterGroups': |
||
| 244 | case 'ldapGroupFilterObjectclass': |
||
| 245 | case 'ldapGroupFilterGroups': |
||
| 246 | case 'ldapLoginFilterAttributes': |
||
| 247 | if(is_array($value)) { |
||
| 248 | $value = implode("\n", $value); |
||
| 249 | } |
||
| 250 | break; |
||
| 251 | //following options are not stored but detected, skip them |
||
| 252 | case 'ldapIgnoreNamingRules': |
||
| 253 | case 'hasPagedResultSupport': |
||
| 254 | case 'ldapUuidUserAttribute': |
||
| 255 | case 'ldapUuidGroupAttribute': |
||
| 256 | continue 2; |
||
| 257 | } |
||
| 258 | if(is_null($value)) { |
||
| 259 | $value = ''; |
||
| 260 | } |
||
| 261 | $this->saveValue($cta[$key], $value); |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param string $varName |
||
| 267 | * @return array|string |
||
| 268 | */ |
||
| 269 | protected function getMultiLine($varName) { |
||
| 270 | $value = $this->getValue($varName); |
||
| 271 | if(empty($value)) { |
||
| 272 | $value = ''; |
||
| 273 | } else { |
||
| 274 | $value = preg_split('/\r\n|\r|\n/', $value); |
||
| 275 | } |
||
| 276 | |||
| 277 | return $value; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Sets multi-line values as arrays |
||
| 282 | * |
||
| 283 | * @param string $varName name of config-key |
||
| 284 | * @param array|string $value to set |
||
| 285 | * @param boolean $trim Trim value? (default: false) |
||
|
|
|||
| 286 | */ |
||
| 287 | 12 | protected function setMultiLine($varName, $value) { |
|
| 317 | |||
| 318 | /** |
||
| 319 | * @param string $varName |
||
| 320 | * @return string |
||
| 321 | */ |
||
| 322 | protected function getPwd($varName) { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @param string $varName |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | protected function getLcValue($varName) { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param string $varName |
||
| 336 | * @return string |
||
| 337 | */ |
||
| 338 | protected function getSystemValue($varName) { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @param string $varName |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | protected function getValue($varName) { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Sets a scalar value. |
||
| 359 | * |
||
| 360 | * @param string $varName name of config key |
||
| 361 | * @param mixed $value to set |
||
| 362 | */ |
||
| 363 | 6 | protected function setValue($varName, $value) { |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Sets a scalar value without trimming. |
||
| 372 | * |
||
| 373 | * @param string $varName name of config key |
||
| 374 | * @param mixed $value to set |
||
| 375 | */ |
||
| 376 | 13 | protected function setRawValue($varName, $value) { |
|
| 379 | |||
| 380 | /** |
||
| 381 | * @param string $varName |
||
| 382 | * @param string $value |
||
| 383 | * @return bool |
||
| 384 | */ |
||
| 385 | protected function saveValue($varName, $value) { |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @return array an associative array with the default values. Keys are correspond |
||
| 393 | * to config-value entries in the database table |
||
| 394 | */ |
||
| 395 | public function getDefaults() { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @return array that maps internal variable names to database fields |
||
| 450 | */ |
||
| 451 | 17 | public function getConfigTranslationArray() { |
|
| 503 | |||
| 504 | } |
||
| 505 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.