1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Lib\Http; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\Psr7\Request as BaseRequest; |
8
|
|
|
use Psr\Http\Message\RequestInterface; |
9
|
|
|
|
10
|
|
|
class Request extends BaseRequest |
11
|
|
|
{ |
12
|
|
|
const METHOD_HEAD = 'HEAD'; |
13
|
|
|
const METHOD_GET = 'GET'; |
14
|
|
|
const METHOD_POST = 'POST'; |
15
|
|
|
|
16
|
|
|
protected $query; |
17
|
|
|
protected $request; |
18
|
|
|
protected $cookie; |
19
|
|
|
protected $server; |
20
|
|
|
protected $files; |
21
|
|
|
|
22
|
4 |
|
public function __construct(array $query = [], array $request = [], array $cookie = [], array $server = [], array $files = []) |
23
|
|
|
{ |
24
|
4 |
|
$this->query = new ParamCollection($query); |
25
|
4 |
|
$this->request = new ParamCollection($request); |
26
|
4 |
|
$this->cookie = new ParamCollection($cookie); |
27
|
4 |
|
$this->server = new ServerCollection($server); |
28
|
4 |
|
$this->files = new ParamCollection($files); |
29
|
|
|
|
30
|
4 |
|
$method = $this->server->has('REQUEST_METHOD') ? $this->server->get('REQUEST_METHOD') : 'GET'; |
31
|
|
|
|
32
|
4 |
|
$requestUri = '/'; |
33
|
4 |
|
if ($this->server->has('REQUEST_URI')) { |
34
|
1 |
|
$requestUri = $this->server->get('REQUEST_URI'); |
35
|
3 |
|
} elseif ($this->server->has('ORIG_PATH_INFO')) { |
36
|
|
|
$requestUri = $this->server->get('ORIG_PATH_INFO'); |
37
|
|
|
$this->server->set('REQUEST_URI', $requestUri); |
38
|
|
|
} |
39
|
|
|
|
40
|
4 |
|
$version = $this->server->has('SERVER_PROTOCOL') ?? mb_substr($this->server->get('SERVER_PROTOCOL'), -3) ?? '1.1'; |
41
|
|
|
|
42
|
4 |
|
parent::__construct($method, $requestUri, $this->server->getHeaders(), http_build_query($this->request->all()), $version); |
43
|
4 |
|
} |
44
|
|
|
|
45
|
1 |
|
public static function createFromGlobals(): RequestInterface |
46
|
|
|
{ |
47
|
1 |
|
foreach ([$_GET, $_POST, $_COOKIE] as &$array) { |
|
|
|
|
48
|
|
|
array_walk($array, function ($value) { |
49
|
|
|
htmlspecialchars($value); |
50
|
1 |
|
}); |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
return new self( |
54
|
1 |
|
$_GET, |
55
|
1 |
|
$_POST, |
56
|
1 |
|
$_COOKIE, |
57
|
1 |
|
$_SERVER, |
58
|
1 |
|
$_FILES |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
Let?s assume that you have the following
foreach
statement:$itemValue
is assigned by reference. This is possible because the expression (in the example$array
) can be used as a reference target.However, if we were to replace
$array
with something different like the result of a function call as inthen assigning by reference is not possible anymore as there is no target that could be modified.
Available Fixes
1. Do not assign by reference
2. Assign to a local variable first
3. Return a reference