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 $certNumber; |
||
24 | private $signer; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | private static $requestHeaders = [ |
||
30 | 'Accept' => 'application/json', |
||
31 | 'Content-Type' => 'application/x-www-form-urlencoded', |
||
32 | ]; |
||
33 | |||
34 | /** |
||
35 | * Init a new instance. |
||
36 | * |
||
37 | * @param string $uri |
||
38 | * @param string $terminalKey |
||
39 | * @param string $certNumber |
||
40 | * @param Closure $signer |
||
41 | * @param Sender $sender |
||
42 | */ |
||
43 | public function __construct(string $uri, string $terminalKey, string $certNumber, Closure $signer, Sender $sender) |
||
51 | |||
52 | /** |
||
53 | * Create a new instance. |
||
54 | * |
||
55 | * @param string $uri |
||
56 | * @param string $terminalKey |
||
57 | * @param string $pemFile |
||
58 | * @param Sender $sender |
||
59 | * @return self |
||
60 | */ |
||
61 | public static function make(string $uri, string $terminalKey, string $pemFile, Sender $sender): self |
||
72 | |||
73 | /** |
||
74 | * Get serial number of X509. |
||
75 | * |
||
76 | * @param string $pemFile |
||
77 | * @return string |
||
78 | */ |
||
79 | private static function getSerialNumber(string $pemFile): string |
||
85 | |||
86 | /** |
||
87 | * Init a new payment session. |
||
88 | * |
||
89 | * @param string $orderId Номер заказа в системе Продавца. |
||
90 | * @param string $cardId Идентификатор карты пополнения. |
||
91 | * @param int $amount Сумма в копейках. |
||
92 | * @param array $extra Массив дополнительных полей, которые могут быть переданы в этом запросе. |
||
93 | * |
||
94 | * @return Init |
||
95 | * @throws HttpException |
||
96 | */ |
||
97 | View Code Duplication | public function init(string $orderId, string $cardId, int $amount, array $extra = []): Init |
|
105 | |||
106 | /** |
||
107 | * Make payout to card. |
||
108 | * |
||
109 | * @param mixed $paymentId |
||
110 | * |
||
111 | * @return Payment |
||
112 | * @throws HttpException |
||
113 | */ |
||
114 | public function payment($paymentId): Payment |
||
118 | |||
119 | /** |
||
120 | * Get op state. |
||
121 | * |
||
122 | * @param mixed $paymentId |
||
123 | * |
||
124 | * @return State |
||
125 | * @throws HttpException |
||
126 | */ |
||
127 | public function getState($paymentId): State |
||
131 | |||
132 | /** |
||
133 | * Init a new payment session and make payout to card. |
||
134 | * |
||
135 | * @param string $orderId Номер заказа в системе Продавца. |
||
136 | * @param string $cardId Идентификатор карты пополнения. |
||
137 | * @param int $amount Сумма в копейках. |
||
138 | * @param array $extra Массив дополнительных полей, которые могут быть переданы в этом запросе. |
||
139 | * |
||
140 | * @return Payment |
||
141 | * @throws HttpException |
||
142 | * @throws ResponseException |
||
143 | */ |
||
144 | public function payout(string $orderId, string $cardId, int $amount, array $extra = []): Payment |
||
158 | |||
159 | /** |
||
160 | * Make a new http request. |
||
161 | * |
||
162 | * @param string $uri |
||
163 | * @param array $body |
||
164 | * |
||
165 | * @return RequestInterface |
||
166 | */ |
||
167 | private function makeRequest(string $uri, array $body = []): RequestInterface |
||
184 | |||
185 | /** |
||
186 | * Get digest and signature for request data. |
||
187 | * |
||
188 | * @param array $body |
||
189 | * @return SecretDataContainer |
||
190 | */ |
||
191 | private function getSecretData(array $body): SecretDataContainer |
||
195 | } |
||
196 |
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.