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 Request implements RequestInterface, LoggerAwareInterface |
||
16 | { |
||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $access; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $request; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $endpointUrl; |
||
31 | |||
32 | /** |
||
33 | * @var LoggerInterface |
||
34 | */ |
||
35 | protected $logger; |
||
36 | |||
37 | /** |
||
38 | * @var Guzzle |
||
39 | */ |
||
40 | protected $client; |
||
41 | |||
42 | /** |
||
43 | * @param LoggerInterface $logger |
||
44 | */ |
||
45 | View Code Duplication | public function __construct(LoggerInterface $logger = null) |
|
55 | |||
56 | /** |
||
57 | * Sets a logger instance on the object. |
||
58 | * |
||
59 | * @param LoggerInterface $logger |
||
60 | * |
||
61 | * @return null |
||
62 | */ |
||
63 | public function setLogger(LoggerInterface $logger) |
||
67 | |||
68 | /** |
||
69 | * Creates a single instance of the Guzzle client |
||
70 | * |
||
71 | * @return null |
||
72 | */ |
||
73 | public function setClient() |
||
77 | |||
78 | /** |
||
79 | * Send request to UPS. |
||
80 | * |
||
81 | * @param string $access The access request xml |
||
82 | * @param string $request The request xml |
||
83 | * @param string $endpointurl The UPS API Endpoint URL |
||
84 | * |
||
85 | * @throws Exception |
||
86 | * todo: make access, request and endpointurl nullable to make the testable |
||
87 | * |
||
88 | * @return ResponseInterface |
||
89 | */ |
||
90 | public function request($access, $request, $endpointurl) |
||
159 | |||
160 | /** |
||
161 | * @param $access |
||
162 | * |
||
163 | * @return $this |
||
164 | */ |
||
165 | public function setAccess($access) |
||
171 | |||
172 | /** |
||
173 | * @return string |
||
174 | */ |
||
175 | public function getAccess() |
||
179 | |||
180 | /** |
||
181 | * @param $request |
||
182 | * |
||
183 | * @return $this |
||
184 | */ |
||
185 | public function setRequest($request) |
||
191 | |||
192 | /** |
||
193 | * @return string |
||
194 | */ |
||
195 | public function getRequest() |
||
199 | |||
200 | /** |
||
201 | * @param $endpointUrl |
||
202 | * |
||
203 | * @return $this |
||
204 | */ |
||
205 | public function setEndpointUrl($endpointUrl) |
||
211 | |||
212 | /** |
||
213 | * @return string |
||
214 | */ |
||
215 | public function getEndpointUrl() |
||
219 | |||
220 | /** |
||
221 | * @param $body |
||
222 | * @return string |
||
223 | */ |
||
224 | protected function convertEncoding($body) |
||
237 | |||
238 | } |
||
239 |
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.