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 Util 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 Util, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Util |
||
23 | { |
||
24 | const COMODO_AUTO_APPLY_URL = "https://secure.comodo.net/products/!AutoApplySSL"; |
||
25 | const COMODO_AUTO_REVOKE_URL = "https://secure.comodo.net/products/!AutoRevokeSSL"; |
||
26 | const COMODO_DCV_MAIL_URL = "https://secure.comodo.net/products/!GetDCVEmailAddressList"; |
||
27 | const COMODO_DCV_RESEND_URL = "https://secure.comodo.net/products/!ResendDCVEmail"; |
||
28 | const COMODO_AUTO_UPDATE_DCV_URL = "https://secure.comodo.net/products/!AutoUpdateDCV"; |
||
29 | const COMODO_PROVIDE_EV_DETAILS_URL = "https://secure.comodo.net/products/!ProvideEVDetails"; |
||
30 | const COMODO_MDC_DOMAIN_DETAILS_URL = "https://secure.comodo.net/products/!GetMDCDomainDetails"; |
||
31 | const COMODO_AUTO_REPLACE_URL = "https://secure.comodo.net/products/!AutoReplaceSSL"; |
||
32 | const COMODO_COLLECT_SSL_URL = "https://secure.comodo.net/products/download/CollectSSL"; |
||
33 | const COMODO_UPDATE_USER_EV_CLICK_THROUGH = "https://secure.comodo.net/products/!UpdateUserEvClickThrough"; |
||
34 | |||
35 | const COMODO_DCV_CODE_URL = "https://secure.comodo.net/products/EnterDCVCode2"; |
||
36 | |||
37 | /** |
||
38 | * @var CommunicationAdapter |
||
39 | */ |
||
40 | protected $communicationAdapter; |
||
41 | |||
42 | /** |
||
43 | * @var ImapAdapter |
||
44 | */ |
||
45 | protected $imapAdapter; |
||
46 | |||
47 | /** |
||
48 | * @var ImapHelper |
||
49 | */ |
||
50 | protected $imapHelper; |
||
51 | |||
52 | /** |
||
53 | * Constructs the Util with a communicationAdapter |
||
54 | * |
||
55 | * @param CommunicationAdapter|null $communicationAdapter |
||
56 | * @param ImapAdapter|null $imapAdapter |
||
57 | * @param ImapHelper|null $imapHelper |
||
58 | */ |
||
59 | public function __construct(CommunicationAdapter $communicationAdapter, ImapAdapter $imapAdapter, ImapHelper $imapHelper) |
||
60 | { |
||
61 | $this->communicationAdapter = $communicationAdapter; |
||
62 | $this->imapAdapter = $imapAdapter; |
||
63 | $this->imapHelper = $imapHelper; |
||
64 | } |
||
65 | |||
66 | |||
67 | /** |
||
68 | * @return CommunicationAdapter |
||
69 | */ |
||
70 | public function getCommunicationAdapter() |
||
74 | |||
75 | /** |
||
76 | * @param CommunicationAdapter $communicationAdapter |
||
77 | * |
||
78 | * @return Util |
||
79 | */ |
||
80 | public function setCommunicationAdapter(CommunicationAdapter $communicationAdapter) |
||
86 | |||
87 | /** |
||
88 | * @return ImapHelper |
||
89 | */ |
||
90 | public function getImapHelper() |
||
94 | |||
95 | /** |
||
96 | * @param ImapHelper $imapHelper |
||
97 | * |
||
98 | * @return Util |
||
99 | */ |
||
100 | public function setImapHelper(ImapHelper $imapHelper) |
||
106 | |||
107 | /** |
||
108 | * @return ImapAdapter |
||
109 | */ |
||
110 | public function getImapAdapter() |
||
111 | { |
||
112 | return $this->imapAdapter; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @param ImapAdapter $imapAdapter |
||
117 | * |
||
118 | * @return Util |
||
119 | */ |
||
120 | public function setImapAdapter(ImapAdapter $imapAdapter) |
||
121 | { |
||
122 | $this->imapAdapter = $imapAdapter; |
||
123 | |||
124 | return $this; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Function apply for a certificate |
||
129 | * |
||
130 | * See documentation of params at https://secure.comodo.net/api/pdf/webhostreseller/sslcertificates/ |
||
131 | * |
||
132 | * @param array $params |
||
133 | * |
||
134 | * @return AutoApplyResult |
||
135 | * @throws Model\Exception\AccountException |
||
136 | * @throws Model\Exception\ArgumentException |
||
137 | * @throws Model\Exception\CSRException |
||
138 | * @throws Model\Exception\RequestException |
||
139 | * @throws Model\Exception\UnknownApiException |
||
140 | * @throws Model\Exception\UnknownException |
||
141 | */ |
||
142 | public function autoApplySSL(array $params) |
||
175 | |||
176 | /** |
||
177 | * @param array $params |
||
178 | * |
||
179 | * @return UpdateUserEvClickThroughResult |
||
180 | * @throws AccountException |
||
181 | * @throws ArgumentException |
||
182 | * @throws CSRException |
||
183 | * @throws RequestException |
||
184 | * @throws UnknownApiException |
||
185 | * @throws UnknownException |
||
186 | */ |
||
187 | public function updateUserEvClickThrough(array $params) |
||
210 | |||
211 | /** |
||
212 | * Function update for a certificate |
||
213 | * |
||
214 | * See documentation of params at https://secure.comodo.net/api/pdf/webhostreseller/sslcertificates/ |
||
215 | * |
||
216 | * @param array $params |
||
217 | * |
||
218 | * @return AutoApplyResult |
||
219 | * @throws Model\Exception\AccountException |
||
220 | * @throws Model\Exception\ArgumentException |
||
221 | * @throws Model\Exception\CSRException |
||
222 | * @throws Model\Exception\RequestException |
||
223 | * @throws Model\Exception\UnknownApiException |
||
224 | * @throws Model\Exception\UnknownException |
||
225 | */ |
||
226 | View Code Duplication | public function autoReplaceSSL(array $params) |
|
252 | |||
253 | /** |
||
254 | * Function to revoke order |
||
255 | * |
||
256 | * See documentation of params at https://secure.comodo.net/api/pdf/webhostreseller/sslcertificates/ |
||
257 | * |
||
258 | * @param array $params |
||
259 | * |
||
260 | * @return bool |
||
261 | * @throws Model\Exception\AccountException |
||
262 | * @throws Model\Exception\ArgumentException |
||
263 | * @throws Model\Exception\RequestException |
||
264 | * @throws Model\Exception\UnknownApiException |
||
265 | * @throws Model\Exception\UnknownException |
||
266 | */ |
||
267 | public function autoRevokeSSL(array $params) |
||
278 | |||
279 | /** |
||
280 | * Function to auto update dcv |
||
281 | * |
||
282 | * See documentation of params at https://secure.comodo.net/api/pdf/webhostreseller/sslcertificates/ |
||
283 | * |
||
284 | * @param array $params |
||
285 | * |
||
286 | * @return bool |
||
287 | * @throws Model\Exception\AccountException |
||
288 | * @throws Model\Exception\ArgumentException |
||
289 | * @throws Model\Exception\RequestException |
||
290 | * @throws Model\Exception\UnknownApiException |
||
291 | * @throws Model\Exception\UnknownException |
||
292 | */ |
||
293 | public function autoUpdateDCV(array $params) |
||
301 | |||
302 | /** |
||
303 | * Function to get details of a certificate |
||
304 | * |
||
305 | * See documentation of params at https://secure.comodo.net/api/pdf/webhostreseller/sslcertificates/ |
||
306 | * |
||
307 | * @param array $params |
||
308 | * |
||
309 | * @return CollectSslResult |
||
310 | * @throws Model\Exception\AccountException |
||
311 | * @throws Model\Exception\ArgumentException |
||
312 | * @throws Model\Exception\CSRException |
||
313 | * @throws Model\Exception\RequestException |
||
314 | * @throws Model\Exception\UnknownApiException |
||
315 | * @throws Model\Exception\UnknownException |
||
316 | */ |
||
317 | public function collectSsl(array $params) |
||
350 | |||
351 | /** |
||
352 | * @param CollectSslResult $object |
||
353 | * @param array $arr |
||
354 | * @param array $timestampFields |
||
355 | * |
||
356 | * @return $this |
||
357 | */ |
||
358 | protected function fill(CollectSslResult $object, array $arr, array $timestampFields = array()) |
||
375 | |||
376 | /** |
||
377 | * Function to resend the DCV Email |
||
378 | * |
||
379 | * See documentation of params at https://secure.comodo.net/api/pdf/webhostreseller/sslcertificates/ |
||
380 | * |
||
381 | * @param array $params |
||
382 | * |
||
383 | * @return bool |
||
384 | * @throws Model\Exception\AccountException |
||
385 | * @throws Model\Exception\ArgumentException |
||
386 | * @throws Model\Exception\RequestException |
||
387 | * @throws Model\Exception\UnknownApiException |
||
388 | * @throws Model\Exception\UnknownException |
||
389 | */ |
||
390 | public function resendDCVEMail(array $params) |
||
399 | |||
400 | /** |
||
401 | * @param array $params |
||
402 | * |
||
403 | * @deprecated Comodo support told this function doesn't have any effect anymore |
||
404 | * |
||
405 | * @return bool |
||
406 | * @throws Model\Exception\AccountException |
||
407 | * @throws Model\Exception\ArgumentException |
||
408 | * @throws Model\Exception\CSRException |
||
409 | * @throws Model\Exception\RequestException |
||
410 | * @throws Model\Exception\UnknownApiException |
||
411 | * @throws Model\Exception\UnknownException |
||
412 | */ |
||
413 | public function provideEVDetails(array $params) |
||
421 | |||
422 | /** |
||
423 | * Function to get the DCV e-mail address-list |
||
424 | * |
||
425 | * See documentation of params at https://secure.comodo.net/api/pdf/webhostreseller/sslcertificates/ |
||
426 | * |
||
427 | * @param array $params |
||
428 | * |
||
429 | * @return GetDCVEMailAddressListResult |
||
430 | * |
||
431 | * @throws Model\Exception\AccountException |
||
432 | * @throws Model\Exception\ArgumentException |
||
433 | * @throws Model\Exception\RequestException |
||
434 | * @throws Model\Exception\UnknownApiException |
||
435 | * @throws Model\Exception\UnknownException |
||
436 | */ |
||
437 | public function getDCVEMailAddressList(array $params) |
||
471 | |||
472 | /** |
||
473 | * Function to get details of a order-number (this API support just one domain) |
||
474 | * |
||
475 | * https://secure.comodo.net/api/pdf/webhostreseller/sslcertificates/GetMDCDomainDetails%20v1.00.pdf |
||
476 | * |
||
477 | * @param array $params |
||
478 | * |
||
479 | * @return GetMDCDomainDetailsResult |
||
480 | * |
||
481 | * @throws Model\Exception\AccountException |
||
482 | * @throws Model\Exception\ArgumentException |
||
483 | * @throws Model\Exception\CSRException |
||
484 | * @throws Model\Exception\RequestException |
||
485 | * @throws Model\Exception\UnknownApiException |
||
486 | * @throws Model\Exception\UnknownException |
||
487 | */ |
||
488 | View Code Duplication | public function getMDCDomainDetails(array $params) |
|
512 | |||
513 | /** |
||
514 | * Function to enter the DCV code, coming from DCV E-Mail |
||
515 | * |
||
516 | * @param array $params |
||
517 | * |
||
518 | * @return bool |
||
519 | * @throws Model\Exception\UnknownException |
||
520 | * @throws Model\Exception\ArgumentException |
||
521 | */ |
||
522 | public function enterDCVCode(array $params) |
||
549 | |||
550 | /** |
||
551 | * @param string $url |
||
552 | * @param array $params |
||
553 | * @param int $type |
||
554 | * |
||
555 | * @return bool |
||
556 | * @throws AccountException |
||
557 | * @throws ArgumentException |
||
558 | * @throws CSRException |
||
559 | * @throws RequestException |
||
560 | * @throws UnknownApiException |
||
561 | * @throws UnknownException |
||
562 | */ |
||
563 | protected function sendBooleanRequest($url, array $params, $type) |
||
580 | |||
581 | /** |
||
582 | * @param string $domainName |
||
583 | * @param null $orderNumbers |
||
584 | * @param \Closure $callbackFunction |
||
585 | * |
||
586 | * @return array |
||
587 | */ |
||
588 | public function getMails($domainName, $orderNumbers = null, \Closure $callbackFunction = null) |
||
607 | |||
608 | /** |
||
609 | * @param bool $markProcessed |
||
610 | * @param \Closure $callbackFunction |
||
611 | * |
||
612 | * @return array |
||
613 | */ |
||
614 | public function getUnprocessedMails($markProcessed = true, \Closure $callbackFunction = null) |
||
630 | |||
631 | /** |
||
632 | * Function to create an exception for API errorcodes |
||
633 | * |
||
634 | * @param array $responseArray |
||
635 | * |
||
636 | * @return AccountException|ArgumentException|CSRException|RequestException|UnknownApiException|UnknownException |
||
637 | * |
||
638 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
639 | */ |
||
640 | protected function createException(array $responseArray) |
||
706 | } |
||
707 |
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.