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 |
||
16 | class GatewayAdapter |
||
17 | { |
||
18 | /** |
||
19 | * Router. |
||
20 | * |
||
21 | * @var RouterInterface |
||
22 | */ |
||
23 | protected $router; |
||
24 | |||
25 | /** |
||
26 | * Gateway. |
||
27 | * |
||
28 | * @var \Omnipay\Common\GatewayInterface|null |
||
29 | */ |
||
30 | protected $gateway; |
||
31 | |||
32 | /** |
||
33 | * Construct. |
||
34 | * |
||
35 | * @param RouterInterface $router |
||
36 | * @param \Omnipay\Common\GatewayInterface|null $gateway |
||
37 | */ |
||
38 | public function __construct(RouterInterface $router, $gateway = null) |
||
43 | |||
44 | /** |
||
45 | * Purchase action. |
||
46 | * |
||
47 | * @param OrderInterface $order Order |
||
48 | * |
||
49 | * @return object |
||
50 | */ |
||
51 | View Code Duplication | public function purchase(OrderInterface $order) |
|
65 | |||
66 | /** |
||
67 | * Cplete purchase action. |
||
68 | * |
||
69 | * @param OrderInterface $order Order |
||
70 | * |
||
71 | * @return object |
||
72 | */ |
||
73 | View Code Duplication | public function completePurchase(OrderInterface $order) |
|
87 | |||
88 | private function getCancelAndReturnUrl() |
||
95 | |||
96 | /** |
||
97 | * Whether offline gateway is set or not. |
||
98 | * |
||
99 | * @return bool |
||
100 | */ |
||
101 | public function isOfflineGateway() |
||
105 | } |
||
106 |
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.