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 |
||
19 | class LPTracker extends LPTrackerBase |
||
20 | { |
||
21 | /** |
||
22 | * @param string $login |
||
23 | * @param string $password |
||
24 | * @param string $serviceName |
||
25 | * @return AccessToken |
||
26 | * @throws LPTrackerSDKException |
||
27 | */ |
||
28 | public function login($login, $password, $serviceName = '') |
||
29 | { |
||
30 | if (empty($login)) { |
||
31 | throw new LPTrackerSDKException('Login is empty'); |
||
32 | } |
||
33 | |||
34 | if (empty($password)) { |
||
35 | throw new LPTrackerSDKException('Password is empty'); |
||
36 | } |
||
37 | |||
38 | if (empty($serviceName)) { |
||
39 | $serviceName = LPTrackerBase::DEFAULT_SERVICE_NAME; |
||
40 | } |
||
41 | $response = LPTrackerRequest::sendRequest( |
||
42 | '/login', |
||
43 | [ |
||
44 | 'login' => $login, |
||
45 | 'password' => $password, |
||
46 | 'service' => $serviceName, |
||
47 | 'version' => LPTrackerBase::VERSION, |
||
48 | ], |
||
49 | 'POST', |
||
50 | null, |
||
51 | $this->address |
||
52 | ); |
||
53 | return new AccessToken($response['token']); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @throws exceptions\LPTrackerResponseException |
||
58 | * @throws exceptions\LPTrackerServerException |
||
59 | */ |
||
60 | public function logout() |
||
64 | |||
65 | /** |
||
66 | * @return Project[] |
||
67 | * @throws exceptions\LPTrackerResponseException |
||
68 | * @throws exceptions\LPTrackerServerException |
||
69 | */ |
||
70 | View Code Duplication | public function getProjects() |
|
79 | |||
80 | /** |
||
81 | * @param Project|int $project |
||
82 | * @return Project |
||
83 | * @throws exceptions\LPTrackerResponseException |
||
84 | * @throws exceptions\LPTrackerServerException |
||
85 | */ |
||
86 | public function getProject($project) |
||
97 | |||
98 | /** |
||
99 | * @return Project[] |
||
100 | * @throws exceptions\LPTrackerResponseException |
||
101 | * @throws exceptions\LPTrackerServerException |
||
102 | * @deprecated Use getProjects() |
||
103 | */ |
||
104 | public function getProjectList() |
||
108 | |||
109 | /** |
||
110 | * @param Project|int $project |
||
111 | * @return Custom[] |
||
112 | * @throws exceptions\LPTrackerResponseException |
||
113 | * @throws exceptions\LPTrackerServerException |
||
114 | */ |
||
115 | View Code Duplication | public function getProjectCustoms($project) |
|
130 | |||
131 | /** |
||
132 | * @param Project|int $project |
||
133 | * @return Stage[] |
||
134 | * @throws exceptions\LPTrackerResponseException |
||
135 | * @throws exceptions\LPTrackerServerException |
||
136 | */ |
||
137 | View Code Duplication | public function getProjectStages($project) |
|
152 | |||
153 | /** |
||
154 | * @param Project|int $project |
||
155 | * @return ContactField[] |
||
156 | * @throws exceptions\LPTrackerResponseException |
||
157 | * @throws exceptions\LPTrackerServerException |
||
158 | */ |
||
159 | View Code Duplication | public function getProjectFields($project) |
|
174 | |||
175 | /** |
||
176 | * @param Project|int $project |
||
177 | * @param string $callbackUrl |
||
178 | * @throws exceptions\LPTrackerResponseException |
||
179 | * @throws exceptions\LPTrackerServerException |
||
180 | */ |
||
181 | View Code Duplication | public function setProjectCallbackUrl($project, $callbackUrl) |
|
182 | { |
||
183 | if ($project instanceof Project) { |
||
184 | $project = $project->getId(); |
||
185 | } else { |
||
186 | $project = (int) $project; |
||
187 | } |
||
188 | $url = '/project/' . $project . '/callback-url'; |
||
189 | LPTrackerRequest::sendRequest($url, ['url' => $callbackUrl], 'PUT', $this->token, $this->address); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * @return Employee[] |
||
194 | * @throws exceptions\LPTrackerResponseException |
||
195 | * @throws exceptions\LPTrackerServerException |
||
196 | */ |
||
197 | View Code Duplication | public function getEmployees() |
|
206 | |||
207 | /** |
||
208 | * @param Project|int $project |
||
209 | * @param array $details |
||
210 | * @param array $contactData |
||
211 | * @param array $fields |
||
212 | * @return Contact |
||
213 | * @throws LPTrackerSDKException |
||
214 | */ |
||
215 | public function createContact( |
||
247 | |||
248 | /** |
||
249 | * @param Contact|int $contact |
||
250 | * @return Contact |
||
251 | * @throws LPTrackerSDKException |
||
252 | */ |
||
253 | View Code Duplication | public function getContact($contact) |
|
268 | |||
269 | /** |
||
270 | * @param Contact $contact |
||
271 | * @return Contact |
||
272 | * @throws LPTrackerSDKException |
||
273 | */ |
||
274 | public function saveContact(Contact $contact) |
||
300 | |||
301 | /** |
||
302 | * @param Contact|int $contact |
||
303 | * @param array $details |
||
304 | * @param array $contactData |
||
305 | * @param array $fields |
||
306 | * @return Contact |
||
307 | * @throws LPTrackerSDKException |
||
308 | */ |
||
309 | public function editContact( |
||
340 | |||
341 | /** |
||
342 | * @param Project|int $project |
||
343 | * @param array $searchOptions |
||
344 | * @return Contact[] |
||
345 | * @throws LPTrackerSDKException |
||
346 | */ |
||
347 | public function searchContacts($project, array $searchOptions = []) |
||
375 | |||
376 | /** |
||
377 | * @param Contact|int $contact |
||
378 | * @return Lead[] |
||
379 | * @throws LPTrackerSDKException |
||
380 | */ |
||
381 | public function getContactLeads($contact) |
||
400 | |||
401 | /** |
||
402 | * @param Contact|int $contact |
||
403 | * @return array |
||
404 | * @throws LPTrackerSDKException |
||
405 | * @deprecated Use getContactLeads() |
||
406 | */ |
||
407 | public function contactLeads($contact) |
||
411 | |||
412 | /** |
||
413 | * @param Contact|int $contact |
||
414 | * @param ContactField|int $field |
||
415 | * @param mixed $newValue |
||
416 | * @return ContactField |
||
417 | * @throws LPTrackerSDKException |
||
418 | * @throws exceptions\LPTrackerResponseException |
||
419 | * @throws exceptions\LPTrackerServerException |
||
420 | */ |
||
421 | public function editContactField($contact, $field, $newValue) |
||
441 | |||
442 | /** |
||
443 | * @param Contact|int $contact |
||
444 | * @param ContactField|int $field |
||
445 | * @param string $newValue |
||
446 | * @return ContactField |
||
447 | * @throws exceptions\LPTrackerResponseException |
||
448 | * @throws exceptions\LPTrackerServerException |
||
449 | * @deprecated Use editContactField() |
||
450 | */ |
||
451 | public function updateContactField($contact, $field, $newValue) |
||
455 | |||
456 | /** |
||
457 | * @param ContactField $field |
||
458 | * @return ContactField |
||
459 | * @throws LPTrackerSDKException |
||
460 | * @throws exceptions\LPTrackerResponseException |
||
461 | * @throws exceptions\LPTrackerServerException |
||
462 | */ |
||
463 | public function saveContactField(ContactField $field) |
||
482 | |||
483 | /** |
||
484 | * @param Project|int $project |
||
485 | * @param array $viewData |
||
486 | * @return View |
||
487 | * @throws LPTrackerSDKException |
||
488 | */ |
||
489 | public function createView($project, array $viewData = []) |
||
505 | |||
506 | /** |
||
507 | * @param View|int $view |
||
508 | * @return View |
||
509 | * @throws LPTrackerSDKException |
||
510 | */ |
||
511 | View Code Duplication | public function getView($view) |
|
526 | |||
527 | /** |
||
528 | * @param View $view |
||
529 | * @return View |
||
530 | * @throws LPTrackerSDKException |
||
531 | */ |
||
532 | View Code Duplication | public function saveView(View $view) |
|
546 | |||
547 | /** |
||
548 | * @param View|int $view |
||
549 | * @param array $viewData |
||
550 | * @return View |
||
551 | * @throws LPTrackerSDKException |
||
552 | */ |
||
553 | public function editView($view, array $viewData = []) |
||
564 | |||
565 | /** |
||
566 | * @param Contact|int $contact |
||
567 | * @param array $leadData |
||
568 | * @param array $options |
||
569 | * @return Lead |
||
570 | * @throws LPTrackerSDKException |
||
571 | */ |
||
572 | public function createLead($contact, array $leadData = [], array $options = []) |
||
599 | |||
600 | /** |
||
601 | * @param Lead|int $lead |
||
602 | * @return Lead |
||
603 | * @throws LPTrackerSDKException |
||
604 | */ |
||
605 | View Code Duplication | public function getLead($lead) |
|
620 | |||
621 | /** |
||
622 | * @param Lead|int $lead |
||
623 | * @param Custom|int $custom |
||
624 | * @return LeadFile |
||
625 | * @throws LPTrackerSDKException |
||
626 | */ |
||
627 | public function getCustomFile($lead, $custom, $file) |
||
654 | |||
655 | /** |
||
656 | * @param Lead $lead |
||
657 | * @return Lead |
||
658 | * @throws LPTrackerSDKException |
||
659 | */ |
||
660 | View Code Duplication | public function saveLead(Lead $lead) |
|
680 | |||
681 | /** |
||
682 | * @param Lead|int $lead |
||
683 | * @param array $leadData |
||
684 | * @return Lead |
||
685 | * @throws LPTrackerSDKException |
||
686 | */ |
||
687 | public function editLead($lead, array $leadData = []) |
||
698 | |||
699 | /** |
||
700 | * @param Lead|int $lead |
||
701 | * @param string $category |
||
702 | * @param string $purpose |
||
703 | * @param float $sum |
||
704 | * @return Lead |
||
705 | * @throws LPTrackerSDKException |
||
706 | */ |
||
707 | public function addPaymentToLead($lead, $category, $purpose, $sum) |
||
740 | |||
741 | /** |
||
742 | * @param Lead|int $lead |
||
743 | * @param string $category |
||
744 | * @param string $purpose |
||
745 | * @param float $sum |
||
746 | * @return Lead |
||
747 | * @throws LPTrackerSDKException |
||
748 | * @deprecated Use addPaymentToLead() |
||
749 | */ |
||
750 | public function addLeadPayment($lead, $category, $purpose, $sum) |
||
754 | |||
755 | /** |
||
756 | * @param Lead|int $lead |
||
757 | */ |
||
758 | View Code Duplication | public function callLead($lead) |
|
768 | |||
769 | /** |
||
770 | * @param Lead|int $lead |
||
771 | * @param int $newStageId |
||
772 | * @param array $options |
||
773 | * @return Lead |
||
774 | * @throws LPTrackerSDKException |
||
775 | */ |
||
776 | View Code Duplication | public function editLeadStage($lead, $newStageId, array $options = []) |
|
791 | |||
792 | /** |
||
793 | * @param Lead|int $lead |
||
794 | * @param int $newFunnelId |
||
795 | * @return Lead |
||
796 | * @throws LPTrackerSDKException |
||
797 | * @deprecated Use editLeadStage() |
||
798 | */ |
||
799 | public function changeLeadFunnel($lead, $newFunnelId) |
||
803 | |||
804 | /** |
||
805 | * @param Lead|int $lead |
||
806 | * @param int $newOwnerId |
||
807 | * @param array $options |
||
808 | * @return Lead |
||
809 | * @throws LPTrackerSDKException |
||
810 | */ |
||
811 | View Code Duplication | public function editLeadOwner($lead, $newOwnerId, array $options = []) |
|
826 | |||
827 | /** |
||
828 | * @param Lead|int $lead |
||
829 | * @return Comment[] |
||
830 | * @throws exceptions\LPTrackerResponseException |
||
831 | * @throws exceptions\LPTrackerServerException |
||
832 | */ |
||
833 | View Code Duplication | public function getLeadComments($lead) |
|
848 | |||
849 | /** |
||
850 | * @param Lead|int $lead |
||
851 | * @param string $text |
||
852 | * @param array $options |
||
853 | * @return Comment |
||
854 | * @throws exceptions\LPTrackerResponseException |
||
855 | * @throws exceptions\LPTrackerServerException |
||
856 | */ |
||
857 | View Code Duplication | public function addCommentToLead($lead, $text, array $options = []) |
|
872 | |||
873 | /** |
||
874 | * @param Lead|int $lead |
||
875 | * @param Custom|int $custom |
||
876 | * @param string $absolutePath |
||
877 | * @throws exceptions\LPTrackerResponseException |
||
878 | * @throws exceptions\LPTrackerServerException |
||
879 | */ |
||
880 | public function addFileToLead($lead, $custom, $absolutePath) |
||
901 | |||
902 | /** |
||
903 | * @param Custom $custom |
||
904 | * @return Custom |
||
905 | * @throws LPTrackerSDKException |
||
906 | */ |
||
907 | public function saveLeadCustom(Custom $custom, array $options = []) |
||
930 | |||
931 | /** |
||
932 | * @param Lead|int $lead |
||
933 | * @param Custom|int $custom |
||
934 | * @param mixed $newValue |
||
935 | * @param array $options |
||
936 | * @return Custom |
||
937 | * @throws LPTrackerSDKException |
||
938 | */ |
||
939 | public function editLeadCustom($lead, $custom, $newValue, array $options = []) |
||
961 | |||
962 | /** |
||
963 | * @param Project|int $project |
||
964 | * @param array $options |
||
965 | * @return CustomField |
||
966 | * @throws exceptions\LPTrackerResponseException |
||
967 | * @throws exceptions\LPTrackerServerException |
||
968 | */ |
||
969 | View Code Duplication | public function createCustom($project, $options) |
|
980 | |||
981 | /** |
||
982 | * @param Project|int $project |
||
983 | * @param int $offset |
||
984 | * @param int $limit |
||
985 | * @param array $sort |
||
986 | * @param bool $isDeal |
||
987 | * @param array $filter |
||
988 | * @return Lead[] |
||
989 | * @throws exceptions\LPTrackerResponseException |
||
990 | * @throws exceptions\LPTrackerServerException |
||
991 | */ |
||
992 | public function getLeads($project, $offset = null, $limit = null, $sort = [], $isDeal = false, $filter = []) |
||
1013 | |||
1014 | /** |
||
1015 | * @param Project|int $project |
||
1016 | * @param int $offset |
||
1017 | * @param int $limit |
||
1018 | * @param array $sort |
||
1019 | * @param bool $isDeal |
||
1020 | * @param array $filter |
||
1021 | * @return Lead[] |
||
1022 | * @throws exceptions\LPTrackerResponseException |
||
1023 | * @throws exceptions\LPTrackerServerException |
||
1024 | * @deprecated Use getLeads() |
||
1025 | */ |
||
1026 | public function getLeadsList($project, $offset = null, $limit = null, $sort = [], $isDeal = false, $filter = []) |
||
1030 | } |
||
1031 |
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.