Completed
Push — master ( c3c208...858dca )
by Henry
08:18
created

Request   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 342
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.11%

Importance

Changes 0
Metric Value
wmc 45
lcom 1
cbo 1
dl 0
loc 342
c 0
b 0
f 0
ccs 70
cts 76
cp 0.9211
rs 8.8

21 Methods

Rating   Name   Duplication   Size   Complexity  
A getArray() 0 4 1
A getStream() 0 8 3
A setStream() 0 4 1
A _loadStream() 0 4 1
B init() 0 13 7
A get() 0 8 3
A set() 0 4 1
A getServer() 0 8 3
A setServer() 0 4 1
A getQuery() 0 8 3
A setQuery() 0 4 1
A getPost() 0 8 3
A setPost() 0 4 1
A getFiles() 0 8 3
A setFiles() 0 4 1
A getSession() 0 8 3
A setSession() 0 4 1
A refreshSession() 0 4 2
A getCookie() 0 8 3
A setCookie() 0 4 1
A refreshCookie() 0 4 2

How to fix   Complexity   

Complex Class

Complex classes like Request often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Request, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace Redaxscript;
3
4
use function array_key_exists;
5
use function file_get_contents;
6
use function is_array;
7
use function json_decode;
8
9
/**
10
 * children class to request globals
11
 *
12
 * @since 2.2.0
13
 *
14
 * @package Redaxscript
15
 * @category Request
16
 * @author Henry Ruhs
17
 */
18
19
class Request extends Singleton
20
{
21
	/**
22
	 * array of the request
23
	 *
24
	 * @var array
25
	 */
26
27
	protected static $_requestArray = [];
28
29
	/**
30
	 * init the class
31
	 *
32
	 * @since 2.4.0
33
	 */
34
35 1
	public function init() : void
36
	{
37
		self::$_requestArray =
38
		[
39 1
			'server' => $_SERVER ? : [],
40
			'get' => $_GET ? : [],
41
			'post' => $_POST ? : [],
42
			'files' => $_FILES ? : [],
43 1
			'stream' => self::_loadStream(),
44
			'session' => $_SESSION ? : [],
45
			'cookie' => $_COOKIE ? : []
46
		];
47 1
	}
48
49
	/**
50
	 * get the value from globals
51
	 *
52
	 * @since 3.0.0
53
	 *
54
	 * @param string $key key of the item
55
	 *
56
	 * @return string|array|null
57
	 */
58
59 2
	public function get(string $key = null)
60
	{
61 2
		if (is_array(self::$_requestArray) && array_key_exists($key, self::$_requestArray))
62
		{
63 1
			return self::$_requestArray[$key];
64
		}
65 1
		return null;
66
	}
67
68
	/**
69
	 * get the array from globals
70
	 *
71
	 * @since 4.0.0
72
	 *
73
	 * @return array
74
	 */
75
76 1
	public function getArray() : array
77
	{
78 1
		return self::$_requestArray;
79
	}
80
81
	/**
82
	 * set the value to globals
83
	 *
84
	 * @since 2.2.0
85
	 *
86
	 * @param string $key key of the item
87
	 * @param string|array|null $value value of the item
88
	 */
89
90 1
	public function set(string $key = null, $value = null) : void
91
	{
92 1
		self::$_requestArray[$key] = $GLOBALS[$key] = $value;
93 1
	}
94
95
	/**
96
	 * get the value from server
97
	 *
98
	 * @since 2.2.0
99
	 *
100
	 * @param string $key key of the item
101
	 *
102
	 * @return string|array|null
103
	 */
104
105 2
	public function getServer(string $key = null)
106
	{
107 2
		if (is_array(self::$_requestArray['server']) && array_key_exists($key, self::$_requestArray['server']))
108
		{
109 1
			return self::$_requestArray['server'][$key];
110
		}
111 1
		return null;
112
	}
113
114
	/**
115
	 * set the value to server
116
	 *
117
	 * @since 2.2.0
118
	 *
119
	 * @param string $key key of the item
120
	 * @param string|array|null $value value of the item
121
	 */
122
123 1
	public function setServer(string $key = null, $value = null) : void
124
	{
125 1
		self::$_requestArray['server'][$key] = $value;
126 1
	}
127
128
	/**
129
	 * get the value from query
130
	 *
131
	 * @since 2.2.0
132
	 *
133
	 * @param string $key key of the item
134
	 *
135
	 * @return string|array|null
136
	 */
137
138 2
	public function getQuery(string $key = null)
139
	{
140 2
		if (is_array(self::$_requestArray['get']) && array_key_exists($key, self::$_requestArray['get']))
141
		{
142 1
			return self::$_requestArray['get'][$key];
143
		}
144 1
		return null;
145
	}
146
147
	/**
148
	 * set the value to query
149
	 *
150
	 * @since 2.2.0
151
	 *
152
	 * @param string $key key of the item
153
	 * @param string|array|null $value value of the item
154
	 */
155
156 1
	public function setQuery(string $key = null, $value = null) : void
157
	{
158 1
		self::$_requestArray['get'][$key] = $value;
159 1
	}
160
161
	/**
162
	 * get the value from post
163
	 *
164
	 * @since 2.2.0
165
	 *
166
	 * @param string $key key of the item
167
	 *
168
	 * @return string|array|null
169
	 */
170
171 2
	public function getPost(string $key = null)
172
	{
173 2
		if (is_array(self::$_requestArray['post']) && array_key_exists($key, self::$_requestArray['post']))
174
		{
175 1
			return self::$_requestArray['post'][$key];
176
		}
177 1
		return null;
178
	}
179
180
	/**
181
	 * set the value to post
182
	 *
183
	 * @since 2.2.0
184
	 *
185
	 * @param string $key key of the item
186
	 * @param string|array|null $value value of the item
187
	 */
188
189 1
	public function setPost(string $key = null, $value = null) : void
190
	{
191 1
		self::$_requestArray['post'][$key] = $value;
192 1
	}
193
194
	/**
195
	 * get the value from files
196
	 *
197
	 * @since 3.0.0
198
	 *
199
	 * @param string $key key of the item
200
	 *
201
	 * @return string|array|null
202
	 */
203
204 2
	public function getFiles(string $key = null)
205
	{
206 2
		if (is_array(self::$_requestArray['files']) && array_key_exists($key, self::$_requestArray['files']))
207
		{
208 1
			return self::$_requestArray['files'][$key];
209
		}
210 1
		return null;
211
	}
212
213
	/**
214
	 * set the value to files
215
	 *
216
	 * @since 3.0.0
217
	 *
218
	 * @param string $key key of the item
219
	 * @param string|array|null $value value of the item
220
	 */
221
222 1
	public function setFiles(string $key = null, $value = null) : void
223
	{
224 1
		self::$_requestArray['files'][$key] = $value;
225 1
	}
226
227
	/**
228
	 * get the value from stream
229
	 *
230
	 * @since 4.2.0
231
	 *
232
	 * @param string $key key of the item
233
	 *
234
	 * @return string|array|null
235
	 */
236
237 2
	public function getStream(string $key = null)
238
	{
239 2
		if (is_array(self::$_requestArray['stream']) && array_key_exists($key, self::$_requestArray['stream']))
240
		{
241 1
			return self::$_requestArray['stream'][$key];
242
		}
243 1
		return null;
244
	}
245
246
	/**
247
	 * set the value to stream
248
	 *
249
	 * @since 4.2.0
250
	 *
251
	 * @param string $key key of the item
252
	 * @param string|array|null $value value of the item
253
	 */
254
255 1
	public function setStream(string $key = null, $value = null) : void
256
	{
257 1
		self::$_requestArray['stream'][$key] = $value;
258 1
	}
259
260
	/**
261
	 * get the value from session
262
	 *
263
	 * @since 2.2.0
264
	 *
265
	 * @param string $key key of the item
266
	 *
267
	 * @return string|array|null
268
	 */
269
270 2
	public function getSession(string $key = null)
271
	{
272 2
		if (is_array(self::$_requestArray['session']) && array_key_exists($key, self::$_requestArray['session']))
273
		{
274 1
			return self::$_requestArray['session'][$key];
275
		}
276 1
		return null;
277
	}
278
279
	/**
280
	 * set the value to session
281
	 *
282
	 * @since 2.2.0
283
	 *
284
	 * @param string $key key of the item
285
	 * @param string|array|null $value value of the item
286
	 */
287
288 1
	public function setSession(string $key = null, $value = null) : void
289
	{
290 1
		self::$_requestArray['session'][$key] = $_SESSION[$key] = $value;
291 1
	}
292
293
	/**
294
	 * refresh the session
295
	 *
296
	 * @since 2.6.2
297
	 */
298
299 1
	public function refreshSession() : void
300
	{
301 1
		self::$_requestArray['session'] = $_SESSION ? : [];
302 1
	}
303
304
	/**
305
	 * get the value from cookie
306
	 *
307
	 * @since 2.2.0
308
	 *
309
	 * @param string $key key of the item
310
	 *
311
	 * @return string|array|null
312
	 */
313
314 2
	public function getCookie(string $key = null)
315
	{
316 2
		if (is_array(self::$_requestArray['cookie']) && array_key_exists($key, self::$_requestArray['cookie']))
317
		{
318 1
			return self::$_requestArray['cookie'][$key];
319
		}
320 1
		return null;
321
	}
322
323
	/**
324
	 * set the value to cookie
325
	 *
326
	 * @since 2.2.0
327
	 *
328
	 * @param string $key key of the item
329
	 * @param string|array|null $value value of the item
330
	 */
331
332 1
	public function setCookie(string $key = null, $value = null) : void
333
	{
334 1
		self::$_requestArray['cookie'][$key] = $_COOKIE[$key] = $value;
335 1
	}
336
337
	/**
338
	 * refresh the cookie
339
	 *
340
	 * @since 2.6.2
341
	 */
342
343 1
	public function refreshCookie() : void
344
	{
345 1
		self::$_requestArray['cookie'] = $_COOKIE ? : [];
346 1
	}
347
348
	/**
349
	 * load the stream
350
	 *
351
	 * @since 4.2.0
352
	 *
353
	 * @return array
354
	 */
355
356 1
	protected function _loadStream() : array
357
	{
358 1
		return (array)json_decode(file_get_contents('php://input'));
359
	}
360
}
361