Completed
Push — master ( 96a033...7625bb )
by Henry
07:07
created

Request   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 342
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 45
lcom 1
cbo 1
dl 0
loc 342
ccs 60
cts 66
cp 0.9091
rs 8.8
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 13 7
A get() 0 8 3
A getArray() 0 4 1
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 getStream() 0 8 3
A setStream() 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
A _loadStream() 0 4 1

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 1
	 */
34
35
	public function init() : void
36
	{
37 1
		self::$_requestArray =
38
		[
39
			'server' => $_SERVER ? : [],
40
			'get' => $_GET ? : [],
41
			'post' => $_POST ? : [],
42
			'files' => $_FILES ? : [],
43
			'stream' => self::_loadStream(),
44 1
			'session' => $_SESSION ? : [],
45
			'cookie' => $_COOKIE ? : []
46
		];
47
	}
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 2
	 * @return string|array|null
57
	 */
58 2
59
	public function get(string $key = null)
60 1
	{
61
		if (is_array(self::$_requestArray) && array_key_exists($key, self::$_requestArray))
62 1
		{
63
			return self::$_requestArray[$key];
64
		}
65
		return null;
66
	}
67
68
	/**
69
	 * get the array from globals
70
	 *
71
	 * @since 4.0.0
72
	 *
73 1
	 * @return array
74
	 */
75 1
76
	public function getArray() : array
77
	{
78
		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 1
	 * @param string|array|null $value value of the item
88
	 */
89 1
90 1
	public function set(string $key = null, $value = null) : void
91
	{
92
		self::$_requestArray[$key] = $GLOBALS[$key] = $value;
93
	}
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 2
	 * @return string|array|null
103
	 */
104 2
105
	public function getServer(string $key = null)
106 1
	{
107
		if (is_array(self::$_requestArray['server']) && array_key_exists($key, self::$_requestArray['server']))
108 1
		{
109
			return self::$_requestArray['server'][$key];
110
		}
111
		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 1
	 * @param string|array|null $value value of the item
121
	 */
122 1
123 1
	public function setServer(string $key = null, $value = null) : void
124
	{
125
		self::$_requestArray['server'][$key] = $value;
126
	}
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 2
	 * @return string|array|null
136
	 */
137 2
138
	public function getQuery(string $key = null)
139 1
	{
140
		if (is_array(self::$_requestArray['get']) && array_key_exists($key, self::$_requestArray['get']))
141 1
		{
142
			return self::$_requestArray['get'][$key];
143
		}
144
		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 1
	 * @param string|array|null $value value of the item
154
	 */
155 1
156 1
	public function setQuery(string $key = null, $value = null) : void
157
	{
158
		self::$_requestArray['get'][$key] = $value;
159
	}
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 2
	 * @return string|array|null
169
	 */
170 2
171
	public function getPost(string $key = null)
172 1
	{
173
		if (is_array(self::$_requestArray['post']) && array_key_exists($key, self::$_requestArray['post']))
174 1
		{
175
			return self::$_requestArray['post'][$key];
176
		}
177
		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 1
	 * @param string|array|null $value value of the item
187
	 */
188 1
189 1
	public function setPost(string $key = null, $value = null) : void
190
	{
191
		self::$_requestArray['post'][$key] = $value;
192
	}
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 2
	 * @return string|array|null
202
	 */
203 2
204
	public function getFiles(string $key = null)
205 1
	{
206
		if (is_array(self::$_requestArray['files']) && array_key_exists($key, self::$_requestArray['files']))
207 1
		{
208
			return self::$_requestArray['files'][$key];
209
		}
210
		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 1
	 * @param string|array|null $value value of the item
220
	 */
221 1
222 1
	public function setFiles(string $key = null, $value = null) : void
223
	{
224
		self::$_requestArray['files'][$key] = $value;
225
	}
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 2
	 * @return string|array|null
235
	 */
236 2
237
	public function getStream(string $key = null)
238 1
	{
239
		if (is_array(self::$_requestArray['stream']) && array_key_exists($key, self::$_requestArray['stream']))
240 1
		{
241
			return self::$_requestArray['stream'][$key];
242
		}
243
		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 1
	 * @param string|array|null $value value of the item
253
	 */
254 1
255 1
	public function setStream(string $key = null, $value = null) : void
256
	{
257
		self::$_requestArray['stream'][$key] = $value;
258
	}
259
260
	/**
261
	 * get the value from session
262
	 *
263 1
	 * @since 2.2.0
264
	 *
265 1
	 * @param string $key key of the item
266 1
	 *
267
	 * @return string|array|null
268
	 */
269
270
	public function getSession(string $key = null)
271
	{
272
		if (is_array(self::$_requestArray['session']) && array_key_exists($key, self::$_requestArray['session']))
273
		{
274
			return self::$_requestArray['session'][$key];
275
		}
276
		return null;
277
	}
278 2
279
	/**
280 2
	 * set the value to session
281
	 *
282 1
	 * @since 2.2.0
283
	 *
284 1
	 * @param string $key key of the item
285
	 * @param string|array|null $value value of the item
286
	 */
287
288
	public function setSession(string $key = null, $value = null) : void
289
	{
290
		self::$_requestArray['session'][$key] = $_SESSION[$key] = $value;
291
	}
292
293
	/**
294
	 * refresh the session
295
	 *
296 1
	 * @since 2.6.2
297
	 */
298 1
299 1
	public function refreshSession() : void
300
	{
301
		self::$_requestArray['session'] = $_SESSION ? : [];
302
	}
303
304
	/**
305
	 * get the value from cookie
306
	 *
307 1
	 * @since 2.2.0
308
	 *
309 1
	 * @param string $key key of the item
310 1
	 *
311
	 * @return string|array|null
312
	 */
313
314
	public function getCookie(string $key = null)
315
	{
316
		if (is_array(self::$_requestArray['cookie']) && array_key_exists($key, self::$_requestArray['cookie']))
317
		{
318
			return self::$_requestArray['cookie'][$key];
319
		}
320
		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
	public function setCookie(string $key = null, $value = null) : void
333
	{
334
		self::$_requestArray['cookie'][$key] = $_COOKIE[$key] = $value;
335
	}
336
337
	/**
338
	 * refresh the cookie
339
	 *
340
	 * @since 2.6.2
341
	 */
342
343
	public function refreshCookie() : void
344
	{
345
		self::$_requestArray['cookie'] = $_COOKIE ? : [];
346
	}
347
348
	/**
349
	 * load the stream
350
	 *
351
	 * @since 4.2.0
352
	 *
353
	 * @return array
354
	 */
355
356
	protected function _loadStream() : array
357
	{
358
		return (array)json_decode(file_get_contents('php://input'));
359
	}
360
}
361