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 declare(strict_types=1); |
||
17 | final class Client |
||
18 | { |
||
19 | use HasSignature, HasSender; |
||
20 | |||
21 | private $baseUri; |
||
22 | private $terminalKey; |
||
23 | private $secretKey; |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | private static $requestHeaders = [ |
||
29 | 'Accept' => 'application/json', |
||
30 | 'Content-Type' => 'application/x-www-form-urlencoded', |
||
31 | ]; |
||
32 | |||
33 | /** |
||
34 | * Init a new instance. |
||
35 | * |
||
36 | * @param string $uri |
||
37 | * @param string $terminalKey |
||
38 | * @param string $secretKey |
||
39 | * @param Sender $sender |
||
40 | */ |
||
41 | 3 | View Code Duplication | public function __construct(string $uri, string $terminalKey, string $secretKey, Sender $sender) |
|
|||
42 | { |
||
43 | 3 | $this->baseUri = rtrim($uri, '/'); |
|
44 | 3 | $this->terminalKey = $terminalKey; |
|
45 | 3 | $this->secretKey = $secretKey; |
|
46 | 3 | $this->setSender($sender); |
|
47 | 3 | } |
|
48 | |||
49 | /** |
||
50 | * Init a new payment session. |
||
51 | * |
||
52 | * @param string $orderId Номер заказа в системе Продавца. |
||
53 | * @param int $amount Сумма в копейках. |
||
54 | * @param array $extra Массив дополнительных полей, которые могут быть переданы в этом запросе. |
||
55 | * |
||
56 | * @return Init |
||
57 | * @throws HttpException |
||
58 | */ |
||
59 | 3 | View Code Duplication | public function init(string $orderId, int $amount, array $extra = []): Init |
66 | |||
67 | /** |
||
68 | * Init a new payment session. |
||
69 | * |
||
70 | * @param string $customerKey |
||
71 | * @param string $orderId |
||
72 | * @param int $amount |
||
73 | * @param array $extra |
||
74 | * |
||
75 | * @return Init |
||
76 | * @throws HttpException |
||
77 | */ |
||
78 | public function rInit(string $customerKey, string $orderId, int $amount, array $extra = []): Init |
||
85 | |||
86 | /** |
||
87 | * Call charge. |
||
88 | * |
||
89 | * @param mixed $paymentId |
||
90 | * @param mixed $rebillId |
||
91 | * |
||
92 | * @return Charge |
||
93 | * |
||
94 | * @throws HttpException |
||
95 | */ |
||
96 | public function charge($paymentId, $rebillId): Charge |
||
103 | |||
104 | /** |
||
105 | * Init a new payment session and call charge. |
||
106 | * |
||
107 | * @param mixed $rebillId |
||
108 | * @param string $orderId |
||
109 | * @param int $amount |
||
110 | * @param array $extra |
||
111 | * |
||
112 | * @return Charge |
||
113 | * @throws HttpException |
||
114 | * @throws ResponseException |
||
115 | */ |
||
116 | public function recurrent($rebillId, string $orderId, int $amount, array $extra = []): Charge |
||
130 | |||
131 | /** |
||
132 | * Send confirm request. |
||
133 | * |
||
134 | * @param int $paymentId |
||
135 | * @param int|null $amount |
||
136 | * @param array $extra |
||
137 | * |
||
138 | * @return Confirm |
||
139 | * @throws HttpException |
||
140 | */ |
||
141 | View Code Duplication | public function confirm(int $paymentId, int $amount = null, array $extra = []): Confirm |
|
151 | |||
152 | /** |
||
153 | * Send cancel request. |
||
154 | * |
||
155 | * @param int $paymentId |
||
156 | * @param int|null $amount |
||
157 | * @param array $extra |
||
158 | * |
||
159 | * @return Cancel |
||
160 | * @throws HttpException |
||
161 | */ |
||
162 | View Code Duplication | public function cancel(int $paymentId, int $amount = null, array $extra = []): Cancel |
|
172 | |||
173 | /** |
||
174 | * Make a new http request. |
||
175 | * |
||
176 | * @param string $uri |
||
177 | * @param array $body |
||
178 | * |
||
179 | * @return RequestInterface |
||
180 | */ |
||
181 | 3 | private function makeRequest(string $uri, array $body = []): RequestInterface |
|
192 | } |
||
193 |
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.