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 |
||
11 | class Lead extends Model |
||
12 | { |
||
13 | |||
14 | /** |
||
15 | * @var integer |
||
16 | */ |
||
17 | protected $id; |
||
18 | |||
19 | /** |
||
20 | * @var integer |
||
21 | */ |
||
22 | protected $contactId; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $name; |
||
28 | |||
29 | /** |
||
30 | * @var integer |
||
31 | */ |
||
32 | protected $funnelId; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $source; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $campaign; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $keyword; |
||
48 | |||
49 | /** |
||
50 | * @var integer |
||
51 | */ |
||
52 | protected $ownerId; |
||
53 | |||
54 | /** |
||
55 | * @var \DateTime |
||
56 | */ |
||
57 | protected $createdAt; |
||
58 | |||
59 | /** |
||
60 | * @var Payment[] |
||
61 | */ |
||
62 | protected $payments = []; |
||
63 | |||
64 | /** |
||
65 | * @var Custom[] |
||
66 | */ |
||
67 | protected $customs = []; |
||
68 | |||
69 | /** |
||
70 | * @var array |
||
71 | */ |
||
72 | protected $options = []; |
||
73 | |||
74 | |||
75 | /** |
||
76 | * Lead constructor. |
||
77 | * |
||
78 | * @param array $leadData |
||
79 | */ |
||
80 | public function __construct(array $leadData = []) |
||
129 | |||
130 | |||
131 | /** |
||
132 | * @return array |
||
133 | */ |
||
134 | public function toArray() |
||
175 | |||
176 | |||
177 | /** |
||
178 | * @return bool |
||
179 | * @throws LPTrackerSDKException |
||
180 | */ |
||
181 | public function validate() |
||
201 | |||
202 | |||
203 | /** |
||
204 | * @return int |
||
205 | */ |
||
206 | public function getId() |
||
210 | |||
211 | |||
212 | /** |
||
213 | * @return int |
||
214 | */ |
||
215 | public function getContactId() |
||
219 | |||
220 | |||
221 | /** |
||
222 | * @return string |
||
223 | */ |
||
224 | public function getName() |
||
228 | |||
229 | |||
230 | /** |
||
231 | * @param string $name |
||
232 | * |
||
233 | * @return $this |
||
234 | */ |
||
235 | public function setName($name) |
||
241 | |||
242 | |||
243 | /** |
||
244 | * @return int |
||
245 | */ |
||
246 | public function getFunnelId() |
||
250 | |||
251 | |||
252 | /** |
||
253 | * @param int $funnelId |
||
254 | * |
||
255 | * @return $this |
||
256 | */ |
||
257 | public function setFunnelId($funnelId) |
||
263 | |||
264 | |||
265 | /** |
||
266 | * @return string |
||
267 | */ |
||
268 | public function getSource() |
||
272 | |||
273 | |||
274 | /** |
||
275 | * @param string $source |
||
276 | * |
||
277 | * @return $this |
||
278 | */ |
||
279 | public function setSource($source) |
||
285 | |||
286 | |||
287 | /** |
||
288 | * @return string |
||
289 | */ |
||
290 | public function getCampaign() |
||
294 | |||
295 | |||
296 | /** |
||
297 | * @param string $campaign |
||
298 | */ |
||
299 | public function setCampaign($campaign) |
||
303 | |||
304 | |||
305 | /** |
||
306 | * @return string |
||
307 | */ |
||
308 | public function getKeyword() |
||
312 | |||
313 | |||
314 | /** |
||
315 | * @param string $keyword |
||
316 | * |
||
317 | * @return $this |
||
318 | */ |
||
319 | public function setKeyword($keyword) |
||
325 | |||
326 | |||
327 | /** |
||
328 | * @return Payment[] |
||
329 | */ |
||
330 | public function getPayments() |
||
334 | |||
335 | |||
336 | /** |
||
337 | * @param array $payments |
||
338 | * |
||
339 | * @return $this |
||
340 | */ |
||
341 | public function setPayments(array $payments) |
||
347 | |||
348 | |||
349 | /** |
||
350 | * @param Payment $payment |
||
351 | * |
||
352 | * @return $this |
||
353 | */ |
||
354 | public function addPayment(Payment $payment) |
||
360 | |||
361 | |||
362 | /** |
||
363 | * @return int |
||
364 | */ |
||
365 | public function getOwnerId() |
||
369 | |||
370 | |||
371 | /** |
||
372 | * @param int $ownerId |
||
373 | * |
||
374 | * @return $this |
||
375 | */ |
||
376 | public function setOwnerId($ownerId) |
||
382 | |||
383 | |||
384 | /** |
||
385 | * @return Custom[] |
||
386 | */ |
||
387 | public function getCustoms() |
||
391 | |||
392 | |||
393 | /** |
||
394 | * @param Custom[] $customs |
||
395 | * |
||
396 | * @return $this |
||
397 | */ |
||
398 | public function setCustoms(array $customs) |
||
404 | |||
405 | |||
406 | /** |
||
407 | * @param Custom $custom |
||
408 | * |
||
409 | * @return $this |
||
410 | */ |
||
411 | public function addCustom(Custom $custom) |
||
417 | |||
418 | |||
419 | /** |
||
420 | * @return \DateTime |
||
421 | */ |
||
422 | public function getCreatedAt() |
||
426 | |||
427 | |||
428 | /** |
||
429 | * @param \DateTime $createdAt |
||
430 | * |
||
431 | * @return $this |
||
432 | */ |
||
433 | public function setCreatedAt($createdAt) |
||
439 | } |