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 $currencyCode; |
||
106 | |||
107 | /** |
||
108 | * @var string |
||
109 | */ |
||
110 | private $invoiceNumber; |
||
111 | |||
112 | /** |
||
113 | * @var DateTime |
||
114 | */ |
||
115 | private $invoiceDate; |
||
116 | |||
117 | /** |
||
118 | * @var string |
||
119 | */ |
||
120 | private $purchaseOrderNumber; |
||
121 | |||
122 | /** |
||
123 | * @var array |
||
124 | */ |
||
125 | private $products = []; |
||
126 | |||
127 | /** |
||
128 | * @var Discount |
||
129 | */ |
||
130 | private $discount; |
||
131 | |||
132 | /** |
||
133 | * @var FreightCharges |
||
134 | */ |
||
135 | private $freightCharges; |
||
136 | |||
137 | /** |
||
138 | * @var bool |
||
139 | */ |
||
140 | private $additionalDocumentIndicator; |
||
141 | |||
142 | /** |
||
143 | * @var EEIFilingOption |
||
144 | */ |
||
145 | private $eeiFilingOption; |
||
146 | |||
147 | /** |
||
148 | * @return array |
||
149 | */ |
||
150 | public static function getTypes() |
||
154 | |||
155 | /** |
||
156 | * @return string |
||
157 | */ |
||
158 | public function getTypeName() |
||
162 | |||
163 | /** |
||
164 | * @param null|object $attributes |
||
165 | */ |
||
166 | 3 | public function __construct($attributes = null) |
|
167 | { |
||
168 | 3 | if (null !== $attributes) { |
|
169 | 2 | if (isset($attributes->FormType)) { |
|
170 | $this->setType($attributes->FormType); |
||
171 | } |
||
172 | 2 | if (isset($attributes->InvoiceNumber)) { |
|
173 | $this->setInvoiceNumber($attributes->InvoiceNumber); |
||
174 | } |
||
175 | 2 | if (isset($attributes->InvoiceDate)) { |
|
176 | $this->setInvoiceDate(new DateTime($attributes->InvoiceDate)); |
||
177 | } |
||
178 | 2 | if (isset($attributes->PurchaseOrderNumber)) { |
|
179 | $this->setPurchaseOrderNumber($attributes->PurchaseOrderNumber); |
||
180 | } |
||
181 | 2 | if (isset($attributes->TermsOfShipment)) { |
|
182 | $this->setTermsOfShipment($attributes->TermsOfShipment); |
||
183 | } |
||
184 | 2 | if (isset($attributes->Comments)) { |
|
185 | $this->setComments($attributes->Comments); |
||
186 | } |
||
187 | 2 | if (isset($attributes->CurrencyCode)) { |
|
188 | $this->setCurrencyCode($attributes->CurrencyCode); |
||
189 | } |
||
190 | 2 | if (isset($attributes->EEIFilingOption)) { |
|
191 | 2 | $this->setEEIFilingOption(new EEIFilingOption($attributes->EEIFilingOption)); |
|
192 | 2 | } |
|
193 | 2 | } |
|
194 | 3 | } |
|
195 | |||
196 | /** |
||
197 | * @param $type string |
||
198 | * |
||
199 | * @return $this |
||
200 | */ |
||
201 | public function setType($type) |
||
207 | |||
208 | /** |
||
209 | * @return string |
||
210 | */ |
||
211 | 1 | public function getType() |
|
215 | |||
216 | /** |
||
217 | * @param $freightCharges FreightCharges |
||
218 | * |
||
219 | * @return $this |
||
220 | */ |
||
221 | public function setFreightCharges(FreightCharges $freightCharges) |
||
227 | |||
228 | /** |
||
229 | * @return FreightCharges |
||
230 | */ |
||
231 | 1 | public function getFreightCharges() |
|
235 | |||
236 | /** |
||
237 | * @param $discount Discount |
||
238 | * |
||
239 | * @return $this |
||
240 | */ |
||
241 | public function setDiscount(Discount $discount) |
||
247 | |||
248 | /** |
||
249 | * @return Discount |
||
250 | */ |
||
251 | 1 | public function getDiscount() |
|
255 | |||
256 | /** |
||
257 | * @param Product $product |
||
258 | * |
||
259 | * @return $this |
||
260 | */ |
||
261 | public function addProduct(Product $product) |
||
267 | |||
268 | /** |
||
269 | * @return array |
||
270 | */ |
||
271 | public function getProducts() |
||
275 | |||
276 | /** |
||
277 | * @param null|DOMDocument $document |
||
278 | * |
||
279 | * @return DOMElement |
||
280 | */ |
||
281 | 1 | public function toNode(DOMDocument $document = null) |
|
282 | { |
||
283 | 1 | if (null === $document) { |
|
284 | $document = new DOMDocument(); |
||
285 | } |
||
286 | |||
287 | 1 | $node = $document->createElement('InternationalForms'); |
|
288 | |||
289 | 1 | if ($this->getType()) { |
|
290 | 1 | $node->appendChild($document->createElement('FormType', $this->getType())); |
|
291 | 1 | } |
|
292 | 1 | if ($this->getInvoiceNumber() !== null) { |
|
293 | $node->appendChild($document->createElement('InvoiceNumber', $this->getInvoiceNumber())); |
||
294 | } |
||
295 | 1 | if ($this->getInvoiceDate() !== null) { |
|
296 | $node->appendChild($document->createElement('InvoiceDate', $this->getInvoiceDate()->format('Ymd'))); |
||
297 | } |
||
298 | 1 | if ($this->getPurchaseOrderNumber() !== null) { |
|
299 | $node->appendChild($document->createElement('PurchaseOrderNumber', $this->getPurchaseOrderNumber())); |
||
300 | } |
||
301 | 1 | if ($this->getTermsOfShipment() !== null) { |
|
302 | $node->appendChild($document->createElement('TermsOfShipment', $this->getTermsOfShipment())); |
||
303 | } |
||
304 | 1 | if ($this->getReasonForExport() !== null) { |
|
305 | $node->appendChild($document->createElement('ReasonForExport', $this->getReasonForExport())); |
||
306 | } |
||
307 | 1 | if ($this->getComments() !== null) { |
|
308 | $node->appendChild($document->createElement('Comments', $this->getComments())); |
||
309 | } |
||
310 | 1 | if ($this->getCurrencyCode() !== null) { |
|
311 | $node->appendChild($document->createElement('CurrencyCode', $this->getCurrencyCode())); |
||
312 | } |
||
313 | 1 | if ($this->getDiscount() !== null) { |
|
314 | $node->appendChild($this->getDiscount()->toNode($document)); |
||
315 | } |
||
316 | 1 | if ($this->getFreightCharges() !== null) { |
|
317 | $node->appendChild($this->getFreightCharges()->toNode($document)); |
||
318 | } |
||
319 | 1 | if ($this->getAdditionalDocumentIndicator() !== null) { |
|
320 | $node->appendChild($document->createElement('AdditionalDocumentIndicator')); |
||
321 | } |
||
322 | 1 | if ($this->getEEIFilingOption() !== null) { |
|
323 | 1 | $node->appendChild($this->getEEIFilingOption()->toNode($document)); |
|
324 | 1 | } |
|
325 | 1 | foreach ($this->products as $product) { |
|
326 | $node->appendChild($product->toNode($document)); |
||
327 | 1 | } |
|
328 | |||
329 | 1 | return $node; |
|
330 | } |
||
331 | |||
332 | /** |
||
333 | * @param $number string |
||
334 | * |
||
335 | * @return $this |
||
336 | */ |
||
337 | public function setInvoiceNumber($number) |
||
343 | |||
344 | /** |
||
345 | * @return string |
||
346 | */ |
||
347 | 1 | public function getInvoiceNumber() |
|
351 | |||
352 | /** |
||
353 | * @param DateTime $date |
||
354 | * |
||
355 | * @return $this |
||
356 | */ |
||
357 | public function setInvoiceDate(DateTime $date) |
||
363 | |||
364 | /** |
||
365 | * @return DateTime |
||
366 | */ |
||
367 | 1 | public function getInvoiceDate() |
|
371 | |||
372 | /** |
||
373 | * @param $number |
||
374 | * |
||
375 | * @return $this |
||
376 | */ |
||
377 | public function setPurchaseOrderNumber($number) |
||
383 | |||
384 | /** |
||
385 | * @return string |
||
386 | */ |
||
387 | 1 | public function getPurchaseOrderNumber() |
|
391 | |||
392 | /** |
||
393 | * @param $terms |
||
394 | * |
||
395 | * @return $this |
||
396 | */ |
||
397 | public function setTermsOfShipment($terms) |
||
403 | |||
404 | /** |
||
405 | * @return string |
||
406 | */ |
||
407 | 1 | public function getTermsOfShipment() |
|
411 | |||
412 | /** |
||
413 | * @param $reason |
||
414 | * |
||
415 | * @return $this |
||
416 | */ |
||
417 | public function setReasonForExport($reason) |
||
427 | |||
428 | /** |
||
429 | * @return string |
||
430 | */ |
||
431 | 1 | public function getReasonForExport() |
|
435 | |||
436 | /** |
||
437 | * @param $comments |
||
438 | * |
||
439 | * @return $this |
||
440 | */ |
||
441 | View Code Duplication | public function setComments($comments) |
|
451 | |||
452 | /** |
||
453 | * @return string |
||
454 | */ |
||
455 | 1 | public function getComments() |
|
459 | |||
460 | /** |
||
461 | * @param $code |
||
462 | * |
||
463 | * @return $this |
||
464 | */ |
||
465 | public function setCurrencyCode($code) |
||
471 | |||
472 | /** |
||
473 | * @return string |
||
474 | */ |
||
475 | 1 | public function getCurrencyCode() |
|
479 | |||
480 | /** |
||
481 | * @param $additionalDocumentIndicator |
||
482 | * |
||
483 | * @return $this |
||
484 | */ |
||
485 | public function setAdditionalDocumentIndicator($additionalDocumentIndicator) |
||
489 | |||
490 | /** |
||
491 | * @return bool |
||
492 | */ |
||
493 | 1 | public function getAdditionalDocumentIndicator() |
|
497 | |||
498 | /** |
||
499 | * @param EEIFilingOption $eeiFilingOption |
||
500 | * |
||
501 | * @return $this |
||
502 | */ |
||
503 | 3 | public function setEEIFilingOption(EEIFilingOption $eeiFilingOption) |
|
509 | |||
510 | /** |
||
511 | * @return EEIFilingOption |
||
512 | */ |
||
513 | 3 | public function getEEIFilingOption() |
|
517 | } |
||
518 |
This check marks private properties in classes that are never used. Those properties can be removed.