CRequestBasic   B
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 313
Duplicated Lines 5.11 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 84.38%

Importance

Changes 10
Bugs 3 Features 0
Metric Value
wmc 38
c 10
b 3
f 0
lcom 1
cbo 0
dl 16
loc 313
rs 8.3999
ccs 81
cts 96
cp 0.8438

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setGlobals() 0 6 4
A getSiteUrl() 0 4 1
A getBaseUrl() 0 4 1
A getScriptName() 0 4 1
A getRouteParts() 0 4 1
A getRoute() 0 4 1
A getServer() 0 4 2
A getGet() 0 4 2
A init() 0 20 2
C extractRoute() 0 41 7
D getCurrentUrl() 0 27 8
A setServer() 8 8 2
A setGet() 8 8 2
A getPost() 0 8 3

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace Anax\Request;
4
5
/**
6
 * Storing information from the request and calculating related essentials.
7
 *
8
 */
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 18
    public function __construct()
41
    {
42 18
        $this->setGlobals();
43 18
    }
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 18
    public function setGlobals($globals = [])
55
    {
56 18
        $this->server = isset($globals['server']) ? array_merge($_SERVER, $globals['server']) : $_SERVER;
57 18
        $this->get    = isset($globals['get'])    ? array_merge($_GET, $globals['get'])       : $_GET;
58 18
        $this->post   = isset($globals['post'])   ? array_merge($_POST, $globals['post'])     : $_POST;
59 18
    }
60
61
62
63
    /**
64
     * Init the request class by reading information from the request.
65
     *
66
     * @return $this
67
     */
68 4
    public function init()
69
    {
70 4
        $this->requestUri = $this->getServer('REQUEST_URI');
71 4
        $scriptName = $this->getServer('SCRIPT_NAME');
72 4
        $this->path = rtrim(dirname($scriptName), '/');
73 4
        $this->scriptName = basename($scriptName);
74
75
        // The route and its parts
76 4
        $this->extractRoute();
77
78
        // Prepare to create siteUrl and baseUrl by using currentUrl
79 4
        $this->currentUrl = $this->getCurrentUrl();
80 4
        $parts = parse_url($this->currentUrl);
81 4
        $this->siteUrl = "{$parts['scheme']}://{$parts['host']}" . (isset($parts['port'])
82 4
            ? ":{$parts['port']}"
83 4
            : '');
84 4
        $this->baseUrl = $this->siteUrl . $this->path;
85
86 4
        return $this;
87
    }
88
89
90
91
    /**
92
     * Get site url.
93
     *
94
     * @return string
95
     */
96 4
    public function getSiteUrl()
97
    {
98 4
        return $this->siteUrl;
99
    }
100
101
102
103
    /**
104
     * Get base url.
105
     *
106
     * @return string
107
     */
108 4
    public function getBaseUrl()
109
    {
110 4
        return $this->baseUrl;
111
    }
112
113
114
115
    /**
116
     * Get script name.
117
     *
118
     * @return string
119
     */
120
    public function getScriptName()
121
    {
122
        return $this->scriptName;
123
    }
124
125
126
127
    /**
128
     * Get route parts.
129
     *
130
     * @return array
131
     */
132
    public function getRouteParts()
133
    {
134
        return $this->routeParts;
135
    }
136
137
138
139
    /**
140
     * Get the route.
141
     *
142
     * @return string as the current extracted route
143
     */
144 6
    public function getRoute()
145
    {
146 6
        return $this->route;
147
    }
148
149
150
151
    /**
152
     * Extract the part containing the route.
153
     *
154
     * @return string as the current extracted route
155
     */
156 10
    public function extractRoute()
157
    {
158 10
        $requestUri = $this->getServer('REQUEST_URI');
159 10
        $scriptName = $this->getServer('SCRIPT_NAME');
160 10
        $scriptPath = dirname($scriptName);
161 10
        $scriptFile = basename($scriptName);
162
163
        // Compare REQUEST_URI and SCRIPT_NAME as long they match,
164
        // leave the rest as current request.
165 10
        $i = 0;
166 10
        $len = min(strlen($requestUri), strlen($scriptPath));
167
        while ($i < $len
168 10
               && $requestUri[$i] == $scriptPath[$i]
169 10
        ) {
170 10
            $i++;
171 10
        }
172 10
        $route = trim(substr($requestUri, $i), '/');
173
174
        // Does the request start with script-name - remove it.
175 10
        $len1 = strlen($route);
176 10
        $len2 = strlen($scriptFile);
177
178
        if ($len2 <= $len1
179 10
            && substr_compare($scriptFile, $route, 0, $len2, true) === 0
180 10
        ) {
181 10
            $route = substr($route, $len2 + 1);
182 10
        }
183
184
        // Remove the ?-part from the query when analysing controller/metod/arg1/arg2
185 10
        $queryPos = strpos($route, '?');
186 10
        if ($queryPos !== false) {
187
            $route = substr($route, 0, $queryPos);
188
        }
189
190 10
        $route = ($route === false) ? '' : $route;
191
192 10
        $this->route = $route;
193 10
        $this->routeParts = explode('/', trim($route, '/'));
194
//var_dump($route);
195 10
        return $this->route;
196
    }
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 11
    public function getCurrentUrl($queryString = true)
208
    {
209 11
        $rs    = $this->getServer('REQUEST_SCHEME');
210 11
        $https = $this->getServer('HTTPS') == 'on' ? true : false;
211 11
        $sn    = $this->getServer('SERVER_NAME');
212 11
        $port  = $this->getServer('SERVER_PORT');
213
214 11
        $port  = ($port == '80')
215 11
            ? ''
216 11
            : (($port == 443 && $https)
217 6
                ? ''
218 11
                : ':' . $port);
219
220 11
        if ($queryString) {
221 11
            $ru = rtrim($this->getServer('REQUEST_URI'), '/');
222 11
        } else {
223
            $ru = rtrim(strtok($this->getServer('REQUEST_URI'), '?'), '/');
224
        }
225
226
227 11
        $url  = $rs ? $rs : 'http';
228 11
        $url .= $https ? 's' : '';
229 11
        $url .= '://';
230 11
        $url .= $sn . $port . htmlspecialchars($ru);
231
        
232 11
        return $url;
233
    }
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 17
    public function getServer($key, $default = null)
246
    {
247 17
        return isset($this->server[$key]) ? $this->server[$key] : $default;
248
    }
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 17 View Code Duplication
    public function setServer($key, $value = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
261
    {
262 17
        if (is_array($key)) {
263
            $this->server = array_merge($this->server, $key);
264
        } else {
265 17
            $this->server[$key] = $value;
266
        }
267 17
    }
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 1
    public function getGet($key, $default = null)
280
    {
281 1
        return isset($this->get[$key]) ? $this->get[$key] : $default;
282
    }
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 1 View Code Duplication
    public function setGet($key, $value = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
295
    {
296 1
        if (is_array($key)) {
297
            $this->get = array_merge($this->get, $key);
298
        } else {
299 1
            $this->get[$key] = $value;
300
        }
301 1
    }
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)
314
    {
315
        if ($key) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $key of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
316
            return isset($this->post[$key]) ? $this->post[$key] : $default;
317
        } else {
318
            return $this->post;
319
        }
320
    }
321
}
322