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 RateInformation |
||
14 | */ |
||
15 | private $rateInformation; |
||
16 | |||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | private $description; |
||
21 | |||
22 | /** |
||
23 | * @var Shipper |
||
24 | */ |
||
25 | private $shipper; |
||
26 | |||
27 | /** |
||
28 | * @var ShipTo; |
||
29 | */ |
||
30 | private $shipTo; |
||
31 | |||
32 | /** |
||
33 | * @var SoldTo |
||
34 | */ |
||
35 | private $soldTo; |
||
36 | |||
37 | /** |
||
38 | * @var ShipFrom |
||
39 | */ |
||
40 | private $shipFrom; |
||
41 | |||
42 | /** |
||
43 | * @var AlternateDeliveryAddress |
||
44 | */ |
||
45 | private $alternateDeliveryAddress; |
||
46 | |||
47 | /** |
||
48 | * @var ShipmentIndicationType |
||
49 | */ |
||
50 | private $shipmentIndicationType; |
||
51 | |||
52 | /** |
||
53 | * @var Service |
||
54 | */ |
||
55 | private $service; |
||
56 | |||
57 | /** |
||
58 | * @var ReturnService |
||
59 | */ |
||
60 | private $returnService; |
||
61 | |||
62 | /** |
||
63 | * @var bool |
||
64 | */ |
||
65 | private $documentsOnly; |
||
66 | |||
67 | /** |
||
68 | * @var Package[] |
||
69 | */ |
||
70 | private $packages = []; |
||
71 | |||
72 | /** |
||
73 | * @var ReferenceNumber |
||
74 | */ |
||
75 | private $referenceNumber; |
||
76 | |||
77 | /** |
||
78 | * @var ReferenceNumber |
||
79 | */ |
||
80 | private $referenceNumber2; |
||
81 | |||
82 | /** |
||
83 | * @var ShipmentServiceOptions |
||
84 | */ |
||
85 | private $shipmentServiceOptions; |
||
86 | |||
87 | /** |
||
88 | * @var bool |
||
89 | */ |
||
90 | private $goodsNotInFreeCirculationIndicator; |
||
91 | |||
92 | /** |
||
93 | * @var string |
||
94 | */ |
||
95 | private $movementReferenceNumber; |
||
96 | |||
97 | /** |
||
98 | * @var InvoiceLineTotal |
||
99 | */ |
||
100 | private $invoiceLineTotal; |
||
101 | |||
102 | /** |
||
103 | * @var ShipmentTotalWeight |
||
104 | */ |
||
105 | private $shipmentTotalWeight; |
||
106 | |||
107 | /** |
||
108 | * @var string |
||
109 | */ |
||
110 | private $numOfPiecesInShipment; |
||
111 | |||
112 | /** |
||
113 | * @var DeliveryTimeInformation |
||
114 | */ |
||
115 | private $deliveryTimeInformation; |
||
116 | |||
117 | 3 | public function __construct() |
|
125 | |||
126 | /** |
||
127 | * @return ShipmentIndicationType |
||
128 | */ |
||
129 | public function getShipmentIndicationType() |
||
133 | |||
134 | /** |
||
135 | * @param ShipmentIndicationType $shipmentIndicationType |
||
136 | */ |
||
137 | public function setShipmentIndicationType(ShipmentIndicationType $shipmentIndicationType) |
||
141 | |||
142 | /** |
||
143 | * @return AlternateDeliveryAddress |
||
144 | */ |
||
145 | public function getAlternateDeliveryAddress() |
||
149 | |||
150 | /** |
||
151 | * @param AlternateDeliveryAddress $alternateDeliveryAddress |
||
152 | */ |
||
153 | public function setAlternateDeliveryAddress(AlternateDeliveryAddress $alternateDeliveryAddress) |
||
157 | |||
158 | /** |
||
159 | * @param Package $package |
||
160 | * |
||
161 | * @return Shipment |
||
162 | */ |
||
163 | public function addPackage(Package $package) |
||
171 | |||
172 | /** |
||
173 | * @return string |
||
174 | */ |
||
175 | public function getDescription() |
||
179 | |||
180 | /** |
||
181 | * @param string $description |
||
182 | * |
||
183 | * @return Shipment |
||
184 | */ |
||
185 | public function setDescription($description) |
||
191 | |||
192 | /** |
||
193 | * @param ReferenceNumber $referenceNumber |
||
194 | * |
||
195 | * @return Shipment |
||
196 | */ |
||
197 | public function setReferenceNumber(ReferenceNumber $referenceNumber) |
||
203 | |||
204 | /** |
||
205 | * @param ReferenceNumber $referenceNumber |
||
206 | * |
||
207 | * @return Shipment |
||
208 | */ |
||
209 | public function setReferenceNumber2(ReferenceNumber $referenceNumber) |
||
215 | |||
216 | /** |
||
217 | * @return ReferenceNumber |
||
218 | */ |
||
219 | public function getReferenceNumber() |
||
223 | |||
224 | /** |
||
225 | * @return ReferenceNumber |
||
226 | */ |
||
227 | public function getReferenceNumber2() |
||
231 | |||
232 | /** |
||
233 | * @return bool |
||
234 | */ |
||
235 | public function getDocumentsOnly() |
||
239 | |||
240 | /** |
||
241 | * @param bool $documentsOnly |
||
242 | * |
||
243 | * @return Shipment |
||
244 | */ |
||
245 | public function setDocumentsOnly($documentsOnly) |
||
251 | |||
252 | /** |
||
253 | * @return Package[] |
||
254 | */ |
||
255 | public function getPackages() |
||
259 | |||
260 | /** |
||
261 | * @param Package[] $packages |
||
262 | * |
||
263 | * @return Shipment |
||
264 | */ |
||
265 | public function setPackages(array $packages) |
||
271 | |||
272 | /** |
||
273 | * @return Service |
||
274 | */ |
||
275 | public function getService() |
||
279 | |||
280 | /** |
||
281 | * @param Service $service |
||
282 | * |
||
283 | * @return Shipment |
||
284 | */ |
||
285 | 3 | public function setService(Service $service) |
|
291 | |||
292 | /** |
||
293 | * @return ReturnService |
||
294 | */ |
||
295 | public function getReturnService() |
||
299 | |||
300 | /** |
||
301 | * @param ReturnService $returnService |
||
302 | * |
||
303 | * @return Shipment |
||
304 | */ |
||
305 | public function setReturnService(ReturnService $returnService) |
||
311 | |||
312 | /** |
||
313 | * @return ShipFrom |
||
314 | */ |
||
315 | public function getShipFrom() |
||
319 | |||
320 | /** |
||
321 | * @param ShipFrom $shipFrom |
||
322 | * |
||
323 | * @return Shipment |
||
324 | */ |
||
325 | public function setShipFrom(ShipFrom $shipFrom) |
||
331 | |||
332 | /** |
||
333 | * @return ShipTo |
||
334 | */ |
||
335 | public function getShipTo() |
||
339 | |||
340 | /** |
||
341 | * @param ShipTo $shipTo |
||
342 | * |
||
343 | * @return Shipment |
||
344 | */ |
||
345 | 3 | public function setShipTo(ShipTo $shipTo) |
|
351 | |||
352 | /** |
||
353 | * @return SoldTo |
||
354 | */ |
||
355 | public function getSoldTo() |
||
359 | |||
360 | /** |
||
361 | * @param SoldTo $soldTo |
||
362 | * |
||
363 | * @return Shipment |
||
364 | */ |
||
365 | public function setSoldTo(SoldTo $soldTo) |
||
371 | |||
372 | /** |
||
373 | * @return ShipmentServiceOptions |
||
374 | */ |
||
375 | public function getShipmentServiceOptions() |
||
379 | |||
380 | /** |
||
381 | * @param ShipmentServiceOptions $shipmentServiceOptions |
||
382 | * |
||
383 | * @return Shipment |
||
384 | */ |
||
385 | 3 | public function setShipmentServiceOptions(ShipmentServiceOptions $shipmentServiceOptions) |
|
391 | |||
392 | /** |
||
393 | * @return Shipper |
||
394 | */ |
||
395 | public function getShipper() |
||
399 | |||
400 | /** |
||
401 | * @param Shipper $shipper |
||
402 | * |
||
403 | * @return Shipment |
||
404 | */ |
||
405 | 3 | public function setShipper(Shipper $shipper) |
|
411 | |||
412 | /** |
||
413 | * @return PaymentInformation |
||
414 | */ |
||
415 | public function getPaymentInformation() |
||
419 | |||
420 | /** |
||
421 | * @param PaymentInformation $paymentInformation |
||
422 | * |
||
423 | * @return Shipment |
||
424 | */ |
||
425 | public function setPaymentInformation(PaymentInformation $paymentInformation) |
||
431 | |||
432 | /** |
||
433 | * If called, returned prices will include negotiated rates (discounts will be applied). |
||
434 | */ |
||
435 | public function showNegotiatedRates() |
||
440 | |||
441 | /** |
||
442 | * @return null|RateInformation |
||
443 | */ |
||
444 | public function getRateInformation() |
||
448 | |||
449 | /** |
||
450 | * @param RateInformation $rateInformation |
||
451 | * |
||
452 | * @return Shipment |
||
453 | */ |
||
454 | public function setRateInformation(RateInformation $rateInformation) |
||
460 | |||
461 | /** |
||
462 | * @return boolean |
||
463 | */ |
||
464 | public function getGoodsNotInFreeCirculationIndicator() |
||
468 | |||
469 | /** |
||
470 | * @param boolean $goodsNotInFreeCirculationIndicator |
||
471 | * @return Shipment |
||
472 | */ |
||
473 | public function setGoodsNotInFreeCirculationIndicator($goodsNotInFreeCirculationIndicator) |
||
479 | |||
480 | /** |
||
481 | * @return string |
||
482 | */ |
||
483 | public function getMovementReferenceNumber() |
||
487 | |||
488 | /** |
||
489 | * @param string $movementReferenceNumber |
||
490 | * @return Shipment |
||
491 | */ |
||
492 | public function setMovementReferenceNumber($movementReferenceNumber) |
||
498 | |||
499 | /** |
||
500 | * @return InvoiceLineTotal |
||
501 | */ |
||
502 | public function getInvoiceLineTotal() |
||
506 | |||
507 | /** |
||
508 | * @param InvoiceLineTotal $invoiceLineTotal |
||
509 | * @return Shipment |
||
510 | */ |
||
511 | public function setInvoiceLineTotal(InvoiceLineTotal $invoiceLineTotal) |
||
517 | |||
518 | /** |
||
519 | * @return string |
||
520 | */ |
||
521 | public function getNumOfPiecesInShipment() |
||
525 | |||
526 | /** |
||
527 | * @param string $numOfPiecesInShipment |
||
528 | * @return Shipment |
||
529 | */ |
||
530 | public function setNumOfPiecesInShipment($numOfPiecesInShipment) |
||
536 | |||
537 | /** |
||
538 | * @return DeliveryTimeInformation |
||
539 | */ |
||
540 | public function getDeliveryTimeInformation() |
||
544 | |||
545 | /** |
||
546 | * @param DeliveryTimeInformation $deliveryTimeInformation |
||
547 | */ |
||
548 | public function setDeliveryTimeInformation(DeliveryTimeInformation $deliveryTimeInformation) |
||
552 | |||
553 | /** |
||
554 | * @return ShipmentTotalWeight |
||
555 | */ |
||
556 | public function getShipmentTotalWeight() |
||
560 | |||
561 | /** |
||
562 | * @param ShipmentTotalWeight $shipmentTotalWeight |
||
563 | */ |
||
564 | public function setShipmentTotalWeight(ShipmentTotalWeight $shipmentTotalWeight) |
||
568 | } |
||
569 |