Request   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 314
Duplicated Lines 6.37 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 34
lcom 1
cbo 1
dl 20
loc 314
rs 9.68
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromHeaders() 0 17 3
A setScheme() 0 4 1
A getScheme() 0 4 1
A setHeaders() 0 4 1
A getHeaders() 0 4 1
A setHost() 0 4 1
A getHost() 0 4 1
A setPath() 10 10 2
A getPath() 0 4 1
A setUri() 10 10 2
A getUri() 0 4 1
A setMethod() 0 4 1
A getMethod() 0 5 3
A setQuery() 0 4 1
A getQuery() 0 4 1
A setBody() 0 4 1
A getBody() 0 4 1
A isPost() 0 4 1
A isGet() 0 4 1
B getParam() 0 24 6
A getPostData() 0 4 2
A setPostData() 0 4 1

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
 * Class Request
4
 *
5
 * @package Faulancer\Http
6
 * @author Florian Knapp <[email protected]>
7
 */
8
namespace Faulancer\Http;
9
10
use Faulancer\Exception\InvalidArgumentException;
11
12
/**
13
 * Class Request
14
 */
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()
81
    {
82
        $path = $_SERVER['REQUEST_URI'];
83
84
        if (strpos($_SERVER['REQUEST_URI'], '?') !== false) {
85
            $path = explode('?', $_SERVER['REQUEST_URI']);
86
            $this->setQuery($path[1]);
87
            $path = $path[0];
88
        }
89
90
        $this->setScheme(!empty($_SERVER['HTTPS']) ? 'https://' : 'http://');
91
        $this->setHost($_SERVER['HTTP_HOST']);
92
        $this->setPath($path);
93
        $this->setMethod($_SERVER['REQUEST_METHOD']);
94
95
        return $this;
96
    }
97
98
    /**
99
     * Set the request scheme
100
     *
101
     * @param string $scheme
102
     */
103
    public function setScheme(string $scheme = '')
104
    {
105
        $this->scheme = $scheme;
106
    }
107
108
    /**
109
     * Get the request scheme
110
     *
111
     * @return string
112
     */
113
    public function getScheme()
114
    {
115
        return $this->scheme;
116
    }
117
118
    /**
119
     * @param array $headers
120
     */
121
    public function setHeaders(array $headers = [])
122
    {
123
        $this->headers = $headers;
124
    }
125
126
    /**
127
     * @return array
128
     */
129
    public function getHeaders()
130
    {
131
        return $this->headers;
132
    }
133
134
    /**
135
     * @param string $host
136
     */
137
    public function setHost(string $host = '')
138
    {
139
        $this->host = $host;
140
    }
141
142
    /**
143
     * @return string
144
     */
145
    public function getHost()
146
    {
147
        return $this->host;
148
    }
149
150
    /**
151
     * Set uri path
152
     *
153
     * @param string $path
154
     */
155 View Code Duplication
    public function setPath(string $path)
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...
156
    {
157
        if (strpos($path, '?') !== false) {
158
            $path = explode('?', $path);
159
            $this->setQuery($path[1]);
160
            $path = $path[0];
161
        }
162
163
        $this->path = $path;
164
    }
165
166
    /**
167
     * Get uri path
168
     *
169
     * @return string
170
     */
171
    public function getPath() :string
172
    {
173
        return $this->path;
174
    }
175
176
    /**
177
     * Set uri path
178
     *
179
     * @param string $uri
180
     */
181 View Code Duplication
    public function setUri(string $uri)
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...
182
    {
183
        if (strpos($uri, '?') !== false) {
184
            $path = explode('?', $uri);
185
            $this->setQuery($path[1]);
186
            $uri = $path[0];
187
        }
188
189
        $this->uri = $uri;
190
    }
191
192
    /**
193
     * Get uri path
194
     *
195
     * @return string
196
     */
197
    public function getUri() :string
198
    {
199
        return $this->uri;
200
    }
201
202
    /**
203
     * Set method
204
     *
205
     * @param string $method
206
     */
207
    public function setMethod(string $method)
208
    {
209
        $this->method = $method;
210
    }
211
212
    /**
213
     * Get method
214
     *
215
     * @return string
216
     */
217
    public function getMethod() :string
218
    {
219
        $serverRequestMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '';
220
        return empty($this->method) ? $serverRequestMethod : $this->method;
221
    }
222
223
    /**
224
     * Set query string
225
     *
226
     * @param string $query
227
     */
228
    public function setQuery(string $query)
229
    {
230
        $this->query = $query;
231
    }
232
233
    /**
234
     * Get query string
235
     *
236
     * @return string
237
     */
238
    public function getQuery() :string
239
    {
240
        return $this->query;
241
    }
242
243
    /**
244
     * @param array $body
245
     */
246
    public function setBody($body)
247
    {
248
       $this->body = $body;
0 ignored issues
show
Documentation Bug introduced by
It seems like $body of type array is incompatible with the declared type string of property $body.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
249
    }
250
251
    /**
252
     * @return string
253
     */
254
    public function getBody()
255
    {
256
        return $this->body;
257
    }
258
259
    /**
260
     * Determine if it's a post request
261
     *
262
     * @return boolean
263
     */
264
    public function isPost() :bool
265
    {
266
        return $this->getMethod() === 'POST';
267
    }
268
269
    /**
270
     * Determine if it's a get request
271
     *
272
     * @return boolean
273
     */
274
    public function isGet() :bool
275
    {
276
        return $this->getMethod() === 'GET';
277
    }
278
279
    /**
280
     * @param string $key
281
     *
282
     * @return mixed
283
     */
284
    public function getParam(string $key)
285
    {
286
        $this->post  = !empty($_POST) ? array_merge($_POST, $this->post) : [];
287
        $this->get   = !empty($_GET) ? array_merge($_GET, $this->get) : [];
288
289
        if (!empty($this->getQuery())) {
290
291
            $query = [];
292
            parse_str($this->getQuery(), $query);
293
294
            if (!empty($query[$key])) {
295
                return $query[$key];
296
            }
297
298
        }
299
300
        $combined = array_merge($this->post, $this->get);
301
302
        if (!empty($combined[$key])) {
303
            return $combined[$key];
304
        }
305
306
        return null;
307
    }
308
309
    /**
310
     * Return the post data
311
     *
312
     * @return array
313
     */
314
    public function getPostData() :array
315
    {
316
        return empty($_POST) ? [] : $_POST;
317
    }
318
319
    /**
320
     * @param $data
321
     * @return void
322
     */
323
    public function setPostData($data)
324
    {
325
        $_POST = $data;
326
    }
327
328
}