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 namespace CodeZero\Curl; |
||
3 | class Request |
||
4 | { |
||
5 | /** |
||
6 | * cURL Wrapper |
||
7 | * |
||
8 | * @var Curl |
||
9 | */ |
||
10 | private $curl; |
||
11 | |||
12 | /** |
||
13 | * Option Parser |
||
14 | * |
||
15 | * @var OptionParser |
||
16 | */ |
||
17 | private $optionParser; |
||
18 | |||
19 | /** |
||
20 | * Response Factory |
||
21 | * |
||
22 | * @var ResponseFactory |
||
23 | */ |
||
24 | private $responseFactory; |
||
25 | |||
26 | /** |
||
27 | * cURL Options |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | private $options = []; |
||
32 | |||
33 | /** |
||
34 | * Constructor |
||
35 | * |
||
36 | * @param Curl $curl |
||
37 | * @param OptionParser $optionParser |
||
38 | * @param ResponseFactory $responseFactory |
||
39 | */ |
||
40 | public function __construct(Curl $curl = null, OptionParser $optionParser = null, ResponseFactory $responseFactory = null) |
||
48 | |||
49 | /** |
||
50 | * Send GET request |
||
51 | * |
||
52 | * @param string $url |
||
53 | * @param array $data |
||
54 | * @param array $headers |
||
55 | * |
||
56 | * @return Response |
||
57 | */ |
||
58 | public function get($url, array $data = [], array $headers = []) |
||
71 | |||
72 | /** |
||
73 | * Send POST request |
||
74 | * |
||
75 | * @param string $url |
||
76 | * @param array $data |
||
77 | * @param array $headers |
||
78 | * |
||
79 | * @return Response |
||
80 | */ |
||
81 | View Code Duplication | public function post($url, array $data = [], array $headers = []) |
|
92 | |||
93 | /** |
||
94 | * Send PUT request |
||
95 | * |
||
96 | * @param string $url |
||
97 | * @param array $data |
||
98 | * @param array $headers |
||
99 | * |
||
100 | * @return Response |
||
101 | */ |
||
102 | View Code Duplication | public function put($url, array $data = [], array $headers = []) |
|
110 | |||
111 | /** |
||
112 | * Send PATCH request |
||
113 | * |
||
114 | * @param string $url |
||
115 | * @param array $data |
||
116 | * @param array $headers |
||
117 | * |
||
118 | * @return Response |
||
119 | */ |
||
120 | View Code Duplication | public function patch($url, array $data = [], array $headers = []) |
|
128 | |||
129 | /** |
||
130 | * Send DELETE request |
||
131 | * |
||
132 | * @param string $url |
||
133 | * @param array $data |
||
134 | * @param array $headers |
||
135 | * |
||
136 | * @return Response |
||
137 | */ |
||
138 | View Code Duplication | public function delete($url, array $data = [], array $headers = []) |
|
146 | |||
147 | /** |
||
148 | * Set or overwrite a cURL option |
||
149 | * |
||
150 | * @param $option |
||
151 | * @param $value |
||
152 | * |
||
153 | * @return void |
||
154 | */ |
||
155 | public function setOption($option, $value) |
||
159 | |||
160 | /** |
||
161 | * Set or overwrite multiple cURL options |
||
162 | * |
||
163 | * @param array $options |
||
164 | * |
||
165 | * @return void |
||
166 | */ |
||
167 | public function setOptions(array $options) |
||
174 | |||
175 | /** |
||
176 | * Unset a cURL option |
||
177 | * |
||
178 | * @param $option |
||
179 | * |
||
180 | * @return void |
||
181 | */ |
||
182 | public function unsetOption($option) |
||
189 | |||
190 | /** |
||
191 | * Unset multiple cURL options |
||
192 | * |
||
193 | * @param array $options |
||
194 | * |
||
195 | * @return void |
||
196 | */ |
||
197 | public function unsetOptions(array $options) |
||
204 | |||
205 | /** |
||
206 | * Check if an option is set |
||
207 | * |
||
208 | * @param $option |
||
209 | * |
||
210 | * @return bool |
||
211 | */ |
||
212 | public function isOptionSet($option) |
||
216 | |||
217 | /** |
||
218 | * Get the value of an option if it is set |
||
219 | * |
||
220 | * @param $option |
||
221 | * |
||
222 | * @return mixed|null |
||
223 | */ |
||
224 | public function getOptionValue($option) |
||
233 | |||
234 | /** |
||
235 | * Set basic authentication |
||
236 | * |
||
237 | * @param string $username |
||
238 | * @param string $password |
||
239 | * |
||
240 | * @return void |
||
241 | */ |
||
242 | public function setBasicAuthentication($username, $password) |
||
249 | |||
250 | /** |
||
251 | * Unset basic authentication |
||
252 | * |
||
253 | * @return void |
||
254 | */ |
||
255 | public function unsetBasicAuthentication() |
||
259 | |||
260 | /** |
||
261 | * Set default cURL options |
||
262 | * |
||
263 | * @return void |
||
264 | */ |
||
265 | private function setDefaultOptions() |
||
274 | |||
275 | /** |
||
276 | * Set cURL request URL |
||
277 | * |
||
278 | * @param string $url |
||
279 | * |
||
280 | * @return void |
||
281 | */ |
||
282 | private function setUrl($url) |
||
286 | |||
287 | /** |
||
288 | * Set request post fields |
||
289 | * |
||
290 | * @param array $data |
||
291 | * |
||
292 | * @return void |
||
293 | */ |
||
294 | private function setData(array $data) |
||
305 | |||
306 | /** |
||
307 | * Set request headers |
||
308 | * |
||
309 | * @param array $headers |
||
310 | * |
||
311 | * @return void |
||
312 | */ |
||
313 | private function setHeaders(array $headers) |
||
324 | |||
325 | /** |
||
326 | * Send a request |
||
327 | * |
||
328 | * @param string $url |
||
329 | * @param array $data |
||
330 | * @param array $headers |
||
331 | * |
||
332 | * @return Response |
||
333 | * @throws RequestException |
||
334 | */ |
||
335 | private function send($url, array $data = [], array $headers = []) |
||
344 | |||
345 | /** |
||
346 | * Execute the cURL request |
||
347 | * |
||
348 | * @return Response |
||
349 | * @throws RequestException |
||
350 | */ |
||
351 | private function executeCurlRequest() |
||
380 | } |
||
381 |
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.