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 | * @var string |
||
| 137 | */ |
||
| 138 | protected $cacheDir; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param array $configValues |
||
| 142 | * @return static |
||
| 143 | */ |
||
| 144 | 60 | public static function fromArray(array $configValues) |
|
| 145 | { |
||
| 146 | 60 | $config = static::of(); |
|
| 147 | 60 | array_walk( |
|
| 148 | $configValues, |
||
| 149 | 60 | function ($value, $key) use ($config) { |
|
| 150 | 58 | $functionName = 'set' . $config->camelize($key); |
|
| 151 | 58 | if (!is_callable([$config, $functionName])) { |
|
| 152 | 1 | throw new InvalidArgumentException(sprintf(Message::SETTER_NOT_IMPLEMENTED, $key)); |
|
| 153 | } |
||
| 154 | 57 | $config->$functionName($value); |
|
| 155 | 60 | } |
|
| 156 | ); |
||
| 157 | |||
| 158 | 59 | return $config; |
|
| 159 | } |
||
| 160 | |||
| 161 | 58 | protected function camelize($scored) |
|
| 162 | { |
||
| 163 | 58 | return lcfirst( |
|
| 164 | implode( |
||
| 165 | 58 | '', |
|
| 166 | array_map( |
||
| 167 | 58 | 'ucfirst', |
|
| 168 | array_map( |
||
| 169 | 58 | 'strtolower', |
|
| 170 | 58 | explode('_', $scored) |
|
| 171 | ) |
||
| 172 | ) |
||
| 173 | ) |
||
| 174 | ); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | 47 | public function getClientSecret() |
|
| 181 | { |
||
| 182 | 47 | return $this->clientSecret; |
|
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param string $clientSecret |
||
| 187 | * @return $this |
||
| 188 | */ |
||
| 189 | 48 | public function setClientSecret($clientSecret) |
|
| 190 | { |
||
| 191 | 48 | $this->clientSecret = $clientSecret; |
|
| 192 | |||
| 193 | 48 | return $this; |
|
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | 51 | public function getClientId() |
|
| 200 | { |
||
| 201 | 51 | return $this->clientId; |
|
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param string $clientId |
||
| 206 | * @return $this |
||
| 207 | */ |
||
| 208 | 48 | public function setClientId($clientId) |
|
| 209 | { |
||
| 210 | 48 | $this->clientId = $clientId; |
|
| 211 | |||
| 212 | 48 | return $this; |
|
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | 349 | public function getProject() |
|
| 219 | { |
||
| 220 | 349 | return $this->project; |
|
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param string $project |
||
| 225 | * @return $this |
||
| 226 | */ |
||
| 227 | 56 | public function setProject($project) |
|
| 228 | { |
||
| 229 | 56 | $this->project = $project; |
|
| 230 | |||
| 231 | 56 | return $this; |
|
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | 314 | public function getScope() |
|
| 257 | |||
| 258 | /** |
||
| 259 | * @param string $scope |
||
| 260 | * @return $this |
||
| 261 | */ |
||
| 262 | 16 | public function setScope($scope) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @return string |
||
| 274 | */ |
||
| 275 | 16 | public function getOauthUrl() |
|
| 285 | |||
| 286 | /** |
||
| 287 | * @param string $oauthUrl |
||
| 288 | * @return $this |
||
| 289 | */ |
||
| 290 | 48 | public function setOauthUrl($oauthUrl) |
|
| 291 | { |
||
| 292 | 48 | $this->oauthUrl = $oauthUrl; |
|
| 293 | |||
| 294 | 48 | return $this; |
|
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | 34 | public function getApiUrl() |
|
| 301 | { |
||
| 302 | 34 | return $this->apiUrl; |
|
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param string $apiUrl |
||
| 307 | * @return $this |
||
| 308 | */ |
||
| 309 | 48 | public function setApiUrl($apiUrl) |
|
| 310 | { |
||
| 311 | 48 | $this->apiUrl = $apiUrl; |
|
| 312 | |||
| 313 | 48 | return $this; |
|
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @return bool |
||
| 318 | */ |
||
| 319 | 47 | public function check() |
|
| 320 | { |
||
| 321 | 47 | if (is_null($this->getClientId())) { |
|
| 322 | 4 | throw new InvalidArgumentException(Message::NO_CLIENT_ID); |
|
| 323 | } |
||
| 324 | |||
| 325 | 43 | if (is_null($this->getClientSecret())) { |
|
| 326 | throw new InvalidArgumentException(Message::NO_CLIENT_SECRET); |
||
| 327 | } |
||
| 328 | |||
| 329 | 43 | if (is_null($this->getProject())) { |
|
| 330 | throw new InvalidArgumentException(Message::NO_PROJECT_ID); |
||
| 331 | } |
||
| 332 | |||
| 333 | 43 | return true; |
|
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @return int |
||
| 338 | */ |
||
| 339 | 1 | public function getBatchPoolSize() |
|
| 340 | { |
||
| 341 | 1 | return $this->batchPoolSize; |
|
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param int $batchPoolSize |
||
| 346 | * @return $this |
||
| 347 | */ |
||
| 348 | 1 | public function setBatchPoolSize($batchPoolSize) |
|
| 349 | { |
||
| 350 | 1 | $this->batchPoolSize = $batchPoolSize; |
|
| 351 | |||
| 352 | 1 | return $this; |
|
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | 37 | public function getAdapter() |
|
| 359 | { |
||
| 360 | 37 | return $this->adapter; |
|
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param string $adapter |
||
| 365 | * @return $this |
||
| 366 | */ |
||
| 367 | public function setAdapter($adapter) |
||
| 368 | { |
||
| 369 | $this->adapter = $adapter; |
||
| 370 | |||
| 371 | return $this; |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @return bool |
||
| 376 | */ |
||
| 377 | 52 | public function getThrowExceptions() |
|
| 378 | { |
||
| 379 | 52 | return $this->throwExceptions; |
|
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @param bool $throwExceptions |
||
| 384 | * @return $this |
||
| 385 | */ |
||
| 386 | 9 | public function setThrowExceptions($throwExceptions) |
|
| 387 | { |
||
| 388 | 9 | $this->throwExceptions = $throwExceptions; |
|
| 389 | |||
| 390 | 9 | return $this; |
|
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @return string |
||
| 395 | */ |
||
| 396 | 37 | public function getAcceptEncoding() |
|
| 397 | { |
||
| 398 | 37 | return $this->acceptEncoding; |
|
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @param string $acceptEncoding |
||
| 403 | * @return $this |
||
| 404 | */ |
||
| 405 | public function setAcceptEncoding($acceptEncoding) |
||
| 406 | { |
||
| 407 | $this->acceptEncoding = $acceptEncoding; |
||
| 408 | |||
| 409 | return $this; |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @return static |
||
| 414 | */ |
||
| 415 | 60 | public static function of() |
|
| 416 | { |
||
| 417 | 60 | return new static(); |
|
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @return string |
||
| 422 | */ |
||
| 423 | 309 | public function getGrantType() |
|
| 427 | |||
| 428 | /** |
||
| 429 | * @param string $grantType |
||
| 430 | * @return $this |
||
| 431 | */ |
||
| 432 | 4 | public function setGrantType($grantType) |
|
| 438 | |||
| 439 | /** |
||
| 440 | * @return string |
||
| 441 | */ |
||
| 442 | 4 | public function getUsername() |
|
| 446 | |||
| 447 | /** |
||
| 448 | * @param string $username |
||
| 449 | * @return $this |
||
| 450 | */ |
||
| 451 | 4 | public function setUsername($username) |
|
| 457 | |||
| 458 | /** |
||
| 459 | * @return string |
||
| 460 | */ |
||
| 461 | 4 | public function getPassword() |
|
| 465 | |||
| 466 | /** |
||
| 467 | * @param string $password |
||
| 468 | * @return $this |
||
| 469 | */ |
||
| 470 | 4 | public function setPassword($password) |
|
| 476 | |||
| 477 | /** |
||
| 478 | * @return string |
||
| 479 | */ |
||
| 480 | 4 | public function getRefreshToken() |
|
| 484 | |||
| 485 | /** |
||
| 486 | * @param string $refreshToken |
||
| 487 | * @return $this |
||
| 488 | */ |
||
| 489 | 4 | public function setRefreshToken($refreshToken) |
|
| 495 | |||
| 496 | /** |
||
| 497 | * @return string |
||
| 498 | */ |
||
| 499 | 40 | public function getCacheDir() |
|
| 503 | |||
| 504 | /** |
||
| 505 | * @param string $cacheDir |
||
| 506 | * @return $this |
||
| 507 | */ |
||
| 508 | public function setCacheDir($cacheDir) |
||
| 514 | } |
||
| 515 |