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 |
||
11 | class WebGateway implements GatewayInterface |
||
12 | { |
||
13 | /** |
||
14 | * Pay an order. |
||
15 | * |
||
16 | * @author yansongda <[email protected]> |
||
17 | * |
||
18 | * @param string $endpoint |
||
19 | * @param array $payload |
||
20 | * |
||
21 | * @throws \Yansongda\Pay\Exceptions\InvalidConfigException |
||
22 | * |
||
23 | * @return Response |
||
24 | */ |
||
25 | public function pay($endpoint, array $payload): Response |
||
42 | |||
43 | /** |
||
44 | * Find. |
||
45 | * |
||
46 | * @author yansongda <[email protected]> |
||
47 | * |
||
48 | * @param $order |
||
49 | * |
||
50 | * @return array |
||
51 | */ |
||
52 | View Code Duplication | public function find($order): array |
|
59 | |||
60 | /** |
||
61 | * Build Html response. |
||
62 | * |
||
63 | * @author yansongda <[email protected]> |
||
64 | * |
||
65 | * @param string $endpoint |
||
66 | * @param array $payload |
||
67 | * @param string $method |
||
68 | * |
||
69 | * @return Response |
||
70 | */ |
||
71 | protected function buildPayHtml($endpoint, $payload, $method = 'POST'): Response |
||
87 | |||
88 | /** |
||
89 | * Get method config. |
||
90 | * |
||
91 | * @author yansongda <[email protected]> |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | protected function getMethod(): string |
||
99 | |||
100 | /** |
||
101 | * Get productCode config. |
||
102 | * |
||
103 | * @author yansongda <[email protected]> |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | protected function getProductCode(): string |
||
111 | } |
||
112 |
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.