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 | const USPS_ENDORSEMENT_RETURN_SERVICE = 1; |
||
9 | const USPS_ENDORSEMENT_FORWARDING_SERVICE = 2; |
||
10 | const USPS_ENDORSEMENT_ADDRESS_SERVICE = 3; |
||
11 | const USPS_ENDORSEMENT_CHANGE_SERVICE = 4; |
||
12 | const USPS_ENDORSEMENT_NO_SERVICE = 5; |
||
13 | /** |
||
14 | * @var PaymentInformation |
||
15 | */ |
||
16 | private $paymentInformation; |
||
17 | |||
18 | /** |
||
19 | * @var ItemizedPaymentInformation |
||
20 | */ |
||
21 | private $itemizedPaymentInformation; |
||
22 | |||
23 | /** |
||
24 | * @var RateInformation |
||
25 | */ |
||
26 | private $rateInformation; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | private $description; |
||
32 | |||
33 | /** |
||
34 | * @var Shipper |
||
35 | */ |
||
36 | private $shipper; |
||
37 | |||
38 | /** |
||
39 | * @var ShipTo; |
||
40 | */ |
||
41 | private $shipTo; |
||
42 | |||
43 | /** |
||
44 | * @var SoldTo |
||
45 | */ |
||
46 | private $soldTo; |
||
47 | |||
48 | /** |
||
49 | * @var ShipFrom |
||
50 | */ |
||
51 | private $shipFrom; |
||
52 | |||
53 | /** |
||
54 | * @var AlternateDeliveryAddress |
||
55 | */ |
||
56 | private $alternateDeliveryAddress; |
||
57 | |||
58 | /** |
||
59 | * @var ShipmentIndicationType |
||
60 | */ |
||
61 | private $shipmentIndicationType; |
||
62 | |||
63 | /** |
||
64 | * @var Service |
||
65 | */ |
||
66 | private $service; |
||
67 | |||
68 | /** |
||
69 | * @var ReturnService |
||
70 | */ |
||
71 | private $returnService; |
||
72 | |||
73 | /** |
||
74 | * @var bool |
||
75 | */ |
||
76 | private $documentsOnly; |
||
77 | |||
78 | /** |
||
79 | * @var Package[] |
||
80 | */ |
||
81 | private $packages = []; |
||
82 | |||
83 | /** |
||
84 | * @var ReferenceNumber |
||
85 | */ |
||
86 | private $referenceNumber; |
||
87 | |||
88 | /** |
||
89 | * @var ReferenceNumber |
||
90 | */ |
||
91 | private $referenceNumber2; |
||
92 | |||
93 | /** |
||
94 | * @var ShipmentServiceOptions |
||
95 | */ |
||
96 | private $shipmentServiceOptions; |
||
97 | |||
98 | /** |
||
99 | * @var bool |
||
100 | */ |
||
101 | private $goodsNotInFreeCirculationIndicator; |
||
102 | |||
103 | /** |
||
104 | * @var string |
||
105 | */ |
||
106 | private $movementReferenceNumber; |
||
107 | |||
108 | /** |
||
109 | * @var InvoiceLineTotal |
||
110 | */ |
||
111 | private $invoiceLineTotal; |
||
112 | |||
113 | /** |
||
114 | * @var ShipmentTotalWeight |
||
115 | */ |
||
116 | private $shipmentTotalWeight; |
||
117 | |||
118 | /** |
||
119 | * @var string |
||
120 | */ |
||
121 | private $numOfPiecesInShipment; |
||
122 | |||
123 | /** |
||
124 | * @var DeliveryTimeInformation |
||
125 | */ |
||
126 | private $deliveryTimeInformation; |
||
127 | /** |
||
128 | * @var bool |
||
129 | */ |
||
130 | private $taxInformationIndicator; |
||
131 | |||
132 | /** |
||
133 | * Required for Mail Innovations. |
||
134 | * |
||
135 | * @var integer |
||
136 | */ |
||
137 | private $uspsEndorsement; |
||
138 | |||
139 | /** |
||
140 | * Required for Mail Innovations. |
||
141 | * |
||
142 | * @var string |
||
143 | */ |
||
144 | private $packageId; |
||
145 | |||
146 | public function __construct() |
||
155 | |||
156 | /** |
||
157 | * @return ShipmentIndicationType |
||
158 | */ |
||
159 | public function getShipmentIndicationType() |
||
163 | |||
164 | /** |
||
165 | * @param ShipmentIndicationType $shipmentIndicationType |
||
166 | */ |
||
167 | public function setShipmentIndicationType(ShipmentIndicationType $shipmentIndicationType) |
||
171 | |||
172 | /** |
||
173 | * @return AlternateDeliveryAddress |
||
174 | */ |
||
175 | public function getAlternateDeliveryAddress() |
||
179 | |||
180 | /** |
||
181 | * @param AlternateDeliveryAddress $alternateDeliveryAddress |
||
182 | */ |
||
183 | public function setAlternateDeliveryAddress(AlternateDeliveryAddress $alternateDeliveryAddress) |
||
187 | |||
188 | /** |
||
189 | * @param Package $package |
||
190 | * |
||
191 | * @return Shipment |
||
192 | */ |
||
193 | public function addPackage(Package $package) |
||
201 | |||
202 | /** |
||
203 | * @return string |
||
204 | */ |
||
205 | public function getDescription() |
||
209 | |||
210 | /** |
||
211 | * @param string $description |
||
212 | * |
||
213 | * @return Shipment |
||
214 | */ |
||
215 | public function setDescription($description) |
||
221 | |||
222 | /** |
||
223 | * @param ReferenceNumber $referenceNumber |
||
224 | * |
||
225 | * @return Shipment |
||
226 | */ |
||
227 | public function setReferenceNumber(ReferenceNumber $referenceNumber) |
||
233 | |||
234 | /** |
||
235 | * @param ReferenceNumber $referenceNumber |
||
236 | * |
||
237 | * @return Shipment |
||
238 | */ |
||
239 | public function setReferenceNumber2(ReferenceNumber $referenceNumber) |
||
245 | |||
246 | /** |
||
247 | * @return ReferenceNumber |
||
248 | */ |
||
249 | public function getReferenceNumber() |
||
253 | |||
254 | /** |
||
255 | * @return ReferenceNumber |
||
256 | */ |
||
257 | public function getReferenceNumber2() |
||
261 | |||
262 | /** |
||
263 | * @return bool |
||
264 | */ |
||
265 | public function getDocumentsOnly() |
||
269 | |||
270 | /** |
||
271 | * @param bool $documentsOnly |
||
272 | * |
||
273 | * @return Shipment |
||
274 | */ |
||
275 | public function setDocumentsOnly($documentsOnly) |
||
281 | |||
282 | /** |
||
283 | * @return Package[] |
||
284 | */ |
||
285 | public function getPackages() |
||
289 | |||
290 | /** |
||
291 | * @param Package[] $packages |
||
292 | * |
||
293 | * @return Shipment |
||
294 | */ |
||
295 | public function setPackages(array $packages) |
||
301 | |||
302 | /** |
||
303 | * @return Service |
||
304 | */ |
||
305 | public function getService() |
||
309 | |||
310 | /** |
||
311 | * @param Service $service |
||
312 | * |
||
313 | * @return Shipment |
||
314 | */ |
||
315 | public function setService(Service $service) |
||
321 | |||
322 | /** |
||
323 | * @return ReturnService |
||
324 | */ |
||
325 | public function getReturnService() |
||
329 | |||
330 | /** |
||
331 | * @param ReturnService $returnService |
||
332 | * |
||
333 | * @return Shipment |
||
334 | */ |
||
335 | public function setReturnService(ReturnService $returnService) |
||
341 | |||
342 | /** |
||
343 | * @return ShipFrom |
||
344 | */ |
||
345 | public function getShipFrom() |
||
349 | |||
350 | /** |
||
351 | * @param ShipFrom $shipFrom |
||
352 | * |
||
353 | * @return Shipment |
||
354 | */ |
||
355 | public function setShipFrom(ShipFrom $shipFrom) |
||
361 | |||
362 | /** |
||
363 | * @return ShipTo |
||
364 | */ |
||
365 | public function getShipTo() |
||
369 | |||
370 | /** |
||
371 | * @param ShipTo $shipTo |
||
372 | * |
||
373 | * @return Shipment |
||
374 | */ |
||
375 | public function setShipTo(ShipTo $shipTo) |
||
381 | |||
382 | /** |
||
383 | * @return SoldTo |
||
384 | */ |
||
385 | public function getSoldTo() |
||
389 | |||
390 | /** |
||
391 | * @param SoldTo $soldTo |
||
392 | * |
||
393 | * @return Shipment |
||
394 | */ |
||
395 | public function setSoldTo(SoldTo $soldTo) |
||
401 | |||
402 | /** |
||
403 | * @return ShipmentServiceOptions |
||
404 | */ |
||
405 | public function getShipmentServiceOptions() |
||
409 | |||
410 | /** |
||
411 | * @param ShipmentServiceOptions $shipmentServiceOptions |
||
412 | * |
||
413 | * @return Shipment |
||
414 | */ |
||
415 | public function setShipmentServiceOptions(ShipmentServiceOptions $shipmentServiceOptions) |
||
421 | |||
422 | /** |
||
423 | * @return Shipper |
||
424 | */ |
||
425 | public function getShipper() |
||
429 | |||
430 | /** |
||
431 | * @param Shipper $shipper |
||
432 | * |
||
433 | * @return Shipment |
||
434 | */ |
||
435 | public function setShipper(Shipper $shipper) |
||
441 | |||
442 | /** |
||
443 | * @return PaymentInformation |
||
444 | */ |
||
445 | public function getPaymentInformation() |
||
449 | |||
450 | /** |
||
451 | * @param PaymentInformation $paymentInformation |
||
452 | * |
||
453 | * @return Shipment |
||
454 | */ |
||
455 | public function setPaymentInformation(PaymentInformation $paymentInformation) |
||
461 | |||
462 | /** |
||
463 | * @return ItemizedPaymentInformation |
||
464 | */ |
||
465 | public function getItemizedPaymentInformation() |
||
469 | |||
470 | /** |
||
471 | * @param ItemizedPaymentInformation $itemizedPaymentInformation |
||
472 | * |
||
473 | * @return Shipment |
||
474 | */ |
||
475 | public function setItemizedPaymentInformation(ItemizedPaymentInformation $itemizedPaymentInformation) |
||
481 | |||
482 | /** |
||
483 | * If called, returned prices will include negotiated rates (discounts will be applied). |
||
484 | */ |
||
485 | public function showNegotiatedRates() |
||
490 | |||
491 | /** |
||
492 | * @return null|RateInformation |
||
493 | */ |
||
494 | public function getRateInformation() |
||
498 | |||
499 | /** |
||
500 | * @param RateInformation $rateInformation |
||
501 | * |
||
502 | * @return Shipment |
||
503 | */ |
||
504 | public function setRateInformation(RateInformation $rateInformation) |
||
510 | |||
511 | /** |
||
512 | * @return boolean |
||
513 | */ |
||
514 | public function getGoodsNotInFreeCirculationIndicator() |
||
518 | |||
519 | /** |
||
520 | * @param boolean $goodsNotInFreeCirculationIndicator |
||
521 | * @return Shipment |
||
522 | */ |
||
523 | public function setGoodsNotInFreeCirculationIndicator($goodsNotInFreeCirculationIndicator) |
||
529 | |||
530 | /** |
||
531 | * @return string |
||
532 | */ |
||
533 | public function getMovementReferenceNumber() |
||
537 | |||
538 | /** |
||
539 | * @param string $movementReferenceNumber |
||
540 | * @return Shipment |
||
541 | */ |
||
542 | public function setMovementReferenceNumber($movementReferenceNumber) |
||
548 | |||
549 | /** |
||
550 | * @return InvoiceLineTotal |
||
551 | */ |
||
552 | public function getInvoiceLineTotal() |
||
556 | |||
557 | /** |
||
558 | * @param InvoiceLineTotal $invoiceLineTotal |
||
559 | * @return Shipment |
||
560 | */ |
||
561 | public function setInvoiceLineTotal(InvoiceLineTotal $invoiceLineTotal) |
||
567 | |||
568 | /** |
||
569 | * @return string |
||
570 | */ |
||
571 | public function getNumOfPiecesInShipment() |
||
575 | |||
576 | /** |
||
577 | * @param string $numOfPiecesInShipment |
||
578 | * @return Shipment |
||
579 | */ |
||
580 | public function setNumOfPiecesInShipment($numOfPiecesInShipment) |
||
586 | |||
587 | /** |
||
588 | * @return DeliveryTimeInformation |
||
589 | */ |
||
590 | public function getDeliveryTimeInformation() |
||
594 | |||
595 | /** |
||
596 | * @param DeliveryTimeInformation $deliveryTimeInformation |
||
597 | */ |
||
598 | public function setDeliveryTimeInformation(DeliveryTimeInformation $deliveryTimeInformation) |
||
602 | |||
603 | /** |
||
604 | * @return ShipmentTotalWeight |
||
605 | */ |
||
606 | public function getShipmentTotalWeight() |
||
610 | |||
611 | /** |
||
612 | * @param ShipmentTotalWeight $shipmentTotalWeight |
||
613 | */ |
||
614 | public function setShipmentTotalWeight(ShipmentTotalWeight $shipmentTotalWeight) |
||
618 | |||
619 | public function getTaxInformationIndicator(): bool |
||
623 | |||
624 | /** |
||
625 | * If called, returned prices will include Tax Information |
||
626 | */ |
||
627 | public function setTaxInformationIndicator(bool $taxInformationIndicator): self |
||
633 | |||
634 | /** |
||
635 | * @return int $uspsEndorsement |
||
636 | */ |
||
637 | public function getUSPSEndorsement() |
||
641 | |||
642 | /** |
||
643 | * @param integer $uspsEndorsement |
||
644 | * |
||
645 | * @return Shipment |
||
646 | */ |
||
647 | public function setUSPSEndorsement($uspsEndorsement) |
||
653 | |||
654 | /** |
||
655 | * @return int $packageId |
||
656 | */ |
||
657 | public function getPackageId() |
||
661 | |||
662 | /** |
||
663 | * @param integer $packageId |
||
664 | * |
||
665 | * @return Shipment |
||
666 | */ |
||
667 | public function setPackageId($packageId) |
||
673 | } |
||
674 |
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.