Request   A
last analyzed

Complexity

Total Complexity 40

Size/Duplication

Total Lines 295
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 40
lcom 1
cbo 1
dl 0
loc 295
ccs 60
cts 66
cp 0.9091
rs 9.2
c 0
b 0
f 0

18 Methods

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