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 | * @var bool |
||
| 123 | */ |
||
| 124 | private $taxInformationIndicator; |
||
| 125 | |||
| 126 | public function __construct() |
||
| 127 | { |
||
| 128 | $this->setShipper(new Shipper()); |
||
| 129 | $this->setShipTo(new ShipTo()); |
||
| 130 | $this->setShipmentServiceOptions(new ShipmentServiceOptions()); |
||
| 131 | $this->setService(new Service()); |
||
| 132 | $this->rateInformation = null; |
||
| 133 | $this->taxInformationIndicator = false; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @return ShipmentIndicationType |
||
| 138 | */ |
||
| 139 | public function getShipmentIndicationType() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @param ShipmentIndicationType $shipmentIndicationType |
||
| 146 | */ |
||
| 147 | public function setShipmentIndicationType(ShipmentIndicationType $shipmentIndicationType) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @return AlternateDeliveryAddress |
||
| 154 | */ |
||
| 155 | public function getAlternateDeliveryAddress() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param AlternateDeliveryAddress $alternateDeliveryAddress |
||
| 162 | */ |
||
| 163 | public function setAlternateDeliveryAddress(AlternateDeliveryAddress $alternateDeliveryAddress) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param Package $package |
||
| 170 | * |
||
| 171 | * @return Shipment |
||
| 172 | */ |
||
| 173 | public function addPackage(Package $package) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @return string |
||
| 184 | */ |
||
| 185 | public function getDescription() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param string $description |
||
| 192 | * |
||
| 193 | * @return Shipment |
||
| 194 | */ |
||
| 195 | public function setDescription($description) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param ReferenceNumber $referenceNumber |
||
| 204 | * |
||
| 205 | * @return Shipment |
||
| 206 | */ |
||
| 207 | public function setReferenceNumber(ReferenceNumber $referenceNumber) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param ReferenceNumber $referenceNumber |
||
| 216 | * |
||
| 217 | * @return Shipment |
||
| 218 | */ |
||
| 219 | public function setReferenceNumber2(ReferenceNumber $referenceNumber) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @return ReferenceNumber |
||
| 228 | */ |
||
| 229 | public function getReferenceNumber() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @return ReferenceNumber |
||
| 236 | */ |
||
| 237 | public function getReferenceNumber2() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | public function getDocumentsOnly() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param bool $documentsOnly |
||
| 252 | * |
||
| 253 | * @return Shipment |
||
| 254 | */ |
||
| 255 | public function setDocumentsOnly($documentsOnly) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @return Package[] |
||
| 264 | */ |
||
| 265 | public function getPackages() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param Package[] $packages |
||
| 272 | * |
||
| 273 | * @return Shipment |
||
| 274 | */ |
||
| 275 | public function setPackages(array $packages) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @return Service |
||
| 284 | */ |
||
| 285 | public function getService() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param Service $service |
||
| 292 | * |
||
| 293 | * @return Shipment |
||
| 294 | */ |
||
| 295 | public function setService(Service $service) |
||
| 296 | { |
||
| 297 | $this->service = $service; |
||
| 298 | |||
| 299 | return $this; |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @return ReturnService |
||
| 304 | */ |
||
| 305 | public function getReturnService() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param ReturnService $returnService |
||
| 312 | * |
||
| 313 | * @return Shipment |
||
| 314 | */ |
||
| 315 | public function setReturnService(ReturnService $returnService) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @return ShipFrom |
||
| 324 | */ |
||
| 325 | public function getShipFrom() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @param ShipFrom $shipFrom |
||
| 332 | * |
||
| 333 | * @return Shipment |
||
| 334 | */ |
||
| 335 | public function setShipFrom(ShipFrom $shipFrom) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @return ShipTo |
||
| 344 | */ |
||
| 345 | public function getShipTo() |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @param ShipTo $shipTo |
||
| 352 | * |
||
| 353 | * @return Shipment |
||
| 354 | */ |
||
| 355 | public function setShipTo(ShipTo $shipTo) |
||
| 356 | { |
||
| 357 | $this->shipTo = $shipTo; |
||
| 358 | |||
| 359 | return $this; |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @return SoldTo |
||
| 364 | */ |
||
| 365 | public function getSoldTo() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @param SoldTo $soldTo |
||
| 372 | * |
||
| 373 | * @return Shipment |
||
| 374 | */ |
||
| 375 | public function setSoldTo(SoldTo $soldTo) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @return ShipmentServiceOptions |
||
| 384 | */ |
||
| 385 | public function getShipmentServiceOptions() |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @param ShipmentServiceOptions $shipmentServiceOptions |
||
| 392 | * |
||
| 393 | * @return Shipment |
||
| 394 | */ |
||
| 395 | public function setShipmentServiceOptions(ShipmentServiceOptions $shipmentServiceOptions) |
||
| 396 | { |
||
| 397 | $this->shipmentServiceOptions = $shipmentServiceOptions; |
||
| 398 | |||
| 399 | return $this; |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @return Shipper |
||
| 404 | */ |
||
| 405 | public function getShipper() |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @param Shipper $shipper |
||
| 412 | * |
||
| 413 | * @return Shipment |
||
| 414 | */ |
||
| 415 | public function setShipper(Shipper $shipper) |
||
| 416 | { |
||
| 417 | $this->shipper = $shipper; |
||
| 418 | |||
| 419 | return $this; |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @return PaymentInformation |
||
| 424 | */ |
||
| 425 | public function getPaymentInformation() |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @param PaymentInformation $paymentInformation |
||
| 432 | * |
||
| 433 | * @return Shipment |
||
| 434 | */ |
||
| 435 | public function setPaymentInformation(PaymentInformation $paymentInformation) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @return ItemizedPaymentInformation |
||
| 444 | */ |
||
| 445 | public function getItemizedPaymentInformation() |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @param ItemizedPaymentInformation $itemizedPaymentInformation |
||
| 452 | * |
||
| 453 | * @return Shipment |
||
| 454 | */ |
||
| 455 | public function setItemizedPaymentInformation(ItemizedPaymentInformation $itemizedPaymentInformation) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * If called, returned prices will include negotiated rates (discounts will be applied). |
||
| 464 | */ |
||
| 465 | public function showNegotiatedRates() |
||
| 470 | |||
| 471 | /** |
||
| 472 | * @return null|RateInformation |
||
| 473 | */ |
||
| 474 | public function getRateInformation() |
||
| 478 | |||
| 479 | /** |
||
| 480 | * @param RateInformation $rateInformation |
||
| 481 | * |
||
| 482 | * @return Shipment |
||
| 483 | */ |
||
| 484 | public function setRateInformation(RateInformation $rateInformation) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * @return boolean |
||
| 493 | */ |
||
| 494 | public function getGoodsNotInFreeCirculationIndicator() |
||
| 498 | |||
| 499 | /** |
||
| 500 | * @param boolean $goodsNotInFreeCirculationIndicator |
||
| 501 | * @return Shipment |
||
| 502 | */ |
||
| 503 | public function setGoodsNotInFreeCirculationIndicator($goodsNotInFreeCirculationIndicator) |
||
| 509 | |||
| 510 | /** |
||
| 511 | * @return string |
||
| 512 | */ |
||
| 513 | public function getMovementReferenceNumber() |
||
| 517 | |||
| 518 | /** |
||
| 519 | * @param string $movementReferenceNumber |
||
| 520 | * @return Shipment |
||
| 521 | */ |
||
| 522 | public function setMovementReferenceNumber($movementReferenceNumber) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * @return InvoiceLineTotal |
||
| 531 | */ |
||
| 532 | public function getInvoiceLineTotal() |
||
| 536 | |||
| 537 | /** |
||
| 538 | * @param InvoiceLineTotal $invoiceLineTotal |
||
| 539 | * @return Shipment |
||
| 540 | */ |
||
| 541 | public function setInvoiceLineTotal(InvoiceLineTotal $invoiceLineTotal) |
||
| 547 | |||
| 548 | /** |
||
| 549 | * @return string |
||
| 550 | */ |
||
| 551 | public function getNumOfPiecesInShipment() |
||
| 555 | |||
| 556 | /** |
||
| 557 | * @param string $numOfPiecesInShipment |
||
| 558 | * @return Shipment |
||
| 559 | */ |
||
| 560 | public function setNumOfPiecesInShipment($numOfPiecesInShipment) |
||
| 566 | |||
| 567 | /** |
||
| 568 | * @return DeliveryTimeInformation |
||
| 569 | */ |
||
| 570 | public function getDeliveryTimeInformation() |
||
| 574 | |||
| 575 | /** |
||
| 576 | * @param DeliveryTimeInformation $deliveryTimeInformation |
||
| 577 | */ |
||
| 578 | public function setDeliveryTimeInformation(DeliveryTimeInformation $deliveryTimeInformation) |
||
| 582 | |||
| 583 | /** |
||
| 584 | * @return ShipmentTotalWeight |
||
| 585 | */ |
||
| 586 | public function getShipmentTotalWeight() |
||
| 590 | |||
| 591 | /** |
||
| 592 | * @param ShipmentTotalWeight $shipmentTotalWeight |
||
| 593 | */ |
||
| 594 | public function setShipmentTotalWeight(ShipmentTotalWeight $shipmentTotalWeight) |
||
| 598 | |||
| 599 | public function getTaxInformationIndicator(): bool |
||
| 600 | { |
||
| 601 | return $this->taxInformationIndicator; |
||
| 602 | } |
||
| 603 | |||
| 604 | /** |
||
| 605 | * If called, returned prices will include Tax Information |
||
| 606 | */ |
||
| 607 | public function setTaxInformationIndicator(bool $taxInformationIndicator): self |
||
| 613 | } |
||
| 614 |