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:
Complex classes like Request often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Request, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class Request |
||
10 | { |
||
11 | /** |
||
12 | * @var string $requestUri Request URI from $_SERVER. |
||
13 | * @var string $scriptName Scriptname from $_SERVER, actual scriptname part. |
||
14 | * @var string $path Scriptname from $_SERVER, path-part. |
||
15 | */ |
||
16 | private $requestUri; |
||
17 | private $scriptName; |
||
18 | private $path; |
||
19 | |||
20 | |||
21 | |||
22 | /** |
||
23 | * @var string $route The route. |
||
24 | * @var array $routeParts The route as an array. |
||
25 | */ |
||
26 | private $route; |
||
27 | private $routeParts; |
||
28 | |||
29 | |||
30 | |||
31 | /** |
||
32 | * @var string $currentUrl Current url. |
||
33 | * @var string $siteUrl Url to this site, http://dbwebb.se. |
||
34 | * @var string $baseUrl Url to root dir, |
||
35 | * siteUrl . /some/installation/directory/. |
||
36 | */ |
||
37 | private $currentUrl; |
||
38 | private $siteUrl; |
||
39 | private $baseUrl; |
||
40 | |||
41 | |||
42 | |||
43 | /** |
||
44 | * @var array $server Mapped to $_SERVER. |
||
45 | * @var array $get Mapped to $_GET. |
||
46 | * @var array $post Mapped to $_POST. |
||
47 | * @var array $body Mapped to request body, defaults to php://input. |
||
48 | */ |
||
49 | private $server; |
||
50 | private $get; |
||
51 | private $post; |
||
52 | private $body; |
||
53 | |||
54 | |||
55 | |||
56 | /** |
||
57 | * Constructor. |
||
58 | */ |
||
59 | 30 | public function __construct() |
|
63 | |||
64 | |||
65 | |||
66 | /** |
||
67 | * Read info from the globals. |
||
68 | * |
||
69 | * @param array $globals use to initiate globals with values. |
||
70 | * |
||
71 | * @return void |
||
72 | */ |
||
73 | 30 | public function setGlobals($globals = []) |
|
91 | |||
92 | |||
93 | |||
94 | /** |
||
95 | * Read info from the globals. |
||
96 | * |
||
97 | * @param array $globals use to initiate globals with values. |
||
|
|||
98 | * |
||
99 | * @return void |
||
100 | */ |
||
101 | 4 | public function unsetGlobals() |
|
107 | |||
108 | |||
109 | |||
110 | /** |
||
111 | * Init the request class by reading information from the request. |
||
112 | * |
||
113 | * @return $this |
||
114 | */ |
||
115 | 10 | public function init() |
|
136 | |||
137 | |||
138 | |||
139 | /** |
||
140 | * Get site url including scheme, host and port. |
||
141 | * |
||
142 | * @return string |
||
143 | */ |
||
144 | 4 | public function getSiteUrl() |
|
148 | |||
149 | |||
150 | |||
151 | /** |
||
152 | * Get base url including site url and path to current index.php. |
||
153 | * |
||
154 | * @return string |
||
155 | */ |
||
156 | 4 | public function getBaseUrl() |
|
160 | |||
161 | |||
162 | |||
163 | /** |
||
164 | * Get script name, index.php or other. |
||
165 | * |
||
166 | * @return string |
||
167 | */ |
||
168 | public function getScriptName() |
||
172 | |||
173 | |||
174 | |||
175 | /** |
||
176 | * Get route path parts in an array. |
||
177 | * |
||
178 | * @return array with route in its parts |
||
179 | */ |
||
180 | public function getRouteParts() |
||
184 | |||
185 | |||
186 | |||
187 | /** |
||
188 | * Get route path as a string. |
||
189 | * |
||
190 | * @return string as the current extracted route |
||
191 | */ |
||
192 | 6 | public function getRoute() |
|
196 | |||
197 | |||
198 | |||
199 | /** |
||
200 | * Get the request method. |
||
201 | * |
||
202 | * @return string as the request method |
||
203 | */ |
||
204 | 1 | public function getMethod() |
|
208 | |||
209 | |||
210 | |||
211 | /** |
||
212 | * Extract the part containing the route. |
||
213 | * |
||
214 | * @todo Should be private, or useful in test? |
||
215 | * |
||
216 | * @return string as the current extracted route |
||
217 | */ |
||
218 | 10 | public function extractRoute() |
|
258 | |||
259 | |||
260 | |||
261 | /** |
||
262 | * Get the current url. |
||
263 | * |
||
264 | * @param boolean $queryString attach query string, default is true. |
||
265 | * |
||
266 | * @return string as current url. |
||
267 | */ |
||
268 | 25 | public function getCurrentUrl($queryString = true) |
|
297 | |||
298 | |||
299 | |||
300 | /** |
||
301 | * Get a value from the _SERVER array and use default if it is not set. |
||
302 | * |
||
303 | * @param string $key to check if it exists in the $_SERVER variable, |
||
304 | * or empty to get whole array. |
||
305 | * @param mixed $default value to return as default |
||
306 | * |
||
307 | * @return mixed |
||
308 | */ |
||
309 | 28 | public function getServer($key = null, $default = null) |
|
317 | |||
318 | |||
319 | |||
320 | /** |
||
321 | * Set variable in the server array. |
||
322 | * |
||
323 | * @param mixed $key the key an the , or an key-value array |
||
324 | * @param string $value the value of the key |
||
325 | * |
||
326 | * @return self |
||
327 | */ |
||
328 | 20 | View Code Duplication | public function setServer($key, $value = null) : object |
337 | |||
338 | |||
339 | |||
340 | /** |
||
341 | * Check if the value from the _GET array exists. |
||
342 | * |
||
343 | * @param string $key to check if it exists in the $_GET variable |
||
344 | * |
||
345 | * @return boolean |
||
346 | */ |
||
347 | 1 | public function hasGet($key) |
|
351 | |||
352 | |||
353 | |||
354 | /** |
||
355 | * Get a value from the _GET array and use default if it is not set. |
||
356 | * |
||
357 | * @param string $key to check if it exists in the $_GET variable, |
||
358 | * or empty to get whole array. |
||
359 | * @param mixed $default value to return as default |
||
360 | * |
||
361 | * @return mixed |
||
362 | */ |
||
363 | 2 | View Code Duplication | public function getGet($key = null, $default = null) |
371 | |||
372 | |||
373 | |||
374 | /** |
||
375 | * Set variable in the get array. |
||
376 | * |
||
377 | * @param mixed $key the key an the value, or an key-value array |
||
378 | * @param string $value the value of the key |
||
379 | * |
||
380 | * @return self |
||
381 | */ |
||
382 | 1 | View Code Duplication | public function setGet($key, $value = null) : object |
391 | |||
392 | |||
393 | |||
394 | /** |
||
395 | * Get a value from the _POST array and use default if it is not set. |
||
396 | * |
||
397 | * @param string $key to check if it exists in the $_POST variable, |
||
398 | * or empty to get whole array. |
||
399 | * @param mixed $default value to return as default |
||
400 | * |
||
401 | * @return mixed |
||
402 | */ |
||
403 | 2 | View Code Duplication | public function getPost($key = null, $default = null) |
411 | |||
412 | |||
413 | |||
414 | /** |
||
415 | * Set variable in the post array. |
||
416 | * |
||
417 | * @param mixed $key the key an the value, or an key-value array |
||
418 | * @param string $value the value of the key |
||
419 | * |
||
420 | * @return self |
||
421 | */ |
||
422 | 1 | View Code Duplication | public function setPost($key, $value = null) : object |
431 | |||
432 | |||
433 | |||
434 | /** |
||
435 | * Set the request body (useful for unit testing). |
||
436 | * |
||
437 | * @return self |
||
438 | */ |
||
439 | public function setBody($body) |
||
443 | |||
444 | |||
445 | |||
446 | /** |
||
447 | * Get the request body. |
||
448 | * |
||
449 | * @return mixed |
||
450 | */ |
||
451 | public function getBody() |
||
457 | |||
458 | |||
459 | |||
460 | /** |
||
461 | * Get the request body from the HTTP request and treat it as |
||
462 | * JSON data. |
||
463 | * |
||
464 | * @throws Anax\Request\Exception when request body is invalid JSON. |
||
465 | * |
||
466 | * @return mixed as the JSON converted content. |
||
467 | */ |
||
468 | public function getBodyAsJson() |
||
476 | } |
||
477 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.