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 GRANT_TYPE = 'grant_type'; |
||
| 69 | |||
| 70 | const GRANT_TYPE_CLIENT = 'client_credentials'; |
||
| 71 | const GRANT_TYPE_PASSWORD = 'password'; |
||
| 72 | const GRANT_TYPE_REFRESH = 'refresh_token'; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | protected $clientSecret; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | protected $clientId; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | protected $project; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | protected $scope = ['manage_project']; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var string |
||
| 96 | */ |
||
| 97 | protected $oauthUrl = 'https://auth.sphere.io'; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var string |
||
| 101 | */ |
||
| 102 | protected $apiUrl = 'https://api.sphere.io'; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var int |
||
| 106 | */ |
||
| 107 | protected $batchPoolSize = 25; |
||
| 108 | |||
| 109 | protected $adapter; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var bool |
||
| 113 | */ |
||
| 114 | protected $throwExceptions = false; |
||
| 115 | |||
| 116 | protected $acceptEncoding = 'gzip'; |
||
| 117 | |||
| 118 | protected $grantType = 'client_credentials'; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @var string |
||
| 122 | */ |
||
| 123 | protected $username; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @var string |
||
| 127 | */ |
||
| 128 | protected $password; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @var string |
||
| 132 | */ |
||
| 133 | protected $refreshToken; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param array $configValues |
||
| 137 | * @return static |
||
| 138 | */ |
||
| 139 | 57 | public static function fromArray(array $configValues) |
|
| 155 | |||
| 156 | 55 | protected function camelize($scored) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | 44 | public function getClientSecret() |
|
| 179 | |||
| 180 | /** |
||
| 181 | * @param string $clientSecret |
||
| 182 | * @return $this |
||
| 183 | */ |
||
| 184 | 45 | public function setClientSecret($clientSecret) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | 48 | public function getClientId() |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @param string $clientId |
||
| 201 | * @return $this |
||
| 202 | */ |
||
| 203 | 45 | public function setClientId($clientId) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | 323 | public function getProject() |
|
| 217 | |||
| 218 | /** |
||
| 219 | * @param string $project |
||
| 220 | * @return $this |
||
| 221 | */ |
||
| 222 | 53 | public function setProject($project) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | 291 | public function getScope() |
|
| 252 | |||
| 253 | /** |
||
| 254 | * @param string $scope |
||
| 255 | * @return $this |
||
| 256 | */ |
||
| 257 | 16 | public function setScope($scope) |
|
| 266 | |||
| 267 | /** |
||
| 268 | * @return string |
||
| 269 | */ |
||
| 270 | 16 | public function getOauthUrl() |
|
| 280 | |||
| 281 | /** |
||
| 282 | * @param string $oauthUrl |
||
| 283 | * @return $this |
||
| 284 | */ |
||
| 285 | 45 | public function setOauthUrl($oauthUrl) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * @return string |
||
| 294 | */ |
||
| 295 | 34 | public function getApiUrl() |
|
| 299 | |||
| 300 | /** |
||
| 301 | * @param string $apiUrl |
||
| 302 | * @return $this |
||
| 303 | */ |
||
| 304 | 45 | public function setApiUrl($apiUrl) |
|
| 305 | { |
||
| 306 | 45 | $this->apiUrl = $apiUrl; |
|
| 307 | |||
| 308 | 45 | return $this; |
|
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @return bool |
||
| 313 | */ |
||
| 314 | 44 | public function check() |
|
| 330 | |||
| 331 | /** |
||
| 332 | * @return int |
||
| 333 | */ |
||
| 334 | 1 | public function getBatchPoolSize() |
|
| 338 | |||
| 339 | /** |
||
| 340 | * @param int $batchPoolSize |
||
| 341 | * @return $this |
||
| 342 | */ |
||
| 343 | 1 | public function setBatchPoolSize($batchPoolSize) |
|
| 344 | { |
||
| 345 | 1 | $this->batchPoolSize = $batchPoolSize; |
|
| 346 | |||
| 347 | 1 | return $this; |
|
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | 37 | public function getAdapter() |
|
| 357 | |||
| 358 | /** |
||
| 359 | * @param string $adapter |
||
| 360 | * @return $this |
||
| 361 | */ |
||
| 362 | public function setAdapter($adapter) |
||
| 363 | { |
||
| 364 | $this->adapter = $adapter; |
||
| 365 | |||
| 366 | return $this; |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @return bool |
||
| 371 | */ |
||
| 372 | 49 | public function getThrowExceptions() |
|
| 376 | |||
| 377 | /** |
||
| 378 | * @param bool $throwExceptions |
||
| 379 | * @return $this |
||
| 380 | */ |
||
| 381 | 9 | public function setThrowExceptions($throwExceptions) |
|
| 382 | { |
||
| 383 | 9 | $this->throwExceptions = $throwExceptions; |
|
| 384 | |||
| 385 | 9 | return $this; |
|
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | 37 | public function getAcceptEncoding() |
|
| 392 | { |
||
| 393 | 37 | return $this->acceptEncoding; |
|
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @param string $acceptEncoding |
||
| 398 | * @return $this |
||
| 399 | */ |
||
| 400 | public function setAcceptEncoding($acceptEncoding) |
||
| 401 | { |
||
| 402 | $this->acceptEncoding = $acceptEncoding; |
||
| 403 | |||
| 404 | return $this; |
||
| 405 | } |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @return static |
||
| 409 | */ |
||
| 410 | 57 | public static function of() |
|
| 414 | |||
| 415 | /** |
||
| 416 | * @return string |
||
| 417 | */ |
||
| 418 | 286 | public function getGrantType() |
|
| 422 | |||
| 423 | /** |
||
| 424 | * @param string $grantType |
||
| 425 | * @return $this |
||
| 426 | */ |
||
| 427 | 4 | public function setGrantType($grantType) |
|
| 433 | |||
| 434 | /** |
||
| 435 | * @return string |
||
| 436 | */ |
||
| 437 | 4 | public function getUsername() |
|
| 441 | |||
| 442 | /** |
||
| 443 | * @param string $username |
||
| 444 | * @return $this |
||
| 445 | */ |
||
| 446 | 4 | public function setUsername($username) |
|
| 452 | |||
| 453 | /** |
||
| 454 | * @return string |
||
| 455 | */ |
||
| 456 | 4 | public function getPassword() |
|
| 460 | |||
| 461 | /** |
||
| 462 | * @param string $password |
||
| 463 | * @return $this |
||
| 464 | */ |
||
| 465 | 4 | public function setPassword($password) |
|
| 471 | |||
| 472 | /** |
||
| 473 | * @return string |
||
| 474 | */ |
||
| 475 | 4 | public function getRefreshToken() |
|
| 479 | |||
| 480 | /** |
||
| 481 | * @param string $refreshToken |
||
| 482 | * @return $this |
||
| 483 | */ |
||
| 484 | 4 | public function setRefreshToken($refreshToken) |
|
| 490 | } |
||
| 491 |