Completed
Push — master ( 541b76...aec89b )
by Henry
05:33
created

includes/Request.php (11 issues)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript;
3
4
/**
5
 * children class to request globals
6
 *
7
 * @since 2.2.0
8
 *
9
 * @package Redaxscript
10
 * @category Request
11
 * @author Henry Ruhs
12
 */
13
14
class Request extends Singleton
15
{
16
	/**
17
	 * array of the request
18
	 *
19
	 * @var array
20
	 */
21
22
	protected static $_requestArray = [];
23
24
	/**
25
	 * init the class
26
	 *
27
	 * @since 2.4.0
28
	 */
29
30 1
	public function init()
0 ignored issues
show
init 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...
init 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...
init 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...
init 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...
init uses the super-global variable $_SESSION 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...
init uses the super-global variable $_COOKIE 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...
31
	{
32
		self::$_requestArray =
33
		[
34 1
			'server' => $_SERVER ? : [],
35
			'get' => $_GET ? : [],
36
			'post' => $_POST ? : [],
37
			'files' => $_FILES ? : [],
38
			'session' => $_SESSION ? : [],
39
			'cookie' => $_COOKIE ? : []
40
		];
41 1
	}
42
43
	/**
44
	 * get the value from globals
45
	 *
46
	 * @since 3.0.0
47
	 *
48
	 * @param string $key key of the item
49
	 * @param string $index index of the array
50
	 *
51
	 * @return string|array|null
52
	 */
53
54 9
	public function get(string $key = null, string $index = null)
55
	{
56
		/* handle index */
57
58 9
		if (is_array(self::$_requestArray) && array_key_exists($index, self::$_requestArray))
59
		{
60 6
			$requestArray = self::$_requestArray[$index];
61
		}
62
		else
63
		{
64 3
			$requestArray = self::$_requestArray;
65
		}
66
67
		/* values as needed */
68
69 9
		if (is_array($requestArray) && array_key_exists($key, $requestArray))
70
		{
71 7
			return $requestArray[$key];
72
		}
73 2
		if (!$key)
74
		{
75 1
			return $requestArray;
76
		}
77 1
		return null;
78
	}
79
80
	/**
81
	 * get the value from server
82
	 *
83
	 * @since 2.2.0
84
	 *
85
	 * @param string $key key of the item
86
	 *
87
	 * @return string|array|null
88
	 */
89
90 1
	public function getServer(string $key = null)
91
	{
92 1
		return self::get($key, 'server');
93
	}
94
95
	/**
96
	 * get the value from query
97
	 *
98
	 * @since 2.2.0
99
	 *
100
	 * @param string $key key of the item
101
	 *
102
	 * @return string|array|null
103
	 */
104
105 1
	public function getQuery(string $key = null)
106
	{
107 1
		return self::get($key, 'get');
108
	}
109
110
	/**
111
	 * get the value from post
112
	 *
113
	 * @since 2.2.0
114
	 *
115
	 * @param string $key key of the item
116
	 *
117
	 * @return string|array|null
118
	 */
119
120 1
	public function getPost(string $key = null)
121
	{
122 1
		return self::get($key, 'post');
123
	}
124
125
	/**
126
	 * get the value from files
127
	 *
128
	 * @since 3.0.0
129
	 *
130
	 * @param string $key key of the item
131
	 *
132
	 * @return string|array|null
133
	 */
134
135 1
	public function getFiles(string $key = null)
136
	{
137 1
		return self::get($key, 'files');
138
	}
139
140
	/**
141
	 * get the value from session
142
	 *
143
	 * @since 2.2.0
144
	 *
145
	 * @param string $key key of the item
146
	 *
147
	 * @return string|array|null
148
	 */
149
150 1
	public function getSession(string $key = null)
151
	{
152 1
		return self::get($key, 'session');
153
	}
154
155
	/**
156
	 * get the value from cookie
157
	 *
158
	 * @since 2.2.0
159
	 *
160
	 * @param string $key key of the item
161
	 *
162
	 * @return string|array|null
163
	 */
164
165 1
	public function getCookie(string $key = null)
166
	{
167 1
		return self::get($key, 'cookie');
168
	}
169
170
	/**
171
	 * set the value to globals
172
	 *
173
	 * @since 2.2.0
174
	 *
175
	 * @param string $key key of the item
176
	 * @param string|array|null $value value of the item
177
	 */
178
179 1
	public function set(string $key = null, $value = null)
0 ignored issues
show
set uses the super-global variable $GLOBALS 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...
180
	{
181 1
		self::$_requestArray[$key] = $GLOBALS[$key] = $value;
182 1
	}
183
184
	/**
185
	 * set the value to server
186
	 *
187
	 * @since 2.2.0
188
	 *
189
	 * @param string $key key of the item
190
	 * @param string|array|null $value value of the item
191
	 */
192
193 1
	public function setServer(string $key = null, $value = null)
194
	{
195 1
		self::$_requestArray['server'][$key] = $value;
196 1
	}
197
198
	/**
199
	 * set the value to query
200
	 *
201
	 * @since 2.2.0
202
	 *
203
	 * @param string $key key of the item
204
	 * @param string|array|null $value value of the item
205
	 */
206
207 1
	public function setQuery(string $key = null, $value = null)
208
	{
209 1
		self::$_requestArray['get'][$key] = $value;
210 1
	}
211
212
	/**
213
	 * set the value to post
214
	 *
215
	 * @since 2.2.0
216
	 *
217
	 * @param string $key key of the item
218
	 * @param string|array|null $value value of the item
219
	 */
220
221 1
	public function setPost(string $key = null, $value = null)
222
	{
223 1
		self::$_requestArray['post'][$key] = $value;
224 1
	}
225
226
	/**
227
	 * set the value to files
228
	 *
229
	 * @since 3.0.0
230
	 *
231
	 * @param string $key key of the item
232
	 * @param string|array|null $value value of the item
233
	 */
234
235 1
	public function setFiles(string $key = null, $value = null)
236
	{
237 1
		self::$_requestArray['files'][$key] = $value;
238 1
	}
239
240
	/**
241
	 * set the value to session
242
	 *
243
	 * @since 2.2.0
244
	 *
245
	 * @param string $key key of the item
246
	 * @param string|array|null $value value of the item
247
	 */
248
249 1
	public function setSession(string $key = null, $value = null)
0 ignored issues
show
setSession uses the super-global variable $_SESSION 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...
250
	{
251 1
		self::$_requestArray['session'][$key] = $_SESSION[$key] = $value;
252 1
	}
253
254
	/**
255
	 * set the value to cookie
256
	 *
257
	 * @since 2.2.0
258
	 *
259
	 * @param string $key key of the item
260
	 * @param string|array|null $value value of the item
261
	 */
262
263 1
	public function setCookie(string $key = null, $value = null)
0 ignored issues
show
setCookie uses the super-global variable $_COOKIE 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...
264
	{
265 1
		self::$_requestArray['cookie'][$key] = $_COOKIE[$key] = $value;
266 1
	}
267
268
	/**
269
	 * refresh the session
270
	 *
271
	 * @since 2.6.2
272
	 */
273
274 1
	public function refreshSession()
0 ignored issues
show
refreshSession uses the super-global variable $_SESSION 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...
275
	{
276 1
		self::$_requestArray['session'] = $_SESSION ? : [];
277 1
	}
278
279
	/**
280
	 * refresh the cookie
281
	 *
282
	 * @since 2.6.2
283
	 */
284
285 1
	public function refreshCookie()
0 ignored issues
show
refreshCookie uses the super-global variable $_COOKIE 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...
286
	{
287 1
		self::$_requestArray['cookie'] = $_COOKIE ? : [];
288 1
	}
289
}
290