Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
14 | class Client extends AbstractClient |
||
15 | { |
||
16 | use UrlHelpersTrait; |
||
17 | |||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $apiList; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | private $urlApi; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | private $urlClient; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $urlConsole; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $apiKey; |
||
42 | |||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $secretKey; |
||
47 | |||
48 | /** |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $ssoKey; |
||
52 | |||
53 | /** |
||
54 | * @var string |
||
55 | */ |
||
56 | private $responseError = 'errortext'; |
||
57 | |||
58 | /** |
||
59 | * @var string |
||
60 | */ |
||
61 | private $responseCode = 'errorcode'; |
||
62 | |||
63 | /** |
||
64 | * @var boolean |
||
65 | */ |
||
66 | private $ssoEnabled = false; |
||
67 | |||
68 | /** |
||
69 | * @var FilesystemCache |
||
70 | */ |
||
71 | private $cache; |
||
72 | |||
73 | /** |
||
74 | * Constructs a new Cloudstack client instance. |
||
75 | * |
||
76 | * @param array $options |
||
77 | * An array of options to set on this client. Options include |
||
78 | * 'apiList', 'urlApi', 'urlClient', 'urlConsole', 'apiKey', |
||
79 | * 'secretKey', 'responseError' and 'responseCode'. |
||
80 | * @param array $collaborators |
||
81 | * An array of collaborators that may be used to override |
||
82 | * this provider's default behavior. Collaborators include |
||
83 | * `requestFactory` and `httpClient`. |
||
84 | */ |
||
85 | public function __construct(array $options = [], array $collaborators = []) |
||
101 | |||
102 | /** |
||
103 | * Returns all options that can be configured. |
||
104 | * |
||
105 | * @return array |
||
106 | */ |
||
107 | protected function getConfigurableOptions() : array |
||
118 | |||
119 | /** |
||
120 | * Returns all options that are required. |
||
121 | * |
||
122 | * @return array |
||
123 | */ |
||
124 | protected function getRequiredOptions() : array |
||
132 | |||
133 | /** |
||
134 | * Verifies that all required options have been passed. |
||
135 | * |
||
136 | * @param array $options |
||
137 | * @return void |
||
138 | * @throws InvalidArgumentException |
||
139 | */ |
||
140 | private function assertRequiredOptions(array $options) : void |
||
150 | |||
151 | /** |
||
152 | * Execute command. |
||
153 | * |
||
154 | * @param string $command |
||
155 | * @param array $options |
||
156 | * @return mixed |
||
157 | */ |
||
158 | public function command(string $command, array $options = []) |
||
168 | |||
169 | /** |
||
170 | * Verifies that all required options have been passed. |
||
171 | * |
||
172 | * @param string $command |
||
173 | * @param array $options |
||
174 | * @return void |
||
175 | * @throws RuntimeException |
||
176 | * @throws InvalidArgumentException |
||
177 | */ |
||
178 | private function assertRequiredCommandOptions(string $command, array $options = []) : void |
||
196 | |||
197 | /** |
||
198 | * Returns command method based on the command. |
||
199 | * |
||
200 | * @param string $command |
||
201 | * @return string |
||
202 | */ |
||
203 | public function getCommandMethod(string $command) : string |
||
211 | |||
212 | /** |
||
213 | * Builds the command URL's query string. |
||
214 | * |
||
215 | * @param array $params |
||
216 | * @return string |
||
217 | */ |
||
218 | public function getCommandQuery(array $params) : string |
||
222 | |||
223 | /** |
||
224 | * Builds the authorization URL. |
||
225 | * |
||
226 | * @param string $command |
||
227 | * @param array $options |
||
228 | * @return string |
||
229 | */ |
||
230 | public function getCommandUrl(string $command, array $options = []) : string |
||
238 | |||
239 | /** |
||
240 | * Returns command parameters based on provided options. |
||
241 | * |
||
242 | * @param string $command |
||
243 | * @param array $options |
||
244 | * @return array |
||
245 | */ |
||
246 | protected function getCommandParameters(string $command, array $options) : array |
||
254 | |||
255 | /** |
||
256 | * Signs the command parameters. |
||
257 | * |
||
258 | * @param array $params |
||
259 | * @return string |
||
260 | */ |
||
261 | protected function signCommandParameters(array $params = []) : string |
||
288 | |||
289 | /** |
||
290 | * Get Cloudstack Client API list. |
||
291 | * |
||
292 | * Tries to load the API list from the cache directory when |
||
293 | * the 'apiList' on the class is empty. |
||
294 | * |
||
295 | * @return array |
||
296 | * @throws RuntimeException |
||
297 | */ |
||
298 | public function getApiList() : array |
||
312 | |||
313 | /** |
||
314 | * Set Cloudstack Client API list. |
||
315 | * |
||
316 | * @param array $apiList |
||
317 | * @return void |
||
318 | */ |
||
319 | public function setApiList(array $apiList) : void |
||
323 | |||
324 | /** |
||
325 | * Appends a query string to a URL. |
||
326 | * |
||
327 | * @param string $url |
||
328 | * @param string $query |
||
329 | * @return string |
||
330 | */ |
||
331 | protected function appendQuery(string $url, string $query) : string |
||
341 | |||
342 | /** |
||
343 | * Build a query string from an array. |
||
344 | * |
||
345 | * @param array $params |
||
346 | * @return string |
||
347 | */ |
||
348 | protected function buildQueryString(array $params) : string |
||
377 | |||
378 | /** |
||
379 | * Flatten query params. |
||
380 | * |
||
381 | * @param array $params |
||
382 | * @return array |
||
383 | */ |
||
384 | protected static function flattenParams(array $params) : array |
||
398 | |||
399 | /** |
||
400 | * Checks a provider response for errors. |
||
401 | * |
||
402 | * @param ResponseInterface $response |
||
403 | * @param array|string $data |
||
404 | * @return void |
||
405 | * @throws ClientException |
||
406 | */ |
||
407 | protected function checkResponse(ResponseInterface $response, $data) : void |
||
422 | |||
423 | /** |
||
424 | * Enable SSO key signing for the next request. |
||
425 | * |
||
426 | * @param boolean $enable |
||
427 | * @return self |
||
428 | */ |
||
429 | public function enableSso(bool $enable = true) : self |
||
435 | /** |
||
436 | * Determine if SSO signing is enabled. |
||
437 | * |
||
438 | * @return boolean |
||
439 | */ |
||
440 | public function isSsoEnabled() : bool |
||
444 | |||
445 | /** |
||
446 | * Get cache driver instance |
||
447 | * @return FilesystemCache |
||
448 | */ |
||
449 | View Code Duplication | private function cache() : FilesystemCache |
|
457 | |||
458 | /** |
||
459 | * Handle dynamic method calls into the method. |
||
460 | * |
||
461 | * @param string $method |
||
462 | * @param array $parameters |
||
463 | * @return mixed |
||
464 | */ |
||
465 | public function __call(string $method, array $parameters) |
||
471 | } |
||
472 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..