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) |
|
142 | { |
||
143 | 2 | $this->setCom($orderDescription); |
|
144 | 1 | } |
|
145 | |||
146 | 2 | View Code Duplication | 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) |
158 | { |
||
159 | 14 | if (!is_int($amount)) { |
|
160 | 1 | throw new InvalidArgumentException("Integer expected. Amount is always in cents"); |
|
161 | } |
||
162 | 13 | if ($amount <= 0) { |
|
163 | 1 | throw new InvalidArgumentException("Amount must be a positive number"); |
|
164 | } |
||
165 | 12 | if ($amount >= 1.0E+15) { |
|
166 | 1 | throw new InvalidArgumentException("Amount is too high"); |
|
167 | } |
||
168 | 11 | $this->parameters['amount'] = $amount; |
|
169 | 11 | } |
|
170 | |||
171 | 14 | public function setCurrency($currency) |
|
172 | { |
||
173 | 14 | if (!in_array(strtoupper($currency), $this->allowedcurrencies)) { |
|
174 | 1 | throw new InvalidArgumentException("Unknown currency"); |
|
175 | } |
||
176 | 13 | $this->parameters['currency'] = $currency; |
|
177 | 13 | } |
|
178 | |||
179 | 8 | View Code Duplication | public function setEmail($email) |
180 | { |
||
181 | 8 | if (strlen($email) > 50) { |
|
182 | 1 | throw new InvalidArgumentException("Email is too long"); |
|
183 | } |
||
184 | 7 | if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { |
|
185 | 1 | throw new InvalidArgumentException("Email is invalid"); |
|
186 | } |
||
187 | 6 | $this->parameters['email'] = $email; |
|
188 | 6 | } |
|
189 | |||
190 | 7 | public function setOwnerAddress($owneraddress) |
|
191 | { |
||
192 | 7 | if (strlen($owneraddress) > 50) { |
|
193 | 1 | throw new InvalidArgumentException("Owner address is too long"); |
|
194 | } |
||
195 | 6 | $this->parameters['owneraddress'] = $owneraddress; |
|
196 | 6 | } |
|
197 | |||
198 | 7 | public function setOwnerZip($ownerzip) |
|
199 | { |
||
200 | 7 | if (strlen($ownerzip) > 10) { |
|
201 | 1 | throw new InvalidArgumentException("Owner Zip is too long"); |
|
202 | } |
||
203 | 6 | $this->parameters['ownerzip'] = $ownerzip; |
|
204 | 6 | } |
|
205 | |||
206 | 7 | public function setOwnerTown($ownertown) |
|
207 | { |
||
208 | 7 | if (strlen($ownertown) > 40) { |
|
209 | 1 | throw new InvalidArgumentException("Owner town is too long"); |
|
210 | } |
||
211 | 6 | $this->parameters['ownertown'] = $ownertown; |
|
212 | 6 | } |
|
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) |
|
220 | { |
||
221 | 7 | $this->setOwnercty($ownercountry); |
|
222 | 6 | } |
|
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 | View Code Duplication | public function setOwnercty($ownercty) |
228 | { |
||
229 | 7 | if (strlen($ownercty) > 2) { |
|
230 | 1 | throw new InvalidArgumentException("Owner country code is too long"); |
|
231 | } |
||
232 | 6 | if (!preg_match('/^[A-Z]{2}$/', strtoupper($ownercty))) { |
|
233 | throw new InvalidArgumentException("Illegal country code"); |
||
234 | } |
||
235 | 6 | $this->parameters['ownercty'] = strtoupper($ownercty); |
|
236 | 6 | } |
|
237 | |||
238 | /** Alias for setOwnertelno() */ |
||
239 | 2 | public function setOwnerPhone($ownerphone) |
|
240 | { |
||
241 | 2 | $this->setOwnertelno($ownerphone); |
|
242 | 1 | } |
|
243 | |||
244 | 2 | public function setOwnertelno($ownertelno) |
|
245 | { |
||
246 | 2 | if (strlen($ownertelno) > 30) { |
|
247 | 1 | throw new InvalidArgumentException("Owner phone is too long"); |
|
248 | } |
||
249 | 1 | $this->parameters['ownertelno'] = $ownertelno; |
|
250 | 1 | } |
|
251 | |||
252 | /** Alias for setComplus() */ |
||
253 | 1 | public function setFeedbackMessage($feedbackMessage) |
|
254 | { |
||
255 | 1 | $this->setComplus($feedbackMessage); |
|
256 | 1 | } |
|
257 | |||
258 | 1 | public function setComplus($complus) |
|
259 | { |
||
260 | 1 | $this->parameters['complus'] = $complus; |
|
261 | 1 | } |
|
262 | |||
263 | 2 | public function setBrand($brand) |
|
264 | { |
||
265 | 2 | if (!array_key_exists($brand, $this->brandsmap)) { |
|
266 | 1 | throw new InvalidArgumentException("Unknown Brand [$brand]."); |
|
267 | } |
||
268 | |||
269 | 1 | $this->setPaymentMethod($this->brandsmap[$brand]); |
|
270 | 1 | $this->parameters['brand'] = $brand; |
|
271 | 1 | } |
|
272 | |||
273 | 2 | public function setPaymentMethod($paymentMethod) |
|
274 | { |
||
275 | 2 | $this->setPm($paymentMethod); |
|
276 | 1 | } |
|
277 | |||
278 | 2 | public function setPm($pm) |
|
279 | { |
||
280 | 2 | if (!in_array($pm, $this->brandsmap)) { |
|
281 | 1 | throw new InvalidArgumentException("Unknown Payment method [$pm]."); |
|
282 | } |
||
283 | 1 | $this->parameters['pm'] = $pm; |
|
284 | 1 | } |
|
285 | |||
286 | 2 | public function setParamvar($paramvar) |
|
287 | { |
||
288 | 2 | if (strlen($paramvar) < 2 || strlen($paramvar) > 50) { |
|
289 | 1 | throw new InvalidArgumentException("Paramvar must be between 2 and 50 characters in length"); |
|
290 | } |
||
291 | 1 | $this->parameters['paramvar'] = $paramvar; |
|
292 | 1 | } |
|
293 | |||
294 | /** Alias for setTp */ |
||
295 | 2 | public function setDynamicTemplateUri($uri) |
|
296 | { |
||
297 | 2 | $this->validateUri($uri); |
|
298 | 1 | $this->setTp($uri); |
|
299 | 1 | } |
|
300 | |||
301 | /** Alias for setTp */ |
||
302 | public function setStaticTemplate($tp) |
||
303 | { |
||
304 | $this->setTp($tp); |
||
305 | } |
||
306 | |||
307 | 1 | public function setTp($tp) |
|
308 | { |
||
309 | 1 | $this->parameters['tp'] = $tp; |
|
310 | 1 | } |
|
311 | |||
312 | 3 | public function setOperation(PaymentOperation $operation) |
|
313 | { |
||
314 | 3 | $this->parameters['operation'] = (string) $operation; |
|
315 | 3 | } |
|
316 | |||
317 | abstract protected function getValidOperations(); |
||
318 | } |
||
319 |
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.