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 |
||
| 56 | class Config implements ContextAwareInterface |
||
| 57 | { |
||
| 58 | use ContextTrait; |
||
| 59 | |||
| 60 | const OAUTH_URL = 'oauth_url'; |
||
| 61 | const CLIENT_ID = 'client_id'; |
||
| 62 | const CLIENT_SECRET = 'client_secret'; |
||
| 63 | const SCOPE = 'scope'; |
||
| 64 | const PROJECT = 'project'; |
||
| 65 | const API_URL = 'api_url'; |
||
| 66 | const USER_NAME = 'username'; |
||
| 67 | const PASSWORD = 'password'; |
||
| 68 | const REFRESH_TOKEN = 'refresh_token'; |
||
| 69 | const ANONYMOUS_ID = 'anonymous_id'; |
||
| 70 | const GRANT_TYPE = 'grant_type'; |
||
| 71 | |||
| 72 | const GRANT_TYPE_CLIENT = 'client_credentials'; |
||
| 73 | const GRANT_TYPE_PASSWORD = 'password'; |
||
| 74 | const GRANT_TYPE_REFRESH = 'refresh_token'; |
||
| 75 | const GRANT_TYPE_ANONYMOUS = 'anonymous_token'; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | protected $clientSecret; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | protected $clientId; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var string |
||
| 89 | */ |
||
| 90 | protected $project; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var array |
||
| 94 | */ |
||
| 95 | protected $scope = ['manage_project']; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var string |
||
| 99 | */ |
||
| 100 | protected $oauthUrl = 'https://auth.sphere.io'; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var string |
||
| 104 | */ |
||
| 105 | protected $apiUrl = 'https://api.sphere.io'; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var int |
||
| 109 | */ |
||
| 110 | protected $batchPoolSize = 25; |
||
| 111 | |||
| 112 | protected $adapter; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var bool |
||
| 116 | */ |
||
| 117 | protected $throwExceptions = false; |
||
| 118 | |||
| 119 | protected $acceptEncoding = 'gzip'; |
||
| 120 | |||
| 121 | protected $grantType = 'client_credentials'; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var string |
||
| 125 | */ |
||
| 126 | protected $username; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var string |
||
| 130 | */ |
||
| 131 | protected $password; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var string |
||
| 135 | */ |
||
| 136 | protected $refreshToken; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var string |
||
| 140 | */ |
||
| 141 | protected $anonymousId; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @var string |
||
| 145 | */ |
||
| 146 | protected $cacheDir; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @var string |
||
| 150 | */ |
||
| 151 | protected $logLevel = LogLevel::INFO; |
||
| 152 | |||
| 153 | protected $messageFormatter; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param array $configValues |
||
| 157 | * @return static |
||
| 158 | */ |
||
| 159 | 98 | public static function fromArray(array $configValues) |
|
| 175 | |||
| 176 | 96 | protected function camelize($scored) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | 82 | public function getClientSecret() |
|
| 199 | |||
| 200 | /** |
||
| 201 | * @param string $clientSecret |
||
| 202 | * @return $this |
||
| 203 | */ |
||
| 204 | 86 | public function setClientSecret($clientSecret) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | 86 | public function getClientId() |
|
| 218 | |||
| 219 | /** |
||
| 220 | * @param string $clientId |
||
| 221 | * @return $this |
||
| 222 | */ |
||
| 223 | 86 | public function setClientId($clientId) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | 517 | public function getProject() |
|
| 237 | |||
| 238 | /** |
||
| 239 | * @param string $project |
||
| 240 | * @return $this |
||
| 241 | */ |
||
| 242 | 94 | public function setProject($project) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * @return string |
||
| 251 | */ |
||
| 252 | 481 | public function getScope() |
|
| 272 | |||
| 273 | /** |
||
| 274 | * @param string $scope |
||
| 275 | * @return $this |
||
| 276 | */ |
||
| 277 | 51 | public function setScope($scope) |
|
| 289 | |||
| 290 | /** |
||
| 291 | * @return string |
||
| 292 | */ |
||
| 293 | 49 | public function getOauthUrl() |
|
| 305 | |||
| 306 | /** |
||
| 307 | * @param string $oauthUrl |
||
| 308 | * @return $this |
||
| 309 | */ |
||
| 310 | 86 | public function setOauthUrl($oauthUrl) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * @return string |
||
| 319 | */ |
||
| 320 | 61 | public function getApiUrl() |
|
| 324 | |||
| 325 | /** |
||
| 326 | * @param string $apiUrl |
||
| 327 | * @return $this |
||
| 328 | */ |
||
| 329 | 86 | public function setApiUrl($apiUrl) |
|
| 335 | |||
| 336 | /** |
||
| 337 | * @return bool |
||
| 338 | */ |
||
| 339 | 82 | public function check() |
|
| 355 | |||
| 356 | /** |
||
| 357 | * @return int |
||
| 358 | */ |
||
| 359 | 1 | public function getBatchPoolSize() |
|
| 363 | |||
| 364 | /** |
||
| 365 | * @param int $batchPoolSize |
||
| 366 | * @return $this |
||
| 367 | */ |
||
| 368 | 1 | public function setBatchPoolSize($batchPoolSize) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * @return string |
||
| 377 | */ |
||
| 378 | 72 | public function getAdapter() |
|
| 382 | |||
| 383 | /** |
||
| 384 | * @param string $adapter |
||
| 385 | * @return $this |
||
| 386 | */ |
||
| 387 | public function setAdapter($adapter) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @return bool |
||
| 396 | */ |
||
| 397 | 92 | public function getThrowExceptions() |
|
| 401 | |||
| 402 | /** |
||
| 403 | * @param bool $throwExceptions |
||
| 404 | * @return $this |
||
| 405 | */ |
||
| 406 | 9 | public function setThrowExceptions($throwExceptions) |
|
| 412 | |||
| 413 | /** |
||
| 414 | * @return string |
||
| 415 | */ |
||
| 416 | 72 | public function getAcceptEncoding() |
|
| 420 | |||
| 421 | /** |
||
| 422 | * @param string $acceptEncoding |
||
| 423 | * @return $this |
||
| 424 | */ |
||
| 425 | public function setAcceptEncoding($acceptEncoding) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @return static |
||
| 434 | */ |
||
| 435 | 98 | public static function of() |
|
| 439 | |||
| 440 | /** |
||
| 441 | * @return string |
||
| 442 | */ |
||
| 443 | 473 | public function getGrantType() |
|
| 447 | |||
| 448 | /** |
||
| 449 | * @param string $grantType |
||
| 450 | * @return $this |
||
| 451 | */ |
||
| 452 | 33 | public function setGrantType($grantType) |
|
| 458 | |||
| 459 | /** |
||
| 460 | * @return string |
||
| 461 | */ |
||
| 462 | 19 | public function getUsername() |
|
| 466 | |||
| 467 | /** |
||
| 468 | * @param string $username |
||
| 469 | * @return $this |
||
| 470 | */ |
||
| 471 | 23 | public function setUsername($username) |
|
| 477 | |||
| 478 | /** |
||
| 479 | * @return string |
||
| 480 | */ |
||
| 481 | 19 | public function getPassword() |
|
| 485 | |||
| 486 | /** |
||
| 487 | * @param string $password |
||
| 488 | * @return $this |
||
| 489 | */ |
||
| 490 | 23 | public function setPassword($password) |
|
| 496 | |||
| 497 | /** |
||
| 498 | * @return string |
||
| 499 | */ |
||
| 500 | 28 | public function getRefreshToken() |
|
| 504 | |||
| 505 | /** |
||
| 506 | * @param string $refreshToken |
||
| 507 | * @return $this |
||
| 508 | */ |
||
| 509 | 28 | public function setRefreshToken($refreshToken) |
|
| 515 | |||
| 516 | /** |
||
| 517 | * @return string |
||
| 518 | */ |
||
| 519 | 11 | public function getAnonymousId() |
|
| 523 | |||
| 524 | /** |
||
| 525 | * @param string $anonymousId |
||
| 526 | * @return string |
||
| 527 | */ |
||
| 528 | 4 | public function setAnonymousId($anonymousId) |
|
| 534 | |||
| 535 | /** |
||
| 536 | * @return string |
||
| 537 | */ |
||
| 538 | 75 | public function getCacheDir() |
|
| 542 | |||
| 543 | /** |
||
| 544 | * @param string $cacheDir |
||
| 545 | * @return $this |
||
| 546 | */ |
||
| 547 | public function setCacheDir($cacheDir) |
||
| 553 | |||
| 554 | /** |
||
| 555 | * @return string |
||
| 556 | */ |
||
| 557 | 39 | public function getLogLevel() |
|
| 561 | |||
| 562 | /** |
||
| 563 | * @param string $logLevel |
||
| 564 | * @return $this |
||
| 565 | */ |
||
| 566 | 1 | public function setLogLevel($logLevel) |
|
| 572 | |||
| 573 | /** |
||
| 574 | * @return mixed |
||
| 575 | */ |
||
| 576 | 39 | public function getMessageFormatter() |
|
| 580 | |||
| 581 | /** |
||
| 582 | * @param mixed $messageFormatter |
||
| 583 | * @return $this |
||
| 584 | */ |
||
| 585 | public function setMessageFormatter($messageFormatter) |
||
| 590 | } |
||
| 591 |