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 | 1 | public function __construct() |
|
| 108 | { |
||
| 109 | 1 | $this->setShipper(new Shipper()); |
|
| 110 | 1 | $this->setShipTo(new ShipTo()); |
|
| 111 | 1 | $this->setShipmentServiceOptions(new ShipmentServiceOptions()); |
|
| 112 | 1 | $this->setService(new Service()); |
|
| 113 | 1 | $this->rateInformation = null; |
|
| 114 | 1 | } |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @return ShipmentIndicationType |
||
| 118 | */ |
||
| 119 | public function getShipmentIndicationType() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param ShipmentIndicationType $shipmentIndicationType |
||
| 126 | */ |
||
| 127 | public function setShipmentIndicationType(ShipmentIndicationType $shipmentIndicationType) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @return AlternateDeliveryAddress |
||
| 134 | */ |
||
| 135 | public function getAlternateDeliveryAddress() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param AlternateDeliveryAddress $alternateDeliveryAddress |
||
| 142 | */ |
||
| 143 | public function setAlternateDeliveryAddress(AlternateDeliveryAddress $alternateDeliveryAddress) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param Package $package |
||
| 150 | * |
||
| 151 | * @return Shipment |
||
| 152 | */ |
||
| 153 | public function addPackage(Package $package) |
||
| 154 | { |
||
| 155 | $packages = $this->getPackages(); |
||
| 156 | $packages[] = $package; |
||
| 157 | $this->setPackages($packages); |
||
| 158 | |||
| 159 | return $this; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @return string |
||
| 164 | */ |
||
| 165 | public function getDescription() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param string $description |
||
| 172 | * |
||
| 173 | * @return Shipment |
||
| 174 | */ |
||
| 175 | public function setDescription($description) |
||
| 176 | { |
||
| 177 | $this->description = $description; |
||
| 178 | |||
| 179 | return $this; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param ReferenceNumber $referenceNumber |
||
| 184 | * |
||
| 185 | * @return Shipment |
||
| 186 | */ |
||
| 187 | public function setReferenceNumber(ReferenceNumber $referenceNumber) |
||
| 188 | { |
||
| 189 | $this->referenceNumber = $referenceNumber; |
||
| 190 | |||
| 191 | return $this; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param ReferenceNumber $referenceNumber |
||
| 196 | * |
||
| 197 | * @return Shipment |
||
| 198 | */ |
||
| 199 | public function setReferenceNumber2(ReferenceNumber $referenceNumber) |
||
| 200 | { |
||
| 201 | $this->referenceNumber2 = $referenceNumber; |
||
| 202 | |||
| 203 | return $this; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @return ReferenceNumber |
||
| 208 | */ |
||
| 209 | public function getReferenceNumber() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @return ReferenceNumber |
||
| 216 | */ |
||
| 217 | public function getReferenceNumber2() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @return bool |
||
| 224 | */ |
||
| 225 | public function getDocumentsOnly() |
||
| 226 | { |
||
| 227 | return $this->documentsOnly; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param bool $documentsOnly |
||
| 232 | * |
||
| 233 | * @return Shipment |
||
| 234 | */ |
||
| 235 | public function setDocumentsOnly($documentsOnly) |
||
| 236 | { |
||
| 237 | $this->documentsOnly = $documentsOnly; |
||
| 238 | |||
| 239 | return $this; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @return Package[] |
||
| 244 | */ |
||
| 245 | public function getPackages() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param Package[] $packages |
||
| 252 | * |
||
| 253 | * @return Shipment |
||
| 254 | */ |
||
| 255 | public function setPackages(array $packages) |
||
| 256 | { |
||
| 257 | $this->packages = $packages; |
||
| 258 | |||
| 259 | return $this; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @return Service |
||
| 264 | */ |
||
| 265 | public function getService() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param Service $service |
||
| 272 | * |
||
| 273 | * @return Shipment |
||
| 274 | */ |
||
| 275 | 1 | public function setService(Service $service) |
|
| 276 | { |
||
| 277 | 1 | $this->service = $service; |
|
| 278 | |||
| 279 | 1 | return $this; |
|
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @return ReturnService |
||
| 284 | */ |
||
| 285 | public function getReturnService() |
||
| 286 | { |
||
| 287 | return $this->returnService; |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param ReturnService $returnService |
||
| 292 | * |
||
| 293 | * @return Shipment |
||
| 294 | */ |
||
| 295 | public function setReturnService(ReturnService $returnService) |
||
| 296 | { |
||
| 297 | $this->returnService = $returnService; |
||
| 298 | |||
| 299 | return $this; |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @return ShipFrom |
||
| 304 | */ |
||
| 305 | public function getShipFrom() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param ShipFrom $shipFrom |
||
| 312 | * |
||
| 313 | * @return Shipment |
||
| 314 | */ |
||
| 315 | public function setShipFrom(ShipFrom $shipFrom) |
||
| 316 | { |
||
| 317 | $this->shipFrom = $shipFrom; |
||
| 318 | |||
| 319 | return $this; |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @return ShipTo |
||
| 324 | */ |
||
| 325 | public function getShipTo() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @param ShipTo $shipTo |
||
| 332 | * |
||
| 333 | * @return Shipment |
||
| 334 | */ |
||
| 335 | 1 | public function setShipTo(ShipTo $shipTo) |
|
| 336 | { |
||
| 337 | 1 | $this->shipTo = $shipTo; |
|
| 338 | |||
| 339 | 1 | return $this; |
|
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @return SoldTo |
||
| 344 | */ |
||
| 345 | public function getSoldTo() |
||
| 346 | { |
||
| 347 | return $this->soldTo; |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @param SoldTo $soldTo |
||
| 352 | * |
||
| 353 | * @return Shipment |
||
| 354 | */ |
||
| 355 | public function setSoldTo(SoldTo $soldTo) |
||
| 356 | { |
||
| 357 | $this->soldTo = $soldTo; |
||
| 358 | |||
| 359 | return $this; |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @return ShipmentServiceOptions |
||
| 364 | */ |
||
| 365 | public function getShipmentServiceOptions() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @param ShipmentServiceOptions $shipmentServiceOptions |
||
| 372 | * |
||
| 373 | * @return Shipment |
||
| 374 | */ |
||
| 375 | 1 | public function setShipmentServiceOptions(ShipmentServiceOptions $shipmentServiceOptions) |
|
| 376 | { |
||
| 377 | 1 | $this->shipmentServiceOptions = $shipmentServiceOptions; |
|
| 378 | |||
| 379 | 1 | return $this; |
|
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @return Shipper |
||
| 384 | */ |
||
| 385 | public function getShipper() |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @param Shipper $shipper |
||
| 392 | * |
||
| 393 | * @return Shipment |
||
| 394 | */ |
||
| 395 | 1 | public function setShipper(Shipper $shipper) |
|
| 396 | { |
||
| 397 | 1 | $this->shipper = $shipper; |
|
| 398 | |||
| 399 | 1 | return $this; |
|
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @return PaymentInformation |
||
| 404 | */ |
||
| 405 | public function getPaymentInformation() |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @param PaymentInformation $paymentInformation |
||
| 412 | * |
||
| 413 | * @return Shipment |
||
| 414 | */ |
||
| 415 | public function setPaymentInformation(PaymentInformation $paymentInformation) |
||
| 416 | { |
||
| 417 | $this->paymentInformation = $paymentInformation; |
||
| 418 | |||
| 419 | return $this; |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * If called, returned prices will include negotiated rates (discounts will be applied). |
||
| 424 | */ |
||
| 425 | public function showNegotiatedRates() |
||
| 426 | { |
||
| 427 | $this->rateInformation = new RateInformation(); |
||
| 428 | $this->rateInformation->setNegotiatedRatesIndicator(true); |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @return null|RateInformation |
||
| 433 | */ |
||
| 434 | public function getRateInformation() |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @param RateInformation $rateInformation |
||
| 441 | * |
||
| 442 | * @return Shipment |
||
| 443 | */ |
||
| 444 | public function setRateInformation(RateInformation $rateInformation) |
||
| 445 | { |
||
| 446 | $this->rateInformation = $rateInformation; |
||
| 447 | |||
| 448 | return $this; |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @return boolean |
||
| 453 | */ |
||
| 454 | public function getGoodsNotInFreeCirculationIndicator() |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @param boolean $goodsNotInFreeCirculationIndicator |
||
| 461 | * @return Shipment |
||
| 462 | */ |
||
| 463 | public function setGoodsNotInFreeCirculationIndicator($goodsNotInFreeCirculationIndicator) |
||
| 464 | { |
||
| 465 | $this->goodsNotInFreeCirculationIndicator = $goodsNotInFreeCirculationIndicator; |
||
| 466 | |||
| 467 | return $this; |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * @return string |
||
| 472 | */ |
||
| 473 | public function getMovementReferenceNumber() |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @param string $movementReferenceNumber |
||
| 480 | * @return Shipment |
||
| 481 | */ |
||
| 482 | public function setMovementReferenceNumber($movementReferenceNumber) |
||
| 483 | { |
||
| 484 | $this->movementReferenceNumber = $movementReferenceNumber; |
||
| 485 | |||
| 486 | return $this; |
||
| 487 | } |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @return InvoiceLineTotal |
||
| 491 | */ |
||
| 492 | public function getInvoiceLineTotal() |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @param InvoiceLineTotal $invoiceLineTotal |
||
| 499 | * @return Shipment |
||
| 500 | */ |
||
| 501 | public function setInvoiceLineTotal(InvoiceLineTotal $invoiceLineTotal) |
||
| 502 | { |
||
| 503 | $this->invoiceLineTotal = $invoiceLineTotal; |
||
| 504 | |||
| 505 | return $this; |
||
| 506 | } |
||
| 507 | |||
| 508 | /** |
||
| 509 | * @return string |
||
| 510 | */ |
||
| 511 | public function getNumOfPiecesInShipment() |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @param string $numOfPiecesInShipment |
||
| 518 | * @return Shipment |
||
| 519 | */ |
||
| 520 | public function setNumOfPiecesInShipment($numOfPiecesInShipment) |
||
| 521 | { |
||
| 522 | $this->numOfPiecesInShipment = $numOfPiecesInShipment; |
||
| 523 | |||
| 524 | return $this; |
||
| 525 | } |
||
| 526 | } |
||
| 527 |