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 ANONYMOUS_ID = 'anonymous_id'; |
||
73 | const GRANT_TYPE = 'grant_type'; |
||
74 | |||
75 | const GRANT_TYPE_CLIENT = 'client_credentials'; |
||
76 | const GRANT_TYPE_PASSWORD = 'password'; |
||
77 | const GRANT_TYPE_REFRESH = 'refresh_token'; |
||
78 | const GRANT_TYPE_ANONYMOUS = 'anonymous_token'; |
||
79 | |||
80 | /** |
||
81 | * @var string |
||
82 | */ |
||
83 | protected $clientSecret; |
||
84 | |||
85 | /** |
||
86 | * @var string |
||
87 | */ |
||
88 | protected $clientId; |
||
89 | |||
90 | /** |
||
91 | * @var string |
||
92 | */ |
||
93 | protected $project; |
||
94 | |||
95 | /** |
||
96 | * @var array |
||
97 | */ |
||
98 | protected $scope = ['manage_project']; |
||
99 | |||
100 | /** |
||
101 | * @var string |
||
102 | */ |
||
103 | protected $oauthUrl = 'https://auth.sphere.io'; |
||
104 | |||
105 | /** |
||
106 | * @var string |
||
107 | */ |
||
108 | protected $apiUrl = 'https://api.sphere.io'; |
||
109 | |||
110 | /** |
||
111 | * @var int |
||
112 | */ |
||
113 | protected $batchPoolSize = 25; |
||
114 | |||
115 | protected $adapter; |
||
116 | |||
117 | /** |
||
118 | * @var bool |
||
119 | */ |
||
120 | protected $throwExceptions = false; |
||
121 | |||
122 | protected $acceptEncoding = 'gzip'; |
||
123 | |||
124 | protected $grantType = 'client_credentials'; |
||
125 | |||
126 | /** |
||
127 | * @var string |
||
128 | */ |
||
129 | protected $username; |
||
130 | |||
131 | /** |
||
132 | * @var string |
||
133 | */ |
||
134 | protected $password; |
||
135 | |||
136 | /** |
||
137 | * @var string |
||
138 | */ |
||
139 | protected $refreshToken; |
||
140 | |||
141 | /** |
||
142 | * @var string |
||
143 | */ |
||
144 | protected $anonymousId; |
||
145 | |||
146 | /** |
||
147 | * @var string |
||
148 | */ |
||
149 | protected $cacheDir; |
||
150 | |||
151 | /** |
||
152 | * @var string |
||
153 | */ |
||
154 | protected $logLevel = LogLevel::INFO; |
||
155 | |||
156 | protected $messageFormatter; |
||
157 | |||
158 | /** |
||
159 | * @var bool |
||
160 | */ |
||
161 | protected $enableCorrelationId = false; |
||
162 | |||
163 | /** |
||
164 | * @var CorrelationIdProvider |
||
165 | */ |
||
166 | protected $correlationIdProvider; |
||
167 | |||
168 | protected $clientOptions = []; |
||
169 | |||
170 | 104 | public function __construct() |
|
174 | |||
175 | /** |
||
176 | * @param array $configValues |
||
177 | * @return static |
||
178 | */ |
||
179 | 101 | public static function fromArray(array $configValues) |
|
195 | |||
196 | 99 | protected function camelize($scored) |
|
211 | |||
212 | /** |
||
213 | * @return string |
||
214 | */ |
||
215 | 84 | public function getClientSecret() |
|
219 | |||
220 | /** |
||
221 | * @param string $clientSecret |
||
222 | * @return $this |
||
223 | */ |
||
224 | 89 | public function setClientSecret($clientSecret) |
|
230 | |||
231 | /** |
||
232 | * @return string |
||
233 | */ |
||
234 | 88 | public function getClientId() |
|
238 | |||
239 | /** |
||
240 | * @param string $clientId |
||
241 | * @return $this |
||
242 | */ |
||
243 | 89 | public function setClientId($clientId) |
|
249 | |||
250 | /** |
||
251 | * @return string |
||
252 | */ |
||
253 | 535 | public function getProject() |
|
257 | |||
258 | /** |
||
259 | * @param string $project |
||
260 | * @return $this |
||
261 | */ |
||
262 | 97 | public function setProject($project) |
|
268 | |||
269 | /** |
||
270 | * @return string |
||
271 | */ |
||
272 | 497 | public function getScope() |
|
292 | |||
293 | /** |
||
294 | * @param string $scope |
||
295 | * @return $this |
||
296 | */ |
||
297 | 54 | public function setScope($scope) |
|
309 | |||
310 | /** |
||
311 | * @return string |
||
312 | */ |
||
313 | 51 | public function getOauthUrl() |
|
325 | |||
326 | /** |
||
327 | * @param string $oauthUrl |
||
328 | * @return $this |
||
329 | */ |
||
330 | 89 | public function setOauthUrl($oauthUrl) |
|
336 | |||
337 | /** |
||
338 | * @return string |
||
339 | */ |
||
340 | 63 | public function getApiUrl() |
|
344 | |||
345 | /** |
||
346 | * @param string $apiUrl |
||
347 | * @return $this |
||
348 | */ |
||
349 | 89 | public function setApiUrl($apiUrl) |
|
355 | |||
356 | /** |
||
357 | * @return bool |
||
358 | */ |
||
359 | 84 | public function check() |
|
375 | |||
376 | /** |
||
377 | * @deprecated use getClientOptions()['concurrency'] instead |
||
378 | * @return int |
||
379 | */ |
||
380 | 1 | public function getBatchPoolSize() |
|
387 | |||
388 | /** |
||
389 | * @deprecated use setClientOptions(['concurrency' => 5]) instead |
||
390 | * @param int $batchPoolSize |
||
391 | * @return $this |
||
392 | */ |
||
393 | 1 | public function setBatchPoolSize($batchPoolSize) |
|
400 | |||
401 | /** |
||
402 | * @return string |
||
403 | */ |
||
404 | 75 | public function getAdapter() |
|
408 | |||
409 | /** |
||
410 | * @param string $adapter |
||
411 | * @return $this |
||
412 | */ |
||
413 | public function setAdapter($adapter) |
||
419 | |||
420 | /** |
||
421 | * @return bool |
||
422 | */ |
||
423 | 122 | public function getThrowExceptions() |
|
427 | |||
428 | /** |
||
429 | * @param bool $throwExceptions |
||
430 | * @return $this |
||
431 | */ |
||
432 | 9 | public function setThrowExceptions($throwExceptions) |
|
438 | |||
439 | /** |
||
440 | * @return string |
||
441 | */ |
||
442 | 74 | public function getAcceptEncoding() |
|
446 | |||
447 | /** |
||
448 | * @param string $acceptEncoding |
||
449 | * @return $this |
||
450 | */ |
||
451 | public function setAcceptEncoding($acceptEncoding) |
||
457 | |||
458 | /** |
||
459 | * @return static |
||
460 | */ |
||
461 | 101 | public static function of() |
|
465 | |||
466 | /** |
||
467 | * @return string |
||
468 | */ |
||
469 | 490 | public function getGrantType() |
|
473 | |||
474 | /** |
||
475 | * @param string $grantType |
||
476 | * @return $this |
||
477 | */ |
||
478 | 33 | public function setGrantType($grantType) |
|
484 | |||
485 | /** |
||
486 | * @return string |
||
487 | */ |
||
488 | 19 | public function getUsername() |
|
492 | |||
493 | /** |
||
494 | * @param string $username |
||
495 | * @return $this |
||
496 | */ |
||
497 | 23 | public function setUsername($username) |
|
503 | |||
504 | /** |
||
505 | * @return string |
||
506 | */ |
||
507 | 19 | public function getPassword() |
|
511 | |||
512 | /** |
||
513 | * @param string $password |
||
514 | * @return $this |
||
515 | */ |
||
516 | 23 | public function setPassword($password) |
|
522 | |||
523 | /** |
||
524 | * @return string |
||
525 | */ |
||
526 | 28 | public function getRefreshToken() |
|
530 | |||
531 | /** |
||
532 | * @param string $refreshToken |
||
533 | * @return $this |
||
534 | */ |
||
535 | 28 | public function setRefreshToken($refreshToken) |
|
541 | |||
542 | /** |
||
543 | * @return string |
||
544 | */ |
||
545 | 11 | public function getAnonymousId() |
|
549 | |||
550 | /** |
||
551 | * @param string $anonymousId |
||
552 | * @return string |
||
553 | */ |
||
554 | 4 | public function setAnonymousId($anonymousId) |
|
560 | |||
561 | /** |
||
562 | * @return string |
||
563 | */ |
||
564 | 77 | public function getCacheDir() |
|
568 | |||
569 | /** |
||
570 | * @param string $cacheDir |
||
571 | * @return $this |
||
572 | */ |
||
573 | public function setCacheDir($cacheDir) |
||
579 | |||
580 | /** |
||
581 | * @return string |
||
582 | */ |
||
583 | 40 | public function getLogLevel() |
|
587 | |||
588 | /** |
||
589 | * @param string $logLevel |
||
590 | * @return $this |
||
591 | */ |
||
592 | 1 | public function setLogLevel($logLevel) |
|
598 | |||
599 | /** |
||
600 | * @return mixed |
||
601 | */ |
||
602 | 40 | public function getMessageFormatter() |
|
606 | |||
607 | /** |
||
608 | * @param mixed $messageFormatter |
||
609 | * @return $this |
||
610 | */ |
||
611 | public function setMessageFormatter($messageFormatter) |
||
616 | |||
617 | /** |
||
618 | * @return CorrelationIdProvider|null |
||
619 | */ |
||
620 | 73 | public function getCorrelationIdProvider() |
|
630 | |||
631 | /** |
||
632 | * @param CorrelationIdProvider $correlationIdProvider |
||
633 | * @return Config |
||
634 | */ |
||
635 | 2 | public function setCorrelationIdProvider(CorrelationIdProvider $correlationIdProvider) |
|
641 | |||
642 | /** |
||
643 | * @return bool |
||
644 | */ |
||
645 | 73 | public function isEnableCorrelationId() |
|
649 | |||
650 | /** |
||
651 | * @param bool $enableCorrelationId |
||
652 | * @return Config |
||
653 | */ |
||
654 | 40 | public function setEnableCorrelationId($enableCorrelationId) |
|
659 | |||
660 | /** |
||
661 | * @return array |
||
662 | */ |
||
663 | 58 | public function getClientOptions() |
|
667 | |||
668 | /** |
||
669 | * @param array $clientOptions |
||
670 | * @return Config |
||
671 | */ |
||
672 | public function setClientOptions(array $clientOptions) |
||
677 | } |
||
678 |