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 | const USPS_ENDORSEMENT_RETURN_SERVICE = 1; |
||
8 | const USPS_ENDORSEMENT_FORWARDING_SERVICE = 2; |
||
9 | const USPS_ENDORSEMENT_ADDRESS_SERVICE = 3; |
||
10 | const USPS_ENDORSEMENT_CHANGE_SERVICE = 4; |
||
11 | const USPS_ENDORSEMENT_NO_SERVICE = 5; |
||
12 | /** |
||
13 | * @var PaymentInformation |
||
14 | */ |
||
15 | private $paymentInformation; |
||
16 | |||
17 | /** |
||
18 | * @var ItemizedPaymentInformation |
||
19 | */ |
||
20 | private $itemizedPaymentInformation; |
||
21 | |||
22 | /** |
||
23 | * @var RateInformation |
||
24 | */ |
||
25 | private $rateInformation; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $description; |
||
31 | |||
32 | /** |
||
33 | * @var Shipper |
||
34 | */ |
||
35 | private $shipper; |
||
36 | |||
37 | /** |
||
38 | * @var ShipTo; |
||
39 | */ |
||
40 | private $shipTo; |
||
41 | |||
42 | /** |
||
43 | * @var SoldTo |
||
44 | */ |
||
45 | private $soldTo; |
||
46 | |||
47 | /** |
||
48 | * @var ShipFrom |
||
49 | */ |
||
50 | private $shipFrom; |
||
51 | |||
52 | /** |
||
53 | * @var AlternateDeliveryAddress |
||
54 | */ |
||
55 | private $alternateDeliveryAddress; |
||
56 | |||
57 | /** |
||
58 | * @var ShipmentIndicationType |
||
59 | */ |
||
60 | private $shipmentIndicationType; |
||
61 | |||
62 | /** |
||
63 | * @var Service |
||
64 | */ |
||
65 | private $service; |
||
66 | |||
67 | /** |
||
68 | * @var ReturnService |
||
69 | */ |
||
70 | private $returnService; |
||
71 | |||
72 | /** |
||
73 | * @var bool |
||
74 | */ |
||
75 | private $documentsOnly; |
||
76 | |||
77 | /** |
||
78 | * @var Package[] |
||
79 | */ |
||
80 | private $packages = []; |
||
81 | |||
82 | /** |
||
83 | * @var ReferenceNumber |
||
84 | */ |
||
85 | private $referenceNumber; |
||
86 | |||
87 | /** |
||
88 | * @var ReferenceNumber |
||
89 | */ |
||
90 | private $referenceNumber2; |
||
91 | |||
92 | /** |
||
93 | * @var ShipmentServiceOptions |
||
94 | */ |
||
95 | private $shipmentServiceOptions; |
||
96 | |||
97 | /** |
||
98 | * @var bool |
||
99 | */ |
||
100 | private $goodsNotInFreeCirculationIndicator; |
||
101 | |||
102 | /** |
||
103 | * @var string |
||
104 | */ |
||
105 | private $movementReferenceNumber; |
||
106 | |||
107 | /** |
||
108 | * @var InvoiceLineTotal |
||
109 | */ |
||
110 | private $invoiceLineTotal; |
||
111 | |||
112 | /** |
||
113 | * @var ShipmentTotalWeight |
||
114 | */ |
||
115 | private $shipmentTotalWeight; |
||
116 | |||
117 | /** |
||
118 | * @var string |
||
119 | */ |
||
120 | private $numOfPiecesInShipment; |
||
121 | |||
122 | /** |
||
123 | * @var DeliveryTimeInformation |
||
124 | */ |
||
125 | private $deliveryTimeInformation; |
||
126 | /** |
||
127 | * @var bool |
||
128 | */ |
||
129 | private $taxInformationIndicator; |
||
130 | |||
131 | /** |
||
132 | * Required for Mail Innovations. |
||
133 | * @var integer |
||
134 | */ |
||
135 | private $uspsEndorsement; |
||
136 | |||
137 | /** |
||
138 | * Required for Mail Innovations. |
||
139 | * @var string |
||
140 | */ |
||
141 | private $packageId; |
||
142 | |||
143 | public function __construct() |
||
152 | |||
153 | /** |
||
154 | * @return ShipmentIndicationType |
||
155 | */ |
||
156 | public function getShipmentIndicationType() |
||
160 | |||
161 | /** |
||
162 | * @param ShipmentIndicationType $shipmentIndicationType |
||
163 | */ |
||
164 | public function setShipmentIndicationType(ShipmentIndicationType $shipmentIndicationType) |
||
168 | |||
169 | /** |
||
170 | * @return AlternateDeliveryAddress |
||
171 | */ |
||
172 | public function getAlternateDeliveryAddress() |
||
176 | |||
177 | /** |
||
178 | * @param AlternateDeliveryAddress $alternateDeliveryAddress |
||
179 | */ |
||
180 | public function setAlternateDeliveryAddress(AlternateDeliveryAddress $alternateDeliveryAddress) |
||
184 | |||
185 | /** |
||
186 | * @param Package $package |
||
187 | * |
||
188 | * @return Shipment |
||
189 | */ |
||
190 | public function addPackage(Package $package) |
||
198 | |||
199 | /** |
||
200 | * @return string |
||
201 | */ |
||
202 | public function getDescription() |
||
206 | |||
207 | /** |
||
208 | * @param string $description |
||
209 | * |
||
210 | * @return Shipment |
||
211 | */ |
||
212 | public function setDescription($description) |
||
218 | |||
219 | /** |
||
220 | * @param ReferenceNumber $referenceNumber |
||
221 | * |
||
222 | * @return Shipment |
||
223 | */ |
||
224 | public function setReferenceNumber(ReferenceNumber $referenceNumber) |
||
230 | |||
231 | /** |
||
232 | * @param ReferenceNumber $referenceNumber |
||
233 | * |
||
234 | * @return Shipment |
||
235 | */ |
||
236 | public function setReferenceNumber2(ReferenceNumber $referenceNumber) |
||
242 | |||
243 | /** |
||
244 | * @return ReferenceNumber |
||
245 | */ |
||
246 | public function getReferenceNumber() |
||
250 | |||
251 | /** |
||
252 | * @return ReferenceNumber |
||
253 | */ |
||
254 | public function getReferenceNumber2() |
||
258 | |||
259 | /** |
||
260 | * @return bool |
||
261 | */ |
||
262 | public function getDocumentsOnly() |
||
266 | |||
267 | /** |
||
268 | * @param bool $documentsOnly |
||
269 | * |
||
270 | * @return Shipment |
||
271 | */ |
||
272 | public function setDocumentsOnly($documentsOnly) |
||
278 | |||
279 | /** |
||
280 | * @return Package[] |
||
281 | */ |
||
282 | public function getPackages() |
||
286 | |||
287 | /** |
||
288 | * @param Package[] $packages |
||
289 | * |
||
290 | * @return Shipment |
||
291 | */ |
||
292 | public function setPackages(array $packages) |
||
298 | |||
299 | /** |
||
300 | * @return Service |
||
301 | */ |
||
302 | public function getService() |
||
306 | |||
307 | /** |
||
308 | * @param Service $service |
||
309 | * |
||
310 | * @return Shipment |
||
311 | */ |
||
312 | public function setService(Service $service) |
||
318 | |||
319 | /** |
||
320 | * @return ReturnService |
||
321 | */ |
||
322 | public function getReturnService() |
||
326 | |||
327 | /** |
||
328 | * @param ReturnService $returnService |
||
329 | * |
||
330 | * @return Shipment |
||
331 | */ |
||
332 | public function setReturnService(ReturnService $returnService) |
||
338 | |||
339 | /** |
||
340 | * @return ShipFrom |
||
341 | */ |
||
342 | public function getShipFrom() |
||
346 | |||
347 | /** |
||
348 | * @param ShipFrom $shipFrom |
||
349 | * |
||
350 | * @return Shipment |
||
351 | */ |
||
352 | public function setShipFrom(ShipFrom $shipFrom) |
||
358 | |||
359 | /** |
||
360 | * @return ShipTo |
||
361 | */ |
||
362 | public function getShipTo() |
||
366 | |||
367 | /** |
||
368 | * @param ShipTo $shipTo |
||
369 | * |
||
370 | * @return Shipment |
||
371 | */ |
||
372 | public function setShipTo(ShipTo $shipTo) |
||
378 | |||
379 | /** |
||
380 | * @return SoldTo |
||
381 | */ |
||
382 | public function getSoldTo() |
||
386 | |||
387 | /** |
||
388 | * @param SoldTo $soldTo |
||
389 | * |
||
390 | * @return Shipment |
||
391 | */ |
||
392 | public function setSoldTo(SoldTo $soldTo) |
||
398 | |||
399 | /** |
||
400 | * @return ShipmentServiceOptions |
||
401 | */ |
||
402 | public function getShipmentServiceOptions() |
||
406 | |||
407 | /** |
||
408 | * @param ShipmentServiceOptions $shipmentServiceOptions |
||
409 | * |
||
410 | * @return Shipment |
||
411 | */ |
||
412 | public function setShipmentServiceOptions(ShipmentServiceOptions $shipmentServiceOptions) |
||
418 | |||
419 | /** |
||
420 | * @return Shipper |
||
421 | */ |
||
422 | public function getShipper() |
||
426 | |||
427 | /** |
||
428 | * @param Shipper $shipper |
||
429 | * |
||
430 | * @return Shipment |
||
431 | */ |
||
432 | public function setShipper(Shipper $shipper) |
||
438 | |||
439 | /** |
||
440 | * @return PaymentInformation |
||
441 | */ |
||
442 | public function getPaymentInformation() |
||
446 | |||
447 | /** |
||
448 | * @param PaymentInformation $paymentInformation |
||
449 | * |
||
450 | * @return Shipment |
||
451 | */ |
||
452 | public function setPaymentInformation(PaymentInformation $paymentInformation) |
||
458 | |||
459 | /** |
||
460 | * @return ItemizedPaymentInformation |
||
461 | */ |
||
462 | public function getItemizedPaymentInformation() |
||
466 | |||
467 | /** |
||
468 | * @param ItemizedPaymentInformation $itemizedPaymentInformation |
||
469 | * |
||
470 | * @return Shipment |
||
471 | */ |
||
472 | public function setItemizedPaymentInformation(ItemizedPaymentInformation $itemizedPaymentInformation) |
||
478 | |||
479 | /** |
||
480 | * If called, returned prices will include negotiated rates (discounts will be applied). |
||
481 | */ |
||
482 | public function showNegotiatedRates() |
||
487 | |||
488 | /** |
||
489 | * @return null|RateInformation |
||
490 | */ |
||
491 | public function getRateInformation() |
||
495 | |||
496 | /** |
||
497 | * @param RateInformation $rateInformation |
||
498 | * |
||
499 | * @return Shipment |
||
500 | */ |
||
501 | public function setRateInformation(RateInformation $rateInformation) |
||
507 | |||
508 | /** |
||
509 | * @return boolean |
||
510 | */ |
||
511 | public function getGoodsNotInFreeCirculationIndicator() |
||
515 | |||
516 | /** |
||
517 | * @param boolean $goodsNotInFreeCirculationIndicator |
||
518 | * @return Shipment |
||
519 | */ |
||
520 | public function setGoodsNotInFreeCirculationIndicator($goodsNotInFreeCirculationIndicator) |
||
526 | |||
527 | /** |
||
528 | * @return string |
||
529 | */ |
||
530 | public function getMovementReferenceNumber() |
||
534 | |||
535 | /** |
||
536 | * @param string $movementReferenceNumber |
||
537 | * @return Shipment |
||
538 | */ |
||
539 | public function setMovementReferenceNumber($movementReferenceNumber) |
||
545 | |||
546 | /** |
||
547 | * @return InvoiceLineTotal |
||
548 | */ |
||
549 | public function getInvoiceLineTotal() |
||
553 | |||
554 | /** |
||
555 | * @param InvoiceLineTotal $invoiceLineTotal |
||
556 | * @return Shipment |
||
557 | */ |
||
558 | public function setInvoiceLineTotal(InvoiceLineTotal $invoiceLineTotal) |
||
564 | |||
565 | /** |
||
566 | * @return string |
||
567 | */ |
||
568 | public function getNumOfPiecesInShipment() |
||
572 | |||
573 | /** |
||
574 | * @param string $numOfPiecesInShipment |
||
575 | * @return Shipment |
||
576 | */ |
||
577 | public function setNumOfPiecesInShipment($numOfPiecesInShipment) |
||
583 | |||
584 | /** |
||
585 | * @return DeliveryTimeInformation |
||
586 | */ |
||
587 | public function getDeliveryTimeInformation() |
||
591 | |||
592 | /** |
||
593 | * @param DeliveryTimeInformation $deliveryTimeInformation |
||
594 | */ |
||
595 | public function setDeliveryTimeInformation(DeliveryTimeInformation $deliveryTimeInformation) |
||
599 | |||
600 | /** |
||
601 | * @return ShipmentTotalWeight |
||
602 | */ |
||
603 | public function getShipmentTotalWeight() |
||
607 | |||
608 | /** |
||
609 | * @param ShipmentTotalWeight $shipmentTotalWeight |
||
610 | */ |
||
611 | public function setShipmentTotalWeight(ShipmentTotalWeight $shipmentTotalWeight) |
||
615 | |||
616 | public function getTaxInformationIndicator(): bool |
||
620 | |||
621 | /** |
||
622 | * If called, returned prices will include Tax Information |
||
623 | */ |
||
624 | public function setTaxInformationIndicator(bool $taxInformationIndicator): self |
||
630 | |||
631 | /** |
||
632 | * @return int $uspsEndorsement |
||
633 | */ |
||
634 | public function getUSPSEndorsement() |
||
638 | |||
639 | /** |
||
640 | * @param integer $uspsEndorsement |
||
641 | * |
||
642 | * @return Shipment |
||
643 | */ |
||
644 | public function setUSPSEndorsement($uspsEndorsement) |
||
650 | |||
651 | /** |
||
652 | * @return int $packageId |
||
653 | */ |
||
654 | public function getPackageId() |
||
658 | |||
659 | /** |
||
660 | * @param integer $packageId |
||
661 | * |
||
662 | * @return Shipment |
||
663 | */ |
||
664 | public function setPackageId($packageId) |
||
670 | } |
||
671 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.