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 |
||
18 | class LPTracker extends LPTrackerBase |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * @param $login |
||
23 | * @param $password |
||
24 | * @param string $serviceName |
||
25 | * |
||
26 | * @return AccessToken |
||
27 | * @throws LPTrackerSDKException |
||
28 | */ |
||
29 | public function login($login, $password, $serviceName = '') |
||
52 | |||
53 | |||
54 | /** |
||
55 | * |
||
56 | */ |
||
57 | public function logout() |
||
61 | |||
62 | |||
63 | /** |
||
64 | * @return Project[] |
||
65 | */ |
||
66 | View Code Duplication | public function getProjectList() |
|
77 | |||
78 | |||
79 | /** |
||
80 | * @param $id |
||
81 | * |
||
82 | * @return Project |
||
83 | */ |
||
84 | View Code Duplication | public function getProject($id) |
|
94 | |||
95 | |||
96 | /** |
||
97 | * @param $project |
||
98 | * |
||
99 | * @return Custom[] |
||
100 | */ |
||
101 | View Code Duplication | public function getProjectCustoms($project) |
|
120 | |||
121 | |||
122 | /** |
||
123 | * @param $project |
||
124 | * |
||
125 | * @return ContactField[] |
||
126 | */ |
||
127 | View Code Duplication | public function getProjectFields($project) |
|
146 | |||
147 | |||
148 | /** |
||
149 | * @param $project |
||
150 | * @param array $details |
||
151 | * @param array $contactData |
||
152 | * @param array $fields |
||
153 | * |
||
154 | * @return Contact |
||
155 | * @throws LPTrackerSDKException |
||
156 | */ |
||
157 | public function createContact( |
||
195 | |||
196 | |||
197 | /** |
||
198 | * @param Contact $contact |
||
199 | * |
||
200 | * @return Contact |
||
201 | * @throws LPTrackerSDKException |
||
202 | */ |
||
203 | View Code Duplication | public function saveContact(Contact $contact) |
|
222 | |||
223 | |||
224 | /** |
||
225 | * @param $contactId |
||
226 | * @param array $details |
||
227 | * @param array $contactData |
||
228 | * @param array $fields |
||
229 | * |
||
230 | * @return Contact |
||
231 | * @throws LPTrackerSDKException |
||
232 | */ |
||
233 | public function editContact( |
||
260 | |||
261 | |||
262 | /** |
||
263 | * @param $contact |
||
264 | * @param $field |
||
265 | * @param $newValue |
||
266 | * |
||
267 | * @return ContactField |
||
268 | */ |
||
269 | public function updateContactField($contact, $field, $newValue) |
||
290 | |||
291 | |||
292 | /** |
||
293 | * @param $contact |
||
294 | * @param array $leadData |
||
295 | * @param array $options |
||
296 | * |
||
297 | * @return Lead |
||
298 | * @throws LPTrackerSDKException |
||
299 | */ |
||
300 | public function createLead($contact, array $leadData = [], array $options = []) |
||
324 | |||
325 | |||
326 | /** |
||
327 | * @param Lead $lead |
||
328 | * |
||
329 | * @return Lead |
||
330 | * @throws LPTrackerSDKException |
||
331 | */ |
||
332 | View Code Duplication | public function saveLead(Lead $lead) |
|
350 | |||
351 | |||
352 | /** |
||
353 | * @param $leadId |
||
354 | * @param array $leadData |
||
355 | * |
||
356 | * @return Lead |
||
357 | */ |
||
358 | public function editLead($leadId, array $leadData = []) |
||
367 | |||
368 | |||
369 | /** |
||
370 | * @param $lead |
||
371 | * |
||
372 | * @return Comment[] |
||
373 | */ |
||
374 | View Code Duplication | public function getLeadComments($lead) |
|
393 | |||
394 | |||
395 | /** |
||
396 | * @param $lead |
||
397 | * @param $text |
||
398 | * |
||
399 | * @return Comment |
||
400 | */ |
||
401 | View Code Duplication | public function addCommentToLead($lead, $text) |
|
421 | |||
422 | |||
423 | /** |
||
424 | * @param Custom $custom |
||
425 | * |
||
426 | * @return Custom |
||
427 | * @throws LPTrackerSDKException |
||
428 | */ |
||
429 | public function saveLeadCustom(Custom $custom) |
||
447 | |||
448 | |||
449 | /** |
||
450 | * @param $lead |
||
451 | * @param $custom |
||
452 | * @param $newValue |
||
453 | * |
||
454 | * @return Custom |
||
455 | */ |
||
456 | public function editLeadCustom($lead, $custom, $newValue) |
||
482 | } |
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.