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 |
||
15 | class PublicAPIClient |
||
16 | { |
||
17 | /** |
||
18 | * @var CredentialsInterface |
||
19 | */ |
||
20 | private $credentials; |
||
21 | |||
22 | /** |
||
23 | * @var TransportInterface |
||
24 | */ |
||
25 | private $transport; |
||
26 | |||
27 | /** |
||
28 | * @var LoggerInterface |
||
29 | */ |
||
30 | private $logger; |
||
31 | |||
32 | /** |
||
33 | * @var ValidatorV1 |
||
34 | */ |
||
35 | private $validator; |
||
36 | |||
37 | /** |
||
38 | * Client constructor. |
||
39 | * @param CredentialsInterface $credentials |
||
40 | * @param TransportInterface $transport |
||
41 | * @param LoggerInterface|null $logger |
||
42 | */ |
||
43 | public function __construct( |
||
53 | |||
54 | /** |
||
55 | * Sends PSR-7 compatible request to Covery and returns |
||
56 | * |
||
57 | * @param RequestInterface $request |
||
58 | * @return string |
||
59 | * @throws IoException |
||
60 | */ |
||
61 | public function send(RequestInterface $request) |
||
83 | |||
84 | /** |
||
85 | * Utility method, that prepares and signs request |
||
86 | * |
||
87 | * @param RequestInterface $request |
||
88 | * @return RequestInterface |
||
89 | */ |
||
90 | private function prepareRequest(RequestInterface $request) |
||
102 | |||
103 | /** |
||
104 | * Utility function, that handles error response from Covery |
||
105 | * |
||
106 | * @param ResponseInterface $response |
||
107 | * @throws Exception |
||
108 | */ |
||
109 | private function handleNot200(ResponseInterface $response) |
||
138 | |||
139 | /** |
||
140 | * Utility method, that reads JSON data |
||
141 | * |
||
142 | * @param $string |
||
143 | * @return mixed|null |
||
144 | * @throws Exception |
||
145 | */ |
||
146 | private function readJson($string) |
||
167 | |||
168 | /** |
||
169 | * Sends request to Covery and returns access level, associated with |
||
170 | * used credentials |
||
171 | * |
||
172 | * This method can be used for Covery health check and availability |
||
173 | * On any problem (network, credentials, server side) this method |
||
174 | * will throw an exception |
||
175 | * |
||
176 | * @return string |
||
177 | * @throws Exception |
||
178 | */ |
||
179 | public function ping() |
||
188 | |||
189 | /** |
||
190 | * Sends envelope to Covery and returns it's ID on Covery side |
||
191 | * Before sending, validation is performed |
||
192 | * |
||
193 | * @param EnvelopeInterface $envelope |
||
194 | * @return int |
||
195 | * @throws Exception |
||
196 | */ |
||
197 | View Code Duplication | public function sendEvent(EnvelopeInterface $envelope) |
|
211 | |||
212 | /** |
||
213 | * Sends postback envelope to Covery and returns it's ID on Covery side |
||
214 | * Before sending, validation is performed |
||
215 | * |
||
216 | * @param EnvelopeInterface $envelope |
||
217 | * @return int |
||
218 | * @throws Exception |
||
219 | */ |
||
220 | View Code Duplication | public function sendPostback(EnvelopeInterface $envelope) |
|
234 | |||
235 | /** |
||
236 | * Sends envelope to Covery for analysis |
||
237 | * |
||
238 | * @param EnvelopeInterface $envelope |
||
239 | * @return Result |
||
240 | * @throws Exception |
||
241 | */ |
||
242 | public function makeDecision(EnvelopeInterface $envelope) |
||
266 | } |
||
267 |
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.