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 string |
||
14 | */ |
||
15 | private $type = self::TYPE_INVOICE; |
||
16 | |||
17 | /** |
||
18 | * Form Types. |
||
19 | */ |
||
20 | const TYPE_INVOICE = '01'; |
||
21 | const TYPE_CO = '03'; |
||
22 | const TYPE_NAFTA_CO = '04'; |
||
23 | const TYPE_PARTIAL_INVOICE = '05'; |
||
24 | const TYPE_PACKINGLIST = '06'; |
||
25 | const TYPE_CUSTOMER_GENERATED_FORMS = '07'; |
||
26 | const TYPE_AIR_FREIGHT_PACKING_LIST = '08'; |
||
27 | const TYPE_CN22_FORMS = '09'; |
||
28 | const TYPE_UPS_PREMIUM_CARE = '10'; |
||
29 | const TYPE_EEI_SHIPMENT_WITH_RETURN_SERVICE = '11'; |
||
30 | |||
31 | private static $typeNames = [ |
||
32 | '01' => 'Invoice', |
||
33 | '03' => 'CO', |
||
34 | '04' => 'NAFTA CO', |
||
35 | '05' => 'Partial Invoice', |
||
36 | '06' => 'Packinglist', |
||
37 | '07' => 'Customer Generated Forms', |
||
38 | '08' => 'Air Freight Packing List', |
||
39 | '09' => 'CN22 Forms', |
||
40 | '10' => 'UPS Premium Care', |
||
41 | '11' => 'EEI. For shipment with return service', |
||
42 | ]; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | private $termsOfShipment; |
||
48 | |||
49 | /** |
||
50 | * Terms of Shipment. |
||
51 | */ |
||
52 | const TOS_COST_AND_FREIGHT = 'CFR'; |
||
53 | const TOS_COST_INSURANCE_AND_FREIGHT = 'CIF'; |
||
54 | const TOS_CARRIAGE_AND_INSURANCE_PAID = 'CIP'; |
||
55 | const TOS_CARRIAGE_PAID_TO = 'CPT'; |
||
56 | const TOS_DELIVERED_AT_FRONTIER = 'DAF'; |
||
57 | const TOS_DELIVERY_DUTY_PAID = 'DDP'; |
||
58 | const TOS_DELIVERY_DUTY_UNPAID = 'DDU'; |
||
59 | const TOS_DELIVERED_EX_QUAY = 'DEQ'; |
||
60 | const TOS_DELIVERED_EX_SHIP = 'DES'; |
||
61 | const TOS_EX_WORKS = 'EXW'; |
||
62 | const TOS_FREE_ALONGSIDE_SHIP = 'FAS'; |
||
63 | const TOS_FREE_CARRIER = 'FCA'; |
||
64 | const TOS_FREE_ON_BOARD = 'FOB'; |
||
65 | |||
66 | private static $termsOfShipmentNames = [ |
||
|
|||
67 | 'CFR' => 'Cost and Freight', |
||
68 | 'CIF' => 'Cost, Insurance and Freight', |
||
69 | 'CIP' => 'Carriage and Insurance Paid', |
||
70 | 'CPT' => 'Carriage Paid To', |
||
71 | 'DAF' => 'Delivered at Frontier', |
||
72 | 'DDP' => 'Delivery Duty Paid', |
||
73 | 'DDU' => 'Delivery Duty Unpaid', |
||
74 | 'DEQ' => 'Delivered Ex Quay', |
||
75 | 'DES' => 'Delivered Ex Ship', |
||
76 | 'EXW' => 'Ex Works', |
||
77 | 'FAS' => 'Free Alongside Ship', |
||
78 | 'FCA' => 'Free Carrier', |
||
79 | 'FOB' => 'Free On Board', |
||
80 | ]; |
||
81 | |||
82 | /** |
||
83 | * @var string |
||
84 | */ |
||
85 | private $reasonForExport; |
||
86 | |||
87 | /** |
||
88 | * Reasons for export. |
||
89 | */ |
||
90 | const RFE_SALE = 'SALE'; |
||
91 | const RFE_GIFT = 'GIFT'; |
||
92 | const RFE_SAMPLE = 'SAMPLE'; |
||
93 | const RFE_RETURN = 'RETURN'; |
||
94 | const RFE_REPAIR = 'REPAIR'; |
||
95 | const RFE_INTERCOMPANYDATA = 'INTERCOMPANYDATA'; |
||
96 | |||
97 | /** |
||
98 | * @var string |
||
99 | */ |
||
100 | private $comments; |
||
101 | |||
102 | /** |
||
103 | * @var string |
||
104 | */ |
||
105 | private $declarationStatement; |
||
106 | |||
107 | /** |
||
108 | * @var string |
||
109 | */ |
||
110 | private $currencyCode; |
||
111 | |||
112 | /** |
||
113 | * @var string |
||
114 | */ |
||
115 | private $invoiceNumber; |
||
116 | |||
117 | /** |
||
118 | * @var DateTime |
||
119 | */ |
||
120 | private $invoiceDate; |
||
121 | |||
122 | /** |
||
123 | * @var string |
||
124 | */ |
||
125 | private $purchaseOrderNumber; |
||
126 | |||
127 | /** |
||
128 | * @var array |
||
129 | */ |
||
130 | private $products = []; |
||
131 | |||
132 | /** |
||
133 | * @var Discount |
||
134 | */ |
||
135 | private $discount; |
||
136 | |||
137 | /** |
||
138 | * @var FreightCharges |
||
139 | */ |
||
140 | private $freightCharges; |
||
141 | |||
142 | /** |
||
143 | * @var bool |
||
144 | */ |
||
145 | private $additionalDocumentIndicator; |
||
146 | |||
147 | /** |
||
148 | * @var EEIFilingOption |
||
149 | */ |
||
150 | private $eeiFilingOption; |
||
151 | |||
152 | /** |
||
153 | * @return array |
||
154 | */ |
||
155 | public static function getTypes() |
||
159 | |||
160 | /** |
||
161 | * @return string |
||
162 | */ |
||
163 | public function getTypeName() |
||
167 | |||
168 | /** |
||
169 | * @param null|object $attributes |
||
170 | */ |
||
171 | 4 | public function __construct($attributes = null) |
|
203 | |||
204 | /** |
||
205 | * @param $type string |
||
206 | * |
||
207 | * @return $this |
||
208 | */ |
||
209 | public function setType($type) |
||
215 | |||
216 | /** |
||
217 | * @return string |
||
218 | */ |
||
219 | 2 | public function getType() |
|
223 | |||
224 | /** |
||
225 | * @param $freightCharges FreightCharges |
||
226 | * |
||
227 | * @return $this |
||
228 | */ |
||
229 | public function setFreightCharges(FreightCharges $freightCharges) |
||
235 | |||
236 | /** |
||
237 | * @return FreightCharges |
||
238 | */ |
||
239 | 2 | public function getFreightCharges() |
|
243 | |||
244 | /** |
||
245 | * @param $discount Discount |
||
246 | * |
||
247 | * @return $this |
||
248 | */ |
||
249 | public function setDiscount(Discount $discount) |
||
255 | |||
256 | /** |
||
257 | * @return Discount |
||
258 | */ |
||
259 | 2 | public function getDiscount() |
|
263 | |||
264 | /** |
||
265 | * @param Product $product |
||
266 | * |
||
267 | * @return $this |
||
268 | */ |
||
269 | public function addProduct(Product $product) |
||
275 | |||
276 | /** |
||
277 | * @return array |
||
278 | */ |
||
279 | public function getProducts() |
||
283 | |||
284 | /** |
||
285 | * @param null|DOMDocument $document |
||
286 | * |
||
287 | * @return DOMElement |
||
288 | */ |
||
289 | 2 | public function toNode(DOMDocument $document = null) |
|
342 | |||
343 | /** |
||
344 | * @param $number string |
||
345 | * |
||
346 | * @return $this |
||
347 | */ |
||
348 | public function setInvoiceNumber($number) |
||
354 | |||
355 | /** |
||
356 | * @return string |
||
357 | */ |
||
358 | 2 | public function getInvoiceNumber() |
|
362 | |||
363 | /** |
||
364 | * @param DateTime $date |
||
365 | * |
||
366 | * @return $this |
||
367 | */ |
||
368 | public function setInvoiceDate(DateTime $date) |
||
374 | |||
375 | /** |
||
376 | * @return DateTime |
||
377 | */ |
||
378 | 2 | public function getInvoiceDate() |
|
382 | |||
383 | /** |
||
384 | * @param $number |
||
385 | * |
||
386 | * @return $this |
||
387 | */ |
||
388 | public function setPurchaseOrderNumber($number) |
||
394 | |||
395 | /** |
||
396 | * @return string |
||
397 | */ |
||
398 | 2 | public function getPurchaseOrderNumber() |
|
402 | |||
403 | /** |
||
404 | * @param $terms |
||
405 | * |
||
406 | * @return $this |
||
407 | */ |
||
408 | public function setTermsOfShipment($terms) |
||
414 | |||
415 | /** |
||
416 | * @return string |
||
417 | */ |
||
418 | 2 | public function getTermsOfShipment() |
|
422 | |||
423 | /** |
||
424 | * @param $reason |
||
425 | * |
||
426 | * @return $this |
||
427 | */ |
||
428 | public function setReasonForExport($reason) |
||
438 | |||
439 | /** |
||
440 | * @return string |
||
441 | */ |
||
442 | 2 | public function getReasonForExport() |
|
446 | |||
447 | /** |
||
448 | * @param $comments |
||
449 | * |
||
450 | * @return $this |
||
451 | */ |
||
452 | View Code Duplication | public function setComments($comments) |
|
462 | |||
463 | /** |
||
464 | * @return string |
||
465 | */ |
||
466 | 2 | public function getComments() |
|
470 | |||
471 | /** |
||
472 | * @param string $statement |
||
473 | * |
||
474 | * @return $this |
||
475 | */ |
||
476 | public function setDeclarationStatement($statement) |
||
482 | |||
483 | /** |
||
484 | * @return string |
||
485 | */ |
||
486 | 2 | public function getDeclarationStatement() |
|
490 | |||
491 | /** |
||
492 | * @param $code |
||
493 | * |
||
494 | * @return $this |
||
495 | */ |
||
496 | public function setCurrencyCode($code) |
||
502 | |||
503 | /** |
||
504 | * @return string |
||
505 | */ |
||
506 | 2 | public function getCurrencyCode() |
|
510 | |||
511 | /** |
||
512 | * @param $additionalDocumentIndicator |
||
513 | * |
||
514 | * @return $this |
||
515 | */ |
||
516 | public function setAdditionalDocumentIndicator($additionalDocumentIndicator) |
||
520 | |||
521 | /** |
||
522 | * @return bool |
||
523 | */ |
||
524 | 2 | public function getAdditionalDocumentIndicator() |
|
528 | |||
529 | /** |
||
530 | * @param EEIFilingOption $eeiFilingOption |
||
531 | * |
||
532 | * @return $this |
||
533 | */ |
||
534 | 3 | public function setEEIFilingOption(EEIFilingOption $eeiFilingOption) |
|
540 | |||
541 | /** |
||
542 | * @return EEIFilingOption |
||
543 | */ |
||
544 | 4 | public function getEEIFilingOption() |
|
548 | } |
||
549 |
This check marks private properties in classes that are never used. Those properties can be removed.