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 string |
||
| 104 | */ |
||
| 105 | private $numOfPiecesInShipment; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var DeliveryTimeInformation |
||
| 109 | */ |
||
| 110 | private $deliveryTimeInformation; |
||
| 111 | |||
| 112 | 3 | public function __construct() |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @return ShipmentIndicationType |
||
| 123 | */ |
||
| 124 | public function getShipmentIndicationType() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param ShipmentIndicationType $shipmentIndicationType |
||
| 131 | */ |
||
| 132 | public function setShipmentIndicationType(ShipmentIndicationType $shipmentIndicationType) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @return AlternateDeliveryAddress |
||
| 139 | */ |
||
| 140 | public function getAlternateDeliveryAddress() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param AlternateDeliveryAddress $alternateDeliveryAddress |
||
| 147 | */ |
||
| 148 | public function setAlternateDeliveryAddress(AlternateDeliveryAddress $alternateDeliveryAddress) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param Package $package |
||
| 155 | * |
||
| 156 | * @return Shipment |
||
| 157 | */ |
||
| 158 | public function addPackage(Package $package) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | public function getDescription() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param string $description |
||
| 177 | * |
||
| 178 | * @return Shipment |
||
| 179 | */ |
||
| 180 | public function setDescription($description) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param ReferenceNumber $referenceNumber |
||
| 189 | * |
||
| 190 | * @return Shipment |
||
| 191 | */ |
||
| 192 | public function setReferenceNumber(ReferenceNumber $referenceNumber) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param ReferenceNumber $referenceNumber |
||
| 201 | * |
||
| 202 | * @return Shipment |
||
| 203 | */ |
||
| 204 | public function setReferenceNumber2(ReferenceNumber $referenceNumber) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @return ReferenceNumber |
||
| 213 | */ |
||
| 214 | public function getReferenceNumber() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @return ReferenceNumber |
||
| 221 | */ |
||
| 222 | public function getReferenceNumber2() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @return bool |
||
| 229 | */ |
||
| 230 | public function getDocumentsOnly() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param bool $documentsOnly |
||
| 237 | * |
||
| 238 | * @return Shipment |
||
| 239 | */ |
||
| 240 | public function setDocumentsOnly($documentsOnly) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @return Package[] |
||
| 249 | */ |
||
| 250 | public function getPackages() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param Package[] $packages |
||
| 257 | * |
||
| 258 | * @return Shipment |
||
| 259 | */ |
||
| 260 | public function setPackages(array $packages) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return Service |
||
| 269 | */ |
||
| 270 | public function getService() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param Service $service |
||
| 277 | * |
||
| 278 | * @return Shipment |
||
| 279 | */ |
||
| 280 | 3 | public function setService(Service $service) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @return ReturnService |
||
| 289 | */ |
||
| 290 | public function getReturnService() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @param ReturnService $returnService |
||
| 297 | * |
||
| 298 | * @return Shipment |
||
| 299 | */ |
||
| 300 | public function setReturnService(ReturnService $returnService) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @return ShipFrom |
||
| 309 | */ |
||
| 310 | public function getShipFrom() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param ShipFrom $shipFrom |
||
| 317 | * |
||
| 318 | * @return Shipment |
||
| 319 | */ |
||
| 320 | public function setShipFrom(ShipFrom $shipFrom) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @return ShipTo |
||
| 329 | */ |
||
| 330 | public function getShipTo() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param ShipTo $shipTo |
||
| 337 | * |
||
| 338 | * @return Shipment |
||
| 339 | */ |
||
| 340 | 3 | public function setShipTo(ShipTo $shipTo) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * @return SoldTo |
||
| 349 | */ |
||
| 350 | public function getSoldTo() |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param SoldTo $soldTo |
||
| 357 | * |
||
| 358 | * @return Shipment |
||
| 359 | */ |
||
| 360 | public function setSoldTo(SoldTo $soldTo) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @return ShipmentServiceOptions |
||
| 369 | */ |
||
| 370 | public function getShipmentServiceOptions() |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param ShipmentServiceOptions $shipmentServiceOptions |
||
| 377 | * |
||
| 378 | * @return Shipment |
||
| 379 | */ |
||
| 380 | 3 | public function setShipmentServiceOptions(ShipmentServiceOptions $shipmentServiceOptions) |
|
| 386 | |||
| 387 | /** |
||
| 388 | * @return Shipper |
||
| 389 | */ |
||
| 390 | public function getShipper() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @param Shipper $shipper |
||
| 397 | * |
||
| 398 | * @return Shipment |
||
| 399 | */ |
||
| 400 | 3 | public function setShipper(Shipper $shipper) |
|
| 406 | |||
| 407 | /** |
||
| 408 | * @return PaymentInformation |
||
| 409 | */ |
||
| 410 | public function getPaymentInformation() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @param PaymentInformation $paymentInformation |
||
| 417 | * |
||
| 418 | * @return Shipment |
||
| 419 | */ |
||
| 420 | public function setPaymentInformation(PaymentInformation $paymentInformation) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * If called, returned prices will include negotiated rates (discounts will be applied). |
||
| 429 | */ |
||
| 430 | public function showNegotiatedRates() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @return null|RateInformation |
||
| 438 | */ |
||
| 439 | public function getRateInformation() |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @param RateInformation $rateInformation |
||
| 446 | * |
||
| 447 | * @return Shipment |
||
| 448 | */ |
||
| 449 | public function setRateInformation(RateInformation $rateInformation) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @return boolean |
||
| 458 | */ |
||
| 459 | public function getGoodsNotInFreeCirculationIndicator() |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @param boolean $goodsNotInFreeCirculationIndicator |
||
| 466 | * @return Shipment |
||
| 467 | */ |
||
| 468 | public function setGoodsNotInFreeCirculationIndicator($goodsNotInFreeCirculationIndicator) |
||
| 474 | |||
| 475 | /** |
||
| 476 | * @return string |
||
| 477 | */ |
||
| 478 | public function getMovementReferenceNumber() |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @param string $movementReferenceNumber |
||
| 485 | * @return Shipment |
||
| 486 | */ |
||
| 487 | public function setMovementReferenceNumber($movementReferenceNumber) |
||
| 493 | |||
| 494 | /** |
||
| 495 | * @return InvoiceLineTotal |
||
| 496 | */ |
||
| 497 | public function getInvoiceLineTotal() |
||
| 501 | |||
| 502 | /** |
||
| 503 | * @param InvoiceLineTotal $invoiceLineTotal |
||
| 504 | * @return Shipment |
||
| 505 | */ |
||
| 506 | public function setInvoiceLineTotal(InvoiceLineTotal $invoiceLineTotal) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * @return string |
||
| 515 | */ |
||
| 516 | public function getNumOfPiecesInShipment() |
||
| 520 | |||
| 521 | /** |
||
| 522 | * @param string $numOfPiecesInShipment |
||
| 523 | * @return Shipment |
||
| 524 | */ |
||
| 525 | public function setNumOfPiecesInShipment($numOfPiecesInShipment) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * @return DeliveryTimeInformation |
||
| 534 | */ |
||
| 535 | public function getDeliveryTimeInformation() |
||
| 539 | |||
| 540 | /** |
||
| 541 | * @param DeliveryTimeInformation $deliveryTimeInformation |
||
| 542 | */ |
||
| 543 | public function setDeliveryTimeInformation(DeliveryTimeInformation $deliveryTimeInformation) |
||
| 547 | } |
||
| 548 |