Complex classes like CustomerInfo 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 CustomerInfo, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class CustomerInfo extends Payload implements CustomerInfoInterface |
||
8 | { |
||
9 | const GENDER_MALE = 'male'; |
||
10 | const GENDER_MALE_2 = 'm'; |
||
11 | const GENDER_FEMALE = 'female'; |
||
12 | const GENDER_FEMALE_2 = 'f'; |
||
13 | |||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | private $bankName; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $bankPhone; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $billingAddress; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $billingCity; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private $billingCountry; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $billingFirstName; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | private $billingLastName; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | private $billingPostal; |
||
53 | |||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | private $billingRegion; |
||
58 | |||
59 | /** |
||
60 | * @var \DateTimeInterface |
||
61 | */ |
||
62 | private $birthDate; |
||
63 | |||
64 | /** |
||
65 | * @var string |
||
66 | */ |
||
67 | private $customerPhone; |
||
68 | |||
69 | /** |
||
70 | * @var string |
||
71 | */ |
||
72 | private $email; |
||
73 | |||
74 | /** |
||
75 | * @var string |
||
76 | */ |
||
77 | private $gender; |
||
78 | |||
79 | /** |
||
80 | * @var string |
||
81 | */ |
||
82 | private $shippingAddress; |
||
83 | |||
84 | /** |
||
85 | * @var string |
||
86 | */ |
||
87 | private $shippingCity; |
||
88 | |||
89 | /** |
||
90 | * @var string |
||
91 | */ |
||
92 | private $shippingCountry; |
||
93 | |||
94 | /** |
||
95 | * @var string |
||
96 | */ |
||
97 | private $shippingFirstName; |
||
98 | |||
99 | /** |
||
100 | * @var string |
||
101 | */ |
||
102 | private $shippingLastName; |
||
103 | |||
104 | /** |
||
105 | * @var string |
||
106 | */ |
||
107 | private $shippingPostal; |
||
108 | |||
109 | /** |
||
110 | * @var string |
||
111 | */ |
||
112 | private $shippingRegion; |
||
113 | |||
114 | /** |
||
115 | * @var string |
||
116 | */ |
||
117 | private $username; |
||
118 | |||
119 | public function getPayload() : array |
||
149 | |||
150 | public function validate() |
||
174 | 6 | ||
175 | 6 | public static function getGenders() : array |
|
184 | 6 | ||
185 | 6 | /** |
|
186 | * @return string |
||
187 | */ |
||
188 | 6 | public function getBankName() : ?string |
|
192 | 6 | ||
193 | 6 | /** |
|
194 | 6 | * @param string $bankName |
|
195 | 6 | * @return CustomerInfo |
|
196 | 6 | */ |
|
197 | 6 | public function setBankName(string $bankName) : self |
|
202 | 6 | ||
203 | 6 | /** |
|
204 | 6 | * @return string |
|
205 | 6 | */ |
|
206 | 6 | public function getBankPhone() : ?string |
|
210 | 6 | ||
211 | 6 | /** |
|
212 | 4 | * @param string $bankPhone |
|
213 | * @return CustomerInfo |
||
214 | 6 | */ |
|
215 | public function setBankPhone(string $bankPhone) : self |
||
220 | 6 | ||
221 | 6 | /** |
|
222 | 6 | * @return string |
|
223 | 6 | */ |
|
224 | 4 | public function getBillingAddress() : ?string |
|
228 | |||
229 | /** |
||
230 | 6 | * @param string $billingAddress |
|
231 | * @return CustomerInfo |
||
232 | 6 | */ |
|
233 | public function setBillingAddress(string $billingAddress) : self |
||
238 | |||
239 | 6 | /** |
|
240 | * @return string |
||
241 | 6 | */ |
|
242 | 6 | public function getBillingCity() : ?string |
|
246 | |||
247 | /** |
||
248 | * @param string $billingCity |
||
249 | 6 | * @return CustomerInfo |
|
250 | */ |
||
251 | 6 | public function setBillingCity(string $billingCity) : self |
|
256 | |||
257 | /** |
||
258 | 6 | * @return string |
|
259 | */ |
||
260 | 6 | public function getBillingCountry() : ?string |
|
264 | |||
265 | /** |
||
266 | * @param string $billingCountry |
||
267 | * @return CustomerInfo |
||
268 | 6 | */ |
|
269 | public function setBillingCountry(string $billingCountry) : self |
||
274 | |||
275 | /** |
||
276 | * @return string |
||
277 | 6 | */ |
|
278 | public function getBillingFirstName() : ?string |
||
282 | |||
283 | /** |
||
284 | * @param string $billingFirstName |
||
285 | * @return CustomerInfo |
||
286 | */ |
||
287 | 6 | public function setBillingFirstName(string $billingFirstName) : self |
|
292 | |||
293 | /** |
||
294 | * @return string |
||
295 | */ |
||
296 | 6 | public function getBillingLastName() : ?string |
|
300 | 6 | ||
301 | /** |
||
302 | * @param string $billingLastName |
||
303 | * @return CustomerInfo |
||
304 | */ |
||
305 | public function setBillingLastName(string $billingLastName) : self |
||
310 | |||
311 | /** |
||
312 | * @return string |
||
313 | */ |
||
314 | public function getBillingPostal() : ?string |
||
318 | 6 | ||
319 | 6 | /** |
|
320 | * @param string $billingPostal |
||
321 | * @return CustomerInfo |
||
322 | */ |
||
323 | public function setBillingPostal(string $billingPostal) : self |
||
328 | |||
329 | /** |
||
330 | * @return string |
||
331 | */ |
||
332 | public function getBillingRegion() : ?string |
||
336 | 6 | ||
337 | 6 | /** |
|
338 | 6 | * @param string $billingRegion |
|
339 | * @return CustomerInfo |
||
340 | */ |
||
341 | public function setBillingRegion(string $billingRegion) : self |
||
346 | 6 | ||
347 | /** |
||
348 | * @return \DateTimeInterface |
||
349 | */ |
||
350 | public function getBirthDate() |
||
354 | |||
355 | 6 | /** |
|
356 | 6 | * @param \DateTimeInterface $birthDate |
|
357 | 6 | * @return CustomerInfo |
|
358 | */ |
||
359 | public function setBirthDate(\DateTimeInterface $birthDate) : self |
||
364 | |||
365 | 6 | /** |
|
366 | * @return string |
||
367 | */ |
||
368 | public function getCustomerPhone() : ?string |
||
372 | 6 | ||
373 | /** |
||
374 | 6 | * @param string $customerPhone |
|
375 | 6 | * @return CustomerInfo |
|
376 | 6 | */ |
|
377 | public function setCustomerPhone(string $customerPhone) : self |
||
382 | 6 | ||
383 | /** |
||
384 | 6 | * @return string |
|
385 | */ |
||
386 | public function getEmail() : ?string |
||
390 | |||
391 | 6 | /** |
|
392 | * @param string $email |
||
393 | 6 | * @return CustomerInfo |
|
394 | 6 | */ |
|
395 | 6 | public function setEmail(string $email) : self |
|
400 | |||
401 | 6 | /** |
|
402 | * @return string |
||
403 | 6 | */ |
|
404 | public function getGender() : ?string |
||
408 | |||
409 | /** |
||
410 | 6 | * @param string $gender |
|
411 | * @return CustomerInfo |
||
412 | 6 | */ |
|
413 | 6 | public function setGender(string $gender) : self |
|
419 | |||
420 | 6 | /** |
|
421 | * @return string |
||
422 | 6 | */ |
|
423 | public function getShippingAddress() : ?string |
||
427 | |||
428 | /** |
||
429 | 6 | * @param string $shippingAddress |
|
430 | * @return CustomerInfo |
||
431 | 6 | */ |
|
432 | 6 | public function setShippingAddress(string $shippingAddress) : self |
|
437 | |||
438 | /** |
||
439 | 6 | * @return string |
|
440 | */ |
||
441 | 6 | public function getShippingCity() : ?string |
|
445 | |||
446 | /** |
||
447 | * @param string $shippingCity |
||
448 | 6 | * @return CustomerInfo |
|
449 | */ |
||
450 | 6 | public function setShippingCity(string $shippingCity) : self |
|
455 | |||
456 | /** |
||
457 | * @return string |
||
458 | 6 | */ |
|
459 | public function getShippingCountry() : ?string |
||
463 | |||
464 | /** |
||
465 | * @param string $shippingCountry |
||
466 | * @return CustomerInfo |
||
467 | 6 | */ |
|
468 | public function setShippingCountry(string $shippingCountry) : self |
||
473 | |||
474 | /** |
||
475 | * @return string |
||
476 | */ |
||
477 | 6 | public function getShippingFirstName() : ?string |
|
481 | |||
482 | /** |
||
483 | * @param string $shippingFirstName |
||
484 | * @return CustomerInfo |
||
485 | */ |
||
486 | 6 | public function setShippingFirstName(string $shippingFirstName) : self |
|
491 | |||
492 | /** |
||
493 | * @return string |
||
494 | */ |
||
495 | public function getShippingLastName() : ?string |
||
499 | |||
500 | /** |
||
501 | * @param string $shippingLastName |
||
502 | * @return CustomerInfo |
||
503 | */ |
||
504 | public function setShippingLastName(string $shippingLastName) : self |
||
509 | 6 | ||
510 | /** |
||
511 | * @return string |
||
512 | */ |
||
513 | public function getShippingPostal() : ?string |
||
517 | 6 | ||
518 | /** |
||
519 | * @param string $shippingPostal |
||
520 | * @return CustomerInfo |
||
521 | */ |
||
522 | public function setShippingPostal(string $shippingPostal) : self |
||
527 | 6 | ||
528 | 6 | /** |
|
529 | * @return string |
||
530 | */ |
||
531 | public function getShippingRegion() : ?string |
||
535 | |||
536 | 6 | /** |
|
537 | * @param string $shippingRegion |
||
538 | * @return CustomerInfo |
||
539 | */ |
||
540 | public function setShippingRegion(string $shippingRegion) : self |
||
545 | 6 | ||
546 | 6 | /** |
|
547 | 6 | * @return string |
|
548 | */ |
||
549 | public function getUsername() : ?string |
||
553 | 6 | ||
554 | /** |
||
555 | 6 | * @param string $username |
|
556 | * @return CustomerInfo |
||
557 | */ |
||
558 | public function setUsername(string $username) : self |
||
563 | } |
||
564 |