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 $currencyCode; |
||
108 | |||
109 | /** |
||
110 | * @var string |
||
111 | */ |
||
112 | private $invoiceNumber; |
||
113 | |||
114 | /** |
||
115 | * @var DateTime |
||
116 | */ |
||
117 | private $invoiceDate; |
||
118 | |||
119 | /** |
||
120 | * @var string |
||
121 | */ |
||
122 | private $purchaseOrderNumber; |
||
123 | |||
124 | /** |
||
125 | * @var array |
||
126 | */ |
||
127 | private $products = []; |
||
128 | |||
129 | /** |
||
130 | * @var Discount |
||
131 | */ |
||
132 | private $discount; |
||
133 | |||
134 | /** |
||
135 | * @var FreightCharges |
||
136 | */ |
||
137 | private $freightCharges; |
||
138 | |||
139 | /** |
||
140 | * @var bool |
||
141 | */ |
||
142 | private $additionalDocumentIndicator; |
||
143 | |||
144 | /** |
||
145 | * @var EEIFilingOption |
||
146 | */ |
||
147 | private $eeiFilingOption; |
||
148 | |||
149 | /** |
||
150 | * @return array |
||
151 | */ |
||
152 | public static function getFormTypes() |
||
156 | |||
157 | /** |
||
158 | * @param string $type |
||
159 | * |
||
160 | * @return string |
||
161 | */ |
||
162 | public function getFormTypeName($type) |
||
166 | |||
167 | /** |
||
168 | * @param null|object $attributes |
||
169 | */ |
||
170 | 4 | public function __construct($attributes = null) |
|
199 | |||
200 | /** |
||
201 | * @param $type string |
||
202 | * |
||
203 | * @return $this |
||
204 | */ |
||
205 | public function setType($type) |
||
209 | |||
210 | /** |
||
211 | * @param array $types |
||
212 | * |
||
213 | * @return $this |
||
214 | */ |
||
215 | public function setTypes(array $types) |
||
221 | |||
222 | /** |
||
223 | * @return string |
||
224 | */ |
||
225 | 2 | public function getTypes() |
|
229 | |||
230 | /** |
||
231 | * @param $freightCharges FreightCharges |
||
232 | * |
||
233 | * @return $this |
||
234 | */ |
||
235 | public function setFreightCharges(FreightCharges $freightCharges) |
||
241 | |||
242 | /** |
||
243 | * @return FreightCharges |
||
244 | */ |
||
245 | 2 | public function getFreightCharges() |
|
249 | |||
250 | /** |
||
251 | * @param $discount Discount |
||
252 | * |
||
253 | * @return $this |
||
254 | */ |
||
255 | public function setDiscount(Discount $discount) |
||
261 | |||
262 | /** |
||
263 | * @return Discount |
||
264 | */ |
||
265 | 2 | public function getDiscount() |
|
269 | |||
270 | /** |
||
271 | * @param Product $product |
||
272 | * |
||
273 | * @return $this |
||
274 | */ |
||
275 | public function addProduct(Product $product) |
||
281 | |||
282 | /** |
||
283 | * @return array |
||
284 | */ |
||
285 | public function getProducts() |
||
289 | |||
290 | /** |
||
291 | * @param null|DOMDocument $document |
||
292 | * |
||
293 | * @return DOMElement |
||
294 | */ |
||
295 | 2 | public function toNode(DOMDocument $document = null) |
|
345 | |||
346 | /** |
||
347 | * @param $number string |
||
348 | * |
||
349 | * @return $this |
||
350 | */ |
||
351 | public function setInvoiceNumber($number) |
||
357 | |||
358 | /** |
||
359 | * @return string |
||
360 | */ |
||
361 | 2 | public function getInvoiceNumber() |
|
365 | |||
366 | /** |
||
367 | * @param DateTime $date |
||
368 | * |
||
369 | * @return $this |
||
370 | */ |
||
371 | public function setInvoiceDate(DateTime $date) |
||
377 | |||
378 | /** |
||
379 | * @return DateTime |
||
380 | */ |
||
381 | 2 | public function getInvoiceDate() |
|
385 | |||
386 | /** |
||
387 | * @param $number |
||
388 | * |
||
389 | * @return $this |
||
390 | */ |
||
391 | public function setPurchaseOrderNumber($number) |
||
397 | |||
398 | /** |
||
399 | * @return string |
||
400 | */ |
||
401 | 2 | public function getPurchaseOrderNumber() |
|
405 | |||
406 | /** |
||
407 | * @param $terms |
||
408 | * |
||
409 | * @return $this |
||
410 | */ |
||
411 | public function setTermsOfShipment($terms) |
||
417 | |||
418 | /** |
||
419 | * @return string |
||
420 | */ |
||
421 | 2 | public function getTermsOfShipment() |
|
425 | |||
426 | /** |
||
427 | * @param $reason |
||
428 | * |
||
429 | * @return $this |
||
430 | */ |
||
431 | public function setReasonForExport($reason) |
||
441 | |||
442 | /** |
||
443 | * @return string |
||
444 | */ |
||
445 | 2 | public function getReasonForExport() |
|
449 | |||
450 | /** |
||
451 | * @param $comments |
||
452 | * |
||
453 | * @return $this |
||
454 | */ |
||
455 | View Code Duplication | public function setComments($comments) |
|
465 | |||
466 | /** |
||
467 | * @return string |
||
468 | */ |
||
469 | 2 | public function getComments() |
|
473 | |||
474 | /** |
||
475 | * @param $code |
||
476 | * |
||
477 | * @return $this |
||
478 | */ |
||
479 | public function setCurrencyCode($code) |
||
485 | |||
486 | /** |
||
487 | * @return string |
||
488 | */ |
||
489 | 2 | public function getCurrencyCode() |
|
493 | |||
494 | /** |
||
495 | * @param $additionalDocumentIndicator |
||
496 | * |
||
497 | * @return $this |
||
498 | */ |
||
499 | public function setAdditionalDocumentIndicator($additionalDocumentIndicator) |
||
503 | |||
504 | /** |
||
505 | * @return bool |
||
506 | */ |
||
507 | 2 | public function getAdditionalDocumentIndicator() |
|
511 | |||
512 | /** |
||
513 | * @param EEIFilingOption $eeiFilingOption |
||
514 | * |
||
515 | * @return $this |
||
516 | */ |
||
517 | 3 | public function setEEIFilingOption(EEIFilingOption $eeiFilingOption) |
|
523 | |||
524 | /** |
||
525 | * @return EEIFilingOption |
||
526 | */ |
||
527 | 4 | public function getEEIFilingOption() |
|
531 | } |
||
532 |
This check marks private properties in classes that are never used. Those properties can be removed.