Complex classes like Customer 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 Customer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Customer extends AbstractEntity implements GeneratorAware, NameInterface, EmailInterface, AddressInterface |
||
12 | { |
||
13 | const IDENTITY = 'Customer'; |
||
14 | |||
15 | public $emailAddress = '[email protected]'; |
||
16 | |||
17 | public $billingFirstName = 'Kevin'; |
||
18 | public $billingLastName = 'Schroeder'; |
||
19 | public $billingCompany = ''; |
||
20 | public $billingAddress = '10451 Jefferson Blvd'; |
||
21 | public $billingAddress2 = ''; |
||
22 | public $billingCity = 'Culver City'; |
||
23 | public $billingRegionId = 'California'; |
||
24 | public $billingPostCode = '90232'; |
||
25 | public $billingCountryId = 'US'; |
||
26 | public $billingTelephone = '123-123-1234'; |
||
27 | public $billingFax = ''; |
||
28 | |||
29 | public $shippingFirstName = 'Kevin'; |
||
30 | public $shippingLastName = 'Schroeder'; |
||
31 | public $shippingCompany = ''; |
||
32 | public $shippingAddress = '10451 Jefferson Blvd'; |
||
33 | public $shippingAddress2 = ''; |
||
34 | public $shippingCity = 'Culver City'; |
||
35 | public $shippingRegionId = 'California'; |
||
36 | public $shippingPostCode = '90232'; |
||
37 | public $shippingCountryId = 'US'; |
||
38 | public $shippingTelephone = '123-123-1234'; |
||
39 | public $shippingFax = ''; |
||
40 | |||
41 | protected $uniqueEmailAddressGenerated = false; |
||
42 | protected $generator; |
||
43 | |||
44 | public function setGenerator(Generator $generator) |
||
48 | |||
49 | public function getCompany() |
||
50 | { |
||
51 | return $this->getBillingCompany(); |
||
52 | } |
||
53 | |||
54 | public function getAddress() |
||
55 | { |
||
56 | return $this->getBillingAddress(); |
||
57 | } |
||
58 | |||
59 | public function getAddress2() |
||
60 | { |
||
61 | return $this->getBillingAddress2(); |
||
62 | } |
||
63 | |||
64 | public function getCity() |
||
65 | { |
||
66 | return $this->getBillingCity(); |
||
67 | } |
||
68 | |||
69 | public function getRegionId() |
||
70 | { |
||
71 | return $this->getBillingRegionId(); |
||
72 | } |
||
73 | |||
74 | public function getPostCode() |
||
75 | { |
||
76 | return $this->getBillingPostCode(); |
||
77 | } |
||
78 | |||
79 | public function getCountryId() |
||
80 | { |
||
81 | return $this->getBillingCountryId(); |
||
82 | } |
||
83 | |||
84 | public function setCompany($value) |
||
85 | { |
||
86 | $this->setBillingCompany($value); |
||
87 | } |
||
88 | |||
89 | public function setAddress($value) |
||
90 | { |
||
91 | $this->setBillingAddress($value); |
||
92 | } |
||
93 | |||
94 | public function setAddress2($value) |
||
95 | { |
||
96 | $this->setBillingAddress2($value); |
||
97 | } |
||
98 | |||
99 | public function setCity($value) |
||
100 | { |
||
101 | $this->setBillingCity($value); |
||
102 | } |
||
103 | |||
104 | public function setRegionId($value) |
||
105 | { |
||
106 | $this->setBillingRegionId($value); |
||
107 | } |
||
108 | |||
109 | public function setPostCode($value) |
||
110 | { |
||
111 | $this->setBillingPostCode($value); |
||
112 | } |
||
113 | |||
114 | public function setCountryId($value) |
||
115 | { |
||
116 | $this->setBillingCountryId($value); |
||
117 | } |
||
118 | |||
119 | public function getFirstName() |
||
120 | { |
||
121 | return $this->getBillingFirstName(); |
||
122 | } |
||
123 | |||
124 | public function getLastName() |
||
125 | { |
||
126 | return $this->getBillingLastName(); |
||
127 | } |
||
128 | |||
129 | public function setFirstName($value) |
||
130 | { |
||
131 | $this->setBillingFirstName($value); |
||
132 | } |
||
133 | |||
134 | public function setLastName($value) |
||
135 | { |
||
136 | $this->setBillingLastName($value); |
||
137 | } |
||
138 | |||
139 | |||
140 | public function generateUniqueEmailAddress($domain = 'example.com') |
||
141 | { |
||
142 | if ($this->generator instanceof NameInterface) { |
||
143 | $this->generator->setFirstName($this->getFirstName()); |
||
144 | $this->generator->setLastName($this->getLastName()); |
||
145 | } |
||
146 | if ($this->generator instanceof EmailInterface) { |
||
147 | $this->generator->setEmailAddress($this->getEmailAddress()); |
||
148 | } |
||
149 | $this->uniqueEmailAddressGenerated = true; |
||
150 | $this->emailAddress = $this->generator->generate($domain); |
||
151 | return $this->emailAddress; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * @return boolean |
||
156 | */ |
||
157 | public function isUniqueEmailAddressGenerated() |
||
161 | |||
162 | /** |
||
163 | * @param boolean $uniqueEmailAddressGenerated |
||
164 | */ |
||
165 | public function setUniqueEmailAddressGenerated($uniqueEmailAddressGenerated) |
||
169 | |||
170 | /** |
||
171 | * @return string |
||
172 | */ |
||
173 | public function getBillingFirstName() |
||
177 | |||
178 | /** |
||
179 | * @param string $billingFirstName |
||
180 | */ |
||
181 | public function setBillingFirstName($billingFirstName) |
||
185 | |||
186 | /** |
||
187 | * @return string |
||
188 | */ |
||
189 | public function getBillingLastName() |
||
193 | |||
194 | /** |
||
195 | * @param string $billingLastName |
||
196 | */ |
||
197 | public function setBillingLastName($billingLastName) |
||
201 | |||
202 | /** |
||
203 | * @return string |
||
204 | */ |
||
205 | public function getBillingCompany() |
||
209 | |||
210 | /** |
||
211 | * @param string $billingCompany |
||
212 | */ |
||
213 | public function setBillingCompany($billingCompany) |
||
217 | |||
218 | /** |
||
219 | * @return string |
||
220 | */ |
||
221 | public function getEmailAddress() |
||
225 | |||
226 | /** |
||
227 | * @param string $emailAddress |
||
228 | */ |
||
229 | public function setEmailAddress($emailAddress) |
||
233 | |||
234 | |||
235 | |||
236 | /** |
||
237 | * @return string |
||
238 | */ |
||
239 | public function getBillingAddress() |
||
243 | |||
244 | /** |
||
245 | * @param string $billingAddress |
||
246 | */ |
||
247 | public function setBillingAddress($billingAddress) |
||
251 | |||
252 | /** |
||
253 | * @return string |
||
254 | */ |
||
255 | public function getBillingAddress2() |
||
259 | |||
260 | /** |
||
261 | * @param string $billingAddress2 |
||
262 | */ |
||
263 | public function setBillingAddress2($billingAddress2) |
||
267 | |||
268 | /** |
||
269 | * @return string |
||
270 | */ |
||
271 | public function getBillingCity() |
||
275 | |||
276 | /** |
||
277 | * @param string $billingCity |
||
278 | */ |
||
279 | public function setBillingCity($billingCity) |
||
283 | |||
284 | /** |
||
285 | * @return string |
||
286 | */ |
||
287 | public function getBillingRegionId() |
||
291 | |||
292 | /** |
||
293 | * @param string $billingRegionId |
||
294 | */ |
||
295 | public function setBillingRegionId($billingRegionId) |
||
299 | |||
300 | /** |
||
301 | * @return string |
||
302 | */ |
||
303 | public function getBillingPostCode() |
||
307 | |||
308 | /** |
||
309 | * @param string $billingPostCode |
||
310 | */ |
||
311 | public function setBillingPostCode($billingPostCode) |
||
315 | |||
316 | /** |
||
317 | * @return string |
||
318 | */ |
||
319 | public function getBillingCountryId() |
||
323 | |||
324 | /** |
||
325 | * @param string $billingCountryId |
||
326 | */ |
||
327 | public function setBillingCountryId($billingCountryId) |
||
331 | |||
332 | /** |
||
333 | * @return string |
||
334 | */ |
||
335 | public function getBillingTelephone() |
||
339 | |||
340 | /** |
||
341 | * @param string $billingTelephone |
||
342 | */ |
||
343 | public function setBillingTelephone($billingTelephone) |
||
347 | |||
348 | /** |
||
349 | * @return string |
||
350 | */ |
||
351 | public function getBillingFax() |
||
355 | |||
356 | /** |
||
357 | * @param string $billingFax |
||
358 | */ |
||
359 | public function setBillingFax($billingFax) |
||
363 | |||
364 | /** |
||
365 | * @return string |
||
366 | */ |
||
367 | public function getShippingFirstName() |
||
371 | |||
372 | /** |
||
373 | * @param string $shippingFirstName |
||
374 | */ |
||
375 | public function setShippingFirstName($shippingFirstName) |
||
379 | |||
380 | /** |
||
381 | * @return string |
||
382 | */ |
||
383 | public function getShippingLastName() |
||
387 | |||
388 | /** |
||
389 | * @param string $shippingLastName |
||
390 | */ |
||
391 | public function setShippingLastName($shippingLastName) |
||
395 | |||
396 | /** |
||
397 | * @return string |
||
398 | */ |
||
399 | public function getShippingCompany() |
||
403 | |||
404 | /** |
||
405 | * @param string $shippingCompany |
||
406 | */ |
||
407 | public function setShippingCompany($shippingCompany) |
||
411 | |||
412 | |||
413 | /** |
||
414 | * @return string |
||
415 | */ |
||
416 | public function getShippingAddress() |
||
420 | |||
421 | /** |
||
422 | * @param string $shippingAddress |
||
423 | */ |
||
424 | public function setShippingAddress($shippingAddress) |
||
428 | |||
429 | /** |
||
430 | * @return string |
||
431 | */ |
||
432 | public function getShippingAddress2() |
||
436 | |||
437 | /** |
||
438 | * @param string $shippingAddress2 |
||
439 | */ |
||
440 | public function setShippingAddress2($shippingAddress2) |
||
444 | |||
445 | /** |
||
446 | * @return string |
||
447 | */ |
||
448 | public function getShippingCity() |
||
452 | |||
453 | /** |
||
454 | * @param string $shippingCity |
||
455 | */ |
||
456 | public function setShippingCity($shippingCity) |
||
460 | |||
461 | /** |
||
462 | * @return string |
||
463 | */ |
||
464 | public function getShippingRegionId() |
||
468 | |||
469 | /** |
||
470 | * @param string $shippingRegionId |
||
471 | */ |
||
472 | public function setShippingRegionId($shippingRegionId) |
||
476 | |||
477 | /** |
||
478 | * @return string |
||
479 | */ |
||
480 | public function getShippingPostCode() |
||
484 | |||
485 | /** |
||
486 | * @param string $shippingPostCode |
||
487 | */ |
||
488 | public function setShippingPostCode($shippingPostCode) |
||
492 | |||
493 | /** |
||
494 | * @return string |
||
495 | */ |
||
496 | public function getShippingCountryId() |
||
500 | |||
501 | /** |
||
502 | * @param string $shippingCountryId |
||
503 | */ |
||
504 | public function setShippingCountryId($shippingCountryId) |
||
508 | |||
509 | /** |
||
510 | * @return string |
||
511 | */ |
||
512 | public function getShippingTelephone() |
||
516 | |||
517 | /** |
||
518 | * @param string $shippingTelephone |
||
519 | */ |
||
520 | public function setShippingTelephone($shippingTelephone) |
||
524 | |||
525 | /** |
||
526 | * @return string |
||
527 | */ |
||
528 | public function getShippingFax() |
||
532 | |||
533 | /** |
||
534 | * @param string $shippingFax |
||
535 | */ |
||
536 | public function setShippingFax($shippingFax) |
||
540 | |||
541 | |||
542 | |||
543 | } |