Complex classes like ConfigService 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 ConfigService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class ConfigService { |
||
| 37 | |||
| 38 | const CIRCLES_ALLOW_CIRCLES = 'allow_circles'; |
||
| 39 | const CIRCLES_CONTACT_BACKEND = 'contact_backend'; |
||
| 40 | const CIRCLES_STILL_FRONTEND = 'still_frontend'; |
||
| 41 | const CIRCLES_SWAP_TO_TEAMS = 'swap_to_teams'; |
||
| 42 | const CIRCLES_ALLOW_FEDERATED_CIRCLES = 'allow_federated'; |
||
| 43 | const CIRCLES_GS_ENABLED = 'gs_enabled'; |
||
| 44 | const CIRCLES_MEMBERS_LIMIT = 'members_limit'; |
||
| 45 | const CIRCLES_ACCOUNTS_ONLY = 'accounts_only'; |
||
| 46 | const CIRCLES_ALLOW_LINKED_GROUPS = 'allow_linked_groups'; |
||
| 47 | const CIRCLES_ALLOW_NON_SSL_LINKS = 'allow_non_ssl_links'; |
||
| 48 | const CIRCLES_NON_SSL_LOCAL = 'local_is_non_ssl'; |
||
| 49 | const CIRCLES_ACTIVITY_ON_CREATION = 'creation_activity'; |
||
| 50 | const CIRCLES_SKIP_INVITATION_STEP = 'skip_invitation_to_closed_circles'; |
||
| 51 | |||
| 52 | const CIRCLES_TEST_ASYNC_LOCK = 'test_async_lock'; |
||
| 53 | const CIRCLES_TEST_ASYNC_INIT = 'test_async_init'; |
||
| 54 | const CIRCLES_TEST_ASYNC_HAND = 'test_async_hand'; |
||
| 55 | const CIRCLES_TEST_ASYNC_COUNT = 'test_async_count'; |
||
| 56 | |||
| 57 | const GS_ENABLED = 'enabled'; |
||
| 58 | const GS_MODE = 'mode'; |
||
| 59 | const GS_KEY = 'key'; |
||
| 60 | const GS_LOOKUP = 'lookup'; |
||
| 61 | |||
| 62 | |||
| 63 | private $defaults = [ |
||
| 64 | self::CIRCLES_ALLOW_CIRCLES => Circle::CIRCLES_ALL, |
||
| 65 | self::CIRCLES_CONTACT_BACKEND => '0', |
||
| 66 | self::CIRCLES_STILL_FRONTEND => '0', |
||
| 67 | self::CIRCLES_TEST_ASYNC_INIT => '0', |
||
| 68 | self::CIRCLES_SWAP_TO_TEAMS => '0', |
||
| 69 | self::CIRCLES_ACCOUNTS_ONLY => '0', |
||
| 70 | self::CIRCLES_MEMBERS_LIMIT => '50', |
||
| 71 | self::CIRCLES_ALLOW_LINKED_GROUPS => '0', |
||
| 72 | self::CIRCLES_ALLOW_FEDERATED_CIRCLES => '0', |
||
| 73 | self::CIRCLES_GS_ENABLED => '0', |
||
| 74 | self::CIRCLES_ALLOW_NON_SSL_LINKS => '0', |
||
| 75 | self::CIRCLES_NON_SSL_LOCAL => '0', |
||
| 76 | self::CIRCLES_ACTIVITY_ON_CREATION => '1', |
||
| 77 | self::CIRCLES_SKIP_INVITATION_STEP => '0' |
||
| 78 | ]; |
||
| 79 | |||
| 80 | /** @var string */ |
||
| 81 | private $appName; |
||
| 82 | |||
| 83 | /** @var IConfig */ |
||
| 84 | private $config; |
||
| 85 | |||
| 86 | /** @var string */ |
||
| 87 | private $userId; |
||
| 88 | |||
| 89 | /** @var IRequest */ |
||
| 90 | private $request; |
||
| 91 | |||
| 92 | /** @var MiscService */ |
||
| 93 | private $miscService; |
||
| 94 | |||
| 95 | /** @var int */ |
||
| 96 | private $allowedCircle = -1; |
||
| 97 | |||
| 98 | /** @var int */ |
||
| 99 | private $allowedLinkedGroups = -1; |
||
| 100 | |||
| 101 | /** @var int */ |
||
| 102 | private $allowedFederatedCircles = -1; |
||
| 103 | |||
| 104 | /** @var int */ |
||
| 105 | private $allowedNonSSLLinks = -1; |
||
| 106 | |||
| 107 | /** @var int */ |
||
| 108 | private $localNonSSL = -1; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * ConfigService constructor. |
||
| 112 | * |
||
| 113 | * @param string $appName |
||
| 114 | * @param IConfig $config |
||
| 115 | * @param IRequest $request |
||
| 116 | * @param string $userId |
||
| 117 | * @param MiscService $miscService |
||
| 118 | */ |
||
| 119 | public function __construct( |
||
| 128 | |||
| 129 | |||
| 130 | public function getLocalAddress() { |
||
| 134 | |||
| 135 | |||
| 136 | /** |
||
| 137 | * returns if this type of circle is allowed by the current configuration. |
||
| 138 | * |
||
| 139 | * @param $type |
||
| 140 | * |
||
| 141 | * @return int |
||
| 142 | */ |
||
| 143 | public function isCircleAllowed($type) { |
||
| 150 | |||
| 151 | |||
| 152 | /** |
||
| 153 | * @return bool |
||
| 154 | */ |
||
| 155 | public function isLinkedGroupsAllowed() { |
||
| 163 | |||
| 164 | |||
| 165 | /** |
||
| 166 | * @return bool |
||
| 167 | */ |
||
| 168 | public function isFederatedCirclesAllowed() { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | public function isInvitationSkipped() { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return bool |
||
| 186 | */ |
||
| 187 | public function isLocalNonSSL() { |
||
| 195 | |||
| 196 | |||
| 197 | /** |
||
| 198 | * @return bool |
||
| 199 | */ |
||
| 200 | public function isNonSSLLinksAllowed() { |
||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * @param string $remote |
||
| 212 | * |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | public function generateRemoteHost($remote) { |
||
| 224 | |||
| 225 | |||
| 226 | /** |
||
| 227 | * Get a value by key |
||
| 228 | * |
||
| 229 | * @param string $key |
||
| 230 | * |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | public function getCoreValue($key) { |
||
| 238 | |||
| 239 | |||
| 240 | /** |
||
| 241 | * Get available hosts |
||
| 242 | * |
||
| 243 | * @return array |
||
| 244 | */ |
||
| 245 | public function getAvailableHosts(): array { |
||
| 248 | |||
| 249 | |||
| 250 | /** |
||
| 251 | * Get a value by key |
||
| 252 | * |
||
| 253 | * @param string $key |
||
| 254 | * |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | public function getAppValue($key) { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Set a value by key |
||
| 269 | * |
||
| 270 | * @param string $key |
||
| 271 | * @param string $value |
||
| 272 | * |
||
| 273 | * @return void |
||
| 274 | */ |
||
| 275 | public function setAppValue($key, $value) { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * remove a key |
||
| 281 | * |
||
| 282 | * @param string $key |
||
| 283 | * |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | public function deleteAppValue($key) { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Get a user value by key |
||
| 292 | * |
||
| 293 | * @param string $key |
||
| 294 | * |
||
| 295 | * @return string |
||
| 296 | */ |
||
| 297 | public function getUserValue($key) { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Set a user value by key |
||
| 303 | * |
||
| 304 | * @param string $key |
||
| 305 | * @param string $value |
||
| 306 | * |
||
| 307 | * @return string |
||
| 308 | * @throws PreConditionNotMetException |
||
| 309 | */ |
||
| 310 | public function setUserValue($key, $value) { |
||
| 313 | |||
| 314 | |||
| 315 | /** |
||
| 316 | * Get a user value by key and user |
||
| 317 | * |
||
| 318 | * @param string $userId |
||
| 319 | * @param string $key |
||
| 320 | * |
||
| 321 | * @param string $default |
||
| 322 | * |
||
| 323 | * @return string |
||
| 324 | */ |
||
| 325 | public function getCoreValueForUser($userId, $key, $default = '') { |
||
| 328 | |||
| 329 | |||
| 330 | /** |
||
| 331 | * Get a user value by key and user |
||
| 332 | * |
||
| 333 | * @param string $userId |
||
| 334 | * @param string $key |
||
| 335 | * |
||
| 336 | * @return string |
||
| 337 | */ |
||
| 338 | public function getValueForUser($userId, $key) { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Set a user value by key |
||
| 344 | * |
||
| 345 | * @param string $userId |
||
| 346 | * @param string $key |
||
| 347 | * @param string $value |
||
| 348 | * |
||
| 349 | * @return string |
||
| 350 | * @throws PreConditionNotMetException |
||
| 351 | */ |
||
| 352 | public function setValueForUser($userId, $key, $value) { |
||
| 355 | |||
| 356 | /** |
||
| 357 | * return the cloud version. |
||
| 358 | * if $complete is true, return a string x.y.z |
||
| 359 | * |
||
| 360 | * @param boolean $complete |
||
| 361 | * |
||
| 362 | * @return string|integer |
||
| 363 | */ |
||
| 364 | public function getCloudVersion($complete = false) { |
||
| 373 | |||
| 374 | |||
| 375 | /** |
||
| 376 | * @return bool |
||
| 377 | */ |
||
| 378 | public function isAccountOnly() { |
||
| 381 | |||
| 382 | |||
| 383 | /** |
||
| 384 | * @return bool |
||
| 385 | */ |
||
| 386 | public function isContactsBackend(): bool { |
||
| 389 | |||
| 390 | |||
| 391 | public function contactsBackendType(): int { |
||
| 394 | |||
| 395 | |||
| 396 | /** |
||
| 397 | * @return bool |
||
| 398 | */ |
||
| 399 | public function stillFrontEnd(): bool { |
||
| 410 | |||
| 411 | |||
| 412 | /** |
||
| 413 | * should the password for a mail share be send to the recipient |
||
| 414 | * |
||
| 415 | * @return bool |
||
| 416 | */ |
||
| 417 | public function sendPasswordByMail() { |
||
| 424 | |||
| 425 | /** |
||
| 426 | * do we require a share by mail to be password protected |
||
| 427 | * |
||
| 428 | * @return bool |
||
| 429 | */ |
||
| 430 | public function enforcePasswordProtection() { |
||
| 437 | |||
| 438 | |||
| 439 | /** |
||
| 440 | * @param string $type |
||
| 441 | * |
||
| 442 | * @throws GSStatusException |
||
| 443 | */ |
||
| 444 | public function getGSStatus(string $type = '') { |
||
| 480 | |||
| 481 | |||
| 482 | /** |
||
| 483 | * @return array |
||
| 484 | */ |
||
| 485 | public function getTrustedDomains(): array { |
||
| 493 | |||
| 494 | |||
| 495 | /** |
||
| 496 | * @return string |
||
| 497 | */ |
||
| 498 | public function getLocalCloudId(): string { |
||
| 501 | |||
| 502 | public function getInstanceId() { |
||
| 505 | |||
| 506 | } |
||
| 507 | |||
| 508 |