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:
1 | <?php |
||
17 | class GatewayDataFormatter implements GatewayDataFormatterInterface |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * Gets the text to use for a gateway's line item name when this is a partial payment |
||
22 | * |
||
23 | * @param \EEI_Payment $payment |
||
24 | * @return string |
||
25 | */ |
||
26 | public function formatPartialPaymentLineItemName(\EEI_Payment $payment) |
||
35 | |||
36 | |||
37 | |||
38 | /** |
||
39 | * Gets the text to use for a gateway's line item description when this is a partial payment |
||
40 | * |
||
41 | * @param \EEI_Payment $payment |
||
42 | * @return string |
||
43 | */ |
||
44 | View Code Duplication | public function formatPartialPaymentLineItemDesc(\EEI_Payment $payment) |
|
57 | |||
58 | |||
59 | |||
60 | /** |
||
61 | * Gets the name to use for a line item when sending line items to the gateway |
||
62 | * |
||
63 | * @param \EEI_Line_Item $line_item |
||
64 | * @param \EEI_Payment $payment |
||
65 | * @return string |
||
66 | */ |
||
67 | public function formatLineItemName(\EEI_Line_Item $line_item, \EEI_Payment $payment) |
||
81 | |||
82 | |||
83 | |||
84 | /** |
||
85 | * Gets the description to use for a line item when sending line items to the gateway |
||
86 | * |
||
87 | * @param \EEI_Line_Item $line_item |
||
88 | * @param \EEI_Payment $payment |
||
89 | * @return string |
||
90 | */ |
||
91 | public function formatLineItemDesc(\EEI_Line_Item $line_item, \EEI_Payment $payment) |
||
101 | |||
102 | |||
103 | |||
104 | /** |
||
105 | * Gets the order description that should generally be sent to gateways |
||
106 | * |
||
107 | * @param \EEI_Payment $payment |
||
108 | * @return string |
||
109 | */ |
||
110 | View Code Duplication | public function formatOrderDescription(\EEI_Payment $payment) |
|
123 | |||
124 | |||
125 | |||
126 | /** |
||
127 | * Formats the amount so it can generally be sent to gateways |
||
128 | * |
||
129 | * @param float $amount |
||
130 | * @return string |
||
131 | */ |
||
132 | public function formatCurrency($amount) |
||
136 | } |
||
137 | // End of file GatewayDataFormatter.php |
||
138 | // Location: core\services\gateways/GatewayDataFormatter.php |
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.