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 Payment 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 Payment, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Payment extends MoipResource |
||
12 | { |
||
13 | /** |
||
14 | * @const string |
||
15 | */ |
||
16 | const PATH = 'payments'; |
||
17 | |||
18 | /** |
||
19 | * @const string |
||
20 | */ |
||
21 | const MULTI_PAYMENTS_PATH = 'multipayments'; |
||
22 | |||
23 | /** |
||
24 | * @const string |
||
25 | */ |
||
26 | const SIMULATOR_PATH = 'simulador'; |
||
27 | |||
28 | /** |
||
29 | * Payment means. |
||
30 | * |
||
31 | * @const string |
||
32 | */ |
||
33 | const METHOD_CREDIT_CARD = 'CREDIT_CARD'; |
||
34 | |||
35 | /** |
||
36 | * Payment means. |
||
37 | * |
||
38 | * @const string |
||
39 | */ |
||
40 | const METHOD_BOLETO = 'BOLETO'; |
||
41 | |||
42 | /** |
||
43 | * Payment means. |
||
44 | * |
||
45 | * @const string |
||
46 | */ |
||
47 | const METHOD_ONLINE_DEBIT = 'ONLINE_DEBIT'; |
||
48 | |||
49 | /** |
||
50 | * Payment means. |
||
51 | * |
||
52 | * @const string |
||
53 | */ |
||
54 | const METHOD_WALLET = 'WALLET'; |
||
55 | |||
56 | /** |
||
57 | * Payment means. |
||
58 | * |
||
59 | * @const string |
||
60 | */ |
||
61 | const METHOD_ONLINE_BANK_DEBIT = 'ONLINE_BANK_DEBIT'; |
||
62 | |||
63 | /** |
||
64 | * @var \Moip\Resource\Orders |
||
65 | */ |
||
66 | private $order; |
||
67 | |||
68 | /** |
||
69 | * Just created, but not initialized yet. |
||
70 | */ |
||
71 | const STATUS_CREATED = 'CREATED'; |
||
72 | |||
73 | /** |
||
74 | * Waiting for the payment. |
||
75 | */ |
||
76 | const STATUS_WAITING = 'WAITING'; |
||
77 | |||
78 | /** |
||
79 | * On risk analysis, it may be automatic or manual. |
||
80 | */ |
||
81 | const STATUS_IN_ANALYSIS = 'IN_ANALYSIS'; |
||
82 | |||
83 | /** |
||
84 | * The amount was reserved on client credit card, it may be caught or discarded until 5 days. |
||
85 | */ |
||
86 | const STATUS_PRE_AUTHORIZED = 'PRE_AUTHORIZED'; |
||
87 | |||
88 | /** |
||
89 | * Payment confirmed by the bank institution. |
||
90 | */ |
||
91 | const STATUS_AUTHORIZED = 'AUTHORIZED'; |
||
92 | |||
93 | /** |
||
94 | * Payment cancelled. |
||
95 | */ |
||
96 | const STATUS_CANCELLED = 'CANCELLED'; |
||
97 | |||
98 | /** |
||
99 | * Payment refunded. |
||
100 | */ |
||
101 | const STATUS_REFUNDED = 'REFUNDED'; |
||
102 | |||
103 | /** |
||
104 | * Paymend reversed (it means that the payment may was not recognized by the client). |
||
105 | */ |
||
106 | const STATUS_REVERSED = 'REVERSED'; |
||
107 | |||
108 | /** |
||
109 | * Payment finalized, the amout is on your account. |
||
110 | */ |
||
111 | const STATUS_SETTLED = 'SETTLED'; |
||
112 | |||
113 | /** |
||
114 | * @var \Moip\Resource\Multiorders |
||
115 | */ |
||
116 | private $multiorder; |
||
117 | |||
118 | /** |
||
119 | * Initializes new instances. |
||
120 | */ |
||
121 | protected function initialize() |
||
127 | |||
128 | /** |
||
129 | * Create a new payment in api MoIP. |
||
130 | * |
||
131 | * @return $this |
||
132 | */ |
||
133 | public function execute() |
||
145 | |||
146 | /** |
||
147 | * Get an payment and multipayment in MoIP. |
||
148 | * |
||
149 | * @param string $id_moip Id MoIP payment |
||
150 | * |
||
151 | * @return stdClass |
||
152 | */ |
||
153 | public function get($id_moip) |
||
161 | |||
162 | /** |
||
163 | * Get id MoIP payment. |
||
164 | * |
||
165 | * |
||
166 | * @return \Moip\Resource\Payment |
||
167 | */ |
||
168 | public function getId() |
||
172 | |||
173 | /** |
||
174 | * Mount payment structure. |
||
175 | * |
||
176 | * @param \stdClass $response |
||
177 | * |
||
178 | * @return Payment |
||
179 | */ |
||
180 | protected function populate(stdClass $response) |
||
202 | |||
203 | /** |
||
204 | * Refunds. |
||
205 | * |
||
206 | * @return Refund |
||
207 | */ |
||
208 | public function refunds() |
||
215 | |||
216 | /** |
||
217 | * Escrows. |
||
218 | * |
||
219 | * @return Escrow |
||
220 | */ |
||
221 | public function escrows() |
||
228 | |||
229 | /** |
||
230 | * Get payment status. |
||
231 | * |
||
232 | * @return string Payment status. Possible values CREATED, WAITING, IN_ANALYSIS, PRE_AUTHORIZED, AUTHORIZED, CANCELLED, REFUNDED, REVERSED, SETTLED |
||
233 | */ |
||
234 | public function getStatus() |
||
238 | |||
239 | /** |
||
240 | * get creation time. |
||
241 | * |
||
242 | * @return \DateTime |
||
243 | */ |
||
244 | public function getCreatedAt() |
||
248 | |||
249 | /** |
||
250 | * Returns when the last update occurred. |
||
251 | * |
||
252 | * @return \DateTime |
||
253 | */ |
||
254 | public function getUpdatedAt() |
||
258 | |||
259 | /** |
||
260 | * Returns the funding instrument. |
||
261 | * |
||
262 | * @return stdClass |
||
263 | */ |
||
264 | public function getFundingInstrument() |
||
269 | |||
270 | /** |
||
271 | * Get href to Boleto |
||
272 | * *. |
||
273 | * |
||
274 | * @return stdClass |
||
275 | */ |
||
276 | public function getHrefBoleto() |
||
280 | |||
281 | /** |
||
282 | * Get LineCode to Boleto |
||
283 | * *. |
||
284 | * |
||
285 | * @return stdClass |
||
286 | */ |
||
287 | public function getLineCodeBoleto() |
||
291 | |||
292 | /** |
||
293 | * Get href from print to Boleto |
||
294 | * *. |
||
295 | * |
||
296 | * @return stdClass |
||
297 | */ |
||
298 | public function getHrefPrintBoleto() |
||
302 | |||
303 | /** |
||
304 | * Get Expirate Date to Boleto |
||
305 | * *. |
||
306 | * |
||
307 | * @return stdClass |
||
308 | */ |
||
309 | public function getExpirationDateBoleto() |
||
313 | |||
314 | /** |
||
315 | * Returns payment amount. |
||
316 | * |
||
317 | * @return stdClass |
||
318 | */ |
||
319 | public function getAmount() |
||
323 | |||
324 | /** |
||
325 | * Returns escrow. |
||
326 | * |
||
327 | * @return stdClass |
||
328 | */ |
||
329 | public function getEscrow() |
||
333 | |||
334 | /** |
||
335 | * Returns order. |
||
336 | * |
||
337 | * @return Order |
||
338 | */ |
||
339 | public function getOrder() |
||
343 | |||
344 | /** |
||
345 | * Returns installment count. |
||
346 | * |
||
347 | * @return stdClass |
||
348 | */ |
||
349 | public function getInstallmentCount() |
||
353 | |||
354 | /** |
||
355 | * Get payments. |
||
356 | * |
||
357 | * @return array |
||
358 | */ |
||
359 | public function getPayments() |
||
363 | |||
364 | /** |
||
365 | * Set means of payment. |
||
366 | * |
||
367 | * @param \stdClass $fundingInstrument |
||
368 | * |
||
369 | * @return $this |
||
370 | */ |
||
371 | public function setFundingInstrument(stdClass $fundingInstrument) |
||
377 | |||
378 | /** |
||
379 | * Set billet. |
||
380 | * |
||
381 | * @param \DateTime|string $expirationDate Expiration date of a billet. |
||
382 | * @param string $logoUri Logo of billet. |
||
383 | * @param array $instructionLines Instructions billet. |
||
384 | * |
||
385 | * @return $this |
||
386 | */ |
||
387 | public function setBoleto($expirationDate, $logoUri, array $instructionLines = []) |
||
408 | |||
409 | /** |
||
410 | * Set credit card holder. |
||
411 | * |
||
412 | * @param \Moip\Resource\Customer $holder |
||
413 | */ |
||
414 | private function setCreditCardHolder(Holder $holder) |
||
432 | |||
433 | /** |
||
434 | * Set credit cardHash. |
||
435 | * |
||
436 | * @param string $hash Credit card hash encripted using Moip.js |
||
437 | * @param \Moip\Resource\Customer $holder |
||
438 | * @param bool $store Flag to know if credit card should be saved. |
||
439 | * |
||
440 | * @return $this |
||
441 | */ |
||
442 | public function setCreditCardHash($hash, Holder $holder, $store = true) |
||
452 | |||
453 | /** |
||
454 | * Set credit card |
||
455 | * Credit card used in a payment. |
||
456 | * The card when returned within a parent resource is presented in its minimum representation. |
||
457 | * |
||
458 | * @param int $expirationMonth Card expiration month |
||
459 | * @param int $expirationYear Year of card expiration. |
||
460 | * @param string $number Card number. |
||
461 | * @param int $cvc Card Security Code. |
||
462 | * @param \Moip\Resource\Customer $holder |
||
463 | * @param bool $store Flag to know if credit card should be saved. |
||
464 | * |
||
465 | * @return $this |
||
466 | */ |
||
467 | public function setCreditCard($expirationMonth, $expirationYear, $number, $cvc, Holder $holder, $store = true) |
||
480 | |||
481 | /** |
||
482 | * Sets data from a previously saved credit card |
||
483 | * Credit card used in a payment. |
||
484 | * Used when the credit card was saved with the customer and the payment made in a future date. |
||
485 | * |
||
486 | * @param string $creditCardId MoIP's Credit Card Id. |
||
487 | * @param int $cvc Card Security Code. |
||
488 | * |
||
489 | * @return $this |
||
490 | */ |
||
491 | public function setCreditCardSaved($creditCardId, $cvc) |
||
501 | |||
502 | /** |
||
503 | * Set installment count. |
||
504 | * |
||
505 | * @param int $installmentCount |
||
506 | * |
||
507 | * @return $this |
||
508 | */ |
||
509 | public function setInstallmentCount($installmentCount) |
||
515 | |||
516 | /** |
||
517 | * Set statement descriptor. |
||
518 | * |
||
519 | * @param string $statementDescriptor |
||
520 | * |
||
521 | * @return $this |
||
522 | */ |
||
523 | public function setStatementDescriptor($statementDescriptor) |
||
529 | |||
530 | /** |
||
531 | * Set payment means made available by banks. |
||
532 | * |
||
533 | * @param string $bankNumber Bank number. Possible values: 001, 237, 341, 041. |
||
534 | * @param \DateTime|string $expirationDate Date of expiration debit. |
||
535 | * @param string $returnUri Return Uri. |
||
536 | * |
||
537 | * @return $this |
||
538 | */ |
||
539 | public function setOnlineBankDebit($bankNumber, $expirationDate, $returnUri) |
||
552 | |||
553 | /** |
||
554 | * Set Multiorders. |
||
555 | * |
||
556 | * @param \Moip\Resource\Multiorders $multiorder |
||
557 | * |
||
558 | * @return $this |
||
559 | */ |
||
560 | public function setMultiorder(Multiorders $multiorder) |
||
566 | |||
567 | /** |
||
568 | * Set order. |
||
569 | * |
||
570 | * @param \Moip\Resource\Orders $order |
||
571 | * |
||
572 | * @return $this |
||
573 | */ |
||
574 | public function setOrder(Orders $order) |
||
580 | |||
581 | /** |
||
582 | * Turns on a delay on credit card payment capture (pre-authorization). |
||
583 | * |
||
584 | * @return $this |
||
585 | */ |
||
586 | public function setDelayCapture() |
||
592 | |||
593 | /** |
||
594 | * Set escrow to a payment. |
||
595 | * |
||
596 | * @param string $description |
||
597 | * |
||
598 | * @return $this |
||
599 | */ |
||
600 | public function setEscrow($description) |
||
607 | |||
608 | /** |
||
609 | * Capture a pre-authorized amount on a credit card payment. |
||
610 | * |
||
611 | * @throws \Exception |
||
612 | * |
||
613 | * @return Payment |
||
614 | */ |
||
615 | View Code Duplication | public function capture() |
|
626 | |||
627 | /** |
||
628 | * Cancel a pre-authorized amount on a credit card payment. |
||
629 | * |
||
630 | * @throws \Exception |
||
631 | * |
||
632 | * @return Payment |
||
633 | */ |
||
634 | View Code Duplication | public function cancel() |
|
645 | |||
646 | /** |
||
647 | * Cancel a pre-authorized amount on a credit card payment. |
||
648 | * |
||
649 | * @throws \Exception |
||
650 | * |
||
651 | * @return Payment |
||
652 | */ |
||
653 | public function avoid() |
||
659 | |||
660 | /** |
||
661 | * Authorize a payment (Available only in sandbox to credit card payment with status IN_ANALYSIS and billet payment with status WAITING). |
||
662 | * |
||
663 | * @return bool |
||
664 | */ |
||
665 | public function authorize($amount = null) |
||
679 | |||
680 | private function isMultipayment($paymentId) |
||
684 | } |
||
685 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.