Complex classes like Client 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 Client, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Client extends AbstractClient |
||
14 | { |
||
15 | use UrlHelpersTrait; |
||
16 | |||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $apiList; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | private $urlApi; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $urlClient; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | private $urlConsole; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $apiKey; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $secretKey; |
||
46 | |||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $ssoKey; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | private $responseError = 'errortext'; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | */ |
||
60 | private $responseCode = 'errorcode'; |
||
61 | |||
62 | /** |
||
63 | * @var boolean |
||
64 | */ |
||
65 | private $ssoEnabled = false; |
||
66 | |||
67 | /** |
||
68 | * Constructs a new Cloudstack client instance. |
||
69 | * |
||
70 | * @param array $options |
||
71 | * An array of options to set on this client. Options include |
||
72 | * 'apiList', 'urlApi', 'urlClient', 'urlConsole', 'apiKey', |
||
73 | * 'secretKey', 'responseError' and 'responseCode'. |
||
74 | * @param array $collaborators |
||
75 | * An array of collaborators that may be used to override |
||
76 | * this provider's default behavior. Collaborators include |
||
77 | * `requestFactory` and `httpClient`. |
||
78 | */ |
||
79 | public function __construct(array $options = [], array $collaborators = []) |
||
95 | |||
96 | /** |
||
97 | * Returns all options that can be configured. |
||
98 | * |
||
99 | * @return array |
||
100 | */ |
||
101 | protected function getConfigurableOptions() : array |
||
112 | |||
113 | /** |
||
114 | * Returns all options that are required. |
||
115 | * |
||
116 | * @return array |
||
117 | */ |
||
118 | protected function getRequiredOptions() : array |
||
126 | |||
127 | /** |
||
128 | * Verifies that all required options have been passed. |
||
129 | * |
||
130 | * @param array $options |
||
131 | * @return void |
||
132 | * @throws InvalidArgumentException |
||
133 | */ |
||
134 | private function assertRequiredOptions(array $options) : void |
||
144 | |||
145 | /** |
||
146 | * Execute command. |
||
147 | * |
||
148 | * @param string $command |
||
149 | * @param array $options |
||
150 | * @return mixed |
||
151 | */ |
||
152 | public function command(string $command, array $options = []) |
||
162 | |||
163 | /** |
||
164 | * Verifies that all required options have been passed. |
||
165 | * |
||
166 | * @param string $command |
||
167 | * @param array $options |
||
168 | * @return void |
||
169 | * @throws RuntimeException |
||
170 | * @throws InvalidArgumentException |
||
171 | */ |
||
172 | private function assertRequiredCommandOptions(string $command, array $options = []) : void |
||
193 | |||
194 | /** |
||
195 | * Check if command is supported |
||
196 | * @param string $command |
||
197 | * @return boolean |
||
198 | */ |
||
199 | protected function isCommandValid(string $command) |
||
203 | |||
204 | /** |
||
205 | * Get required parameter names |
||
206 | * @param string $command |
||
207 | * @return array |
||
208 | */ |
||
209 | protected function getRequiredCommandParameters(string $command) |
||
220 | |||
221 | /** |
||
222 | * Returns command method based on the command. |
||
223 | * |
||
224 | * @param string $command |
||
225 | * @return string |
||
226 | */ |
||
227 | public function getCommandMethod(string $command) : string |
||
235 | |||
236 | /** |
||
237 | * Builds the command URL's query string. |
||
238 | * |
||
239 | * @param array $params |
||
240 | * @return string |
||
241 | */ |
||
242 | public function getCommandQuery(array $params) : string |
||
246 | |||
247 | /** |
||
248 | * Builds the authorization URL. |
||
249 | * |
||
250 | * @param string $command |
||
251 | * @param array $options |
||
252 | * @return string |
||
253 | */ |
||
254 | public function getCommandUrl(string $command, array $options = []) : string |
||
262 | |||
263 | /** |
||
264 | * Returns command parameters based on provided options. |
||
265 | * |
||
266 | * @param string $command |
||
267 | * @param array $options |
||
268 | * @return array |
||
269 | */ |
||
270 | protected function getCommandParameters(string $command, array $options) : array |
||
278 | |||
279 | /** |
||
280 | * Signs the command parameters. |
||
281 | * |
||
282 | * @param array $params |
||
283 | * @return string |
||
284 | */ |
||
285 | protected function signCommandParameters(array $params = []) : string |
||
312 | |||
313 | /** |
||
314 | * Get Cloudstack Client API list. |
||
315 | * |
||
316 | * Tries to load the API list from the cache directory when |
||
317 | * the 'apiList' on the class is empty. |
||
318 | * |
||
319 | * @return array |
||
320 | * @throws RuntimeException |
||
321 | */ |
||
322 | public function getApiList() : array |
||
338 | |||
339 | /** |
||
340 | * Set Cloudstack Client API list. |
||
341 | * |
||
342 | * @param array $apiList |
||
343 | * @return void |
||
344 | */ |
||
345 | public function setApiList(array $apiList) : void |
||
349 | |||
350 | /** |
||
351 | * Appends a query string to a URL. |
||
352 | * |
||
353 | * @param string $url |
||
354 | * @param string $query |
||
355 | * @return string |
||
356 | */ |
||
357 | protected function appendQuery(string $url, string $query) : string |
||
367 | |||
368 | /** |
||
369 | * Build a query string from an array. |
||
370 | * |
||
371 | * @param array $params |
||
372 | * @return string |
||
373 | */ |
||
374 | protected function buildQueryString(array $params) : string |
||
403 | |||
404 | /** |
||
405 | * Flatten query params. |
||
406 | * |
||
407 | * @param array $params |
||
408 | * @return array |
||
409 | */ |
||
410 | protected static function flattenParams(array $params) : array |
||
424 | |||
425 | /** |
||
426 | * Checks a provider response for errors. |
||
427 | * |
||
428 | * @param ResponseInterface $response |
||
429 | * @param array|string $data |
||
430 | * @return void |
||
431 | * @throws ClientException |
||
432 | */ |
||
433 | protected function checkResponse(ResponseInterface $response, $data) : void |
||
448 | |||
449 | /** |
||
450 | * Enable SSO key signing for the next request. |
||
451 | * |
||
452 | * @param boolean $enable |
||
453 | * @return self |
||
454 | */ |
||
455 | public function enableSso(bool $enable = true) : self |
||
461 | /** |
||
462 | * Determine if SSO signing is enabled. |
||
463 | * |
||
464 | * @return boolean |
||
465 | */ |
||
466 | public function isSsoEnabled() : bool |
||
470 | |||
471 | /** |
||
472 | * Handle dynamic method calls into the method. |
||
473 | * |
||
474 | * @param mixed $method |
||
475 | * @param array $parameters |
||
476 | * @return mixed |
||
477 | */ |
||
478 | public function __call($method, array $parameters) |
||
484 | } |
||
485 |