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 ANONYMOUS_ID = 'anonymous_id'; |
||
69 | const GRANT_TYPE = 'grant_type'; |
||
70 | |||
71 | const GRANT_TYPE_CLIENT = 'client_credentials'; |
||
72 | const GRANT_TYPE_PASSWORD = 'password'; |
||
73 | const GRANT_TYPE_REFRESH = 'refresh_token'; |
||
74 | const GRANT_TYPE_ANONYMOUS = 'anonymous_token'; |
||
75 | |||
76 | /** |
||
77 | * @var string |
||
78 | */ |
||
79 | protected $clientSecret; |
||
80 | |||
81 | /** |
||
82 | * @var string |
||
83 | */ |
||
84 | protected $clientId; |
||
85 | |||
86 | /** |
||
87 | * @var string |
||
88 | */ |
||
89 | protected $project; |
||
90 | |||
91 | /** |
||
92 | * @var array |
||
93 | */ |
||
94 | protected $scope = ['manage_project']; |
||
95 | |||
96 | /** |
||
97 | * @var string |
||
98 | */ |
||
99 | protected $oauthUrl = 'https://auth.sphere.io'; |
||
100 | |||
101 | /** |
||
102 | * @var string |
||
103 | */ |
||
104 | protected $apiUrl = 'https://api.sphere.io'; |
||
105 | |||
106 | /** |
||
107 | * @var int |
||
108 | */ |
||
109 | protected $batchPoolSize = 25; |
||
110 | |||
111 | protected $adapter; |
||
112 | |||
113 | /** |
||
114 | * @var bool |
||
115 | */ |
||
116 | protected $throwExceptions = false; |
||
117 | |||
118 | protected $acceptEncoding = 'gzip'; |
||
119 | |||
120 | protected $grantType = 'client_credentials'; |
||
121 | |||
122 | /** |
||
123 | * @var string |
||
124 | */ |
||
125 | protected $username; |
||
126 | |||
127 | /** |
||
128 | * @var string |
||
129 | */ |
||
130 | protected $password; |
||
131 | |||
132 | /** |
||
133 | * @var string |
||
134 | */ |
||
135 | protected $refreshToken; |
||
136 | |||
137 | /** |
||
138 | * @var string |
||
139 | */ |
||
140 | protected $anonymousId; |
||
141 | |||
142 | /** |
||
143 | * @var string |
||
144 | */ |
||
145 | protected $cacheDir; |
||
146 | |||
147 | /** |
||
148 | * @param array $configValues |
||
149 | * @return static |
||
150 | */ |
||
151 | 94 | public static function fromArray(array $configValues) |
|
167 | |||
168 | 92 | protected function camelize($scored) |
|
183 | |||
184 | /** |
||
185 | * @return string |
||
186 | */ |
||
187 | 79 | public function getClientSecret() |
|
191 | |||
192 | /** |
||
193 | * @param string $clientSecret |
||
194 | * @return $this |
||
195 | */ |
||
196 | 82 | public function setClientSecret($clientSecret) |
|
202 | |||
203 | /** |
||
204 | * @return string |
||
205 | */ |
||
206 | 83 | public function getClientId() |
|
210 | |||
211 | /** |
||
212 | * @param string $clientId |
||
213 | * @return $this |
||
214 | */ |
||
215 | 82 | public function setClientId($clientId) |
|
221 | |||
222 | /** |
||
223 | * @return string |
||
224 | */ |
||
225 | 513 | public function getProject() |
|
229 | |||
230 | /** |
||
231 | * @param string $project |
||
232 | * @return $this |
||
233 | */ |
||
234 | 90 | public function setProject($project) |
|
240 | |||
241 | /** |
||
242 | * @return string |
||
243 | */ |
||
244 | 478 | public function getScope() |
|
264 | |||
265 | /** |
||
266 | * @param string $scope |
||
267 | * @return $this |
||
268 | */ |
||
269 | 50 | public function setScope($scope) |
|
281 | |||
282 | /** |
||
283 | * @return string |
||
284 | */ |
||
285 | 48 | public function getOauthUrl() |
|
297 | |||
298 | /** |
||
299 | * @param string $oauthUrl |
||
300 | * @return $this |
||
301 | */ |
||
302 | 82 | public function setOauthUrl($oauthUrl) |
|
308 | |||
309 | /** |
||
310 | * @return string |
||
311 | */ |
||
312 | 58 | public function getApiUrl() |
|
316 | |||
317 | /** |
||
318 | * @param string $apiUrl |
||
319 | * @return $this |
||
320 | */ |
||
321 | 82 | public function setApiUrl($apiUrl) |
|
327 | |||
328 | /** |
||
329 | * @return bool |
||
330 | */ |
||
331 | 78 | public function check() |
|
347 | |||
348 | /** |
||
349 | * @return int |
||
350 | */ |
||
351 | 1 | public function getBatchPoolSize() |
|
355 | |||
356 | /** |
||
357 | * @param int $batchPoolSize |
||
358 | * @return $this |
||
359 | */ |
||
360 | 1 | public function setBatchPoolSize($batchPoolSize) |
|
366 | |||
367 | /** |
||
368 | * @return string |
||
369 | */ |
||
370 | 68 | public function getAdapter() |
|
374 | |||
375 | /** |
||
376 | * @param string $adapter |
||
377 | * @return $this |
||
378 | */ |
||
379 | public function setAdapter($adapter) |
||
385 | |||
386 | /** |
||
387 | * @return bool |
||
388 | */ |
||
389 | 89 | public function getThrowExceptions() |
|
393 | |||
394 | /** |
||
395 | * @param bool $throwExceptions |
||
396 | * @return $this |
||
397 | */ |
||
398 | 9 | public function setThrowExceptions($throwExceptions) |
|
404 | |||
405 | /** |
||
406 | * @return string |
||
407 | */ |
||
408 | 68 | public function getAcceptEncoding() |
|
412 | |||
413 | /** |
||
414 | * @param string $acceptEncoding |
||
415 | * @return $this |
||
416 | */ |
||
417 | public function setAcceptEncoding($acceptEncoding) |
||
423 | |||
424 | /** |
||
425 | * @return static |
||
426 | */ |
||
427 | 94 | public static function of() |
|
431 | |||
432 | /** |
||
433 | * @return string |
||
434 | */ |
||
435 | 470 | public function getGrantType() |
|
439 | |||
440 | /** |
||
441 | * @param string $grantType |
||
442 | * @return $this |
||
443 | */ |
||
444 | 33 | public function setGrantType($grantType) |
|
450 | |||
451 | /** |
||
452 | * @return string |
||
453 | */ |
||
454 | 19 | public function getUsername() |
|
458 | |||
459 | /** |
||
460 | * @param string $username |
||
461 | * @return $this |
||
462 | */ |
||
463 | 23 | public function setUsername($username) |
|
469 | |||
470 | /** |
||
471 | * @return string |
||
472 | */ |
||
473 | 19 | public function getPassword() |
|
477 | |||
478 | /** |
||
479 | * @param string $password |
||
480 | * @return $this |
||
481 | */ |
||
482 | 23 | public function setPassword($password) |
|
488 | |||
489 | /** |
||
490 | * @return string |
||
491 | */ |
||
492 | 28 | public function getRefreshToken() |
|
496 | |||
497 | /** |
||
498 | * @param string $refreshToken |
||
499 | * @return $this |
||
500 | */ |
||
501 | 28 | public function setRefreshToken($refreshToken) |
|
507 | |||
508 | /** |
||
509 | * @return string |
||
510 | */ |
||
511 | 11 | public function getAnonymousId() |
|
515 | |||
516 | /** |
||
517 | * @param string $anonymousId |
||
518 | * @return string |
||
519 | */ |
||
520 | 4 | public function setAnonymousId($anonymousId) |
|
526 | |||
527 | /** |
||
528 | * @return string |
||
529 | */ |
||
530 | 71 | public function getCacheDir() |
|
534 | |||
535 | /** |
||
536 | * @param string $cacheDir |
||
537 | * @return $this |
||
538 | */ |
||
539 | public function setCacheDir($cacheDir) |
||
545 | } |
||
546 |