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 |
||
9 | class CRequestBasic |
||
10 | { |
||
11 | |||
12 | |||
13 | /** |
||
14 | * Properties |
||
15 | * |
||
16 | */ |
||
17 | private $requestUri; // Request URI from $_SERVER |
||
18 | private $scriptName; // Scriptname from $_SERVER, actual scriptname part |
||
19 | private $path; // Scriptname from $_SERVER, path-part |
||
20 | |||
21 | private $route; // The route |
||
22 | private $routeParts; // The route as an array |
||
23 | |||
24 | |||
25 | private $currentUrl; // Current url |
||
26 | private $siteUrl; // Url to this site, http://dbwebb.se |
||
27 | private $baseUrl; // Url to root dir, siteUrl . /some/installation/directory/ |
||
28 | |||
29 | private $server; // Mapped to $_SERVER |
||
30 | private $get; // Mapped to $_GET |
||
31 | private $post; // Mapped to $_POST |
||
32 | |||
33 | |||
34 | |||
35 | /** |
||
36 | * Constructor. |
||
37 | * |
||
38 | * |
||
39 | */ |
||
40 | public function __construct() |
||
44 | |||
45 | |||
46 | |||
47 | /** |
||
48 | * Read info from the globals. |
||
49 | * |
||
50 | * @param array $globals use to initiate globals with values. |
||
51 | * |
||
52 | * @return void |
||
53 | */ |
||
54 | public function setGlobals($globals = []) |
||
60 | |||
61 | |||
62 | |||
63 | /** |
||
64 | * Init the request class by reading information from the request. |
||
65 | * |
||
66 | * @return $this |
||
67 | */ |
||
68 | public function init() |
||
88 | |||
89 | |||
90 | |||
91 | /** |
||
92 | * Get site url. |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | public function getSiteUrl() |
||
100 | |||
101 | |||
102 | |||
103 | /** |
||
104 | * Get base url. |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | public function getBaseUrl() |
||
112 | |||
113 | |||
114 | |||
115 | /** |
||
116 | * Get script name. |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | public function getScriptName() |
||
124 | |||
125 | |||
126 | |||
127 | /** |
||
128 | * Get route parts. |
||
129 | * |
||
130 | * @return array |
||
131 | */ |
||
132 | public function getRouteParts() |
||
136 | |||
137 | |||
138 | |||
139 | /** |
||
140 | * Get the route. |
||
141 | * |
||
142 | * @return string as the current extracted route |
||
143 | */ |
||
144 | public function getRoute() |
||
148 | |||
149 | |||
150 | |||
151 | /** |
||
152 | * Extract the part containing the route. |
||
153 | * |
||
154 | * @return string as the current extracted route |
||
155 | */ |
||
156 | public function extractRoute() |
||
197 | |||
198 | |||
199 | |||
200 | /** |
||
201 | * Get the current url. |
||
202 | * |
||
203 | * @param boolean $queryString attach query string, default is true. |
||
204 | * |
||
205 | * @return string as current url. |
||
206 | */ |
||
207 | public function getCurrentUrl($queryString = true) |
||
234 | |||
235 | |||
236 | |||
237 | /** |
||
238 | * Get a value from the _SERVER array and use default if it is not set. |
||
239 | * |
||
240 | * @param string $key to check if it exists in the $_SERVER variable |
||
241 | * @param string $default value to return as default |
||
242 | * |
||
243 | * @return mixed |
||
244 | */ |
||
245 | public function getServer($key, $default = null) |
||
249 | |||
250 | |||
251 | |||
252 | /** |
||
253 | * Set variable in the server array. |
||
254 | * |
||
255 | * @param mixed $key the key an the , or an key-value array |
||
256 | * @param string $value the value of the key |
||
257 | * |
||
258 | * @return $this |
||
259 | */ |
||
260 | View Code Duplication | public function setServer($key, $value = null) |
|
268 | |||
269 | |||
270 | |||
271 | /** |
||
272 | * Get a value from the _GET array and use default if it is not set. |
||
273 | * |
||
274 | * @param string $key to check if it exists in the $_GET variable |
||
275 | * @param string $default value to return as default |
||
276 | * |
||
277 | * @return mixed |
||
278 | */ |
||
279 | public function getGet($key, $default = null) |
||
283 | |||
284 | |||
285 | |||
286 | /** |
||
287 | * Set variable in the get array. |
||
288 | * |
||
289 | * @param mixed $key the key an the , or an key-value array |
||
290 | * @param string $value the value of the key |
||
291 | * |
||
292 | * @return $this |
||
293 | */ |
||
294 | View Code Duplication | public function setGet($key, $value = null) |
|
302 | |||
303 | |||
304 | |||
305 | /** |
||
306 | * Get a value from the _POST array and use default if it is not set. |
||
307 | * |
||
308 | * @param string $key to check if it exists in the $_POST variable |
||
309 | * @param string $default value to return as default |
||
310 | * |
||
311 | * @return mixed |
||
312 | */ |
||
313 | public function getPost($key = null, $default = null) |
||
321 | } |
||
322 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: