Complex classes like Shipment 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 Shipment, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class Shipment |
||
6 | { |
||
7 | /** |
||
8 | * @var PaymentInformation |
||
9 | */ |
||
10 | private $paymentInformation; |
||
11 | |||
12 | /** |
||
13 | * @var ItemizedPaymentInformation |
||
14 | */ |
||
15 | private $itemizedPaymentInformation; |
||
16 | |||
17 | /** |
||
18 | * @var RateInformation |
||
19 | */ |
||
20 | private $rateInformation; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | private $description; |
||
26 | |||
27 | /** |
||
28 | * @var Shipper |
||
29 | */ |
||
30 | private $shipper; |
||
31 | |||
32 | /** |
||
33 | * @var ShipTo; |
||
34 | */ |
||
35 | private $shipTo; |
||
36 | |||
37 | /** |
||
38 | * @var SoldTo |
||
39 | */ |
||
40 | private $soldTo; |
||
41 | |||
42 | /** |
||
43 | * @var ShipFrom |
||
44 | */ |
||
45 | private $shipFrom; |
||
46 | |||
47 | /** |
||
48 | * @var AlternateDeliveryAddress |
||
49 | */ |
||
50 | private $alternateDeliveryAddress; |
||
51 | |||
52 | /** |
||
53 | * @var ShipmentIndicationType |
||
54 | */ |
||
55 | private $shipmentIndicationType; |
||
56 | |||
57 | /** |
||
58 | * @var Service |
||
59 | */ |
||
60 | private $service; |
||
61 | |||
62 | /** |
||
63 | * @var ReturnService |
||
64 | */ |
||
65 | private $returnService; |
||
66 | |||
67 | /** |
||
68 | * @var bool |
||
69 | */ |
||
70 | private $documentsOnly; |
||
71 | |||
72 | /** |
||
73 | * @var Package[] |
||
74 | */ |
||
75 | private $packages = []; |
||
76 | |||
77 | /** |
||
78 | * @var ReferenceNumber |
||
79 | */ |
||
80 | private $referenceNumber; |
||
81 | |||
82 | /** |
||
83 | * @var ReferenceNumber |
||
84 | */ |
||
85 | private $referenceNumber2; |
||
86 | |||
87 | /** |
||
88 | * @var ShipmentServiceOptions |
||
89 | */ |
||
90 | private $shipmentServiceOptions; |
||
91 | |||
92 | /** |
||
93 | * @var bool |
||
94 | */ |
||
95 | private $goodsNotInFreeCirculationIndicator; |
||
96 | |||
97 | /** |
||
98 | * @var string |
||
99 | */ |
||
100 | private $movementReferenceNumber; |
||
101 | |||
102 | /** |
||
103 | * @var InvoiceLineTotal |
||
104 | */ |
||
105 | private $invoiceLineTotal; |
||
106 | |||
107 | /** |
||
108 | * @var ShipmentTotalWeight |
||
109 | */ |
||
110 | private $shipmentTotalWeight; |
||
111 | |||
112 | /** |
||
113 | * @var string |
||
114 | */ |
||
115 | private $numOfPiecesInShipment; |
||
116 | |||
117 | /** |
||
118 | * @var DeliveryTimeInformation |
||
119 | */ |
||
120 | private $deliveryTimeInformation; |
||
121 | |||
122 | 3 | public function __construct() |
|
130 | |||
131 | /** |
||
132 | * @return ShipmentIndicationType |
||
133 | */ |
||
134 | public function getShipmentIndicationType() |
||
138 | |||
139 | /** |
||
140 | * @param ShipmentIndicationType $shipmentIndicationType |
||
141 | */ |
||
142 | public function setShipmentIndicationType(ShipmentIndicationType $shipmentIndicationType) |
||
146 | |||
147 | /** |
||
148 | * @return AlternateDeliveryAddress |
||
149 | */ |
||
150 | public function getAlternateDeliveryAddress() |
||
154 | |||
155 | /** |
||
156 | * @param AlternateDeliveryAddress $alternateDeliveryAddress |
||
157 | */ |
||
158 | public function setAlternateDeliveryAddress(AlternateDeliveryAddress $alternateDeliveryAddress) |
||
162 | |||
163 | /** |
||
164 | * @param Package $package |
||
165 | * |
||
166 | * @return Shipment |
||
167 | */ |
||
168 | public function addPackage(Package $package) |
||
176 | |||
177 | /** |
||
178 | * @return string |
||
179 | */ |
||
180 | public function getDescription() |
||
184 | |||
185 | /** |
||
186 | * @param string $description |
||
187 | * |
||
188 | * @return Shipment |
||
189 | */ |
||
190 | public function setDescription($description) |
||
196 | |||
197 | /** |
||
198 | * @param ReferenceNumber $referenceNumber |
||
199 | * |
||
200 | * @return Shipment |
||
201 | */ |
||
202 | public function setReferenceNumber(ReferenceNumber $referenceNumber) |
||
208 | |||
209 | /** |
||
210 | * @param ReferenceNumber $referenceNumber |
||
211 | * |
||
212 | * @return Shipment |
||
213 | */ |
||
214 | public function setReferenceNumber2(ReferenceNumber $referenceNumber) |
||
220 | |||
221 | /** |
||
222 | * @return ReferenceNumber |
||
223 | */ |
||
224 | public function getReferenceNumber() |
||
228 | |||
229 | /** |
||
230 | * @return ReferenceNumber |
||
231 | */ |
||
232 | public function getReferenceNumber2() |
||
236 | |||
237 | /** |
||
238 | * @return bool |
||
239 | */ |
||
240 | public function getDocumentsOnly() |
||
244 | |||
245 | /** |
||
246 | * @param bool $documentsOnly |
||
247 | * |
||
248 | * @return Shipment |
||
249 | */ |
||
250 | public function setDocumentsOnly($documentsOnly) |
||
256 | |||
257 | /** |
||
258 | * @return Package[] |
||
259 | */ |
||
260 | public function getPackages() |
||
264 | |||
265 | /** |
||
266 | * @param Package[] $packages |
||
267 | * |
||
268 | * @return Shipment |
||
269 | */ |
||
270 | public function setPackages(array $packages) |
||
276 | |||
277 | /** |
||
278 | * @return Service |
||
279 | */ |
||
280 | public function getService() |
||
284 | |||
285 | /** |
||
286 | * @param Service $service |
||
287 | * |
||
288 | * @return Shipment |
||
289 | */ |
||
290 | 3 | public function setService(Service $service) |
|
296 | |||
297 | /** |
||
298 | * @return ReturnService |
||
299 | */ |
||
300 | public function getReturnService() |
||
304 | |||
305 | /** |
||
306 | * @param ReturnService $returnService |
||
307 | * |
||
308 | * @return Shipment |
||
309 | */ |
||
310 | public function setReturnService(ReturnService $returnService) |
||
316 | |||
317 | /** |
||
318 | * @return ShipFrom |
||
319 | */ |
||
320 | public function getShipFrom() |
||
324 | |||
325 | /** |
||
326 | * @param ShipFrom $shipFrom |
||
327 | * |
||
328 | * @return Shipment |
||
329 | */ |
||
330 | public function setShipFrom(ShipFrom $shipFrom) |
||
336 | |||
337 | /** |
||
338 | * @return ShipTo |
||
339 | */ |
||
340 | public function getShipTo() |
||
344 | |||
345 | /** |
||
346 | * @param ShipTo $shipTo |
||
347 | * |
||
348 | * @return Shipment |
||
349 | */ |
||
350 | 3 | public function setShipTo(ShipTo $shipTo) |
|
356 | |||
357 | /** |
||
358 | * @return SoldTo |
||
359 | */ |
||
360 | public function getSoldTo() |
||
364 | |||
365 | /** |
||
366 | * @param SoldTo $soldTo |
||
367 | * |
||
368 | * @return Shipment |
||
369 | */ |
||
370 | public function setSoldTo(SoldTo $soldTo) |
||
376 | |||
377 | /** |
||
378 | * @return ShipmentServiceOptions |
||
379 | */ |
||
380 | public function getShipmentServiceOptions() |
||
384 | |||
385 | /** |
||
386 | * @param ShipmentServiceOptions $shipmentServiceOptions |
||
387 | * |
||
388 | * @return Shipment |
||
389 | */ |
||
390 | 3 | public function setShipmentServiceOptions(ShipmentServiceOptions $shipmentServiceOptions) |
|
396 | |||
397 | /** |
||
398 | * @return Shipper |
||
399 | */ |
||
400 | public function getShipper() |
||
404 | |||
405 | /** |
||
406 | * @param Shipper $shipper |
||
407 | * |
||
408 | * @return Shipment |
||
409 | */ |
||
410 | 3 | public function setShipper(Shipper $shipper) |
|
416 | |||
417 | /** |
||
418 | * @return PaymentInformation |
||
419 | */ |
||
420 | public function getPaymentInformation() |
||
424 | |||
425 | /** |
||
426 | * @param PaymentInformation $paymentInformation |
||
427 | * |
||
428 | * @return Shipment |
||
429 | */ |
||
430 | public function setPaymentInformation(PaymentInformation $paymentInformation) |
||
436 | |||
437 | /** |
||
438 | * @return ItemizedPaymentInformation |
||
439 | */ |
||
440 | public function getItemizedPaymentInformation() |
||
444 | |||
445 | /** |
||
446 | * @param ItemizedPaymentInformation $itemizedPaymentInformation |
||
447 | * |
||
448 | * @return Shipment |
||
449 | */ |
||
450 | public function setItemizedPaymentInformation(ItemizedPaymentInformation $itemizedPaymentInformation) |
||
456 | |||
457 | /** |
||
458 | * If called, returned prices will include negotiated rates (discounts will be applied). |
||
459 | */ |
||
460 | public function showNegotiatedRates() |
||
465 | |||
466 | /** |
||
467 | * @return null|RateInformation |
||
468 | */ |
||
469 | public function getRateInformation() |
||
473 | |||
474 | /** |
||
475 | * @param RateInformation $rateInformation |
||
476 | * |
||
477 | * @return Shipment |
||
478 | */ |
||
479 | public function setRateInformation(RateInformation $rateInformation) |
||
485 | |||
486 | /** |
||
487 | * @return boolean |
||
488 | */ |
||
489 | public function getGoodsNotInFreeCirculationIndicator() |
||
493 | |||
494 | /** |
||
495 | * @param boolean $goodsNotInFreeCirculationIndicator |
||
496 | * @return Shipment |
||
497 | */ |
||
498 | public function setGoodsNotInFreeCirculationIndicator($goodsNotInFreeCirculationIndicator) |
||
504 | |||
505 | /** |
||
506 | * @return string |
||
507 | */ |
||
508 | public function getMovementReferenceNumber() |
||
512 | |||
513 | /** |
||
514 | * @param string $movementReferenceNumber |
||
515 | * @return Shipment |
||
516 | */ |
||
517 | public function setMovementReferenceNumber($movementReferenceNumber) |
||
523 | |||
524 | /** |
||
525 | * @return InvoiceLineTotal |
||
526 | */ |
||
527 | public function getInvoiceLineTotal() |
||
531 | |||
532 | /** |
||
533 | * @param InvoiceLineTotal $invoiceLineTotal |
||
534 | * @return Shipment |
||
535 | */ |
||
536 | public function setInvoiceLineTotal(InvoiceLineTotal $invoiceLineTotal) |
||
542 | |||
543 | /** |
||
544 | * @return string |
||
545 | */ |
||
546 | public function getNumOfPiecesInShipment() |
||
550 | |||
551 | /** |
||
552 | * @param string $numOfPiecesInShipment |
||
553 | * @return Shipment |
||
554 | */ |
||
555 | public function setNumOfPiecesInShipment($numOfPiecesInShipment) |
||
561 | |||
562 | /** |
||
563 | * @return DeliveryTimeInformation |
||
564 | */ |
||
565 | public function getDeliveryTimeInformation() |
||
569 | |||
570 | /** |
||
571 | * @param DeliveryTimeInformation $deliveryTimeInformation |
||
572 | */ |
||
573 | public function setDeliveryTimeInformation(DeliveryTimeInformation $deliveryTimeInformation) |
||
577 | |||
578 | /** |
||
579 | * @return ShipmentTotalWeight |
||
580 | */ |
||
581 | public function getShipmentTotalWeight() |
||
585 | |||
586 | /** |
||
587 | * @param ShipmentTotalWeight $shipmentTotalWeight |
||
588 | */ |
||
589 | public function setShipmentTotalWeight(ShipmentTotalWeight $shipmentTotalWeight) |
||
593 | } |
||
594 |