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 AbstractPaymentRequest 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 AbstractPaymentRequest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | abstract class AbstractPaymentRequest extends AbstractRequest |
||
17 | { |
||
18 | protected $brandsmap = array( |
||
19 | 'Acceptgiro' => 'Acceptgiro', |
||
20 | 'AIRPLUS' => 'CreditCard', |
||
21 | 'American Express' => 'CreditCard', |
||
22 | 'Aurora' => 'CreditCard', |
||
23 | 'Aurore' => 'CreditCard', |
||
24 | 'Bank transfer' => 'Bank transfer', |
||
25 | 'Bank transfer BE' => 'Bank transfer BE', |
||
26 | 'Bank transfer DE' => 'Bank transfer DE', |
||
27 | 'Bank transfer FR' => 'Bank transfer FR', |
||
28 | 'Bank transfer NL' => 'Bank transfer NL', |
||
29 | 'BCMC' => 'CreditCard', |
||
30 | 'Belfius Direct Net' => 'Belfius Direct Net', |
||
31 | 'Billy' => 'CreditCard', |
||
32 | 'cashU' => 'cashU', |
||
33 | 'CB' => 'CreditCard', |
||
34 | 'CBC Online' => 'CBC Online', |
||
35 | 'CENTEA Online' => 'CENTEA Online', |
||
36 | 'Cofinoga' => 'CreditCard', |
||
37 | 'Dankort' => 'CreditCard', |
||
38 | 'Dexia Direct Net' => 'Dexia Direct Net', |
||
39 | 'Diners Club' => 'CreditCard', |
||
40 | 'Direct Debits AT' => 'Direct Debits AT', |
||
41 | 'Direct Debits DE' => 'Direct Debits DE', |
||
42 | 'Direct Debits NL' => 'Direct Debits NL', |
||
43 | 'DirectEbankingDE' => 'DirectEbankingDE', |
||
44 | 'DirectEbankingAT' => 'DirectEbankingAT', |
||
45 | 'DirectEbankingIT' => 'DirectEbankingIT', |
||
46 | 'DirectEbankingBE' => 'DirectEbankingBE', |
||
47 | 'DirectEbankingFR' => 'DirectEbankingFR', |
||
48 | 'eDankort' => 'eDankort', |
||
49 | 'EPS' => 'EPS', |
||
50 | 'Fortis Pay Button' => 'Fortis Pay Button', |
||
51 | 'giropay' => 'giropay', |
||
52 | 'iDEAL' => 'iDEAL', |
||
53 | 'ING HomePay' => 'ING HomePay', |
||
54 | 'InterSolve' => 'InterSolve', |
||
55 | 'JCB' => 'CreditCard', |
||
56 | 'KBC Online' => 'KBC Online', |
||
57 | 'Maestro' => 'CreditCard', |
||
58 | 'MaestroUK' => 'CreditCard', |
||
59 | 'MasterCard' => 'CreditCard', |
||
60 | 'MiniTix' => 'MiniTix', |
||
61 | 'MPASS' => 'MPASS', |
||
62 | 'NetReserve' => 'CreditCard', |
||
63 | 'Payment on Delivery' => 'Payment on Delivery', |
||
64 | 'PAYPAL' => 'PAYPAL', |
||
65 | 'paysafecard' => 'paysafecard', |
||
66 | 'PingPing' => 'PingPing', |
||
67 | 'PostFinance + card' => 'PostFinance Card', |
||
68 | 'PostFinance e-finance' => 'PostFinance e-finance', |
||
69 | 'PRIVILEGE' => 'CreditCard', |
||
70 | 'Sofort Uberweisung' => 'DirectEbanking', |
||
71 | 'Solo' => 'CreditCard', |
||
72 | 'TUNZ' => 'TUNZ', |
||
73 | 'UATP' => 'CreditCard', |
||
74 | 'UNEUROCOM' => 'UNEUROCOM', |
||
75 | 'VISA' => 'CreditCard', |
||
76 | 'Wallie' => 'Wallie', |
||
77 | ); |
||
78 | |||
79 | /** Note this is public to allow easy modification, if need be. */ |
||
80 | public $allowedcurrencies = array( |
||
81 | 'AED', |
||
82 | 'ANG', |
||
83 | 'ARS', |
||
84 | 'AUD', |
||
85 | 'AWG', |
||
86 | 'BGN', |
||
87 | 'BRL', |
||
88 | 'BYR', |
||
89 | 'CAD', |
||
90 | 'CHF', |
||
91 | 'CNY', |
||
92 | 'CZK', |
||
93 | 'DKK', |
||
94 | 'EEK', |
||
95 | 'EGP', |
||
96 | 'EUR', |
||
97 | 'GBP', |
||
98 | 'GEL', |
||
99 | 'HKD', |
||
100 | 'HRK', |
||
101 | 'HUF', |
||
102 | 'ILS', |
||
103 | 'ISK', |
||
104 | 'JPY', |
||
105 | 'KRW', |
||
106 | 'LTL', |
||
107 | 'LVL', |
||
108 | 'MAD', |
||
109 | 'MXN', |
||
110 | 'MYR', |
||
111 | 'NOK', |
||
112 | 'NZD', |
||
113 | 'PLN', |
||
114 | 'RON', |
||
115 | 'RUB', |
||
116 | 'SEK', |
||
117 | 'SGD', |
||
118 | 'SKK', |
||
119 | 'THB', |
||
120 | 'TRY', |
||
121 | 'UAH', |
||
122 | 'USD', |
||
123 | 'XAF', |
||
124 | 'XOF', |
||
125 | 'XPF', |
||
126 | 'ZAR' |
||
127 | ); |
||
128 | |||
129 | 15 | View Code Duplication | public function setOrderid($orderid) |
|
|||
130 | { |
||
131 | 15 | if (strlen($orderid) > 40) { |
|
132 | 1 | throw new InvalidArgumentException("Orderid cannot be longer than 40 characters"); |
|
133 | } |
||
134 | 14 | if (preg_match('/[^a-zA-Z0-9_-]/', $orderid)) { |
|
135 | 1 | throw new InvalidArgumentException("Order id cannot contain special characters"); |
|
136 | } |
||
137 | 13 | $this->parameters['orderid'] = $orderid; |
|
138 | 13 | } |
|
139 | |||
140 | /** Friend alias for setCom() */ |
||
141 | 2 | public function setOrderDescription($orderDescription) |
|
145 | |||
146 | 2 | public function setCom($com) |
|
147 | { |
||
148 | 2 | if (strlen($com) > 100) { |
|
149 | 1 | throw new InvalidArgumentException("Order description cannot be longer than 100 characters"); |
|
150 | } |
||
151 | 1 | $this->parameters['com'] = $com; |
|
152 | 1 | } |
|
153 | |||
154 | /** |
||
155 | * Set amount in cents, eg EUR 12.34 is written as 1234 |
||
156 | */ |
||
157 | 14 | View Code Duplication | public function setAmount($amount) |
170 | |||
171 | 14 | public function setCurrency($currency) |
|
178 | |||
179 | 8 | View Code Duplication | public function setEmail($email) |
189 | |||
190 | 7 | public function setOwnerAddress($owneraddress) |
|
197 | |||
198 | 7 | public function setOwnerZip($ownerzip) |
|
205 | |||
206 | 7 | public function setOwnerTown($ownertown) |
|
213 | |||
214 | /** |
||
215 | * Alias for setOwnercty |
||
216 | * |
||
217 | * @see http://www.iso.org/iso/country_codes/iso_3166_code_lists/english_country_names_and_code_elements.htm |
||
218 | */ |
||
219 | 7 | public function setOwnerCountry($ownercountry) |
|
223 | |||
224 | /** |
||
225 | * @see http://www.iso.org/iso/country_codes/iso_3166_code_lists/english_country_names_and_code_elements.htm |
||
226 | */ |
||
227 | 7 | public function setOwnercty($ownercty) |
|
234 | |||
235 | /** Alias for setOwnertelno() */ |
||
236 | 2 | public function setOwnerPhone($ownerphone) |
|
240 | |||
241 | 2 | public function setOwnertelno($ownertelno) |
|
248 | |||
249 | /** Alias for setComplus() */ |
||
250 | 1 | public function setFeedbackMessage($feedbackMessage) |
|
254 | |||
255 | 1 | public function setComplus($complus) |
|
259 | |||
260 | 2 | public function setBrand($brand) |
|
269 | |||
270 | 2 | public function setPaymentMethod($paymentMethod) |
|
274 | |||
275 | 2 | public function setPm($pm) |
|
282 | |||
283 | 2 | public function setParamvar($paramvar) |
|
290 | |||
291 | /** Alias for setTp */ |
||
292 | 2 | public function setDynamicTemplateUri($uri) |
|
297 | |||
298 | /** Alias for setTp */ |
||
299 | public function setStaticTemplate($tp) |
||
303 | |||
304 | 1 | public function setTp($tp) |
|
308 | |||
309 | 3 | public function setOperation(PaymentOperation $operation) |
|
313 | |||
314 | abstract protected function getValidOperations(); |
||
315 | } |
||
316 |
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.