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 |
||
56 | class Config implements ContextAwareInterface |
||
57 | { |
||
58 | use ContextTrait; |
||
59 | |||
60 | const OAUTH_URL = 'oauth_url'; |
||
61 | const CLIENT_ID = 'client_id'; |
||
62 | const CLIENT_SECRET = 'client_secret'; |
||
63 | const SCOPE = 'scope'; |
||
64 | const PROJECT = 'project'; |
||
65 | const API_URL = 'api_url'; |
||
66 | const USER_NAME = 'username'; |
||
67 | const PASSWORD = 'password'; |
||
68 | const REFRESH_TOKEN = 'refresh_token'; |
||
69 | const ANONYMOUS_ID = 'anonymous_id'; |
||
70 | const GRANT_TYPE = 'grant_type'; |
||
71 | |||
72 | const GRANT_TYPE_CLIENT = 'client_credentials'; |
||
73 | const GRANT_TYPE_PASSWORD = 'password'; |
||
74 | const GRANT_TYPE_REFRESH = 'refresh_token'; |
||
75 | const GRANT_TYPE_ANONYMOUS = 'anonymous_token'; |
||
76 | |||
77 | /** |
||
78 | * @var string |
||
79 | */ |
||
80 | protected $clientSecret; |
||
81 | |||
82 | /** |
||
83 | * @var string |
||
84 | */ |
||
85 | protected $clientId; |
||
86 | |||
87 | /** |
||
88 | * @var string |
||
89 | */ |
||
90 | protected $project; |
||
91 | |||
92 | /** |
||
93 | * @var array |
||
94 | */ |
||
95 | protected $scope = ['manage_project']; |
||
96 | |||
97 | /** |
||
98 | * @var string |
||
99 | */ |
||
100 | protected $oauthUrl = 'https://auth.sphere.io'; |
||
101 | |||
102 | /** |
||
103 | * @var string |
||
104 | */ |
||
105 | protected $apiUrl = 'https://api.sphere.io'; |
||
106 | |||
107 | /** |
||
108 | * @var int |
||
109 | */ |
||
110 | protected $batchPoolSize = 25; |
||
111 | |||
112 | protected $adapter; |
||
113 | |||
114 | /** |
||
115 | * @var bool |
||
116 | */ |
||
117 | protected $throwExceptions = false; |
||
118 | |||
119 | protected $acceptEncoding = 'gzip'; |
||
120 | |||
121 | protected $grantType = 'client_credentials'; |
||
122 | |||
123 | /** |
||
124 | * @var string |
||
125 | */ |
||
126 | protected $username; |
||
127 | |||
128 | /** |
||
129 | * @var string |
||
130 | */ |
||
131 | protected $password; |
||
132 | |||
133 | /** |
||
134 | * @var string |
||
135 | */ |
||
136 | protected $refreshToken; |
||
137 | |||
138 | /** |
||
139 | * @var string |
||
140 | */ |
||
141 | protected $anonymousId; |
||
142 | |||
143 | /** |
||
144 | * @var string |
||
145 | */ |
||
146 | protected $cacheDir; |
||
147 | |||
148 | /** |
||
149 | * @var string |
||
150 | */ |
||
151 | protected $logLevel = LogLevel::INFO; |
||
152 | |||
153 | /** |
||
154 | * @param array $configValues |
||
155 | * @return static |
||
156 | */ |
||
157 | 98 | public static function fromArray(array $configValues) |
|
173 | |||
174 | 96 | protected function camelize($scored) |
|
189 | |||
190 | /** |
||
191 | * @return string |
||
192 | */ |
||
193 | 82 | public function getClientSecret() |
|
197 | |||
198 | /** |
||
199 | * @param string $clientSecret |
||
200 | * @return $this |
||
201 | */ |
||
202 | 86 | public function setClientSecret($clientSecret) |
|
208 | |||
209 | /** |
||
210 | * @return string |
||
211 | */ |
||
212 | 86 | public function getClientId() |
|
216 | |||
217 | /** |
||
218 | * @param string $clientId |
||
219 | * @return $this |
||
220 | */ |
||
221 | 86 | public function setClientId($clientId) |
|
227 | |||
228 | /** |
||
229 | * @return string |
||
230 | */ |
||
231 | 517 | public function getProject() |
|
235 | |||
236 | /** |
||
237 | * @param string $project |
||
238 | * @return $this |
||
239 | */ |
||
240 | 94 | public function setProject($project) |
|
246 | |||
247 | /** |
||
248 | * @return string |
||
249 | */ |
||
250 | 481 | public function getScope() |
|
270 | |||
271 | /** |
||
272 | * @param string $scope |
||
273 | * @return $this |
||
274 | */ |
||
275 | 51 | public function setScope($scope) |
|
287 | |||
288 | /** |
||
289 | * @return string |
||
290 | */ |
||
291 | 49 | public function getOauthUrl() |
|
303 | |||
304 | /** |
||
305 | * @param string $oauthUrl |
||
306 | * @return $this |
||
307 | */ |
||
308 | 86 | public function setOauthUrl($oauthUrl) |
|
314 | |||
315 | /** |
||
316 | * @return string |
||
317 | */ |
||
318 | 61 | public function getApiUrl() |
|
322 | |||
323 | /** |
||
324 | * @param string $apiUrl |
||
325 | * @return $this |
||
326 | */ |
||
327 | 86 | public function setApiUrl($apiUrl) |
|
333 | |||
334 | /** |
||
335 | * @return bool |
||
336 | */ |
||
337 | 82 | public function check() |
|
353 | |||
354 | /** |
||
355 | * @return int |
||
356 | */ |
||
357 | 1 | public function getBatchPoolSize() |
|
361 | |||
362 | /** |
||
363 | * @param int $batchPoolSize |
||
364 | * @return $this |
||
365 | */ |
||
366 | 1 | public function setBatchPoolSize($batchPoolSize) |
|
372 | |||
373 | /** |
||
374 | * @return string |
||
375 | */ |
||
376 | 72 | public function getAdapter() |
|
380 | |||
381 | /** |
||
382 | * @param string $adapter |
||
383 | * @return $this |
||
384 | */ |
||
385 | public function setAdapter($adapter) |
||
391 | |||
392 | /** |
||
393 | * @return bool |
||
394 | */ |
||
395 | 91 | public function getThrowExceptions() |
|
399 | |||
400 | /** |
||
401 | * @param bool $throwExceptions |
||
402 | * @return $this |
||
403 | */ |
||
404 | 9 | public function setThrowExceptions($throwExceptions) |
|
410 | |||
411 | /** |
||
412 | * @return string |
||
413 | */ |
||
414 | 72 | public function getAcceptEncoding() |
|
418 | |||
419 | /** |
||
420 | * @param string $acceptEncoding |
||
421 | * @return $this |
||
422 | */ |
||
423 | public function setAcceptEncoding($acceptEncoding) |
||
429 | |||
430 | /** |
||
431 | * @return static |
||
432 | */ |
||
433 | 98 | public static function of() |
|
437 | |||
438 | /** |
||
439 | * @return string |
||
440 | */ |
||
441 | 473 | public function getGrantType() |
|
445 | |||
446 | /** |
||
447 | * @param string $grantType |
||
448 | * @return $this |
||
449 | */ |
||
450 | 33 | public function setGrantType($grantType) |
|
456 | |||
457 | /** |
||
458 | * @return string |
||
459 | */ |
||
460 | 19 | public function getUsername() |
|
464 | |||
465 | /** |
||
466 | * @param string $username |
||
467 | * @return $this |
||
468 | */ |
||
469 | 23 | public function setUsername($username) |
|
475 | |||
476 | /** |
||
477 | * @return string |
||
478 | */ |
||
479 | 19 | public function getPassword() |
|
483 | |||
484 | /** |
||
485 | * @param string $password |
||
486 | * @return $this |
||
487 | */ |
||
488 | 23 | public function setPassword($password) |
|
494 | |||
495 | /** |
||
496 | * @return string |
||
497 | */ |
||
498 | 28 | public function getRefreshToken() |
|
502 | |||
503 | /** |
||
504 | * @param string $refreshToken |
||
505 | * @return $this |
||
506 | */ |
||
507 | 28 | public function setRefreshToken($refreshToken) |
|
513 | |||
514 | /** |
||
515 | * @return string |
||
516 | */ |
||
517 | 11 | public function getAnonymousId() |
|
521 | |||
522 | /** |
||
523 | * @param string $anonymousId |
||
524 | * @return string |
||
525 | */ |
||
526 | 4 | public function setAnonymousId($anonymousId) |
|
532 | |||
533 | /** |
||
534 | * @return string |
||
535 | */ |
||
536 | 75 | public function getCacheDir() |
|
540 | |||
541 | /** |
||
542 | * @param string $cacheDir |
||
543 | * @return $this |
||
544 | */ |
||
545 | public function setCacheDir($cacheDir) |
||
551 | |||
552 | /** |
||
553 | * @return string |
||
554 | */ |
||
555 | 39 | public function getLogLevel() |
|
559 | |||
560 | /** |
||
561 | * @param string $logLevel |
||
562 | * @return $this |
||
563 | */ |
||
564 | 1 | public function setLogLevel($logLevel) |
|
570 | } |
||
571 |