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 |
||
58 | class Config implements ContextAwareInterface |
||
59 | { |
||
60 | use ContextTrait; |
||
61 | |||
62 | const OAUTH_URL = 'oauth_url'; |
||
63 | const CLIENT_ID = 'client_id'; |
||
64 | const CLIENT_SECRET = 'client_secret'; |
||
65 | const SCOPE = 'scope'; |
||
66 | const PROJECT = 'project'; |
||
67 | const API_URL = 'api_url'; |
||
68 | const USER_NAME = 'username'; |
||
69 | const PASSWORD = 'password'; |
||
70 | const REFRESH_TOKEN = 'refresh_token'; |
||
71 | const ANONYMOUS_ID = 'anonymous_id'; |
||
72 | const GRANT_TYPE = 'grant_type'; |
||
73 | |||
74 | const GRANT_TYPE_CLIENT = 'client_credentials'; |
||
75 | const GRANT_TYPE_PASSWORD = 'password'; |
||
76 | const GRANT_TYPE_REFRESH = 'refresh_token'; |
||
77 | const GRANT_TYPE_ANONYMOUS = 'anonymous_token'; |
||
78 | |||
79 | /** |
||
80 | * @var string |
||
81 | */ |
||
82 | protected $clientSecret; |
||
83 | |||
84 | /** |
||
85 | * @var string |
||
86 | */ |
||
87 | protected $clientId; |
||
88 | |||
89 | /** |
||
90 | * @var string |
||
91 | */ |
||
92 | protected $project; |
||
93 | |||
94 | /** |
||
95 | * @var array |
||
96 | */ |
||
97 | protected $scope = ['manage_project']; |
||
98 | |||
99 | /** |
||
100 | * @var string |
||
101 | */ |
||
102 | protected $oauthUrl = 'https://auth.sphere.io'; |
||
103 | |||
104 | /** |
||
105 | * @var string |
||
106 | */ |
||
107 | protected $apiUrl = 'https://api.sphere.io'; |
||
108 | |||
109 | /** |
||
110 | * @var int |
||
111 | */ |
||
112 | protected $batchPoolSize = 25; |
||
113 | |||
114 | protected $adapter; |
||
115 | |||
116 | /** |
||
117 | * @var bool |
||
118 | */ |
||
119 | protected $throwExceptions = false; |
||
120 | |||
121 | protected $acceptEncoding = 'gzip'; |
||
122 | |||
123 | protected $grantType = 'client_credentials'; |
||
124 | |||
125 | /** |
||
126 | * @var string |
||
127 | */ |
||
128 | protected $username; |
||
129 | |||
130 | /** |
||
131 | * @var string |
||
132 | */ |
||
133 | protected $password; |
||
134 | |||
135 | /** |
||
136 | * @var string |
||
137 | */ |
||
138 | protected $refreshToken; |
||
139 | |||
140 | /** |
||
141 | * @var string |
||
142 | */ |
||
143 | protected $anonymousId; |
||
144 | |||
145 | /** |
||
146 | * @var string |
||
147 | */ |
||
148 | protected $cacheDir; |
||
149 | |||
150 | /** |
||
151 | * @var string |
||
152 | */ |
||
153 | protected $logLevel = LogLevel::INFO; |
||
154 | |||
155 | protected $messageFormatter; |
||
156 | |||
157 | /** |
||
158 | * @var bool |
||
159 | */ |
||
160 | protected $enableCorrelationId = false; |
||
161 | |||
162 | /** |
||
163 | * @var CorrelationIdProvider |
||
164 | */ |
||
165 | protected $correlationIdProvider; |
||
166 | |||
167 | /** |
||
168 | * @param array $configValues |
||
169 | * @return static |
||
170 | */ |
||
171 | 100 | public static function fromArray(array $configValues) |
|
187 | |||
188 | 98 | protected function camelize($scored) |
|
203 | |||
204 | /** |
||
205 | * @return string |
||
206 | */ |
||
207 | 84 | public function getClientSecret() |
|
211 | |||
212 | /** |
||
213 | * @param string $clientSecret |
||
214 | * @return $this |
||
215 | */ |
||
216 | 88 | public function setClientSecret($clientSecret) |
|
222 | |||
223 | /** |
||
224 | * @return string |
||
225 | */ |
||
226 | 88 | public function getClientId() |
|
230 | |||
231 | /** |
||
232 | * @param string $clientId |
||
233 | * @return $this |
||
234 | */ |
||
235 | 88 | public function setClientId($clientId) |
|
241 | |||
242 | /** |
||
243 | * @return string |
||
244 | */ |
||
245 | 530 | public function getProject() |
|
249 | |||
250 | /** |
||
251 | * @param string $project |
||
252 | * @return $this |
||
253 | */ |
||
254 | 96 | public function setProject($project) |
|
260 | |||
261 | /** |
||
262 | * @return string |
||
263 | */ |
||
264 | 493 | public function getScope() |
|
284 | |||
285 | /** |
||
286 | * @param string $scope |
||
287 | * @return $this |
||
288 | */ |
||
289 | 53 | public function setScope($scope) |
|
301 | |||
302 | /** |
||
303 | * @return string |
||
304 | */ |
||
305 | 51 | public function getOauthUrl() |
|
317 | |||
318 | /** |
||
319 | * @param string $oauthUrl |
||
320 | * @return $this |
||
321 | */ |
||
322 | 88 | public function setOauthUrl($oauthUrl) |
|
328 | |||
329 | /** |
||
330 | * @return string |
||
331 | */ |
||
332 | 62 | public function getApiUrl() |
|
336 | |||
337 | /** |
||
338 | * @param string $apiUrl |
||
339 | * @return $this |
||
340 | */ |
||
341 | 88 | public function setApiUrl($apiUrl) |
|
347 | |||
348 | /** |
||
349 | * @return bool |
||
350 | */ |
||
351 | 84 | public function check() |
|
367 | |||
368 | /** |
||
369 | * @return int |
||
370 | */ |
||
371 | 1 | public function getBatchPoolSize() |
|
375 | |||
376 | /** |
||
377 | * @param int $batchPoolSize |
||
378 | * @return $this |
||
379 | */ |
||
380 | 1 | public function setBatchPoolSize($batchPoolSize) |
|
386 | |||
387 | /** |
||
388 | * @return string |
||
389 | */ |
||
390 | 74 | public function getAdapter() |
|
394 | |||
395 | /** |
||
396 | * @param string $adapter |
||
397 | * @return $this |
||
398 | */ |
||
399 | public function setAdapter($adapter) |
||
405 | |||
406 | /** |
||
407 | * @return bool |
||
408 | */ |
||
409 | 122 | public function getThrowExceptions() |
|
413 | |||
414 | /** |
||
415 | * @param bool $throwExceptions |
||
416 | * @return $this |
||
417 | */ |
||
418 | 9 | public function setThrowExceptions($throwExceptions) |
|
424 | |||
425 | /** |
||
426 | * @return string |
||
427 | */ |
||
428 | 74 | public function getAcceptEncoding() |
|
432 | |||
433 | /** |
||
434 | * @param string $acceptEncoding |
||
435 | * @return $this |
||
436 | */ |
||
437 | public function setAcceptEncoding($acceptEncoding) |
||
443 | |||
444 | /** |
||
445 | * @return static |
||
446 | */ |
||
447 | 100 | public static function of() |
|
451 | |||
452 | /** |
||
453 | * @return string |
||
454 | */ |
||
455 | 486 | public function getGrantType() |
|
459 | |||
460 | /** |
||
461 | * @param string $grantType |
||
462 | * @return $this |
||
463 | */ |
||
464 | 33 | public function setGrantType($grantType) |
|
470 | |||
471 | /** |
||
472 | * @return string |
||
473 | */ |
||
474 | 19 | public function getUsername() |
|
478 | |||
479 | /** |
||
480 | * @param string $username |
||
481 | * @return $this |
||
482 | */ |
||
483 | 23 | public function setUsername($username) |
|
489 | |||
490 | /** |
||
491 | * @return string |
||
492 | */ |
||
493 | 19 | public function getPassword() |
|
497 | |||
498 | /** |
||
499 | * @param string $password |
||
500 | * @return $this |
||
501 | */ |
||
502 | 23 | public function setPassword($password) |
|
508 | |||
509 | /** |
||
510 | * @return string |
||
511 | */ |
||
512 | 28 | public function getRefreshToken() |
|
516 | |||
517 | /** |
||
518 | * @param string $refreshToken |
||
519 | * @return $this |
||
520 | */ |
||
521 | 28 | public function setRefreshToken($refreshToken) |
|
527 | |||
528 | /** |
||
529 | * @return string |
||
530 | */ |
||
531 | 11 | public function getAnonymousId() |
|
535 | |||
536 | /** |
||
537 | * @param string $anonymousId |
||
538 | * @return string |
||
539 | */ |
||
540 | 4 | public function setAnonymousId($anonymousId) |
|
546 | |||
547 | /** |
||
548 | * @return string |
||
549 | */ |
||
550 | 77 | public function getCacheDir() |
|
554 | |||
555 | /** |
||
556 | * @param string $cacheDir |
||
557 | * @return $this |
||
558 | */ |
||
559 | public function setCacheDir($cacheDir) |
||
565 | |||
566 | /** |
||
567 | * @return string |
||
568 | */ |
||
569 | 40 | public function getLogLevel() |
|
573 | |||
574 | /** |
||
575 | * @param string $logLevel |
||
576 | * @return $this |
||
577 | */ |
||
578 | 1 | public function setLogLevel($logLevel) |
|
584 | |||
585 | /** |
||
586 | * @return mixed |
||
587 | */ |
||
588 | 40 | public function getMessageFormatter() |
|
592 | |||
593 | /** |
||
594 | * @param mixed $messageFormatter |
||
595 | * @return $this |
||
596 | */ |
||
597 | public function setMessageFormatter($messageFormatter) |
||
602 | |||
603 | /** |
||
604 | * @return CorrelationIdProvider|null |
||
605 | */ |
||
606 | 73 | public function getCorrelationIdProvider() |
|
616 | |||
617 | /** |
||
618 | * @param CorrelationIdProvider $correlationIdProvider |
||
619 | * @return Config |
||
620 | */ |
||
621 | 2 | public function setCorrelationIdProvider(CorrelationIdProvider $correlationIdProvider) |
|
627 | |||
628 | /** |
||
629 | * @return bool |
||
630 | */ |
||
631 | 73 | public function isEnableCorrelationId() |
|
635 | |||
636 | /** |
||
637 | * @param bool $enableCorrelationId |
||
638 | * @return Config |
||
639 | */ |
||
640 | 39 | public function setEnableCorrelationId($enableCorrelationId) |
|
645 | } |
||
646 |