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 |
||
11 | class Client extends AbstractClient |
||
12 | { |
||
13 | use UrlHelpersTrait; |
||
14 | |||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | protected $apiList; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $urlApi; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $urlClient; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $urlConsole; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $apiKey; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $secretKey; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $ssoKey; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | private $responseError = 'errortext'; |
||
54 | |||
55 | /** |
||
56 | * @var string |
||
57 | */ |
||
58 | private $responseCode = 'errorcode'; |
||
59 | |||
60 | /** |
||
61 | * @var boolean |
||
62 | */ |
||
63 | private $ssoEnabled = false; |
||
64 | |||
65 | /** |
||
66 | * Constructs a new Cloudstack client instance. |
||
67 | * |
||
68 | * @param array $options |
||
69 | * An array of options to set on this client. Options include |
||
70 | * 'apiList', 'urlApi', 'urlClient', 'urlConsole', 'apiKey', |
||
71 | * 'secretKey', 'responseError' and 'responseCode'. |
||
72 | * @param array $collaborators |
||
73 | * An array of collaborators that may be used to override |
||
74 | * this provider's default behavior. Collaborators include |
||
75 | * `requestFactory` and `httpClient`. |
||
76 | */ |
||
77 | public function __construct(array $options = [], array $collaborators = []) |
||
93 | |||
94 | /** |
||
95 | * Returns all options that can be configured. |
||
96 | * |
||
97 | * @return array |
||
98 | */ |
||
99 | protected function getConfigurableOptions() |
||
110 | |||
111 | /** |
||
112 | * Returns all options that are required. |
||
113 | * |
||
114 | * @return array |
||
115 | */ |
||
116 | protected function getRequiredOptions() |
||
124 | |||
125 | /** |
||
126 | * Verifies that all required options have been passed. |
||
127 | * |
||
128 | * @param array $options |
||
129 | * @return void |
||
130 | * @throws InvalidArgumentException |
||
131 | */ |
||
132 | private function assertRequiredOptions(array $options) |
||
142 | |||
143 | /** |
||
144 | * Execute command. |
||
145 | * |
||
146 | * @param string $command |
||
147 | * @param array $options |
||
148 | * @return mixed |
||
149 | */ |
||
150 | public function command(string $command, array $options = []) |
||
160 | |||
161 | /** |
||
162 | * Verifies that all required options have been passed. |
||
163 | * |
||
164 | * @param string $command |
||
165 | * @param array $options |
||
166 | * @return void |
||
167 | * @throws RuntimeException |
||
168 | * @throws InvalidArgumentException |
||
169 | */ |
||
170 | private function assertRequiredCommandOptions(string $command, array $options = []) |
||
191 | |||
192 | /** |
||
193 | * Check if command is supported |
||
194 | * @param string $command |
||
195 | * @return boolean |
||
196 | */ |
||
197 | protected function isCommandValid(string $command) |
||
201 | |||
202 | /** |
||
203 | * Get required parameter names |
||
204 | * @param string $command |
||
205 | * @return array |
||
206 | */ |
||
207 | protected function getRequiredCommandParameters(string $command) |
||
218 | |||
219 | /** |
||
220 | * Returns command method based on the command. |
||
221 | * |
||
222 | * @param string $command |
||
223 | * @return string |
||
224 | */ |
||
225 | public function getCommandMethod(string $command) |
||
233 | |||
234 | /** |
||
235 | * Builds the command URL's query string. |
||
236 | * |
||
237 | * @param array $params |
||
238 | * @return string |
||
239 | */ |
||
240 | public function getCommandQuery(array $params) |
||
244 | |||
245 | /** |
||
246 | * Builds the authorization URL. |
||
247 | * |
||
248 | * @param string $command |
||
249 | * @param array $options |
||
250 | * @return string |
||
251 | */ |
||
252 | public function getCommandUrl(string $command, array $options = []) |
||
260 | |||
261 | /** |
||
262 | * Returns command parameters based on provided options. |
||
263 | * |
||
264 | * @param string $command |
||
265 | * @param array $options |
||
266 | * @return array |
||
267 | */ |
||
268 | protected function getCommandParameters(string $command, array $options) |
||
276 | |||
277 | /** |
||
278 | * Signs the command parameters. |
||
279 | * |
||
280 | * @param array $params |
||
281 | * @return string |
||
282 | */ |
||
283 | protected function signCommandParameters(array $params = []) |
||
310 | |||
311 | /** |
||
312 | * Get Cloudstack Client API list. |
||
313 | * |
||
314 | * Tries to load the API list from the cache directory when |
||
315 | * the 'apiList' on the class is empty. |
||
316 | * |
||
317 | * @return array |
||
318 | * @throws RuntimeException |
||
319 | */ |
||
320 | public function getApiList() |
||
336 | |||
337 | /** |
||
338 | * Set Cloudstack Client API list. |
||
339 | * |
||
340 | * @param array $apiList |
||
341 | * @return void |
||
342 | */ |
||
343 | public function setApiList(array $apiList) |
||
347 | |||
348 | /** |
||
349 | * Appends a query string to a URL. |
||
350 | * |
||
351 | * @param string $url |
||
352 | * @param string $query |
||
353 | * @return string |
||
354 | */ |
||
355 | protected function appendQuery(string $url, string $query) |
||
365 | |||
366 | /** |
||
367 | * Build a query string from an array. |
||
368 | * |
||
369 | * @param array $params |
||
370 | * @return string |
||
371 | */ |
||
372 | protected function buildQueryString(array $params) |
||
401 | |||
402 | /** |
||
403 | * Flatten query params. |
||
404 | * |
||
405 | * @param array $params |
||
406 | * @return array |
||
407 | */ |
||
408 | protected static function flattenParams(array $params) |
||
422 | |||
423 | /** |
||
424 | * Checks a provider response for errors. |
||
425 | * |
||
426 | * @param ResponseInterface $response |
||
427 | * @param array|string $data |
||
428 | * @return void |
||
429 | * @throws ClientException |
||
430 | */ |
||
431 | protected function checkResponse(ResponseInterface $response, $data) |
||
446 | |||
447 | /** |
||
448 | * Enable SSO key signing for the next request. |
||
449 | * |
||
450 | * @param boolean $enable |
||
451 | * @return self |
||
452 | */ |
||
453 | public function enableSso(bool $enable = true) |
||
459 | /** |
||
460 | * Determine if SSO signing is enabled. |
||
461 | * |
||
462 | * @return boolean |
||
463 | */ |
||
464 | public function isSsoEnabled() |
||
468 | |||
469 | /** |
||
470 | * Handle dynamic method calls into the method. |
||
471 | * |
||
472 | * @param mixed $method |
||
473 | * @param array $parameters |
||
474 | * @return mixed |
||
475 | */ |
||
476 | public function __call($method, array $parameters) |
||
482 | } |
||
483 |