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 |
||
18 | class Rate extends Ups |
||
19 | { |
||
20 | const ENDPOINT = '/Rate'; |
||
21 | |||
22 | /** |
||
23 | * @var RequestInterface |
||
24 | */ |
||
25 | private $request; |
||
26 | |||
27 | /** |
||
28 | * @var ResponseInterface |
||
29 | * todo: make private |
||
30 | */ |
||
31 | public $response; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $requestOption; |
||
37 | |||
38 | /** |
||
39 | * @param $rateRequest |
||
40 | * |
||
41 | * @throws Exception |
||
42 | * |
||
43 | * @return RateResponse |
||
44 | */ |
||
45 | View Code Duplication | public function shopRates($rateRequest) |
|
57 | |||
58 | /** |
||
59 | * @param $rateRequest |
||
60 | * |
||
61 | * @throws Exception |
||
62 | * |
||
63 | * @return RateResponse |
||
64 | */ |
||
65 | View Code Duplication | public function getRate($rateRequest) |
|
77 | |||
78 | /** |
||
79 | * Creates and sends a request for the given shipment. This handles checking for |
||
80 | * errors in the response back from UPS. |
||
81 | * |
||
82 | * @param RateRequest $rateRequest |
||
83 | * |
||
84 | * @throws Exception |
||
85 | * |
||
86 | * @return RateResponse |
||
87 | */ |
||
88 | View Code Duplication | protected function sendRequest(RateRequest $rateRequest) |
|
108 | |||
109 | /** |
||
110 | * Create the Rate request. |
||
111 | * |
||
112 | * @param RateRequest $rateRequest The request details. Refer to the UPS documentation for available structure |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | private function createRequest(RateRequest $rateRequest) |
||
196 | |||
197 | /** |
||
198 | * Format the response. |
||
199 | * |
||
200 | * @param SimpleXMLElement $response |
||
201 | * |
||
202 | * @return RateResponse |
||
203 | */ |
||
204 | private function formatResponse(SimpleXMLElement $response) |
||
213 | |||
214 | /** |
||
215 | * @return RequestInterface |
||
216 | */ |
||
217 | View Code Duplication | public function getRequest() |
|
225 | |||
226 | /** |
||
227 | * @param RequestInterface $request |
||
228 | * |
||
229 | * @return $this |
||
230 | */ |
||
231 | public function setRequest(RequestInterface $request) |
||
237 | |||
238 | /** |
||
239 | * @return ResponseInterface |
||
240 | */ |
||
241 | public function getResponse() |
||
245 | |||
246 | /** |
||
247 | * @param ResponseInterface $response |
||
248 | * |
||
249 | * @return $this |
||
250 | */ |
||
251 | public function setResponse(ResponseInterface $response) |
||
257 | } |
||
258 |
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.