Passed
Push — master ( d0466a...c02546 )
by Zlatin
01:22
created

ServerRequest::getAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace DevOp\Core\Http;
3
4
use DevOp\Core\Http\Request;
5
use Psr\Http\Message\UriInterface;
6
use Psr\Http\Message\StreamInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
9
class ServerRequest extends Request implements ServerRequestInterface
10
{
11
12
    use MessageTrait;
13
    use RequestTrait;
14
15
    /**
16
     * @var array
17
     */
18
    private $attributes = [];
19
20
    /**
21
     * @var array
22
     */
23
    private $cookieParams = [];
24
25
    /**
26
     * @var string
27
     */
28
    private $parsedBody;
29
30
    /**
31
     * @var array
32
     */
33
    private $queryParams = [];
34
35
    /**
36
     * @var array
37
     */
38
    private $serverParams = [];
39
40
    /**
41
     * @var array
42
     */
43
    private $uploadedFiles = [];
44
45
    /**
46
     * @return self
47
     */
48
    public static function createFromGlobals()
49
    {
50
51
        $uri = Uri::createFromGlobals();
52
53
        $method = 'GET';
54
        if (isset($_SERVER['REQUEST_METHOD'])) {
55
            $method = $_SERVER['REQUEST_METHOD'];
56
        }
57
58
        $headers = [];
59
        if (function_exists('getallheaders')) {
60
            $headers = getallheaders();
61
        }
62
63
        $body = new Stream('php://memory');
64
65
        $protocol = '1.1';
66
        if (isset($_SERVER['SERVER_PROTOCOL'])) {
67
            $protocol = str_replace('HTTP/', '', $_SERVER['SERVER_PROTOCOL']);
68
        }
69
70
        $serverRequest = new ServerRequest($method, $uri, $headers, $body, $protocol, $_SERVER);
0 ignored issues
show
Bug introduced by
It seems like $headers can also be of type false; however, parameter $headers of DevOp\Core\Http\ServerRequest::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
        $serverRequest = new ServerRequest($method, $uri, /** @scrutinizer ignore-type */ $headers, $body, $protocol, $_SERVER);
Loading history...
71
72
        return $serverRequest
73
                ->withParsedBody($_POST)
74
                ->withQueryParams($_GET)
75
                ->withCookieParams($_COOKIE)
76
                ->withUploadedFiles(UploadedFile::createFromGlobal());
0 ignored issues
show
Bug introduced by
DevOp\Core\Http\UploadedFile::createFromGlobal() of type DevOp\Core\Http\UploadedFile is incompatible with the type array expected by parameter $uploadedFiles of DevOp\Core\Http\ServerRequest::withUploadedFiles(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
                ->withUploadedFiles(/** @scrutinizer ignore-type */ UploadedFile::createFromGlobal());
Loading history...
77
    }
78
79
    /**
80
     * 
81
     * @param string $method
82
     * @param UriInterface|string $uri
83
     * @param array $headers
84
     * @param StreamInterface|null $body
85
     * @param string|null $version
86
     * @param array $serverParams
87
     */
88 2
    public function __construct($method, $uri, array $headers = [], $body = 'php://memory', $version = '1.1', array $serverParams = [])
89
    {
90 2
        parent::__construct($method, $uri, $headers, $body, $version);
91 2
        $this->serverParams = $serverParams;
92 2
    }
93
    
94
    /**
95
     * @return string
96
     */
97
    public function getAttribute($name, $default = null)
98
    {
99
        if (array_key_exists($name, $this->attributes)) {
100
            return $this->attributes[$name];
101
        }
102
        return $default;
103
    }
104
105
    /**
106
     * @return array
107
     */
108 2
    public function getAttributes()
109
    {
110 2
        return $this->attributes;
111
    }
112
113
    /**
114
     * @return array
115
     */
116
    public function getCookieParams()
117
    {
118
        return $this->cookieParams;
119
    }
120
121
    /**
122
     * @return string
123
     */
124
    public function getParsedBody()
125
    {
126
        return $this->parsedBody;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->parsedBody returns the type string which is incompatible with the return type mandated by Psr\Http\Message\ServerR...erface::getParsedBody() of object|null|array.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
127
    }
128
129
    /**
130
     * @return array
131
     */
132
    public function getQueryParams()
133
    {
134
        return $this->queryParams;
135
    }
136
137
    /**
138
     * @return array
139
     */
140
    public function getServerParams()
141
    {
142
        return $this->serverParams;
143
    }
144
145
    /**
146
     * @return array
147
     */
148
    public function getUploadedFiles()
149
    {
150
        return $this->uploadedFiles;
151
    }
152
153
    /**
154
     * @param string $name
155
     * @param string $value
156
     * @return \DevOp\Core\Http\ServerRequest
157
     */
158
    public function withAttribute($name, $value)
159
    {
160
        $clone = clone $this;
161
        $clone->attributes[$name] = $value;
162
163
        return $clone;
164
    }
165
166
    /**
167
     * @param array $cookies
168
     * @return \DevOp\Core\Http\ServerRequest
169
     */
170
    public function withCookieParams(array $cookies)
171
    {
172
        $clone = clone $this;
173
        $clone->cookieParams = $cookies;
174
175
        return $clone;
176
    }
177
178
    /**
179
     * @param string $data
180
     * @return \DevOp\Core\Http\ServerRequest
181
     */
182
    public function withParsedBody($data)
183
    {
184
        $clone = clone $this;
185
        $clone->parsedBody = $data;
186
187
        return $clone;
188
    }
189
190
    /**
191
     * @param array $query
192
     * @return \DevOp\Core\Http\ServerRequest
193
     */
194
    public function withQueryParams(array $query)
195
    {
196
        $clone = clone $this;
197
        $clone->queryParams = $query;
198
199
        return $clone;
200
    }
201
202
    /**
203
     * @param UploadedFile[] $uploadedFiles
204
     * @return \DevOp\Core\Http\ServerRequest
205
     */
206
    public function withUploadedFiles(array $uploadedFiles)
207
    {
208
        $clone = clone $this;
209
        $clone->uploadedFiles = $uploadedFiles;
210
211
        return $clone;
212
    }
213
214
    /**
215
     * @param string $name
216
     * @return \DevOp\Core\Http\ServerRequest|$this
217
     */
218
    public function withoutAttribute($name)
219
    {
220
        if (!array_key_exists($name, $this->attributes)) {
221
            return $this;
222
        }
223
224
        $clone = clone $this;
225
        unset($clone->attributes[$name]);
226
227
        return $clone;
228
    }
229
}
230