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 |
||
| 58 | class Config implements ContextAwareInterface |
||
| 59 | { |
||
| 60 | use ContextTrait; |
||
| 61 | |||
| 62 | const OAUTH_URL = 'oauth_url'; |
||
| 63 | const CLIENT_ID = 'client_id'; |
||
| 64 | const CLIENT_SECRET = 'client_secret'; |
||
| 65 | const SCOPE = 'scope'; |
||
| 66 | const PROJECT = 'project'; |
||
| 67 | const API_URL = 'api_url'; |
||
| 68 | const USER_NAME = 'username'; |
||
| 69 | const PASSWORD = 'password'; |
||
| 70 | const REFRESH_TOKEN = 'refresh_token'; |
||
| 71 | const ANONYMOUS_ID = 'anonymous_id'; |
||
| 72 | const GRANT_TYPE = 'grant_type'; |
||
| 73 | |||
| 74 | const GRANT_TYPE_CLIENT = 'client_credentials'; |
||
| 75 | const GRANT_TYPE_PASSWORD = 'password'; |
||
| 76 | const GRANT_TYPE_REFRESH = 'refresh_token'; |
||
| 77 | const GRANT_TYPE_ANONYMOUS = 'anonymous_token'; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | protected $clientSecret; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | protected $clientId; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var string |
||
| 91 | */ |
||
| 92 | protected $project; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | protected $scope = ['manage_project']; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var string |
||
| 101 | */ |
||
| 102 | protected $oauthUrl = 'https://auth.sphere.io'; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var string |
||
| 106 | */ |
||
| 107 | protected $apiUrl = 'https://api.sphere.io'; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var int |
||
| 111 | */ |
||
| 112 | protected $batchPoolSize = 25; |
||
| 113 | |||
| 114 | protected $adapter; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var bool |
||
| 118 | */ |
||
| 119 | protected $throwExceptions = false; |
||
| 120 | |||
| 121 | protected $acceptEncoding = 'gzip'; |
||
| 122 | |||
| 123 | protected $grantType = 'client_credentials'; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @var string |
||
| 127 | */ |
||
| 128 | protected $username; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @var string |
||
| 132 | */ |
||
| 133 | protected $password; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @var string |
||
| 137 | */ |
||
| 138 | protected $refreshToken; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @var string |
||
| 142 | */ |
||
| 143 | protected $anonymousId; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @var string |
||
| 147 | */ |
||
| 148 | protected $cacheDir; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @var string |
||
| 152 | */ |
||
| 153 | protected $logLevel = LogLevel::INFO; |
||
| 154 | |||
| 155 | protected $messageFormatter; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @var bool |
||
| 159 | */ |
||
| 160 | protected $enableCorrelationId = false; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @var CorrelationIdProvider |
||
| 164 | */ |
||
| 165 | protected $correlationIdProvider; |
||
| 166 | |||
| 167 | protected $clientOptions = []; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param array $configValues |
||
| 171 | * @return static |
||
| 172 | */ |
||
| 173 | 101 | public static function fromArray(array $configValues) |
|
| 189 | |||
| 190 | 99 | protected function camelize($scored) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | 84 | public function getClientSecret() |
|
| 213 | |||
| 214 | /** |
||
| 215 | * @param string $clientSecret |
||
| 216 | * @return $this |
||
| 217 | */ |
||
| 218 | 89 | public function setClientSecret($clientSecret) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | 88 | public function getClientId() |
|
| 232 | |||
| 233 | /** |
||
| 234 | * @param string $clientId |
||
| 235 | * @return $this |
||
| 236 | */ |
||
| 237 | 89 | public function setClientId($clientId) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | 534 | public function getProject() |
|
| 251 | |||
| 252 | /** |
||
| 253 | * @param string $project |
||
| 254 | * @return $this |
||
| 255 | */ |
||
| 256 | 97 | public function setProject($project) |
|
| 262 | |||
| 263 | /** |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | 496 | public function getScope() |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @param string $scope |
||
| 289 | * @return $this |
||
| 290 | */ |
||
| 291 | 54 | public function setScope($scope) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * @return string |
||
| 306 | */ |
||
| 307 | 51 | public function getOauthUrl() |
|
| 319 | |||
| 320 | /** |
||
| 321 | * @param string $oauthUrl |
||
| 322 | * @return $this |
||
| 323 | */ |
||
| 324 | 89 | public function setOauthUrl($oauthUrl) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * @return string |
||
| 333 | */ |
||
| 334 | 63 | public function getApiUrl() |
|
| 338 | |||
| 339 | /** |
||
| 340 | * @param string $apiUrl |
||
| 341 | * @return $this |
||
| 342 | */ |
||
| 343 | 89 | public function setApiUrl($apiUrl) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * @return bool |
||
| 352 | */ |
||
| 353 | 84 | public function check() |
|
| 369 | |||
| 370 | /** |
||
| 371 | * @deprecated use getClientOptions()['concurrency'] instead |
||
| 372 | * @return int |
||
| 373 | */ |
||
| 374 | 1 | public function getBatchPoolSize() |
|
| 381 | |||
| 382 | /** |
||
| 383 | * @deprecated use setClientOptions(['concurrency' => 5]) instead |
||
| 384 | * @param int $batchPoolSize |
||
| 385 | * @return $this |
||
| 386 | */ |
||
| 387 | 1 | public function setBatchPoolSize($batchPoolSize) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * @return string |
||
| 397 | */ |
||
| 398 | 75 | public function getAdapter() |
|
| 402 | |||
| 403 | /** |
||
| 404 | * @param string $adapter |
||
| 405 | * @return $this |
||
| 406 | */ |
||
| 407 | public function setAdapter($adapter) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @return bool |
||
| 416 | */ |
||
| 417 | 122 | public function getThrowExceptions() |
|
| 421 | |||
| 422 | /** |
||
| 423 | * @param bool $throwExceptions |
||
| 424 | * @return $this |
||
| 425 | */ |
||
| 426 | 9 | public function setThrowExceptions($throwExceptions) |
|
| 432 | |||
| 433 | /** |
||
| 434 | * @return string |
||
| 435 | */ |
||
| 436 | 74 | public function getAcceptEncoding() |
|
| 440 | |||
| 441 | /** |
||
| 442 | * @param string $acceptEncoding |
||
| 443 | * @return $this |
||
| 444 | */ |
||
| 445 | public function setAcceptEncoding($acceptEncoding) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @return static |
||
| 454 | */ |
||
| 455 | 101 | public static function of() |
|
| 459 | |||
| 460 | /** |
||
| 461 | * @return string |
||
| 462 | */ |
||
| 463 | 489 | public function getGrantType() |
|
| 467 | |||
| 468 | /** |
||
| 469 | * @param string $grantType |
||
| 470 | * @return $this |
||
| 471 | */ |
||
| 472 | 33 | public function setGrantType($grantType) |
|
| 478 | |||
| 479 | /** |
||
| 480 | * @return string |
||
| 481 | */ |
||
| 482 | 19 | public function getUsername() |
|
| 486 | |||
| 487 | /** |
||
| 488 | * @param string $username |
||
| 489 | * @return $this |
||
| 490 | */ |
||
| 491 | 23 | public function setUsername($username) |
|
| 497 | |||
| 498 | /** |
||
| 499 | * @return string |
||
| 500 | */ |
||
| 501 | 19 | public function getPassword() |
|
| 505 | |||
| 506 | /** |
||
| 507 | * @param string $password |
||
| 508 | * @return $this |
||
| 509 | */ |
||
| 510 | 23 | public function setPassword($password) |
|
| 516 | |||
| 517 | /** |
||
| 518 | * @return string |
||
| 519 | */ |
||
| 520 | 28 | public function getRefreshToken() |
|
| 524 | |||
| 525 | /** |
||
| 526 | * @param string $refreshToken |
||
| 527 | * @return $this |
||
| 528 | */ |
||
| 529 | 28 | public function setRefreshToken($refreshToken) |
|
| 535 | |||
| 536 | /** |
||
| 537 | * @return string |
||
| 538 | */ |
||
| 539 | 11 | public function getAnonymousId() |
|
| 543 | |||
| 544 | /** |
||
| 545 | * @param string $anonymousId |
||
| 546 | * @return string |
||
| 547 | */ |
||
| 548 | 4 | public function setAnonymousId($anonymousId) |
|
| 554 | |||
| 555 | /** |
||
| 556 | * @return string |
||
| 557 | */ |
||
| 558 | 77 | public function getCacheDir() |
|
| 562 | |||
| 563 | /** |
||
| 564 | * @param string $cacheDir |
||
| 565 | * @return $this |
||
| 566 | */ |
||
| 567 | public function setCacheDir($cacheDir) |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @return string |
||
| 576 | */ |
||
| 577 | 40 | public function getLogLevel() |
|
| 581 | |||
| 582 | /** |
||
| 583 | * @param string $logLevel |
||
| 584 | * @return $this |
||
| 585 | */ |
||
| 586 | 1 | public function setLogLevel($logLevel) |
|
| 592 | |||
| 593 | /** |
||
| 594 | * @return mixed |
||
| 595 | */ |
||
| 596 | 40 | public function getMessageFormatter() |
|
| 600 | |||
| 601 | /** |
||
| 602 | * @param mixed $messageFormatter |
||
| 603 | * @return $this |
||
| 604 | */ |
||
| 605 | public function setMessageFormatter($messageFormatter) |
||
| 610 | |||
| 611 | /** |
||
| 612 | * @return CorrelationIdProvider|null |
||
| 613 | */ |
||
| 614 | 73 | public function getCorrelationIdProvider() |
|
| 624 | |||
| 625 | /** |
||
| 626 | * @param CorrelationIdProvider $correlationIdProvider |
||
| 627 | * @return Config |
||
| 628 | */ |
||
| 629 | 2 | public function setCorrelationIdProvider(CorrelationIdProvider $correlationIdProvider) |
|
| 635 | |||
| 636 | /** |
||
| 637 | * @return bool |
||
| 638 | */ |
||
| 639 | 73 | public function isEnableCorrelationId() |
|
| 643 | |||
| 644 | /** |
||
| 645 | * @param bool $enableCorrelationId |
||
| 646 | * @return Config |
||
| 647 | */ |
||
| 648 | 40 | public function setEnableCorrelationId($enableCorrelationId) |
|
| 653 | |||
| 654 | /** |
||
| 655 | * @return array |
||
| 656 | */ |
||
| 657 | 58 | public function getClientOptions() |
|
| 661 | |||
| 662 | /** |
||
| 663 | * @param array $clientOptions |
||
| 664 | * @return Config |
||
| 665 | */ |
||
| 666 | public function setClientOptions(array $clientOptions) |
||
| 671 | } |
||
| 672 |