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 |
||
24 | class Util |
||
25 | { |
||
26 | const AUTO_REFUND_REASON_VALIDATION = 1; |
||
27 | const AUTO_REFUND_REASON_INACTIVE = 2; |
||
28 | const AUTO_REFUND_REASON_TYPE = 3; |
||
29 | const AUTO_REFUND_REASON_BRAND = 4; |
||
30 | const AUTO_REFUND_REASON_REJECTED = 5; |
||
31 | const AUTO_REFUND_REASON_MALWARE = 6; |
||
32 | const AUTO_REFUND_REASON_PHISHING = 7; |
||
33 | const AUTO_REFUND_REASON_SAFE_BROWSING = 8; |
||
34 | const AUTO_REFUND_REASON_AUTHORITY = 9; |
||
35 | const AUTO_REFUND_REASON_PRICE = 10; |
||
36 | const AUTO_REFUND_REASON_OTHER = 11; |
||
37 | |||
38 | const COMODO_AUTO_REFUND_URL = 'https://secure.comodo.net/products/!AutoRefund'; |
||
39 | const COMODO_AUTO_APPLY_URL = 'https://secure.comodo.net/products/!AutoApplySSL'; |
||
40 | const COMODO_AUTO_REVOKE_URL = 'https://secure.comodo.net/products/!AutoRevokeSSL'; |
||
41 | const COMODO_DCV_MAIL_URL = 'https://secure.comodo.net/products/!GetDCVEmailAddressList'; |
||
42 | const COMODO_DCV_RESEND_URL = 'https://secure.comodo.net/products/!ResendDCVEmail'; |
||
43 | const COMODO_AUTO_UPDATE_DCV_URL = 'https://secure.comodo.net/products/!AutoUpdateDCV'; |
||
44 | const COMODO_PROVIDE_EV_DETAILS_URL = 'https://secure.comodo.net/products/!ProvideEVDetails'; |
||
45 | const COMODO_MDC_DOMAIN_DETAILS_URL = 'https://secure.comodo.net/products/!GetMDCDomainDetails'; |
||
46 | const COMODO_AUTO_REPLACE_URL = 'https://secure.comodo.net/products/!AutoReplaceSSL'; |
||
47 | const COMODO_COLLECT_SSL_URL = 'https://secure.comodo.net/products/download/CollectSSL'; |
||
48 | const COMODO_UPDATE_USER_EV_CLICK_THROUGH = 'https://secure.comodo.net/products/!UpdateUserEvClickThrough'; |
||
49 | const COMODO_WEB_HOST_REPORT = 'https://secure.comodo.net/products/!WebHostReport'; |
||
50 | const COMODO_SSLCHECKER = 'https://secure.comodo.com/sslchecker'; |
||
51 | |||
52 | const COMODO_DCV_CODE_URL = 'https://secure.comodo.net/products/EnterDCVCode2'; |
||
53 | |||
54 | /** |
||
55 | * @var CommunicationAdapter |
||
56 | */ |
||
57 | protected $communicationAdapter; |
||
58 | |||
59 | /** |
||
60 | * @var ImapAdapter |
||
61 | */ |
||
62 | protected $imapAdapter; |
||
63 | |||
64 | /** |
||
65 | * @var ImapHelper |
||
66 | */ |
||
67 | protected $imapHelper; |
||
68 | |||
69 | /** |
||
70 | * Constructs the Util with a communicationAdapter |
||
71 | * |
||
72 | * @param CommunicationAdapter|null $communicationAdapter |
||
73 | * @param ImapAdapter|null $imapAdapter |
||
74 | * @param ImapHelper|null $imapHelper |
||
75 | */ |
||
76 | public function __construct(CommunicationAdapter $communicationAdapter, ImapAdapter $imapAdapter, ImapHelper $imapHelper) |
||
82 | |||
83 | |||
84 | /** |
||
85 | * @return CommunicationAdapter |
||
86 | */ |
||
87 | public function getCommunicationAdapter() |
||
91 | |||
92 | /** |
||
93 | * @param CommunicationAdapter $communicationAdapter |
||
94 | * |
||
95 | * @return Util |
||
96 | */ |
||
97 | public function setCommunicationAdapter(CommunicationAdapter $communicationAdapter) |
||
103 | |||
104 | /** |
||
105 | * @return ImapHelper |
||
106 | */ |
||
107 | public function getImapHelper() |
||
111 | |||
112 | /** |
||
113 | * @param ImapHelper $imapHelper |
||
114 | * |
||
115 | * @return Util |
||
116 | */ |
||
117 | public function setImapHelper(ImapHelper $imapHelper) |
||
123 | |||
124 | /** |
||
125 | * @return ImapAdapter |
||
126 | */ |
||
127 | public function getImapAdapter() |
||
131 | |||
132 | /** |
||
133 | * @param ImapAdapter $imapAdapter |
||
134 | * |
||
135 | * @return Util |
||
136 | */ |
||
137 | public function setImapAdapter(ImapAdapter $imapAdapter) |
||
143 | |||
144 | /** |
||
145 | * Refunds a ordered certificate |
||
146 | * |
||
147 | * @param array $params |
||
148 | * |
||
149 | * @return bool |
||
150 | * |
||
151 | * @throws AccountException |
||
152 | * @throws ArgumentException |
||
153 | * @throws CSRException |
||
154 | * @throws RequestException |
||
155 | * @throws UnknownApiException |
||
156 | * @throws UnknownException |
||
157 | */ |
||
158 | public function autoRefund(array $params) |
||
178 | |||
179 | /** |
||
180 | * Function apply for a certificate |
||
181 | * |
||
182 | * See documentation of params at https://secure.comodo.net/api/pdf/webhostreseller/sslcertificates/ |
||
183 | * |
||
184 | * @param array $params |
||
185 | * |
||
186 | * @return AutoApplyResult |
||
187 | * @throws Model\Exception\AccountException |
||
188 | * @throws Model\Exception\ArgumentException |
||
189 | * @throws Model\Exception\CSRException |
||
190 | * @throws Model\Exception\RequestException |
||
191 | * @throws Model\Exception\UnknownApiException |
||
192 | * @throws Model\Exception\UnknownException |
||
193 | */ |
||
194 | public function autoApplySSL(array $params) |
||
232 | |||
233 | /** |
||
234 | * @param array $params |
||
235 | * |
||
236 | * @return UpdateUserEvClickThroughResult |
||
237 | * @throws AccountException |
||
238 | * @throws ArgumentException |
||
239 | * @throws CSRException |
||
240 | * @throws RequestException |
||
241 | * @throws UnknownApiException |
||
242 | * @throws UnknownException |
||
243 | */ |
||
244 | public function updateUserEvClickThrough(array $params) |
||
267 | |||
268 | /** |
||
269 | * Function update for a certificate |
||
270 | * |
||
271 | * See documentation of params at https://secure.comodo.net/api/pdf/webhostreseller/sslcertificates/ |
||
272 | * |
||
273 | * @param array $params |
||
274 | * |
||
275 | * @return AutoReplaceResult |
||
276 | * @throws Model\Exception\AccountException |
||
277 | * @throws Model\Exception\ArgumentException |
||
278 | * @throws Model\Exception\CSRException |
||
279 | * @throws Model\Exception\RequestException |
||
280 | * @throws Model\Exception\UnknownApiException |
||
281 | * @throws Model\Exception\UnknownException |
||
282 | */ |
||
283 | public function autoReplaceSSL(array $params) |
||
314 | |||
315 | /** |
||
316 | * Function to revoke order |
||
317 | * |
||
318 | * See documentation of params at https://secure.comodo.net/api/pdf/webhostreseller/sslcertificates/ |
||
319 | * |
||
320 | * @param array $params |
||
321 | * |
||
322 | * @return bool |
||
323 | * @throws Model\Exception\AccountException |
||
324 | * @throws Model\Exception\ArgumentException |
||
325 | * @throws Model\Exception\RequestException |
||
326 | * @throws Model\Exception\UnknownApiException |
||
327 | * @throws Model\Exception\UnknownException |
||
328 | */ |
||
329 | public function autoRevokeSSL(array $params) |
||
340 | |||
341 | /** |
||
342 | * Function to auto update dcv |
||
343 | * |
||
344 | * See documentation of params at https://secure.comodo.net/api/pdf/webhostreseller/sslcertificates/ |
||
345 | * |
||
346 | * @param array $params |
||
347 | * |
||
348 | * @return bool |
||
349 | * @throws Model\Exception\AccountException |
||
350 | * @throws Model\Exception\ArgumentException |
||
351 | * @throws Model\Exception\RequestException |
||
352 | * @throws Model\Exception\UnknownApiException |
||
353 | * @throws Model\Exception\UnknownException |
||
354 | */ |
||
355 | public function autoUpdateDCV(array $params) |
||
363 | |||
364 | /** |
||
365 | * Function to get details of a certificate |
||
366 | * |
||
367 | * See documentation of params at https://secure.comodo.net/api/pdf/webhostreseller/sslcertificates/ |
||
368 | * |
||
369 | * @param array $params |
||
370 | * |
||
371 | * @return CollectSslResult |
||
372 | * @throws Model\Exception\AccountException |
||
373 | * @throws Model\Exception\ArgumentException |
||
374 | * @throws Model\Exception\CSRException |
||
375 | * @throws Model\Exception\RequestException |
||
376 | * @throws Model\Exception\UnknownApiException |
||
377 | * @throws Model\Exception\UnknownException |
||
378 | */ |
||
379 | public function collectSsl(array $params) |
||
425 | |||
426 | /** |
||
427 | * Function to resend the DCV Email |
||
428 | * |
||
429 | * See documentation of params at https://secure.comodo.net/api/pdf/webhostreseller/sslcertificates/ |
||
430 | * |
||
431 | * @param array $params |
||
432 | * |
||
433 | * @return bool |
||
434 | * @throws Model\Exception\AccountException |
||
435 | * @throws Model\Exception\ArgumentException |
||
436 | * @throws Model\Exception\RequestException |
||
437 | * @throws Model\Exception\UnknownApiException |
||
438 | * @throws Model\Exception\UnknownException |
||
439 | */ |
||
440 | public function resendDCVEMail(array $params) |
||
449 | |||
450 | /** |
||
451 | * @param array $params |
||
452 | * |
||
453 | * @deprecated Comodo support told this function doesn't have any effect anymore |
||
454 | * |
||
455 | * @return bool |
||
456 | * @throws Model\Exception\AccountException |
||
457 | * @throws Model\Exception\ArgumentException |
||
458 | * @throws Model\Exception\CSRException |
||
459 | * @throws Model\Exception\RequestException |
||
460 | * @throws Model\Exception\UnknownApiException |
||
461 | * @throws Model\Exception\UnknownException |
||
462 | */ |
||
463 | public function provideEVDetails(array $params) |
||
471 | |||
472 | /** |
||
473 | * Function to get the DCV e-mail address-list |
||
474 | * |
||
475 | * See documentation of params at https://secure.comodo.net/api/pdf/webhostreseller/sslcertificates/ |
||
476 | * |
||
477 | * @param array $params |
||
478 | * |
||
479 | * @return GetDCVEMailAddressListResult |
||
480 | * @throws AccountException |
||
481 | * @throws ArgumentException |
||
482 | * @throws CSRException |
||
483 | * @throws RequestException |
||
484 | * @throws UnknownApiException |
||
485 | * @throws UnknownException |
||
486 | */ |
||
487 | public function getDCVEMailAddressList(array $params) |
||
525 | |||
526 | /** |
||
527 | * Function to get details of a order-number (this API support just one domain) |
||
528 | * |
||
529 | * https://secure.comodo.net/api/pdf/webhostreseller/sslcertificates/GetMDCDomainDetails%20v1.00.pdf |
||
530 | * |
||
531 | * @param array $params |
||
532 | * |
||
533 | * @return GetMDCDomainDetailsResult |
||
534 | * |
||
535 | * @throws Model\Exception\AccountException |
||
536 | * @throws Model\Exception\ArgumentException |
||
537 | * @throws Model\Exception\CSRException |
||
538 | * @throws Model\Exception\RequestException |
||
539 | * @throws Model\Exception\UnknownApiException |
||
540 | * @throws Model\Exception\UnknownException |
||
541 | */ |
||
542 | View Code Duplication | public function getMDCDomainDetails(array $params) |
|
566 | |||
567 | /** |
||
568 | * Function to do a sslcheck |
||
569 | * |
||
570 | * https://secure.comodo.net/api/pdf/latest/SSLChecker.pdf |
||
571 | * |
||
572 | * @param array $params |
||
573 | * |
||
574 | * @return GetMDCDomainDetailsResult |
||
575 | * |
||
576 | * @throws Model\Exception\AccountException |
||
577 | * @throws Model\Exception\ArgumentException |
||
578 | * @throws Model\Exception\CSRException |
||
579 | * @throws Model\Exception\RequestException |
||
580 | * @throws Model\Exception\UnknownApiException |
||
581 | * @throws Model\Exception\UnknownException |
||
582 | */ |
||
583 | public function sslChecker(array $params) |
||
637 | |||
638 | /** |
||
639 | * Function to call WebHostReport api |
||
640 | * |
||
641 | * https://secure.comodo.net/products/!WebHostReport |
||
642 | * |
||
643 | * Details for params usage: |
||
644 | * https://secure.comodo.net/api/pdf/latest/WebHostReport.pdf |
||
645 | * |
||
646 | * @param array $params |
||
647 | * |
||
648 | * @return WebHostReportResult |
||
649 | * |
||
650 | * @throws Model\Exception\AccountException |
||
651 | * @throws Model\Exception\ArgumentException |
||
652 | * @throws Model\Exception\CSRException |
||
653 | * @throws Model\Exception\RequestException |
||
654 | * @throws Model\Exception\UnknownApiException |
||
655 | * @throws Model\Exception\UnknownException |
||
656 | */ |
||
657 | View Code Duplication | public function webHostReport(array $params) { |
|
679 | |||
680 | /** |
||
681 | * Function to enter the DCV code, coming from DCV E-Mail |
||
682 | * |
||
683 | * @param array $params |
||
684 | * |
||
685 | * @return bool |
||
686 | * @throws Model\Exception\UnknownException |
||
687 | * @throws Model\Exception\ArgumentException |
||
688 | */ |
||
689 | public function enterDCVCode(array $params) |
||
716 | |||
717 | /** |
||
718 | * @param string $domainName |
||
719 | * @param null $orderNumbers |
||
720 | * @param \Closure $callbackFunction |
||
721 | * |
||
722 | * @return array |
||
723 | */ |
||
724 | public function getMails($domainName, $orderNumbers = null, \Closure $callbackFunction = null) |
||
744 | |||
745 | /** |
||
746 | * @param bool $markProcessed |
||
747 | * @param \Closure $callbackFunction |
||
748 | * |
||
749 | * @return array |
||
750 | */ |
||
751 | public function getUnprocessedMails($markProcessed = true, \Closure $callbackFunction = null) |
||
765 | |||
766 | /** |
||
767 | * Function to create an exception for API errorcodes |
||
768 | * |
||
769 | * @param array|mixed $responseArray |
||
770 | * |
||
771 | * @return AccountException|ArgumentException|CSRException|RequestException|UnknownApiException|UnknownException |
||
772 | * |
||
773 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
774 | */ |
||
775 | protected function createException($responseArray) |
||
849 | |||
850 | /** |
||
851 | * @param CollectSslResult $object |
||
852 | * @param array $arr |
||
853 | * @param array $timestampFields |
||
854 | * |
||
855 | * @return $this |
||
856 | */ |
||
857 | protected function fill(CollectSslResult $object, array $arr, array $timestampFields = array()) |
||
874 | |||
875 | /** |
||
876 | * @param string $url |
||
877 | * @param array $params |
||
878 | * @param int $type |
||
879 | * |
||
880 | * @return bool |
||
881 | * @throws AccountException |
||
882 | * @throws ArgumentException |
||
883 | * @throws CSRException |
||
884 | * @throws RequestException |
||
885 | * @throws UnknownApiException |
||
886 | * @throws UnknownException |
||
887 | */ |
||
888 | protected function sendBooleanRequest($url, array $params, $type) |
||
905 | } |
||
906 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.