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); |
||
14 | final class Client |
||
15 | { |
||
16 | use HasSignature, HasSender; |
||
17 | |||
18 | private $baseUri; |
||
19 | private $terminalKey; |
||
20 | private $pemFile; |
||
21 | |||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | private static $requestHeaders = [ |
||
26 | 'Accept' => 'application/json', |
||
27 | 'Content-Type' => 'application/x-www-form-urlencoded', |
||
28 | ]; |
||
29 | |||
30 | /** |
||
31 | * Init a new instance. |
||
32 | * |
||
33 | * @param string $uri |
||
34 | * @param string $terminalKey |
||
35 | * @param string $pemFile |
||
36 | * @param Sender $sender |
||
37 | */ |
||
38 | public function __construct($uri, $terminalKey, $pemFile, Sender $sender) |
||
45 | |||
46 | /** |
||
47 | * {@inheritdoc} |
||
48 | */ |
||
49 | public function getPemFile() |
||
53 | |||
54 | /** |
||
55 | * Init a new payment session. |
||
56 | * |
||
57 | * @param string $orderId Номер заказа в системе Продавца. |
||
58 | * @param string $cardId Идентификатор карты пополнения. |
||
59 | * @param int $amount Сумма в копейках. |
||
60 | * @param array $extra Массив дополнительных полей, которые могут быть переданы в этом запросе. |
||
61 | * |
||
62 | * @return Init |
||
63 | * @throws HttpException |
||
64 | */ |
||
65 | View Code Duplication | public function init(string $orderId, string $cardId, int $amount, array $extra = []): Init |
|
73 | |||
74 | /** |
||
75 | * Make payout to card. |
||
76 | * |
||
77 | * @param mixed $paymentId |
||
78 | * |
||
79 | * @return Payment |
||
80 | * @throws HttpException |
||
81 | */ |
||
82 | public function payment($paymentId): Payment |
||
86 | |||
87 | /** |
||
88 | * Init a new payment session and make payout to card. |
||
89 | * |
||
90 | * @param string $orderId Номер заказа в системе Продавца. |
||
91 | * @param string $cardId Идентификатор карты пополнения. |
||
92 | * @param int $amount Сумма в копейках. |
||
93 | * @param array $extra Массив дополнительных полей, которые могут быть переданы в этом запросе. |
||
94 | * |
||
95 | * @return Payment |
||
96 | * @throws HttpException |
||
97 | * @throws ResponseException |
||
98 | */ |
||
99 | View Code Duplication | public function payout(string $orderId, string $cardId, int $amount, array $extra = []): Payment |
|
113 | |||
114 | /** |
||
115 | * Make a new http request. |
||
116 | * |
||
117 | * @param string $uri |
||
118 | * @param array $body |
||
119 | * |
||
120 | * @return RequestInterface |
||
121 | */ |
||
122 | private function makeRequest(string $uri, array $body = []): RequestInterface |
||
132 | } |
||
133 |
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.