Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 59 | class Config implements ContextAwareInterface |
||
| 60 | { |
||
| 61 | use ContextTrait; |
||
| 62 | |||
| 63 | const OAUTH_URL = 'oauth_url'; |
||
| 64 | const CLIENT_ID = 'client_id'; |
||
| 65 | const CLIENT_SECRET = 'client_secret'; |
||
| 66 | const SCOPE = 'scope'; |
||
| 67 | const PROJECT = 'project'; |
||
| 68 | const API_URL = 'api_url'; |
||
| 69 | const USER_NAME = 'username'; |
||
| 70 | const PASSWORD = 'password'; |
||
| 71 | const REFRESH_TOKEN = 'refresh_token'; |
||
| 72 | const BEARER_TOKEN = 'bearer_token'; |
||
| 73 | const ANONYMOUS_ID = 'anonymous_id'; |
||
| 74 | const GRANT_TYPE = 'grant_type'; |
||
| 75 | |||
| 76 | const GRANT_TYPE_CLIENT = 'client_credentials'; |
||
| 77 | const GRANT_TYPE_PASSWORD = 'password'; |
||
| 78 | const GRANT_TYPE_REFRESH = 'refresh_token'; |
||
| 79 | const GRANT_TYPE_ANONYMOUS = 'anonymous_token'; |
||
| 80 | const GRANT_TYPE_BEARER_TOKEN = 'bearer_token'; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | protected $clientSecret; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var string |
||
| 89 | */ |
||
| 90 | protected $clientId; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var string |
||
| 94 | */ |
||
| 95 | protected $project; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | protected $scope = ['manage_project']; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var string |
||
| 104 | */ |
||
| 105 | protected $oauthUrl = 'https://auth.sphere.io'; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var string |
||
| 109 | */ |
||
| 110 | protected $apiUrl = 'https://api.sphere.io'; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var int |
||
| 114 | */ |
||
| 115 | protected $batchPoolSize = 25; |
||
| 116 | |||
| 117 | protected $adapter; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var bool |
||
| 121 | */ |
||
| 122 | protected $throwExceptions = false; |
||
| 123 | |||
| 124 | protected $acceptEncoding = 'gzip'; |
||
| 125 | |||
| 126 | protected $grantType = 'client_credentials'; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var string |
||
| 130 | */ |
||
| 131 | protected $username; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var string |
||
| 135 | */ |
||
| 136 | protected $password; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var string |
||
| 140 | */ |
||
| 141 | protected $refreshToken; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @var string |
||
| 145 | */ |
||
| 146 | protected $bearerToken; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @var string |
||
| 150 | */ |
||
| 151 | protected $anonymousId; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @var string |
||
| 155 | */ |
||
| 156 | protected $cacheDir; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @var string |
||
| 160 | */ |
||
| 161 | protected $logLevel = LogLevel::INFO; |
||
| 162 | |||
| 163 | protected $messageFormatter; |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @var bool |
||
| 167 | */ |
||
| 168 | protected $enableCorrelationId = false; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @var CorrelationIdProvider |
||
| 172 | */ |
||
| 173 | protected $correlationIdProvider; |
||
| 174 | |||
| 175 | protected $clientOptions = []; |
||
| 176 | |||
| 177 | 106 | public function __construct() |
|
| 181 | |||
| 182 | /** |
||
| 183 | * @param array $configValues |
||
| 184 | * @return static |
||
| 185 | */ |
||
| 186 | 103 | public static function fromArray(array $configValues) |
|
| 202 | |||
| 203 | 101 | protected function camelize($scored) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * @return string |
||
| 221 | */ |
||
| 222 | 86 | public function getClientSecret() |
|
| 226 | |||
| 227 | /** |
||
| 228 | * @param string $clientSecret |
||
| 229 | * @return $this |
||
| 230 | */ |
||
| 231 | 89 | public function setClientSecret($clientSecret) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | 90 | public function getClientId() |
|
| 245 | |||
| 246 | /** |
||
| 247 | * @param string $clientId |
||
| 248 | * @return $this |
||
| 249 | */ |
||
| 250 | 89 | public function setClientId($clientId) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | 568 | public function getProject() |
|
| 264 | |||
| 265 | /** |
||
| 266 | * @param string $project |
||
| 267 | * @return $this |
||
| 268 | */ |
||
| 269 | 99 | public function setProject($project) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * @return string |
||
| 278 | */ |
||
| 279 | 529 | public function getScope() |
|
| 299 | |||
| 300 | /** |
||
| 301 | * @param string $scope |
||
| 302 | * @return $this |
||
| 303 | */ |
||
| 304 | 54 | public function setScope($scope) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * @return string |
||
| 319 | */ |
||
| 320 | 52 | public function getOauthUrl() |
|
| 332 | |||
| 333 | /** |
||
| 334 | * @param string $oauthUrl |
||
| 335 | * @return $this |
||
| 336 | */ |
||
| 337 | 89 | public function setOauthUrl($oauthUrl) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | 64 | public function getApiUrl() |
|
| 351 | |||
| 352 | /** |
||
| 353 | * @param string $apiUrl |
||
| 354 | * @return $this |
||
| 355 | */ |
||
| 356 | 89 | public function setApiUrl($apiUrl) |
|
| 362 | |||
| 363 | /** |
||
| 364 | * @return bool |
||
| 365 | */ |
||
| 366 | 87 | public function check() |
|
| 382 | |||
| 383 | /** |
||
| 384 | * @deprecated use getClientOptions()['concurrency'] instead |
||
| 385 | * @return int |
||
| 386 | */ |
||
| 387 | 1 | public function getBatchPoolSize() |
|
| 394 | |||
| 395 | /** |
||
| 396 | * @deprecated use setClientOptions(['concurrency' => 5]) instead |
||
| 397 | * @param int $batchPoolSize |
||
| 398 | * @return $this |
||
| 399 | */ |
||
| 400 | 1 | public function setBatchPoolSize($batchPoolSize) |
|
| 407 | |||
| 408 | /** |
||
| 409 | * @return string |
||
| 410 | */ |
||
| 411 | 75 | public function getAdapter() |
|
| 415 | |||
| 416 | /** |
||
| 417 | * @param string $adapter |
||
| 418 | * @return $this |
||
| 419 | */ |
||
| 420 | public function setAdapter($adapter) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @return bool |
||
| 429 | */ |
||
| 430 | 87 | public function getThrowExceptions() |
|
| 434 | |||
| 435 | /** |
||
| 436 | * @param bool $throwExceptions |
||
| 437 | * @return $this |
||
| 438 | */ |
||
| 439 | 9 | public function setThrowExceptions($throwExceptions) |
|
| 445 | |||
| 446 | /** |
||
| 447 | * @return string |
||
| 448 | */ |
||
| 449 | 74 | public function getAcceptEncoding() |
|
| 453 | |||
| 454 | /** |
||
| 455 | * @param string $acceptEncoding |
||
| 456 | * @return $this |
||
| 457 | */ |
||
| 458 | public function setAcceptEncoding($acceptEncoding) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * @return static |
||
| 467 | */ |
||
| 468 | 103 | public static function of() |
|
| 472 | |||
| 473 | /** |
||
| 474 | * @return string |
||
| 475 | */ |
||
| 476 | 527 | public function getGrantType() |
|
| 480 | |||
| 481 | /** |
||
| 482 | * @param string $grantType |
||
| 483 | * @return $this |
||
| 484 | */ |
||
| 485 | 35 | public function setGrantType($grantType) |
|
| 491 | |||
| 492 | /** |
||
| 493 | * @return string |
||
| 494 | */ |
||
| 495 | 19 | public function getUsername() |
|
| 499 | |||
| 500 | /** |
||
| 501 | * @param string $username |
||
| 502 | * @return $this |
||
| 503 | */ |
||
| 504 | 23 | public function setUsername($username) |
|
| 510 | |||
| 511 | /** |
||
| 512 | * @return string |
||
| 513 | */ |
||
| 514 | 19 | public function getPassword() |
|
| 518 | |||
| 519 | /** |
||
| 520 | * @param string $password |
||
| 521 | * @return $this |
||
| 522 | */ |
||
| 523 | 23 | public function setPassword($password) |
|
| 529 | |||
| 530 | /** |
||
| 531 | * @return string |
||
| 532 | */ |
||
| 533 | 28 | public function getRefreshToken() |
|
| 537 | |||
| 538 | /** |
||
| 539 | * @param string $refreshToken |
||
| 540 | * @return $this |
||
| 541 | */ |
||
| 542 | 28 | public function setRefreshToken($refreshToken) |
|
| 548 | |||
| 549 | /** |
||
| 550 | * @return string |
||
| 551 | */ |
||
| 552 | 11 | public function getAnonymousId() |
|
| 556 | |||
| 557 | /** |
||
| 558 | * @param string $anonymousId |
||
| 559 | * @return string |
||
| 560 | */ |
||
| 561 | 4 | public function setAnonymousId($anonymousId) |
|
| 567 | |||
| 568 | /** |
||
| 569 | * @return string |
||
| 570 | */ |
||
| 571 | 78 | public function getCacheDir() |
|
| 575 | |||
| 576 | /** |
||
| 577 | * @param string $cacheDir |
||
| 578 | * @return $this |
||
| 579 | */ |
||
| 580 | public function setCacheDir($cacheDir) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * @return string |
||
| 589 | */ |
||
| 590 | 40 | public function getLogLevel() |
|
| 594 | |||
| 595 | /** |
||
| 596 | * @param string $logLevel |
||
| 597 | * @return $this |
||
| 598 | */ |
||
| 599 | 1 | public function setLogLevel($logLevel) |
|
| 605 | |||
| 606 | /** |
||
| 607 | * @return mixed |
||
| 608 | */ |
||
| 609 | 40 | public function getMessageFormatter() |
|
| 613 | |||
| 614 | /** |
||
| 615 | * @param mixed $messageFormatter |
||
| 616 | * @return $this |
||
| 617 | */ |
||
| 618 | public function setMessageFormatter($messageFormatter) |
||
| 623 | |||
| 624 | /** |
||
| 625 | * @return CorrelationIdProvider|null |
||
| 626 | */ |
||
| 627 | 73 | public function getCorrelationIdProvider() |
|
| 637 | |||
| 638 | /** |
||
| 639 | * @param CorrelationIdProvider $correlationIdProvider |
||
| 640 | * @return Config |
||
| 641 | */ |
||
| 642 | 2 | public function setCorrelationIdProvider(CorrelationIdProvider $correlationIdProvider) |
|
| 648 | |||
| 649 | /** |
||
| 650 | * @return bool |
||
| 651 | */ |
||
| 652 | 73 | public function isEnableCorrelationId() |
|
| 656 | |||
| 657 | /** |
||
| 658 | * @param bool $enableCorrelationId |
||
| 659 | * @return Config |
||
| 660 | */ |
||
| 661 | 40 | public function setEnableCorrelationId($enableCorrelationId) |
|
| 666 | |||
| 667 | /** |
||
| 668 | * @return array |
||
| 669 | */ |
||
| 670 | 58 | public function getClientOptions() |
|
| 674 | |||
| 675 | /** |
||
| 676 | * @param array $clientOptions |
||
| 677 | * @return Config |
||
| 678 | */ |
||
| 679 | public function setClientOptions(array $clientOptions) |
||
| 684 | |||
| 685 | /** |
||
| 686 | * @return string |
||
| 687 | */ |
||
| 688 | 2 | public function getBearerToken() |
|
| 692 | |||
| 693 | /** |
||
| 694 | * @param string $bearerToken |
||
| 695 | * @return Config |
||
| 696 | */ |
||
| 697 | 2 | public function setBearerToken($bearerToken) |
|
| 702 | } |
||
| 703 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.