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 |
||
199 | |||
200 | /** |
||
201 | * Check if command is supported |
||
202 | * @param string $command |
||
203 | * @return boolean |
||
204 | */ |
||
205 | protected function isCommandValid(string $command) |
||
209 | |||
210 | /** |
||
211 | * Get required parameter names |
||
212 | * @param string $command |
||
213 | * @return array |
||
214 | */ |
||
215 | protected function getRequiredCommandParameters(string $command) |
||
226 | |||
227 | /** |
||
228 | * Returns command method based on the command. |
||
229 | * |
||
230 | * @param string $command |
||
231 | * @return string |
||
232 | */ |
||
233 | public function getCommandMethod(string $command) : string |
||
241 | |||
242 | /** |
||
243 | * Builds the command URL's query string. |
||
244 | * |
||
245 | * @param array $params |
||
246 | * @return string |
||
247 | */ |
||
248 | public function getCommandQuery(array $params) : string |
||
252 | |||
253 | /** |
||
254 | * Builds the authorization URL. |
||
255 | * |
||
256 | * @param string $command |
||
257 | * @param array $options |
||
258 | * @return string |
||
259 | */ |
||
260 | public function getCommandUrl(string $command, array $options = []) : string |
||
268 | |||
269 | /** |
||
270 | * Returns command parameters based on provided options. |
||
271 | * |
||
272 | * @param string $command |
||
273 | * @param array $options |
||
274 | * @return array |
||
275 | */ |
||
276 | protected function getCommandParameters(string $command, array $options) : array |
||
284 | |||
285 | /** |
||
286 | * Signs the command parameters. |
||
287 | * |
||
288 | * @param array $params |
||
289 | * @return string |
||
290 | */ |
||
291 | protected function signCommandParameters(array $params = []) : string |
||
318 | |||
319 | /** |
||
320 | * Get Cloudstack Client API list. |
||
321 | * |
||
322 | * Tries to load the API list from the cache directory when |
||
323 | * the 'apiList' on the class is empty. |
||
324 | * |
||
325 | * @return array |
||
326 | * @throws RuntimeException |
||
327 | */ |
||
328 | public function getApiList() : array |
||
342 | |||
343 | /** |
||
344 | * Set Cloudstack Client API list. |
||
345 | * |
||
346 | * @param array $apiList |
||
347 | * @return void |
||
348 | */ |
||
349 | public function setApiList(array $apiList) : void |
||
353 | |||
354 | /** |
||
355 | * Appends a query string to a URL. |
||
356 | * |
||
357 | * @param string $url |
||
358 | * @param string $query |
||
359 | * @return string |
||
360 | */ |
||
361 | protected function appendQuery(string $url, string $query) : string |
||
371 | |||
372 | /** |
||
373 | * Build a query string from an array. |
||
374 | * |
||
375 | * @param array $params |
||
376 | * @return string |
||
377 | */ |
||
378 | protected function buildQueryString(array $params) : string |
||
407 | |||
408 | /** |
||
409 | * Flatten query params. |
||
410 | * |
||
411 | * @param array $params |
||
412 | * @return array |
||
413 | */ |
||
414 | protected static function flattenParams(array $params) : array |
||
428 | |||
429 | /** |
||
430 | * Checks a provider response for errors. |
||
431 | * |
||
432 | * @param ResponseInterface $response |
||
433 | * @param array|string $data |
||
434 | * @return void |
||
435 | * @throws ClientException |
||
436 | */ |
||
437 | protected function checkResponse(ResponseInterface $response, $data) : void |
||
452 | |||
453 | /** |
||
454 | * Enable SSO key signing for the next request. |
||
455 | * |
||
456 | * @param boolean $enable |
||
457 | * @return self |
||
458 | */ |
||
459 | public function enableSso(bool $enable = true) : self |
||
465 | /** |
||
466 | * Determine if SSO signing is enabled. |
||
467 | * |
||
468 | * @return boolean |
||
469 | */ |
||
470 | public function isSsoEnabled() : bool |
||
474 | |||
475 | /** |
||
476 | * Get cache driver instance |
||
477 | * @return FilesystemCache |
||
478 | */ |
||
479 | View Code Duplication | private function cache() : FilesystemCache |
|
487 | |||
488 | /** |
||
489 | * Handle dynamic method calls into the method. |
||
490 | * |
||
491 | * @param mixed $method |
||
492 | * @param array $parameters |
||
493 | * @return mixed |
||
494 | */ |
||
495 | public function __call($method, array $parameters) |
||
501 | } |
||
502 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.