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 Lead 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 Lead, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class Lead extends Model |
||
8 | { |
||
9 | /** |
||
10 | * @var integer |
||
11 | */ |
||
12 | protected $id; |
||
13 | |||
14 | /** |
||
15 | * @var integer |
||
16 | */ |
||
17 | protected $contactId; |
||
18 | |||
19 | /** |
||
20 | * @var Contact |
||
21 | */ |
||
22 | protected $contact; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $name; |
||
28 | |||
29 | /** |
||
30 | * @var integer |
||
31 | */ |
||
32 | protected $funnelId; |
||
33 | |||
34 | /** |
||
35 | * @var View |
||
36 | */ |
||
37 | protected $view; |
||
38 | |||
39 | /** |
||
40 | * @var integer |
||
41 | */ |
||
42 | protected $ownerId; |
||
43 | |||
44 | /** |
||
45 | * @var \DateTime |
||
46 | */ |
||
47 | protected $createdAt; |
||
48 | |||
49 | /** |
||
50 | * @var Payment[] |
||
51 | */ |
||
52 | protected $payments = []; |
||
53 | |||
54 | /** |
||
55 | * @var Custom[] |
||
56 | */ |
||
57 | protected $customs = []; |
||
58 | |||
59 | /** |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $options = []; |
||
63 | |||
64 | public function __construct(array $leadData = []) |
||
120 | |||
121 | /** |
||
122 | * @param bool $toSave |
||
123 | * @return array |
||
124 | */ |
||
125 | public function toArray($toSave = false) |
||
168 | |||
169 | /** |
||
170 | * @return bool |
||
171 | * @throws LPTrackerSDKException |
||
172 | */ |
||
173 | public function validate() |
||
196 | |||
197 | /** |
||
198 | * @return int |
||
199 | */ |
||
200 | public function getId() |
||
204 | |||
205 | /** |
||
206 | * @return int |
||
207 | */ |
||
208 | public function getContactId() |
||
212 | |||
213 | /** |
||
214 | * @return Contact |
||
215 | */ |
||
216 | public function getContact() |
||
220 | |||
221 | /** |
||
222 | * @return string |
||
223 | */ |
||
224 | public function getName() |
||
228 | |||
229 | /** |
||
230 | * @param string $name |
||
231 | * @return $this |
||
232 | */ |
||
233 | public function setName($name) |
||
238 | |||
239 | /** |
||
240 | * @return int |
||
241 | */ |
||
242 | public function getFunnelId() |
||
246 | |||
247 | /** |
||
248 | * @param int $funnelId |
||
249 | * @return $this |
||
250 | */ |
||
251 | public function setFunnelId($funnelId) |
||
256 | |||
257 | /** |
||
258 | * @return View |
||
259 | */ |
||
260 | public function getView() |
||
264 | |||
265 | /** |
||
266 | * @param View $view |
||
267 | * @return $this |
||
268 | */ |
||
269 | public function setView(View $view) |
||
274 | |||
275 | /** |
||
276 | * @return Payment[] |
||
277 | */ |
||
278 | public function getPayments() |
||
282 | |||
283 | /** |
||
284 | * @param array $payments |
||
285 | * @return $this |
||
286 | */ |
||
287 | public function setPayments(array $payments) |
||
292 | |||
293 | /** |
||
294 | * @param Payment $payment |
||
295 | * @return $this |
||
296 | */ |
||
297 | public function addPayment(Payment $payment) |
||
302 | |||
303 | /** |
||
304 | * @return int |
||
305 | */ |
||
306 | public function getOwnerId() |
||
310 | |||
311 | /** |
||
312 | * @param int $ownerId |
||
313 | * @return $this |
||
314 | */ |
||
315 | public function setOwnerId($ownerId) |
||
320 | |||
321 | /** |
||
322 | * @return Custom[] |
||
323 | */ |
||
324 | public function getCustoms() |
||
328 | |||
329 | /** |
||
330 | * @param Custom[] $customs |
||
331 | * @return $this |
||
332 | */ |
||
333 | public function setCustoms(array $customs) |
||
338 | |||
339 | /** |
||
340 | * @param Custom $custom |
||
341 | * @return $this |
||
342 | */ |
||
343 | public function addCustom(Custom $custom) |
||
348 | |||
349 | /** |
||
350 | * @return \DateTime |
||
351 | */ |
||
352 | public function getCreatedAt() |
||
356 | |||
357 | /** |
||
358 | * @param \DateTime $createdAt |
||
359 | * @return $this |
||
360 | */ |
||
361 | public function setCreatedAt($createdAt) |
||
366 | } |
||
367 |
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.