Test Failed
Push — master ( 849d86...c80995 )
by Jay
12:00
created

Http   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 248
Duplicated Lines 6.45 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 16
loc 248
ccs 56
cts 56
cp 1
rs 10
wmc 29
lcom 2
cbo 1

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A get() 0 6 2
A has() 0 6 2
A getGet() 0 4 2
A getPost() 0 4 2
A getServer() 0 4 2
A getFiles() 0 4 2
A isAjax() 0 5 1
A getMethod() 0 4 1
A getRequestUri() 0 9 2
A setPostValue() 8 8 1
A setGetValue() 8 8 1
A hasPost() 0 4 1
A hasGet() 0 4 1
A hasServer() 0 4 1
A hasFiles() 0 4 1
A getReferer() 0 4 1
A getHeaders() 0 4 2
A getHeader() 0 4 2
A hasHeader() 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
namespace FMUP\Request;
3
4
use FMUP\Request;
5
6
class Http extends Request
7
{
8
    const HTTP_X_REQUESTED_WITH = 'HTTP_X_REQUESTED_WITH';
9
    const HTTP_X_REQUESTED_WITH_AJAX = 'XMLHttpRequest';
10
11
    const REQUEST_URI = 'REQUEST_URI';
12
    const REQUEST_SCHEME = 'REQUEST_SCHEME';
13
    const QUERY_STRING = 'QUERY_STRING';
14
15
    const REQUEST_METHOD = 'REQUEST_METHOD';
16
    const REQUEST_METHOD_GET = 'GET';
17
    const REQUEST_METHOD_POST = 'POST';
18
19
    const SERVER_NAME = 'SERVER_NAME';
20
    const HTTP_REFERER = 'HTTP_REFERER';
21
    const HTTP_HOST = 'HTTP_HOST';
22
23
    const AUTHORIZATION_TOKEN = 'Authorization-Token';
24
25
    protected $get = array();
26
    protected $post = array();
27
    protected $server = array();
28
    protected $files = array();
29
30
    /**
31
     * mapped to HTTP values
32
     */
33 27
    public function __construct()
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $_GET which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
__construct uses the super-global variable $_POST which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
__construct uses the super-global variable $_SERVER which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
__construct uses the super-global variable $_FILES which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
34
    {
35 27
        $this->get = $_GET;
36 27
        $this->post = $_POST;
37 27
        $this->server = $_SERVER;
38 27
        $this->files = $_FILES;
39 27
    }
40
41
    /**
42
     * Get a value in the request depending HTTP method
43
     * @param string $name Name to retrieve
44
     * @param mixed $defaultValue Value returned if name is not defined in query
45
     * @return mixed
46
     */
47 1
    public function get($name, $defaultValue = null)
48
    {
49 1
        return ($this->getMethod() == self::REQUEST_METHOD_POST)
50 1
            ? $this->getPost($name, $defaultValue)
51 1
            : $this->getGet($name, $defaultValue);
52
    }
53
54
    /**
55
     * Check if param exists
56
     * @param string $name
57
     * @return bool
58
     */
59 1
    public function has($name)
60
    {
61 1
        return ($this->getMethod() == self::REQUEST_METHOD_POST)
62 1
            ? $this->hasPost($name)
63 1
            : $this->hasGet($name);
64
    }
65
66
    /**
67
     * Return requested value in GET method
68
     * @param string $name Name to retrieve
69
     * @param mixed $defaultValue Value returned if name is not defined in query
70
     * @return mixed
71
     */
72 2
    public function getGet($name, $defaultValue = null)
73
    {
74 2
        return $this->hasGet($name) ? $this->get[$name] : $defaultValue;
75
    }
76
77
    /**
78
     * Return requested value in POST method
79
     * @param string $name Name to retrieve
80
     * @param mixed $defaultValue Value returned if name is not defined in query
81
     * @return mixed
82
     */
83 2
    public function getPost($name, $defaultValue = null)
84
    {
85 2
        return $this->hasPost($name) ? $this->post[$name] : $defaultValue;
86
    }
87
88
    /**
89
     * Retrieve a value defined on server
90
     * @param string $name Name to retrieve
91
     * @param mixed $defaultValue Value returned if name is not defined in query
92
     * @return mixed
93
     */
94 7
    public function getServer($name, $defaultValue = null)
95
    {
96 7
        return $this->hasServer($name) ? $this->server[$name] : $defaultValue;
97
    }
98
99
    /**
100
     * Retrieve a value defined on files
101
     * @param int $name
102
     * @param mixed $defaultValue Value returned if name is not defined in query
103
     * @return mixed
104
     */
105 1
    public function getFiles($name, $defaultValue = null)
106
    {
107 1
        return $this->hasFiles($name) ? $this->files[$name] : $defaultValue;
108
    }
109
110
    /**
111
     * Check whether request is an Ajax request
112
     * @return bool
113
     */
114 1
    public function isAjax()
115
    {
116 1
        $requestClient = $this->getServer(self::HTTP_X_REQUESTED_WITH);
117 1
        return (strtolower($requestClient) == strtolower(self::HTTP_X_REQUESTED_WITH_AJAX));
118
    }
119
120
    /**
121
     * Get HTTP method (GET/POST/DELETE...)
122
     * @return string
123
     */
124 3
    public function getMethod()
125
    {
126 3
        return $this->getServer(self::REQUEST_METHOD);
127
    }
128
129
    /**
130
     * Retrieve called URI
131
     * @param bool $withQuerySting must return query string in the request
132
     * @return string
133
     */
134 1
    public function getRequestUri($withQuerySting = false)
135
    {
136 1
        $requestUri = $this->getServer(self::REQUEST_URI);
137
        return (string)(
138 1
            $withQuerySting
139 1
            ? $requestUri
140 1
            : str_replace('?' . $this->getServer(self::QUERY_STRING), '', $requestUri)
141
        );
142
    }
143
144
    /**
145
     * Define a POST value
146
     * @param string $name Name to define
147
     * @param mixed $value Value to define
148
     * @return $this
149
     */
150 1 View Code Duplication
    public function setPostValue($name, $value)
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...
Coding Style introduced by
setPostValue uses the super-global variable $_POST which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
setPostValue uses the super-global variable $_REQUEST which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
151
    {
152 1
        $this->post[$name] = $value;
153
        //for compliance purpose only - do not use
154 1
        $_POST[$name] = $value;
155 1
        $_REQUEST[$name] = $value;
156 1
        return $this;
157
    }
158
159
    /**
160
     * Define a GET value
161
     * @param string $name Name to define
162
     * @param mixed $value Value to define
163
     * @return $this
164
     */
165 1 View Code Duplication
    public function setGetValue($name, $value)
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...
Coding Style introduced by
setGetValue uses the super-global variable $_GET which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
setGetValue uses the super-global variable $_REQUEST which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
166
    {
167 1
        $this->get[$name] = $value;
168
        //for compliance purpose only - do not use
169 1
        $_GET[$name] = $value;
170 1
        $_REQUEST[$name] = $value;
171 1
        return $this;
172
    }
173
174
    /**
175
     * Check whether selected post exists
176
     * @param string $name
177
     * @return bool
178
     */
179 3
    public function hasPost($name)
180
    {
181 3
        return array_key_exists($name, $this->post);
182
    }
183
184
    /**
185
     * Check whether selected get exists
186
     * @param string $name
187
     * @return bool
188
     */
189 3
    public function hasGet($name)
190
    {
191 3
        return array_key_exists($name, $this->get);
192
    }
193
194
    /**
195
     * Check whether selected server value exists
196
     * @param string $name
197
     * @return bool
198
     */
199 7
    public function hasServer($name)
200
    {
201 7
        return array_key_exists($name, $this->server);
202
    }
203
204
    /**
205
     * Check whether selected files value exists
206
     * @param string $name
207
     * @return bool
208
     */
209 1
    public function hasFiles($name)
210
    {
211 1
        return array_key_exists($name, $this->files);
212
    }
213
214
    /**
215
     * Return request referer
216
     * @return string
217
     */
218 1
    public function getReferer()
219
    {
220 1
        return $this->getServer(self::HTTP_REFERER);
221
    }
222
223
    /**
224
     * Return request headers
225
     * @codeCoverageIgnore
226
     * @return array
227
     */
228
    public function getHeaders()
229
    {
230
        return (array) (function_exists('getallheaders') ? getallheaders() : false);
231
    }
232
233
    /**
234
     * Return requested key in headers HTTP
235
     * @param string $key
236
     * @param mixed $default
237
     * @return mixed
238
     */
239 1
    public function getHeader($key, $default = null)
240
    {
241 1
        return $this->hasHeader($key) ? $this->getHeaders()[$key] : $default;
242
    }
243
244
    /**
245
     * Check whether selected key exists
246
     * @param string $key
247
     * @return bool
248
     */
249 2
    public function hasHeader($key)
250
    {
251 2
        return array_key_exists($key, $this->getHeaders());
252
    }
253
}
254