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 |
||
11 | class Client |
||
12 | { |
||
13 | const RESPONSE_SHORT = 1; |
||
14 | const RESPONSE_FULL = 2; |
||
15 | |||
16 | protected $_host; |
||
17 | protected $_port; |
||
18 | protected $_protocol; |
||
19 | protected $_login; |
||
20 | protected $_password; |
||
21 | protected $_proxy = ''; |
||
22 | protected $_secretKey; |
||
23 | protected $_version = ''; |
||
24 | |||
25 | protected $_operatorsCache = []; |
||
26 | |||
27 | /** |
||
28 | * @var callable |
||
29 | */ |
||
30 | protected $_verifyResponseCallback; |
||
31 | |||
32 | /** |
||
33 | * Create client. |
||
34 | * |
||
35 | * @param string $host |
||
36 | * @param int $port |
||
37 | * @param string $protocol |
||
38 | */ |
||
39 | public function __construct($host, $port = 8443, $protocol = 'https') |
||
45 | |||
46 | /** |
||
47 | * Setup credentials for authentication. |
||
48 | * |
||
49 | * @param string $login |
||
50 | * @param string $password |
||
51 | */ |
||
52 | public function setCredentials($login, $password) |
||
57 | |||
58 | /** |
||
59 | * Define secret key for alternative authentication. |
||
60 | * |
||
61 | * @param string $secretKey |
||
62 | */ |
||
63 | public function setSecretKey($secretKey) |
||
67 | |||
68 | /** |
||
69 | * Set proxy server for requests. |
||
70 | * |
||
71 | * @param string $proxy |
||
72 | */ |
||
73 | public function setProxy($proxy) |
||
77 | |||
78 | /** |
||
79 | * Set default version for requests. |
||
80 | * |
||
81 | * @param string $version |
||
82 | */ |
||
83 | public function setVersion($version) |
||
87 | |||
88 | /** |
||
89 | * Set custom function to verify response of API call according your own needs. Default verifying will be used if it is not specified. |
||
90 | * |
||
91 | * @param callable|null $function |
||
92 | */ |
||
93 | public function setVerifyResponse(callable $function = null) |
||
97 | |||
98 | /** |
||
99 | * Retrieve host used for communication. |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | public function getHost() |
||
107 | |||
108 | /** |
||
109 | * Retrieve port used for communication. |
||
110 | * |
||
111 | * @return int |
||
112 | */ |
||
113 | public function getPort() |
||
117 | |||
118 | /** |
||
119 | * Retrieve name of the protocol (http or https) used for communication. |
||
120 | * |
||
121 | * @return string |
||
122 | */ |
||
123 | public function getProtocol() |
||
127 | |||
128 | /** |
||
129 | * Retrieve XML template for packet. |
||
130 | * |
||
131 | * @param string|null $version |
||
132 | * |
||
133 | * @return SimpleXMLElement |
||
134 | */ |
||
135 | public function getPacket($version = null) |
||
143 | |||
144 | /** |
||
145 | * Perform API request. |
||
146 | * |
||
147 | * @param string|array|SimpleXMLElement $request |
||
148 | * @param int $mode |
||
149 | * |
||
150 | * @return XmlResponse |
||
151 | */ |
||
152 | public function request($request, $mode = self::RESPONSE_SHORT) |
||
180 | |||
181 | /** |
||
182 | * Perform HTTP request to end-point. |
||
183 | * |
||
184 | * @param string $request |
||
185 | * |
||
186 | * @throws Client\Exception |
||
187 | * |
||
188 | * @return XmlResponse |
||
189 | */ |
||
190 | private function _performHttpRequest($request) |
||
218 | |||
219 | /** |
||
220 | * Perform multiple API requests using single HTTP request. |
||
221 | * |
||
222 | * @param $requests |
||
223 | * @param int $mode |
||
224 | * |
||
225 | * @throws Client\Exception |
||
226 | * |
||
227 | * @return array |
||
228 | */ |
||
229 | public function multiRequest($requests, $mode = self::RESPONSE_SHORT) |
||
267 | |||
268 | /** |
||
269 | * Retrieve list of headers needed for request. |
||
270 | * |
||
271 | * @return array |
||
272 | */ |
||
273 | protected function _getHeaders() |
||
289 | |||
290 | /** |
||
291 | * Verify that response does not contain errors. |
||
292 | * |
||
293 | * @param XmlResponse $xml |
||
294 | * |
||
295 | * @throws Exception |
||
296 | */ |
||
297 | protected function _verifyResponse($xml) |
||
310 | |||
311 | /** |
||
312 | * Expand short syntax (some.method.call) into full XML representation. |
||
313 | * |
||
314 | * @param string $request |
||
315 | * @param SimpleXMLElement $xml |
||
316 | * |
||
317 | * @return string |
||
318 | */ |
||
319 | protected function _expandRequestShortSyntax($request, SimpleXMLElement $xml) |
||
331 | |||
332 | /** |
||
333 | * Convert array to XML representation. |
||
334 | * |
||
335 | * @param array $array |
||
336 | * @param SimpleXMLElement $xml |
||
337 | * @param string $parentEl |
||
338 | * |
||
339 | * @return SimpleXMLElement |
||
340 | */ |
||
341 | protected function _arrayToXml(array $array, SimpleXMLElement $xml, $parentEl = null) |
||
354 | |||
355 | /** |
||
356 | * @param array $array |
||
357 | * |
||
358 | * @return bool |
||
359 | */ |
||
360 | protected function _isAssocArray(array $array) |
||
364 | |||
365 | /** |
||
366 | * @param string $name |
||
367 | * |
||
368 | * @return \PleskX\Api\Operator |
||
369 | */ |
||
370 | protected function _getOperator($name) |
||
379 | |||
380 | /** |
||
381 | * @return Operator\Server |
||
382 | */ |
||
383 | public function server() |
||
387 | |||
388 | /** |
||
389 | * @return Operator\Customer |
||
390 | */ |
||
391 | public function customer() |
||
395 | |||
396 | /** |
||
397 | * @return Operator\Webspace |
||
398 | */ |
||
399 | public function webspace() |
||
403 | |||
404 | /** |
||
405 | * @return Operator\Subdomain |
||
406 | */ |
||
407 | public function subdomain() |
||
411 | |||
412 | /** |
||
413 | * @return Operator\Dns |
||
414 | */ |
||
415 | public function dns() |
||
419 | |||
420 | /** |
||
421 | * @return Operator\DnsTemplate |
||
422 | */ |
||
423 | public function dnsTemplate() |
||
427 | |||
428 | /** |
||
429 | * @return Operator\DatabaseServer |
||
430 | */ |
||
431 | public function databaseServer() |
||
435 | |||
436 | /** |
||
437 | * @return Operator\Mail |
||
438 | */ |
||
439 | public function mail() |
||
443 | |||
444 | /** |
||
445 | * @return Operator\Certificate |
||
446 | */ |
||
447 | public function certificate() |
||
451 | |||
452 | /** |
||
453 | * @return Operator\SiteAlias |
||
454 | */ |
||
455 | public function siteAlias() |
||
459 | |||
460 | /** |
||
461 | * @return Operator\Ip |
||
462 | */ |
||
463 | public function ip() |
||
467 | |||
468 | /** |
||
469 | * @return Operator\EventLog |
||
470 | */ |
||
471 | public function eventLog() |
||
475 | |||
476 | /** |
||
477 | * @return Operator\SecretKey |
||
478 | */ |
||
479 | public function secretKey() |
||
483 | |||
484 | /** |
||
485 | * @return Operator\Ui |
||
486 | */ |
||
487 | public function ui() |
||
491 | |||
492 | /** |
||
493 | * @return Operator\ServicePlan |
||
494 | */ |
||
495 | public function servicePlan() |
||
499 | |||
500 | /** |
||
501 | * @return Operator\VirtualDirectory |
||
502 | */ |
||
503 | public function virtualDirectory() |
||
507 | |||
508 | /** |
||
509 | * @return Operator\Database |
||
510 | */ |
||
511 | public function database() |
||
515 | |||
516 | /** |
||
517 | * @return Operator\Session |
||
518 | */ |
||
519 | public function session() |
||
523 | |||
524 | /** |
||
525 | * @return Operator\Locale |
||
526 | */ |
||
527 | public function locale() |
||
531 | |||
532 | /** |
||
533 | * @return Operator\LogRotation |
||
534 | */ |
||
535 | public function logRotation() |
||
539 | |||
540 | /** |
||
541 | * @return Operator\ProtectedDirectory |
||
542 | */ |
||
543 | public function protectedDirectory() |
||
547 | |||
548 | /** |
||
549 | * @return Operator\Reseller |
||
550 | */ |
||
551 | public function reseller() |
||
555 | |||
556 | /** |
||
557 | * @return Operator\ResellerPlan |
||
558 | */ |
||
559 | public function resellerPlan() |
||
563 | |||
564 | /** |
||
565 | * @return Operator\Aps |
||
566 | */ |
||
567 | public function aps() |
||
571 | |||
572 | /** |
||
573 | * @return Operator\ServicePlanAddon |
||
574 | */ |
||
575 | public function servicePlanAddon() |
||
579 | |||
580 | /** |
||
581 | * @return Operator\Site |
||
582 | */ |
||
583 | public function site() |
||
587 | |||
588 | /** |
||
589 | * @return Operator\PhpHandler |
||
590 | */ |
||
591 | public function phpHandler() |
||
595 | } |
||
596 |
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.