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 | * @return array |
||
139 | */ |
||
140 | public static function getTypes() |
||
144 | |||
145 | /** |
||
146 | * @return string |
||
147 | */ |
||
148 | public function getTypeName() |
||
152 | |||
153 | /** |
||
154 | * @param null|object $attributes |
||
155 | */ |
||
156 | public function __construct($attributes = null) |
||
157 | { |
||
158 | if (null !== $attributes) { |
||
159 | if (isset($attributes->FormType)) { |
||
160 | $this->setType($attributes->FormType); |
||
161 | } |
||
162 | if (isset($attributes->InvoiceNumber)) { |
||
163 | $this->setInvoiceNumber($attributes->InvoiceNumber); |
||
164 | } |
||
165 | if (isset($attributes->InvoiceDate)) { |
||
166 | $this->setInvoiceDate(new DateTime($attributes->InvoiceDate)); |
||
167 | } |
||
168 | if (isset($attributes->PurchaseOrderNumber)) { |
||
169 | $this->setPurchaseOrderNumber($attributes->PurchaseOrderNumber); |
||
170 | } |
||
171 | if (isset($attributes->TermsOfShipment)) { |
||
172 | $this->setTermsOfShipment($attributes->TermsOfShipment); |
||
173 | } |
||
174 | if (isset($attributes->Comments)) { |
||
175 | $this->setComments($attributes->Comments); |
||
176 | } |
||
177 | if (isset($attributes->CurrencyCode)) { |
||
178 | $this->setCurrencyCode($attributes->CurrencyCode); |
||
179 | } |
||
180 | } |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @param $type string |
||
185 | * |
||
186 | * @return $this |
||
187 | */ |
||
188 | public function setType($type) |
||
189 | { |
||
190 | $this->type = $type; |
||
191 | |||
192 | return $this; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * @return string |
||
197 | */ |
||
198 | public function getType() |
||
202 | |||
203 | /** |
||
204 | * @param $freightCharges FreightCharges |
||
205 | * |
||
206 | * @return $this |
||
207 | */ |
||
208 | public function setFreightCharges(FreightCharges $freightCharges) |
||
209 | { |
||
210 | $this->freightCharges = $freightCharges; |
||
211 | |||
212 | return $this; |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * @return FreightCharges |
||
217 | */ |
||
218 | public function getFreightCharges() |
||
222 | |||
223 | /** |
||
224 | * @param $discount Discount |
||
225 | * |
||
226 | * @return $this |
||
227 | */ |
||
228 | public function setDiscount(Discount $discount) |
||
229 | { |
||
230 | $this->discount = $discount; |
||
231 | |||
232 | return $this; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * @return Discount |
||
237 | */ |
||
238 | public function getDiscount() |
||
242 | |||
243 | /** |
||
244 | * @param Product $product |
||
245 | * |
||
246 | * @return $this |
||
247 | */ |
||
248 | public function addProduct(Product $product) |
||
249 | { |
||
250 | array_push($this->products, $product); |
||
251 | |||
252 | return $this; |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * @return array |
||
257 | */ |
||
258 | public function getProducts() |
||
262 | |||
263 | /** |
||
264 | * @param null|DOMDocument $document |
||
265 | * |
||
266 | * @return DOMElement |
||
267 | */ |
||
268 | public function toNode(DOMDocument $document = null) |
||
269 | { |
||
270 | if (null === $document) { |
||
271 | $document = new DOMDocument(); |
||
272 | } |
||
273 | |||
274 | $node = $document->createElement('InternationalForms'); |
||
275 | |||
276 | if ($this->getType()) { |
||
277 | $node->appendChild($document->createElement('FormType', $this->getType())); |
||
278 | } |
||
279 | if ($this->getInvoiceNumber() !== null) { |
||
280 | $node->appendChild($document->createElement('InvoiceNumber', $this->getInvoiceNumber())); |
||
281 | } |
||
282 | if ($this->getInvoiceDate() !== null) { |
||
283 | $node->appendChild($document->createElement('InvoiceDate', $this->getInvoiceDate()->format('Ymd'))); |
||
284 | } |
||
285 | if ($this->getPurchaseOrderNumber() !== null) { |
||
286 | $node->appendChild($document->createElement('PurchaseOrderNumber', $this->getPurchaseOrderNumber())); |
||
287 | } |
||
288 | if ($this->getTermsOfShipment() !== null) { |
||
289 | $node->appendChild($document->createElement('TermsOfShipment', $this->getTermsOfShipment())); |
||
290 | } |
||
291 | if ($this->getReasonForExport() !== null) { |
||
292 | $node->appendChild($document->createElement('ReasonForExport', $this->getReasonForExport())); |
||
293 | } |
||
294 | if ($this->getComments() !== null) { |
||
295 | $node->appendChild($document->createElement('Comments', $this->getComments())); |
||
296 | } |
||
297 | if ($this->getCurrencyCode() !== null) { |
||
298 | $node->appendChild($document->createElement('CurrencyCode', $this->getCurrencyCode())); |
||
299 | } |
||
300 | if ($this->getDiscount() !== null) { |
||
301 | $node->appendChild($this->getDiscount()->toNode($document)); |
||
302 | } |
||
303 | if ($this->getFreightCharges() !== null) { |
||
304 | $node->appendChild($this->getFreightCharges()->toNode($document)); |
||
305 | } |
||
306 | foreach ($this->products as $product) { |
||
307 | $node->appendChild($product->toNode($document)); |
||
308 | } |
||
309 | |||
310 | return $node; |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * @param $number string |
||
315 | * |
||
316 | * @return $this |
||
317 | */ |
||
318 | public function setInvoiceNumber($number) |
||
319 | { |
||
320 | $this->invoiceNumber = $number; |
||
321 | |||
322 | return $this; |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * @return string |
||
327 | */ |
||
328 | public function getInvoiceNumber() |
||
332 | |||
333 | /** |
||
334 | * @param DateTime $date |
||
335 | * |
||
336 | * @return $this |
||
337 | */ |
||
338 | public function setInvoiceDate(DateTime $date) |
||
339 | { |
||
340 | $this->invoiceDate = $date; |
||
341 | |||
342 | return $this; |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * @return DateTime |
||
347 | */ |
||
348 | public function getInvoiceDate() |
||
352 | |||
353 | /** |
||
354 | * @param $number |
||
355 | * |
||
356 | * @return $this |
||
357 | */ |
||
358 | public function setPurchaseOrderNumber($number) |
||
359 | { |
||
360 | $this->purchaseOrderNumber = $number; |
||
361 | |||
362 | return $this; |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * @return string |
||
367 | */ |
||
368 | public function getPurchaseOrderNumber() |
||
372 | |||
373 | /** |
||
374 | * @param $terms |
||
375 | * |
||
376 | * @return $this |
||
377 | */ |
||
378 | public function setTermsOfShipment($terms) |
||
379 | { |
||
380 | $this->termsOfShipment = $terms; |
||
381 | |||
382 | return $this; |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * @return string |
||
387 | */ |
||
388 | public function getTermsOfShipment() |
||
392 | |||
393 | /** |
||
394 | * @param $reason |
||
395 | * |
||
396 | * @return $this |
||
397 | */ |
||
398 | public function setReasonForExport($reason) |
||
399 | { |
||
400 | if (strlen($reason) > 20) { |
||
401 | $reason = substr($reason, 0, 20); |
||
402 | } |
||
403 | |||
404 | $this->reasonForExport = $reason; |
||
405 | |||
406 | return $this; |
||
407 | } |
||
408 | |||
409 | /** |
||
410 | * @return string |
||
411 | */ |
||
412 | public function getReasonForExport() |
||
416 | |||
417 | /** |
||
418 | * @param $comments |
||
419 | * |
||
420 | * @return $this |
||
421 | */ |
||
422 | View Code Duplication | public function setComments($comments) |
|
423 | { |
||
424 | if (strlen($comments) > 150) { |
||
425 | $comments = substr($comments, 0, 150); |
||
426 | } |
||
427 | |||
428 | $this->comments = $comments; |
||
429 | |||
430 | return $this; |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * @return string |
||
435 | */ |
||
436 | public function getComments() |
||
440 | |||
441 | /** |
||
442 | * @param $code |
||
443 | * |
||
444 | * @return $this |
||
445 | */ |
||
446 | public function setCurrencyCode($code) |
||
447 | { |
||
448 | $this->currencyCode = $code; |
||
449 | |||
450 | return $this; |
||
451 | } |
||
452 | |||
453 | /** |
||
454 | * @return string |
||
455 | */ |
||
456 | public function getCurrencyCode() |
||
460 | } |
||
461 |
This check marks private properties in classes that are never used. Those properties can be removed.