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 Paybox 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 Paybox, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | final class Paybox |
||
18 | { |
||
19 | const VERSION_DIRECT = '00103'; |
||
20 | const VERSION_DIRECT_PLUS = '00104'; |
||
21 | |||
22 | const VERSIONS = [ |
||
23 | 'direct' => self::VERSION_DIRECT, |
||
24 | 'direct_plus' => self::VERSION_DIRECT_PLUS, |
||
25 | ]; |
||
26 | |||
27 | const CURRENCY_EURO = 978; |
||
28 | const CURRENCY_US_DOLLAR = 840; |
||
29 | const CURRENCY_CFA = 952; |
||
30 | |||
31 | const CURRENCIES = [ |
||
32 | 'euro' => self::CURRENCY_EURO, |
||
33 | 'us_dollar' => self::CURRENCY_US_DOLLAR, |
||
34 | 'cfa' => self::CURRENCY_CFA, |
||
35 | ]; |
||
36 | |||
37 | const API_URL_PRODUCTION = 'https://ppps.paybox.com/PPPS.php'; |
||
38 | const API_URL_RESCUE = 'https://ppps1.paybox.com/PPPS.php'; |
||
39 | const API_URL_TEST = 'https://preprod-ppps.paybox.com/PPPS.php'; |
||
40 | |||
41 | /** |
||
42 | * @var AbstractHttpClient |
||
43 | */ |
||
44 | private $httpClient; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | private $options; |
||
50 | |||
51 | public function __construct(array $options = [], AbstractHttpClient $httpClient = null) |
||
62 | |||
63 | /** |
||
64 | * @param string $DATEVAL |
||
65 | * @param int $MONTANT |
||
66 | * @param string $PORTEUR |
||
67 | * @param string $REFERENCE |
||
68 | * @param string|null $AUTORISATION |
||
69 | * @param int|null $DEVISE |
||
70 | * |
||
71 | * @return PayboxResponse |
||
72 | * |
||
73 | * @throws Exception\PayboxException |
||
74 | */ |
||
75 | View Code Duplication | public function authorize($DATEVAL, $MONTANT, $PORTEUR, $REFERENCE, $AUTORISATION = null, $DEVISE = null) |
|
93 | |||
94 | /** |
||
95 | * @param int $MONTANT |
||
96 | * @param int $NUMAPPEL |
||
97 | * @param int $NUMTRANS |
||
98 | * @param string $REFERENCE |
||
99 | * @param int|null $DEVISE |
||
100 | * |
||
101 | * @return PayboxResponse |
||
102 | * |
||
103 | * @throws Exception\PayboxException |
||
104 | */ |
||
105 | View Code Duplication | public function debit($MONTANT, $NUMAPPEL, $NUMTRANS, $REFERENCE, $DEVISE = null) |
|
120 | |||
121 | /** |
||
122 | * @param string $DATEVAL |
||
123 | * @param int $MONTANT |
||
124 | * @param string $PORTEUR |
||
125 | * @param string $REFERENCE |
||
126 | * @param string|null $AUTORISATION |
||
127 | * @param int|null $DEVISE |
||
128 | * |
||
129 | * @return PayboxResponse |
||
130 | * |
||
131 | * @throws Exception\PayboxException |
||
132 | */ |
||
133 | View Code Duplication | public function authorizeAndCapture($DATEVAL, $MONTANT, $PORTEUR, $REFERENCE, $AUTORISATION = null, $DEVISE = null) |
|
151 | |||
152 | /** |
||
153 | * @param string $DATEVAL |
||
154 | * @param int $MONTANT |
||
155 | * @param string $PORTEUR |
||
156 | * @param string $REFERENCE |
||
157 | * @param int|null $DEVISE |
||
158 | * |
||
159 | * @return PayboxResponse |
||
160 | * |
||
161 | * @throws Exception\PayboxException |
||
162 | */ |
||
163 | View Code Duplication | public function credit($DATEVAL, $MONTANT, $PORTEUR, $REFERENCE, $DEVISE = null) |
|
178 | |||
179 | /** |
||
180 | * @param int $MONTANT |
||
181 | * @param int $NUMAPPEL |
||
182 | * @param int $NUMTRANSL |
||
183 | * @param string $REFERENCE |
||
184 | * @param int|null $DEVISE |
||
185 | * |
||
186 | * @return PayboxResponse |
||
187 | * |
||
188 | * @throws Exception\PayboxException |
||
189 | */ |
||
190 | View Code Duplication | public function cancel($MONTANT, $NUMAPPEL, $NUMTRANSL, $REFERENCE, $DEVISE = null) |
|
205 | |||
206 | /** |
||
207 | * @param int $MONTANT |
||
208 | * @param string $REFERENCE |
||
209 | * @param int|null $DEVISE |
||
210 | * |
||
211 | * @return PayboxResponse |
||
212 | * |
||
213 | * @throws Exception\PayboxException |
||
214 | */ |
||
215 | public function check($MONTANT, $REFERENCE, $DEVISE = null) |
||
228 | |||
229 | /** |
||
230 | * @param string $DATEVAL |
||
231 | * @param int $MONTANT |
||
232 | * @param string $PORTEUR |
||
233 | * @param string $REFERENCE |
||
234 | * @param int|null $DEVISE |
||
235 | * |
||
236 | * @return PayboxResponse |
||
237 | * |
||
238 | * @throws Exception\PayboxException |
||
239 | */ |
||
240 | View Code Duplication | public function transact($DATEVAL, $MONTANT, $PORTEUR, $REFERENCE, $DEVISE = null) |
|
255 | |||
256 | /** |
||
257 | * @param int $MONTANT |
||
258 | * @param int $NUMAPPEL |
||
259 | * @param int $NUMTRANS |
||
260 | * @param string|null $AUTORISATION |
||
261 | * @param int|null $DEVISE |
||
262 | * |
||
263 | * @return PayboxResponse |
||
264 | * |
||
265 | * @throws Exception\PayboxException |
||
266 | */ |
||
267 | View Code Duplication | public function updateAmount($MONTANT, $NUMAPPEL, $NUMTRANS, $AUTORISATION = null, $DEVISE = null) |
|
284 | |||
285 | /** |
||
286 | * @param int $MONTANT |
||
287 | * @param int $NUMAPPEL |
||
288 | * @param int $NUMTRANS |
||
289 | * @param int|null $DEVISE |
||
290 | * |
||
291 | * @return PayboxResponse |
||
292 | * |
||
293 | * @throws Exception\PayboxException |
||
294 | */ |
||
295 | public function refund($MONTANT, $NUMAPPEL, $NUMTRANS, $DEVISE = null) |
||
309 | |||
310 | /** |
||
311 | * @param int $NUMTRANS |
||
312 | * |
||
313 | * @return PayboxResponse |
||
314 | * |
||
315 | * @throws Exception\PayboxException |
||
316 | */ |
||
317 | public function inquiry($NUMTRANS) |
||
325 | |||
326 | /** |
||
327 | * @param string $DATEVAL |
||
328 | * @param string $PORTEUR |
||
329 | * @param string $REFABONNE |
||
330 | * @param string $REFERENCE |
||
331 | * @param string|null $AUTORISATION |
||
332 | * @param string|null $DEVISE |
||
333 | * |
||
334 | * @return PayboxResponse |
||
335 | * |
||
336 | * @throws Exception\PayboxException |
||
337 | */ |
||
338 | View Code Duplication | public function authorizeSubscriber($DATEVAL, $PORTEUR, $REFABONNE, $REFERENCE, $AUTORISATION = null, $DEVISE = null) |
|
356 | |||
357 | /** |
||
358 | * @param int $MONTANT |
||
359 | * @param int $NUMAPPEL |
||
360 | * @param int $NUMTRANS |
||
361 | * @param string $REFERENCE |
||
362 | * @param int|null $DEVISE |
||
363 | * |
||
364 | * @return PayboxResponse |
||
365 | * |
||
366 | * @throws Exception\PayboxException |
||
367 | */ |
||
368 | View Code Duplication | public function debitSubscriber($MONTANT, $NUMAPPEL, $NUMTRANS, $REFERENCE, $DEVISE = null) |
|
383 | |||
384 | /** |
||
385 | * @param string $DATEVAL |
||
386 | * @param int $MONTANT |
||
387 | * @param string $PORTEUR |
||
388 | * @param string $REFABONNE |
||
389 | * @param string $REFERENCE |
||
390 | * @param int|null $DEVISE |
||
391 | * |
||
392 | * @return PayboxResponse |
||
393 | * |
||
394 | * @throws Exception\PayboxException |
||
395 | */ |
||
396 | View Code Duplication | public function authorizeAndCaptureSubscriber($DATEVAL, $MONTANT, $PORTEUR, $REFABONNE, $REFERENCE, $DEVISE = null) |
|
412 | |||
413 | /** |
||
414 | * @param string $DATEVAL |
||
415 | * @param int $MONTANT |
||
416 | * @param string $PORTEUR |
||
417 | * @param string $REFABONNE |
||
418 | * @param string $REFERENCE |
||
419 | * @param int|null $DEVISE |
||
420 | * |
||
421 | * @return PayboxResponse |
||
422 | * |
||
423 | * @throws Exception\PayboxException |
||
424 | */ |
||
425 | View Code Duplication | public function creditSubscriber($DATEVAL, $MONTANT, $PORTEUR, $REFABONNE, $REFERENCE, $DEVISE = null) |
|
441 | |||
442 | /** |
||
443 | * @param string $DATEVAL |
||
444 | * @param int $MONTANT |
||
445 | * @param int $NUMAPPEL |
||
446 | * @param int $NUMTRANS |
||
447 | * @param string $PORTEUR |
||
448 | * @param string $REFABONNE |
||
449 | * @param string $REFERENCE |
||
450 | * @param int|null $DEVISE |
||
451 | * |
||
452 | * @return PayboxResponse |
||
453 | * |
||
454 | * @throws Exception\PayboxException |
||
455 | */ |
||
456 | public function cancelSubscriberTransaction($DATEVAL, $MONTANT, $NUMAPPEL, $NUMTRANS, $PORTEUR, $REFABONNE, $REFERENCE, $DEVISE = null) |
||
474 | |||
475 | /** |
||
476 | * @param string $DATEVAL |
||
477 | * @param int $MONTANT |
||
478 | * @param string $PORTEUR |
||
479 | * @param string $REFABONNE |
||
480 | * @param string $REFERENCE |
||
481 | * @param string|null $AUTORISATION |
||
482 | * @param int|null $DEVISE |
||
483 | * |
||
484 | * @return PayboxResponse |
||
485 | * |
||
486 | * @throws Exception\PayboxException |
||
487 | */ |
||
488 | View Code Duplication | public function registerSubscriber($DATEVAL, $MONTANT, $PORTEUR, $REFABONNE, $REFERENCE, $AUTORISATION = null, $DEVISE = null) |
|
507 | |||
508 | /** |
||
509 | * @param string $DATEVAL |
||
510 | * @param int $MONTANT |
||
511 | * @param string $PORTEUR |
||
512 | * @param string $REFABONNE |
||
513 | * @param string|null $AUTORISATION |
||
514 | * @param int|null $DEVISE |
||
515 | * |
||
516 | * @return PayboxResponse |
||
517 | * |
||
518 | * @throws Exception\PayboxException |
||
519 | */ |
||
520 | View Code Duplication | public function updateSubscriber($DATEVAL, $MONTANT, $PORTEUR, $REFABONNE, $AUTORISATION = null, $DEVISE = null) |
|
538 | |||
539 | /** |
||
540 | * @param string $REFABONNE |
||
541 | * |
||
542 | * @return PayboxResponse |
||
543 | * |
||
544 | * @throws Exception\PayboxException |
||
545 | */ |
||
546 | public function deleteSubscriber($REFABONNE) |
||
554 | |||
555 | /** |
||
556 | * @param string $DATEVAL |
||
557 | * @param int $MONTANT |
||
558 | * @param string $PORTEUR |
||
559 | * @param string $REFABONNE |
||
560 | * @param string $REFERENCE |
||
561 | * @param int|null $DEVISE |
||
562 | * |
||
563 | * @return PayboxResponse |
||
564 | * |
||
565 | * @throws Exception\PayboxException |
||
566 | */ |
||
567 | View Code Duplication | public function transactSubscriber($DATEVAL, $MONTANT, $PORTEUR, $REFABONNE, $REFERENCE, $DEVISE = null) |
|
583 | |||
584 | private function configureOptions(OptionsResolver $resolver) |
||
610 | |||
611 | /** |
||
612 | * @param string $operation |
||
613 | * @param OptionsResolver $resolver |
||
614 | */ |
||
615 | private function configurePayboxCallParameters($operation, OptionsResolver $resolver) |
||
686 | } |
||
687 |
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.