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 |
||
| 55 | class Config implements ContextAwareInterface |
||
| 56 | { |
||
| 57 | use ContextTrait; |
||
| 58 | |||
| 59 | const OAUTH_URL = 'oauth_url'; |
||
| 60 | const CLIENT_ID = 'client_id'; |
||
| 61 | const CLIENT_SECRET = 'client_secret'; |
||
| 62 | const SCOPE = 'scope'; |
||
| 63 | const PROJECT = 'project'; |
||
| 64 | const API_URL = 'api_url'; |
||
| 65 | const USER_NAME = 'username'; |
||
| 66 | const PASSWORD = 'password'; |
||
| 67 | const REFRESH_TOKEN = 'refresh_token'; |
||
| 68 | const ANONYMOUS_ID = 'anonymous_id'; |
||
| 69 | const GRANT_TYPE = 'grant_type'; |
||
| 70 | |||
| 71 | const GRANT_TYPE_CLIENT = 'client_credentials'; |
||
| 72 | const GRANT_TYPE_PASSWORD = 'password'; |
||
| 73 | const GRANT_TYPE_REFRESH = 'refresh_token'; |
||
| 74 | const GRANT_TYPE_ANONYMOUS = 'anonymous_token'; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $clientSecret; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $clientId; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var string |
||
| 88 | */ |
||
| 89 | protected $project; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var array |
||
| 93 | */ |
||
| 94 | protected $scope = ['manage_project']; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var string |
||
| 98 | */ |
||
| 99 | protected $oauthUrl = 'https://auth.sphere.io'; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var string |
||
| 103 | */ |
||
| 104 | protected $apiUrl = 'https://api.sphere.io'; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var int |
||
| 108 | */ |
||
| 109 | protected $batchPoolSize = 25; |
||
| 110 | |||
| 111 | protected $adapter; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var bool |
||
| 115 | */ |
||
| 116 | protected $throwExceptions = false; |
||
| 117 | |||
| 118 | protected $acceptEncoding = 'gzip'; |
||
| 119 | |||
| 120 | protected $grantType = 'client_credentials'; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var string |
||
| 124 | */ |
||
| 125 | protected $username; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var string |
||
| 129 | */ |
||
| 130 | protected $password; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var string |
||
| 134 | */ |
||
| 135 | protected $refreshToken; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @var string |
||
| 139 | */ |
||
| 140 | protected $anonymousId; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @var string |
||
| 144 | 60 | */ |
|
| 145 | protected $cacheDir; |
||
| 146 | 60 | ||
| 147 | 60 | /** |
|
| 148 | * @param array $configValues |
||
| 149 | 60 | * @return static |
|
| 150 | 58 | */ |
|
| 151 | 58 | public static function fromArray(array $configValues) |
|
| 167 | 58 | ||
| 168 | protected function camelize($scored) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | public function getClientSecret() |
||
| 191 | 48 | ||
| 192 | /** |
||
| 193 | 48 | * @param string $clientSecret |
|
| 194 | * @return $this |
||
| 195 | */ |
||
| 196 | public function setClientSecret($clientSecret) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | public function getClientId() |
||
| 210 | 48 | ||
| 211 | /** |
||
| 212 | 48 | * @param string $clientId |
|
| 213 | * @return $this |
||
| 214 | */ |
||
| 215 | public function setClientId($clientId) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @return string |
||
| 224 | */ |
||
| 225 | public function getProject() |
||
| 229 | 56 | ||
| 230 | /** |
||
| 231 | 56 | * @param string $project |
|
| 232 | * @return $this |
||
| 233 | */ |
||
| 234 | public function setProject($project) |
||
| 240 | 327 | ||
| 241 | /** |
||
| 242 | 327 | * @return string |
|
| 243 | 327 | */ |
|
| 244 | 327 | public function getScope() |
|
| 264 | 16 | ||
| 265 | 11 | /** |
|
| 266 | * @param string $scope |
||
| 267 | 16 | * @return $this |
|
| 268 | */ |
||
| 269 | 16 | public function setScope($scope) |
|
| 278 | 16 | ||
| 279 | 13 | /** |
|
| 280 | 4 | * @return string |
|
| 281 | */ |
||
| 282 | 12 | public function getOauthUrl() |
|
| 294 | 48 | ||
| 295 | /** |
||
| 296 | * @param string $oauthUrl |
||
| 297 | * @return $this |
||
| 298 | */ |
||
| 299 | public function setOauthUrl($oauthUrl) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | 48 | public function getApiUrl() |
|
| 313 | 48 | ||
| 314 | /** |
||
| 315 | * @param string $apiUrl |
||
| 316 | * @return $this |
||
| 317 | */ |
||
| 318 | public function setApiUrl($apiUrl) |
||
| 324 | |||
| 325 | 43 | /** |
|
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | public function check() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @return int |
||
| 347 | */ |
||
| 348 | 1 | public function getBatchPoolSize() |
|
| 352 | 1 | ||
| 353 | /** |
||
| 354 | * @param int $batchPoolSize |
||
| 355 | * @return $this |
||
| 356 | */ |
||
| 357 | public function setBatchPoolSize($batchPoolSize) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @return string |
||
| 366 | */ |
||
| 367 | public function getAdapter() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param string $adapter |
||
| 374 | * @return $this |
||
| 375 | */ |
||
| 376 | public function setAdapter($adapter) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @return bool |
||
| 385 | */ |
||
| 386 | 9 | public function getThrowExceptions() |
|
| 390 | 9 | ||
| 391 | /** |
||
| 392 | * @param bool $throwExceptions |
||
| 393 | * @return $this |
||
| 394 | */ |
||
| 395 | public function setThrowExceptions($throwExceptions) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @return string |
||
| 404 | */ |
||
| 405 | public function getAcceptEncoding() |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @param string $acceptEncoding |
||
| 412 | * @return $this |
||
| 413 | */ |
||
| 414 | public function setAcceptEncoding($acceptEncoding) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @return static |
||
| 423 | 322 | */ |
|
| 424 | public static function of() |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @return string |
||
| 431 | */ |
||
| 432 | 4 | public function getGrantType() |
|
| 436 | 4 | ||
| 437 | /** |
||
| 438 | * @param string $grantType |
||
| 439 | * @return $this |
||
| 440 | */ |
||
| 441 | public function setGrantType($grantType) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @return string |
||
| 450 | */ |
||
| 451 | 4 | public function getUsername() |
|
| 455 | 4 | ||
| 456 | /** |
||
| 457 | * @param string $username |
||
| 458 | * @return $this |
||
| 459 | */ |
||
| 460 | public function setUsername($username) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @return string |
||
| 469 | */ |
||
| 470 | 4 | public function getPassword() |
|
| 474 | 4 | ||
| 475 | /** |
||
| 476 | * @param string $password |
||
| 477 | * @return $this |
||
| 478 | */ |
||
| 479 | public function setPassword($password) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @return string |
||
| 488 | */ |
||
| 489 | 4 | public function getRefreshToken() |
|
| 493 | 4 | ||
| 494 | /** |
||
| 495 | * @param string $refreshToken |
||
| 496 | * @return $this |
||
| 497 | */ |
||
| 498 | public function setRefreshToken($refreshToken) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @return string |
||
| 507 | */ |
||
| 508 | public function getAnonymousId() |
||
| 512 | |||
| 513 | /** |
||
| 514 | * @param string $anonymousId |
||
| 515 | * @return string |
||
| 516 | */ |
||
| 517 | public function setAnonymousId($anonymousId) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * @return string |
||
| 526 | */ |
||
| 527 | public function getCacheDir() |
||
| 531 | |||
| 532 | /** |
||
| 533 | * @param string $cacheDir |
||
| 534 | * @return $this |
||
| 535 | */ |
||
| 536 | public function setCacheDir($cacheDir) |
||
| 542 | } |
||
| 543 |