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 |
||
19 | class Connector extends HttpBrowser |
||
20 | { |
||
21 | /** |
||
22 | * @var RouterInterface |
||
23 | */ |
||
24 | protected $router; |
||
25 | |||
26 | /** |
||
27 | * Request with the current global environent |
||
28 | * @var ServerRequestInterface |
||
29 | */ |
||
30 | protected $baseRequest; |
||
31 | |||
32 | /** |
||
33 | * Request with the current global environent |
||
34 | * @var ResponseInterface |
||
35 | */ |
||
36 | protected $baseResponse; |
||
37 | |||
38 | /** |
||
39 | * @var RequestConvertor |
||
40 | */ |
||
41 | protected $requestConvertor; |
||
42 | |||
43 | /** |
||
44 | * @var ResponseConvertor |
||
45 | */ |
||
46 | protected $responseConvertor; |
||
47 | |||
48 | |||
49 | /** |
||
50 | * Set the router |
||
51 | * |
||
52 | * @param RouterInterface $router |
||
53 | */ |
||
54 | 3 | public function setRouter(RouterInterface $router) |
|
58 | |||
59 | /** |
||
60 | * Get the router |
||
61 | * |
||
62 | * @return RouterInterface |
||
63 | */ |
||
64 | 4 | public function getRouter() |
|
68 | |||
69 | |||
70 | /** |
||
71 | * Set the base request |
||
72 | * |
||
73 | * @param ServerRequestInterface $request |
||
74 | */ |
||
75 | 6 | public function setBaseRequest(ServerRequestInterface $request) |
|
83 | |||
84 | /** |
||
85 | * Get the base request |
||
86 | * |
||
87 | * @return ServerRequestInterface |
||
88 | */ |
||
89 | 6 | View Code Duplication | public function getBaseRequest() |
103 | |||
104 | |||
105 | /** |
||
106 | * Set the base response |
||
107 | * |
||
108 | * @param ResponseInterface $response |
||
109 | */ |
||
110 | 6 | public function setBaseResponse(ResponseInterface $response) |
|
118 | |||
119 | /** |
||
120 | * Get the base response |
||
121 | * |
||
122 | * @return ResponseInterface |
||
123 | */ |
||
124 | 6 | View Code Duplication | public function getBaseResponse() |
138 | |||
139 | |||
140 | /** |
||
141 | * Reset the request |
||
142 | */ |
||
143 | 3 | protected function resetInput() |
|
149 | |||
150 | /** |
||
151 | * Reset the response |
||
152 | */ |
||
153 | 3 | protected function resetOutput() |
|
164 | |||
165 | /** |
||
166 | * Reset the request and response. |
||
167 | * This is only required when the request and/or response are bound to the global environment. |
||
168 | */ |
||
169 | 3 | public function reset() |
|
174 | |||
175 | |||
176 | /** |
||
177 | * Set the request convertor |
||
178 | * |
||
179 | * @param RequestConvertor $convertor |
||
180 | */ |
||
181 | 3 | public function setRequestConvertor(RequestConvertor $convertor) |
|
185 | |||
186 | /** |
||
187 | * Get the request convertor |
||
188 | * |
||
189 | * @return RequestConvertor |
||
190 | */ |
||
191 | 4 | public function getRequestConvertor() |
|
199 | |||
200 | |||
201 | /** |
||
202 | * Set the response convertor |
||
203 | * |
||
204 | * @param ResponseConvertor $convertor |
||
205 | */ |
||
206 | 3 | public function setResponseConvertor(ResponseConvertor $convertor) |
|
210 | |||
211 | /** |
||
212 | * Get the response convertor |
||
213 | * |
||
214 | * @return ResponseConvertor |
||
215 | */ |
||
216 | 4 | public function getResponseConvertor() |
|
224 | |||
225 | |||
226 | /** |
||
227 | * Makes a request. |
||
228 | * |
||
229 | * @param BrowserKitRequest $request |
||
230 | * @return BrowserKitResponse |
||
231 | */ |
||
232 | 3 | protected function doRequest($request): BrowserKitResponse |
|
247 | } |
||
248 |
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.