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 extends AbstractHttp |
||
16 | { |
||
17 | |||
18 | /** |
||
19 | * The current scheme |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $scheme = ''; |
||
23 | |||
24 | /** |
||
25 | * The current host |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $host = ''; |
||
29 | |||
30 | /** |
||
31 | * The current path string |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $path = ''; |
||
35 | |||
36 | /** |
||
37 | * The current uri |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $uri = ''; |
||
41 | |||
42 | /** |
||
43 | * The current method |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $method = ''; |
||
47 | |||
48 | /** |
||
49 | * Custom headers |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $headers = []; |
||
53 | |||
54 | /** |
||
55 | * The current query string |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $query = ''; |
||
59 | |||
60 | /** |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $body = ''; |
||
64 | |||
65 | /** |
||
66 | * @var array |
||
67 | */ |
||
68 | protected $get = []; |
||
69 | |||
70 | /** |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $post = []; |
||
74 | |||
75 | /** |
||
76 | * Set attributes automatically |
||
77 | * |
||
78 | * @return self |
||
79 | */ |
||
80 | public function createFromHeaders() |
||
97 | |||
98 | /** |
||
99 | * Set the request scheme |
||
100 | * |
||
101 | * @param string $scheme |
||
102 | */ |
||
103 | public function setScheme(string $scheme = '') |
||
107 | |||
108 | /** |
||
109 | * Get the request scheme |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | public function getScheme() |
||
117 | |||
118 | /** |
||
119 | * @param array $headers |
||
120 | */ |
||
121 | public function setHeaders(array $headers = []) |
||
125 | |||
126 | /** |
||
127 | * @return array |
||
128 | */ |
||
129 | public function getHeaders() |
||
133 | |||
134 | /** |
||
135 | * @param string $host |
||
136 | */ |
||
137 | public function setHost(string $host = '') |
||
141 | |||
142 | /** |
||
143 | * @return string |
||
144 | */ |
||
145 | public function getHost() |
||
149 | |||
150 | /** |
||
151 | * Set uri path |
||
152 | * |
||
153 | * @param string $path |
||
154 | */ |
||
155 | View Code Duplication | public function setPath(string $path) |
|
165 | |||
166 | /** |
||
167 | * Get uri path |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | public function getPath() :string |
||
175 | |||
176 | /** |
||
177 | * Set uri path |
||
178 | * |
||
179 | * @param string $uri |
||
180 | */ |
||
181 | View Code Duplication | public function setUri(string $uri) |
|
191 | |||
192 | /** |
||
193 | * Get uri path |
||
194 | * |
||
195 | * @return string |
||
196 | */ |
||
197 | public function getUri() :string |
||
201 | |||
202 | /** |
||
203 | * Set method |
||
204 | * |
||
205 | * @param string $method |
||
206 | */ |
||
207 | public function setMethod(string $method) |
||
211 | |||
212 | /** |
||
213 | * Get method |
||
214 | * |
||
215 | * @return string |
||
216 | */ |
||
217 | public function getMethod() :string |
||
222 | |||
223 | /** |
||
224 | * Set query string |
||
225 | * |
||
226 | * @param string $query |
||
227 | */ |
||
228 | public function setQuery(string $query) |
||
232 | |||
233 | /** |
||
234 | * Get query string |
||
235 | * |
||
236 | * @return string |
||
237 | */ |
||
238 | public function getQuery() :string |
||
242 | |||
243 | /** |
||
244 | * @param array $body |
||
245 | */ |
||
246 | public function setBody($body) |
||
250 | |||
251 | /** |
||
252 | * @return string |
||
253 | */ |
||
254 | public function getBody() |
||
258 | |||
259 | /** |
||
260 | * Determine if it's a post request |
||
261 | * |
||
262 | * @return boolean |
||
263 | */ |
||
264 | public function isPost() :bool |
||
268 | |||
269 | /** |
||
270 | * Determine if it's a get request |
||
271 | * |
||
272 | * @return boolean |
||
273 | */ |
||
274 | public function isGet() :bool |
||
278 | |||
279 | /** |
||
280 | * @param string $key |
||
281 | * |
||
282 | * @return mixed |
||
283 | */ |
||
284 | public function getParam(string $key) |
||
308 | |||
309 | /** |
||
310 | * Return the post data |
||
311 | * |
||
312 | * @return array |
||
313 | */ |
||
314 | public function getPostData() :array |
||
318 | |||
319 | /** |
||
320 | * @param $data |
||
321 | * @return void |
||
322 | */ |
||
323 | public function setPostData($data) |
||
327 | |||
328 | } |
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.