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 LPTracker 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 LPTracker, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class LPTracker extends LPTrackerBase |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * @param $login |
||
25 | * @param $password |
||
26 | * @param string $serviceName |
||
27 | * |
||
28 | * @return AccessToken |
||
29 | * @throws LPTrackerSDKException |
||
30 | */ |
||
31 | public function login($login, $password, $serviceName = '') |
||
54 | |||
55 | |||
56 | /** |
||
57 | * @throws exceptions\LPTrackerResponseException |
||
58 | * @throws exceptions\LPTrackerServerException |
||
59 | */ |
||
60 | public function logout() |
||
64 | |||
65 | |||
66 | /** |
||
67 | * @return Project[] |
||
68 | * @throws exceptions\LPTrackerResponseException |
||
69 | * @throws exceptions\LPTrackerServerException |
||
70 | */ |
||
71 | View Code Duplication | public function getProjectList() |
|
82 | |||
83 | |||
84 | /** |
||
85 | * @param $id |
||
86 | * |
||
87 | * @return Project |
||
88 | * @throws exceptions\LPTrackerResponseException |
||
89 | * @throws exceptions\LPTrackerServerException |
||
90 | */ |
||
91 | View Code Duplication | public function getProject($id) |
|
101 | |||
102 | |||
103 | /** |
||
104 | * @param $project |
||
105 | * |
||
106 | * @return Custom[] |
||
107 | * @throws exceptions\LPTrackerResponseException |
||
108 | * @throws exceptions\LPTrackerServerException |
||
109 | */ |
||
110 | public function getProjectCustoms($project) |
||
129 | |||
130 | |||
131 | /** |
||
132 | * @param $project |
||
133 | * |
||
134 | * @return ContactField[] |
||
135 | * @throws exceptions\LPTrackerResponseException |
||
136 | * @throws exceptions\LPTrackerServerException |
||
137 | */ |
||
138 | public function getProjectFields($project) |
||
157 | |||
158 | /** |
||
159 | * @param $project |
||
160 | * @param $callbackUrl |
||
161 | * |
||
162 | * @throws exceptions\LPTrackerResponseException |
||
163 | * @throws exceptions\LPTrackerServerException |
||
164 | * @author Yuri Nazarenko / rezident <[email protected]> |
||
165 | */ |
||
166 | public function setProjectCallbackUrl($project, $callbackUrl) |
||
177 | |||
178 | |||
179 | /** |
||
180 | * @param $project |
||
181 | * @param array $details |
||
182 | * @param array $contactData |
||
183 | * @param array $fields |
||
184 | * |
||
185 | * @return Contact |
||
186 | * @throws LPTrackerSDKException |
||
187 | */ |
||
188 | public function createContact( |
||
229 | |||
230 | |||
231 | /** |
||
232 | * @param $contact |
||
233 | * |
||
234 | * @return Contact |
||
235 | * @throws LPTrackerSDKException |
||
236 | */ |
||
237 | View Code Duplication | public function getContact($contact) |
|
257 | |||
258 | |||
259 | /** |
||
260 | * @param Contact $contact |
||
261 | * |
||
262 | * @return Contact |
||
263 | * @throws LPTrackerSDKException |
||
264 | */ |
||
265 | public function saveContact(Contact $contact) |
||
293 | |||
294 | |||
295 | /** |
||
296 | * @param $contactId |
||
297 | * @param array $details |
||
298 | * @param array $contactData |
||
299 | * @param array $fields |
||
300 | * |
||
301 | * @return Contact |
||
302 | * @throws LPTrackerSDKException |
||
303 | */ |
||
304 | public function editContact( |
||
334 | |||
335 | |||
336 | /** |
||
337 | * @param $project |
||
338 | * @param array $searchOptions |
||
339 | * |
||
340 | * @return array |
||
341 | * @throws LPTrackerSDKException |
||
342 | */ |
||
343 | public function searchContacts($project, array $searchOptions = []) |
||
376 | |||
377 | |||
378 | /** |
||
379 | * @param $contact |
||
380 | * |
||
381 | * @return array |
||
382 | * @throws LPTrackerSDKException |
||
383 | */ |
||
384 | public function contactLeads($contact) |
||
405 | |||
406 | |||
407 | /** |
||
408 | * @param $contact |
||
409 | * @param $field |
||
410 | * @param $newValue |
||
411 | * |
||
412 | * @return ContactField |
||
413 | * @throws exceptions\LPTrackerResponseException |
||
414 | * @throws exceptions\LPTrackerServerException |
||
415 | */ |
||
416 | public function updateContactField($contact, $field, $newValue) |
||
437 | |||
438 | |||
439 | /** |
||
440 | * @param $project |
||
441 | * @param array $viewData |
||
442 | * |
||
443 | * @return View |
||
444 | * @throws LPTrackerSDKException |
||
445 | */ |
||
446 | public function createView($project, array $viewData = []) |
||
467 | |||
468 | |||
469 | /** |
||
470 | * @param $view |
||
471 | * |
||
472 | * @return View |
||
473 | * @throws LPTrackerSDKException |
||
474 | */ |
||
475 | View Code Duplication | public function getView($view) |
|
495 | |||
496 | |||
497 | /** |
||
498 | * @param View $view |
||
499 | * |
||
500 | * @return View |
||
501 | * @throws LPTrackerSDKException |
||
502 | */ |
||
503 | View Code Duplication | public function saveView(View $view) |
|
521 | |||
522 | |||
523 | /** |
||
524 | * @param $viewId |
||
525 | * @param array $viewData |
||
526 | * |
||
527 | * @return View |
||
528 | * @throws LPTrackerSDKException |
||
529 | */ |
||
530 | public function editView($viewId, array $viewData = []) |
||
539 | |||
540 | |||
541 | /** |
||
542 | * @param $contact |
||
543 | * @param array $leadData |
||
544 | * @param array $options |
||
545 | * |
||
546 | * @return Lead |
||
547 | * @throws LPTrackerSDKException |
||
548 | */ |
||
549 | public function createLead($contact, array $leadData = [], array $options = []) |
||
584 | |||
585 | |||
586 | /** |
||
587 | * @param $lead |
||
588 | * |
||
589 | * @return Lead |
||
590 | * @throws LPTrackerSDKException |
||
591 | */ |
||
592 | View Code Duplication | public function getLead($lead) |
|
612 | |||
613 | |||
614 | /** |
||
615 | * @param Lead $lead |
||
616 | * |
||
617 | * @return Lead |
||
618 | * @throws LPTrackerSDKException |
||
619 | */ |
||
620 | View Code Duplication | public function saveLead(Lead $lead) |
|
639 | |||
640 | |||
641 | /** |
||
642 | * @param $leadId |
||
643 | * @param array $leadData |
||
644 | * |
||
645 | * @return Lead |
||
646 | * @throws LPTrackerSDKException |
||
647 | */ |
||
648 | public function editLead($leadId, array $leadData = []) |
||
657 | |||
658 | |||
659 | /** |
||
660 | * @param $lead |
||
661 | * @param string $category |
||
662 | * @param string $purpose |
||
663 | * @param float $sum |
||
664 | * |
||
665 | * @return Lead |
||
666 | * @throws LPTrackerSDKException |
||
667 | */ |
||
668 | public function addLeadPayment($lead, $category, $purpose, $sum) |
||
706 | |||
707 | |||
708 | /** |
||
709 | * @param $lead |
||
710 | * @param $newFunnelId |
||
711 | * |
||
712 | * @return Lead |
||
713 | * @throws LPTrackerSDKException |
||
714 | */ |
||
715 | View Code Duplication | public function changeLeadFunnel($lead, $newFunnelId) |
|
733 | |||
734 | |||
735 | /** |
||
736 | * @param $lead |
||
737 | * |
||
738 | * @return Comment[] |
||
739 | * @throws exceptions\LPTrackerResponseException |
||
740 | * @throws exceptions\LPTrackerServerException |
||
741 | */ |
||
742 | public function getLeadComments($lead) |
||
761 | |||
762 | |||
763 | /** |
||
764 | * @param $lead |
||
765 | * @param $text |
||
766 | * |
||
767 | * @return Comment |
||
768 | * @throws exceptions\LPTrackerResponseException |
||
769 | * @throws exceptions\LPTrackerServerException |
||
770 | */ |
||
771 | View Code Duplication | public function addCommentToLead($lead, $text) |
|
791 | |||
792 | /** |
||
793 | * @param Lead|int $lead |
||
794 | * @param Custom|int $custom |
||
795 | * @param string $absolutePath |
||
796 | * |
||
797 | * @throws exceptions\LPTrackerResponseException |
||
798 | * @throws exceptions\LPTrackerServerException |
||
799 | * |
||
800 | * @author Yuri Nazarenko / rezident <[email protected]> |
||
801 | */ |
||
802 | public function addFileToLead($lead, $custom, $absolutePath) |
||
822 | |||
823 | /** |
||
824 | * @param Custom $custom |
||
825 | * |
||
826 | * @return Custom |
||
827 | * @throws LPTrackerSDKException |
||
828 | */ |
||
829 | public function saveLeadCustom(Custom $custom) |
||
847 | |||
848 | |||
849 | /** |
||
850 | * @param $lead |
||
851 | * @param $custom |
||
852 | * @param $newValue |
||
853 | * |
||
854 | * @return Custom |
||
855 | * @throws LPTrackerSDKException |
||
856 | */ |
||
857 | public function editLeadCustom($lead, $custom, $newValue) |
||
883 | |||
884 | /** |
||
885 | * @param Project|int $project |
||
886 | * @param array $options |
||
887 | * |
||
888 | * @return CustomField |
||
889 | * |
||
890 | * @throws exceptions\LPTrackerResponseException |
||
891 | * @throws exceptions\LPTrackerServerException |
||
892 | * |
||
893 | * @author Yuri Nazarenko / rezident <[email protected]> |
||
894 | */ |
||
895 | public function createCustom($project, $options) |
||
903 | |||
904 | /** |
||
905 | * @param Project|int $project |
||
906 | * |
||
907 | * @param int $offset |
||
908 | * @param int $limit |
||
909 | * @param array $sort |
||
910 | * @param bool $isDeal |
||
911 | * |
||
912 | * @return Lead[] |
||
913 | * |
||
914 | * @throws exceptions\LPTrackerResponseException |
||
915 | * @throws exceptions\LPTrackerServerException |
||
916 | * @author Yuri Nazarenko / rezident <[email protected]> |
||
917 | */ |
||
918 | public function getLeadsList($project, $offset = null, $limit = null, $sort = [], $isDeal = false) |
||
938 | } |
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.