@@ -35,10 +35,9 @@ trait RequestTrait
35
35
protected $ ipAddress = '' ;
36
36
37
37
/**
38
- * Stores values we've retrieved from
39
- * PHP globals.
38
+ * Stores values we've retrieved from PHP globals.
40
39
*
41
- * @var array
40
+ * @var array{get?: array, post?: array, request?: array, cookie?: array, server?: array}
42
41
*/
43
42
protected $ globals = [];
44
43
@@ -204,22 +203,27 @@ public function getServer($index = null, $filter = null, $flags = null)
204
203
* @param array|int|null $flags
205
204
*
206
205
* @return mixed
206
+ *
207
+ * @deprecated 4.4.4 This method does not work from the beginning. Use `env()`.
207
208
*/
208
209
public function getEnv ($ index = null , $ filter = null , $ flags = null )
209
210
{
211
+ // @phpstan-ignore-next-line
210
212
return $ this ->fetchGlobal ('env ' , $ index , $ filter , $ flags );
211
213
}
212
214
213
215
/**
214
216
* Allows manually setting the value of PHP global, like $_GET, $_POST, etc.
215
217
*
218
+ * @param string $name Supergrlobal name (lowercase)
219
+ * @phpstan-param 'get'|'post'|'request'|'cookie'|'server' $name
216
220
* @param mixed $value
217
221
*
218
222
* @return $this
219
223
*/
220
- public function setGlobal (string $ method , $ value )
224
+ public function setGlobal (string $ name , $ value )
221
225
{
222
- $ this ->globals [$ method ] = $ value ;
226
+ $ this ->globals [$ name ] = $ value ;
223
227
224
228
return $ this ;
225
229
}
@@ -234,19 +238,18 @@ public function setGlobal(string $method, $value)
234
238
*
235
239
* http://php.net/manual/en/filter.filters.sanitize.php
236
240
*
237
- * @param string $method Input filter constant
241
+ * @param string $name Supergrlobal name (lowercase)
242
+ * @phpstan-param 'get'|'post'|'request'|'cookie'|'server' $name
238
243
* @param array|string|null $index
239
244
* @param int|null $filter Filter constant
240
245
* @param array|int|null $flags Options
241
246
*
242
247
* @return array|bool|float|int|object|string|null
243
248
*/
244
- public function fetchGlobal (string $ method , $ index = null , ?int $ filter = null , $ flags = null )
249
+ public function fetchGlobal (string $ name , $ index = null , ?int $ filter = null , $ flags = null )
245
250
{
246
- $ method = strtolower ($ method );
247
-
248
- if (! isset ($ this ->globals [$ method ])) {
249
- $ this ->populateGlobals ($ method );
251
+ if (! isset ($ this ->globals [$ name ])) {
252
+ $ this ->populateGlobals ($ name );
250
253
}
251
254
252
255
// Null filters cause null values to return.
@@ -257,9 +260,9 @@ public function fetchGlobal(string $method, $index = null, ?int $filter = null,
257
260
if ($ index === null ) {
258
261
$ values = [];
259
262
260
- foreach ($ this ->globals [$ method ] as $ key => $ value ) {
263
+ foreach ($ this ->globals [$ name ] as $ key => $ value ) {
261
264
$ values [$ key ] = is_array ($ value )
262
- ? $ this ->fetchGlobal ($ method , $ key , $ filter , $ flags )
265
+ ? $ this ->fetchGlobal ($ name , $ key , $ filter , $ flags )
263
266
: filter_var ($ value , $ filter , $ flags );
264
267
}
265
268
@@ -271,15 +274,15 @@ public function fetchGlobal(string $method, $index = null, ?int $filter = null,
271
274
$ output = [];
272
275
273
276
foreach ($ index as $ key ) {
274
- $ output [$ key ] = $ this ->fetchGlobal ($ method , $ key , $ filter , $ flags );
277
+ $ output [$ key ] = $ this ->fetchGlobal ($ name , $ key , $ filter , $ flags );
275
278
}
276
279
277
280
return $ output ;
278
281
}
279
282
280
283
// Does the index contain array notation?
281
284
if (($ count = preg_match_all ('/(?:^[^\[]+)|\[[^]]*\]/ ' , $ index , $ matches )) > 1 ) {
282
- $ value = $ this ->globals [$ method ];
285
+ $ value = $ this ->globals [$ name ];
283
286
284
287
for ($ i = 0 ; $ i < $ count ; $ i ++) {
285
288
$ key = trim ($ matches [0 ][$ i ], '[] ' );
@@ -297,7 +300,7 @@ public function fetchGlobal(string $method, $index = null, ?int $filter = null,
297
300
}
298
301
299
302
if (! isset ($ value )) {
300
- $ value = $ this ->globals [$ method ][$ index ] ?? null ;
303
+ $ value = $ this ->globals [$ name ][$ index ] ?? null ;
301
304
}
302
305
303
306
if (is_array ($ value )
@@ -326,20 +329,23 @@ public function fetchGlobal(string $method, $index = null, ?int $filter = null,
326
329
}
327
330
328
331
/**
329
- * Saves a copy of the current state of one of several PHP globals
332
+ * Saves a copy of the current state of one of several PHP globals,
330
333
* so we can retrieve them later.
331
334
*
335
+ * @param string $name Superglobal name (lowercase)
336
+ * @phpstan-param 'get'|'post'|'request'|'cookie'|'server' $name
337
+ *
332
338
* @return void
333
339
*/
334
- protected function populateGlobals (string $ method )
340
+ protected function populateGlobals (string $ name )
335
341
{
336
- if (! isset ($ this ->globals [$ method ])) {
337
- $ this ->globals [$ method ] = [];
342
+ if (! isset ($ this ->globals [$ name ])) {
343
+ $ this ->globals [$ name ] = [];
338
344
}
339
345
340
346
// Don't populate ENV as it might contain
341
347
// sensitive data that we don't want to get logged.
342
- switch ($ method ) {
348
+ switch ($ name ) {
343
349
case 'get ' :
344
350
$ this ->globals ['get ' ] = $ _GET ;
345
351
break ;
0 commit comments