Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like InternationalForms 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 InternationalForms, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class InternationalForms implements NodeInterface |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var array |
||
| 14 | */ |
||
| 15 | private $types = [ |
||
| 16 | self::TYPE_INVOICE |
||
| 17 | ]; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Form Types. |
||
| 21 | */ |
||
| 22 | const TYPE_INVOICE = '01'; |
||
| 23 | const TYPE_CO = '03'; |
||
| 24 | const TYPE_NAFTA_CO = '04'; |
||
| 25 | const TYPE_PARTIAL_INVOICE = '05'; |
||
| 26 | const TYPE_PACKINGLIST = '06'; |
||
| 27 | const TYPE_CUSTOMER_GENERATED_FORMS = '07'; |
||
| 28 | const TYPE_AIR_FREIGHT_PACKING_LIST = '08'; |
||
| 29 | const TYPE_CN22_FORMS = '09'; |
||
| 30 | const TYPE_UPS_PREMIUM_CARE = '10'; |
||
| 31 | const TYPE_EEI_SHIPMENT_WITH_RETURN_SERVICE = '11'; |
||
| 32 | |||
| 33 | private static $typeNames = [ |
||
| 34 | '01' => 'Invoice', |
||
| 35 | '03' => 'CO', |
||
| 36 | '04' => 'NAFTA CO', |
||
| 37 | '05' => 'Partial Invoice', |
||
| 38 | '06' => 'Packinglist', |
||
| 39 | '07' => 'Customer Generated Forms', |
||
| 40 | '08' => 'Air Freight Packing List', |
||
| 41 | '09' => 'CN22 Forms', |
||
| 42 | '10' => 'UPS Premium Care', |
||
| 43 | '11' => 'EEI. For shipment with return service', |
||
| 44 | ]; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | private $termsOfShipment; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Terms of Shipment. |
||
| 53 | */ |
||
| 54 | const TOS_COST_AND_FREIGHT = 'CFR'; |
||
| 55 | const TOS_COST_INSURANCE_AND_FREIGHT = 'CIF'; |
||
| 56 | const TOS_CARRIAGE_AND_INSURANCE_PAID = 'CIP'; |
||
| 57 | const TOS_CARRIAGE_PAID_TO = 'CPT'; |
||
| 58 | const TOS_DELIVERED_AT_FRONTIER = 'DAF'; |
||
| 59 | const TOS_DELIVERY_DUTY_PAID = 'DDP'; |
||
| 60 | const TOS_DELIVERY_DUTY_UNPAID = 'DDU'; |
||
| 61 | const TOS_DELIVERED_EX_QUAY = 'DEQ'; |
||
| 62 | const TOS_DELIVERED_EX_SHIP = 'DES'; |
||
| 63 | const TOS_EX_WORKS = 'EXW'; |
||
| 64 | const TOS_FREE_ALONGSIDE_SHIP = 'FAS'; |
||
| 65 | const TOS_FREE_CARRIER = 'FCA'; |
||
| 66 | const TOS_FREE_ON_BOARD = 'FOB'; |
||
| 67 | |||
| 68 | private static $termsOfShipmentNames = [ |
||
|
|
|||
| 69 | 'CFR' => 'Cost and Freight', |
||
| 70 | 'CIF' => 'Cost, Insurance and Freight', |
||
| 71 | 'CIP' => 'Carriage and Insurance Paid', |
||
| 72 | 'CPT' => 'Carriage Paid To', |
||
| 73 | 'DAF' => 'Delivered at Frontier', |
||
| 74 | 'DDP' => 'Delivery Duty Paid', |
||
| 75 | 'DDU' => 'Delivery Duty Unpaid', |
||
| 76 | 'DEQ' => 'Delivered Ex Quay', |
||
| 77 | 'DES' => 'Delivered Ex Ship', |
||
| 78 | 'EXW' => 'Ex Works', |
||
| 79 | 'FAS' => 'Free Alongside Ship', |
||
| 80 | 'FCA' => 'Free Carrier', |
||
| 81 | 'FOB' => 'Free On Board', |
||
| 82 | ]; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | private $reasonForExport; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Reasons for export. |
||
| 91 | */ |
||
| 92 | const RFE_SALE = 'SALE'; |
||
| 93 | const RFE_GIFT = 'GIFT'; |
||
| 94 | const RFE_SAMPLE = 'SAMPLE'; |
||
| 95 | const RFE_RETURN = 'RETURN'; |
||
| 96 | const RFE_REPAIR = 'REPAIR'; |
||
| 97 | const RFE_INTERCOMPANYDATA = 'INTERCOMPANYDATA'; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var string |
||
| 101 | */ |
||
| 102 | private $comments; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var string |
||
| 106 | */ |
||
| 107 | private $declarationStatement; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var string |
||
| 111 | */ |
||
| 112 | private $currencyCode; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var string |
||
| 116 | */ |
||
| 117 | private $invoiceNumber; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var DateTime |
||
| 121 | */ |
||
| 122 | private $invoiceDate; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var string |
||
| 126 | */ |
||
| 127 | private $purchaseOrderNumber; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var array |
||
| 131 | */ |
||
| 132 | private $products = []; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @var Discount |
||
| 136 | */ |
||
| 137 | private $discount; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @var FreightCharges |
||
| 141 | */ |
||
| 142 | private $freightCharges; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @var bool |
||
| 146 | */ |
||
| 147 | private $additionalDocumentIndicator; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @var EEIFilingOption |
||
| 151 | */ |
||
| 152 | private $eeiFilingOption; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @return array |
||
| 156 | */ |
||
| 157 | public static function getFormTypes() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param string $type |
||
| 164 | * |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | public function getFormTypeName($type) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param null|object $attributes |
||
| 174 | */ |
||
| 175 | 4 | public function __construct($attributes = null) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * @param $type string |
||
| 210 | * |
||
| 211 | * @return $this |
||
| 212 | */ |
||
| 213 | public function setType($type) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param array $types |
||
| 220 | * |
||
| 221 | * @return $this |
||
| 222 | */ |
||
| 223 | public function setTypes(array $types) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | 2 | public function getTypes() |
|
| 237 | |||
| 238 | /** |
||
| 239 | * @param $freightCharges FreightCharges |
||
| 240 | * |
||
| 241 | * @return $this |
||
| 242 | */ |
||
| 243 | public function setFreightCharges(FreightCharges $freightCharges) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @return FreightCharges |
||
| 252 | */ |
||
| 253 | 2 | public function getFreightCharges() |
|
| 257 | |||
| 258 | /** |
||
| 259 | * @param $discount Discount |
||
| 260 | * |
||
| 261 | * @return $this |
||
| 262 | */ |
||
| 263 | public function setDiscount(Discount $discount) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @return Discount |
||
| 272 | */ |
||
| 273 | 2 | public function getDiscount() |
|
| 277 | |||
| 278 | /** |
||
| 279 | * @param Product $product |
||
| 280 | * |
||
| 281 | * @return $this |
||
| 282 | */ |
||
| 283 | public function addProduct(Product $product) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @return array |
||
| 292 | */ |
||
| 293 | public function getProducts() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param null|DOMDocument $document |
||
| 300 | * |
||
| 301 | * @return DOMElement |
||
| 302 | */ |
||
| 303 | 2 | public function toNode(DOMDocument $document = null) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * @param $number string |
||
| 359 | * |
||
| 360 | * @return $this |
||
| 361 | */ |
||
| 362 | public function setInvoiceNumber($number) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @return string |
||
| 371 | */ |
||
| 372 | 2 | public function getInvoiceNumber() |
|
| 376 | |||
| 377 | /** |
||
| 378 | * @param DateTime $date |
||
| 379 | * |
||
| 380 | * @return $this |
||
| 381 | */ |
||
| 382 | public function setInvoiceDate(DateTime $date) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @return DateTime |
||
| 391 | */ |
||
| 392 | 2 | public function getInvoiceDate() |
|
| 396 | |||
| 397 | /** |
||
| 398 | * @param $number |
||
| 399 | * |
||
| 400 | * @return $this |
||
| 401 | */ |
||
| 402 | public function setPurchaseOrderNumber($number) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @return string |
||
| 411 | */ |
||
| 412 | 2 | public function getPurchaseOrderNumber() |
|
| 416 | |||
| 417 | /** |
||
| 418 | * @param $terms |
||
| 419 | * |
||
| 420 | * @return $this |
||
| 421 | */ |
||
| 422 | public function setTermsOfShipment($terms) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @return string |
||
| 431 | */ |
||
| 432 | 2 | public function getTermsOfShipment() |
|
| 436 | |||
| 437 | /** |
||
| 438 | * @param $reason |
||
| 439 | * |
||
| 440 | * @return $this |
||
| 441 | */ |
||
| 442 | public function setReasonForExport($reason) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @return string |
||
| 455 | */ |
||
| 456 | 2 | public function getReasonForExport() |
|
| 460 | |||
| 461 | /** |
||
| 462 | * @param $comments |
||
| 463 | * |
||
| 464 | * @return $this |
||
| 465 | */ |
||
| 466 | View Code Duplication | public function setComments($comments) |
|
| 476 | |||
| 477 | /** |
||
| 478 | * @return string |
||
| 479 | */ |
||
| 480 | 2 | public function getComments() |
|
| 484 | |||
| 485 | /** |
||
| 486 | * @param string $statement |
||
| 487 | * |
||
| 488 | * @return $this |
||
| 489 | */ |
||
| 490 | public function setDeclarationStatement($statement) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @return string |
||
| 499 | */ |
||
| 500 | 2 | public function getDeclarationStatement() |
|
| 504 | |||
| 505 | /** |
||
| 506 | * @param $code |
||
| 507 | * |
||
| 508 | * @return $this |
||
| 509 | */ |
||
| 510 | public function setCurrencyCode($code) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * @return string |
||
| 519 | */ |
||
| 520 | 2 | public function getCurrencyCode() |
|
| 524 | |||
| 525 | /** |
||
| 526 | * @param $additionalDocumentIndicator |
||
| 527 | * |
||
| 528 | * @return $this |
||
| 529 | */ |
||
| 530 | public function setAdditionalDocumentIndicator($additionalDocumentIndicator) |
||
| 534 | |||
| 535 | /** |
||
| 536 | * @return bool |
||
| 537 | */ |
||
| 538 | 2 | public function getAdditionalDocumentIndicator() |
|
| 542 | |||
| 543 | /** |
||
| 544 | * @param EEIFilingOption $eeiFilingOption |
||
| 545 | * |
||
| 546 | * @return $this |
||
| 547 | */ |
||
| 548 | 3 | public function setEEIFilingOption(EEIFilingOption $eeiFilingOption) |
|
| 554 | |||
| 555 | /** |
||
| 556 | * @return EEIFilingOption |
||
| 557 | */ |
||
| 558 | 4 | public function getEEIFilingOption() |
|
| 562 | } |
||
| 563 |
This check marks private properties in classes that are never used. Those properties can be removed.