This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Weew\Http; |
||
4 | |||
5 | use Weew\Url\IUrl; |
||
6 | use Weew\Url\Url; |
||
7 | |||
8 | class HttpRequest implements IHttpRequest { |
||
9 | /** |
||
10 | * @var IHttpHeaders |
||
11 | */ |
||
12 | protected $headers; |
||
13 | |||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $method; |
||
18 | |||
19 | /** |
||
20 | * @var IUrl |
||
21 | */ |
||
22 | protected $url; |
||
23 | |||
24 | /** |
||
25 | * @var mixed |
||
26 | */ |
||
27 | protected $content; |
||
28 | |||
29 | /** |
||
30 | * @var IHttpData |
||
31 | */ |
||
32 | protected $data; |
||
33 | |||
34 | /** |
||
35 | * @var IContentTypeDataMatcher |
||
36 | */ |
||
37 | protected $contentTypeDataMatcher; |
||
38 | |||
39 | /** |
||
40 | * @var ICookieJar |
||
41 | */ |
||
42 | protected $cookieJar; |
||
43 | |||
44 | /** |
||
45 | * @var IHttpBasicAuth |
||
46 | */ |
||
47 | protected $basicAuth; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $protocol = HttpProtocol::HTTP; |
||
53 | |||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $version = HttpProtocol::CURRENT_VERSION; |
||
58 | |||
59 | /** |
||
60 | * @var ISuperGlobal |
||
61 | */ |
||
62 | protected $serverGlobal; |
||
63 | |||
64 | /** |
||
65 | * @param string $method |
||
66 | * @param null|IUrl $url |
||
67 | * @param IHttpHeaders $headers |
||
68 | */ |
||
69 | public function __construct( |
||
70 | $method = HttpRequestMethod::GET, |
||
71 | IUrl $url = null, |
||
72 | IHttpHeaders $headers = null |
||
73 | ) { |
||
74 | if ( ! $url instanceof IUrl) { |
||
75 | $url = $this->createUrl(); |
||
76 | } |
||
77 | |||
78 | if ( ! $headers instanceof IHttpHeaders) { |
||
79 | $headers = $this->createHeaders(); |
||
80 | } |
||
81 | |||
82 | $this->setUrl($url); |
||
83 | $this->setMethod($method); |
||
84 | $this->setHeaders($headers); |
||
85 | $this->setCookieJar($this->createCookieJar()); |
||
86 | $this->setBasicAuth($this->createBasicAuth()); |
||
87 | $this->setContentTypeDataMatcher($this->createContentTypeDataMatcher()); |
||
88 | $this->setServerGlobal($this->createServerGlobal()); |
||
89 | |||
90 | $this->setDefaults(); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @return IHttpHeaders |
||
95 | */ |
||
96 | public function getHeaders() { |
||
97 | return $this->headers; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param IHttpHeaders $headers |
||
102 | */ |
||
103 | public function setHeaders(IHttpHeaders $headers) { |
||
104 | $this->headers = $headers; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @return ICookieJar |
||
109 | */ |
||
110 | public function getCookieJar() { |
||
111 | return $this->cookieJar; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * @param ICookieJar $cookieJar |
||
116 | */ |
||
117 | public function setCookieJar(ICookieJar $cookieJar) { |
||
118 | $this->cookieJar = $cookieJar; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @param $method |
||
123 | * |
||
124 | * @see HttpRequestMethods |
||
125 | */ |
||
126 | public function setMethod($method) { |
||
127 | $this->method = $method; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @return string |
||
132 | * @see HttpRequestMethods |
||
133 | */ |
||
134 | public function getMethod() { |
||
135 | return $this->method; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @return IUrl |
||
140 | */ |
||
141 | public function getUrl() { |
||
142 | return $this->url; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @param IUrl $url |
||
147 | */ |
||
148 | public function setUrl(IUrl $url) { |
||
149 | $this->url = $url; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param $content |
||
154 | */ |
||
155 | View Code Duplication | public function setContent($content) { |
|
0 ignored issues
–
show
|
|||
156 | if (is_array($content) || is_object($content)) { |
||
157 | $this->getData()->setData($content); |
||
0 ignored issues
–
show
It seems like
$content can also be of type object ; however, Weew\Http\IHttpData::setData() does only seem to accept array , maybe add an additional type check?
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check: /**
* @return array|string
*/
function returnsDifferentValues($x) {
if ($x) {
return 'foo';
}
return array();
}
$x = returnsDifferentValues($y);
if (is_array($x)) {
// $x is an array.
}
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue. ![]() |
|||
158 | } else { |
||
159 | $this->content = (string) $content; |
||
160 | } |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @return mixed |
||
165 | */ |
||
166 | public function getContent() { |
||
167 | return $this->content; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @return bool |
||
172 | */ |
||
173 | public function hasContent() { |
||
174 | return $this->content !== null; |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * @return string |
||
179 | */ |
||
180 | public function getAccept() { |
||
181 | return $this->getHeaders()->find('accept'); |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * @param string $accept |
||
186 | */ |
||
187 | public function setAccept($accept) { |
||
188 | $this->getHeaders()->set('accept', $accept); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @return string |
||
193 | */ |
||
194 | public function getContentType() { |
||
195 | return $this->getHeaders()->find('content-type'); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @param string $contentType |
||
200 | */ |
||
201 | public function setContentType($contentType) { |
||
202 | $this->getHeaders()->set('content-type', $contentType); |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * @return IHttpData |
||
207 | */ |
||
208 | public function getData() { |
||
209 | if ( ! $this->data instanceof IHttpData) { |
||
210 | $this->setData($this->createData()); |
||
211 | } |
||
212 | |||
213 | return $this->data; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @param IHttpData $data |
||
218 | */ |
||
219 | public function setData(IHttpData $data) { |
||
220 | $this->data = $data; |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * @return IContentTypeDataMatcher |
||
225 | */ |
||
226 | public function getContentTypeDataMatcher() { |
||
227 | return $this->contentTypeDataMatcher; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * @param IContentTypeDataMatcher $contentTypeDataMatcher |
||
232 | */ |
||
233 | public function setContentTypeDataMatcher( |
||
234 | IContentTypeDataMatcher $contentTypeDataMatcher |
||
235 | ) { |
||
236 | $this->contentTypeDataMatcher = $contentTypeDataMatcher; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * @return IHttpBasicAuth |
||
241 | */ |
||
242 | public function getBasicAuth() { |
||
243 | return $this->basicAuth; |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * @param IHttpBasicAuth $basicAuth |
||
248 | */ |
||
249 | public function setBasicAuth(IHttpBasicAuth $basicAuth) { |
||
250 | $this->basicAuth = $basicAuth; |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * @return string |
||
255 | */ |
||
256 | public function getProtocol() { |
||
257 | return $this->protocol; |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * @param $protocol |
||
262 | * |
||
263 | * @see HttpProtocol |
||
264 | */ |
||
265 | public function setProtocol($protocol) { |
||
266 | $this->protocol = $protocol; |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * @return string |
||
271 | */ |
||
272 | public function getProtocolVersion() { |
||
273 | return $this->version; |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * @param $version |
||
278 | * |
||
279 | * @see HttpProtocol |
||
280 | */ |
||
281 | public function setProtocolVersion($version) { |
||
282 | $this->version = $version; |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * @return bool |
||
287 | */ |
||
288 | public function isSecure() { |
||
289 | return $this->getProtocol() == HttpProtocol::HTTPS; |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * @return ISuperGlobal |
||
294 | */ |
||
295 | public function getServerGlobal() { |
||
296 | return $this->serverGlobal; |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * @param ISuperGlobal $serverGlobal |
||
301 | */ |
||
302 | public function setServerGlobal(ISuperGlobal $serverGlobal) { |
||
303 | $this->serverGlobal = $serverGlobal; |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * Retrieve a value from url query or message body. |
||
308 | * |
||
309 | * @param string $key |
||
310 | * @param null $default |
||
311 | * |
||
312 | * @return mixed |
||
313 | */ |
||
314 | public function getParameter($key, $default = null) { |
||
315 | $value = $this->getUrl()->getQuery()->get($key); |
||
316 | |||
317 | if ($value === null) { |
||
318 | $value = $this->getData()->get($key, $default); |
||
319 | } |
||
320 | |||
321 | return $value; |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * @return array |
||
326 | */ |
||
327 | public function toArray() { |
||
328 | return [ |
||
329 | 'protocol' => $this->getProtocol(), |
||
330 | 'version' => $this->getProtocolVersion(), |
||
331 | 'method' => $this->getMethod(), |
||
332 | 'url' => $this->getUrl()->toString(), |
||
333 | 'headers' => $this->getHeaders()->toArray(), |
||
334 | 'data' => $this->getData()->toArray(), |
||
335 | 'query' => $this->getUrl()->getQuery()->toArray(), |
||
336 | 'cookies' => $this->getCookieJar()->toArray(), |
||
337 | 'content' => $this->getContent(), |
||
338 | ]; |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * Use this as hook to extend your custom request. |
||
343 | */ |
||
344 | protected function setDefaults() { |
||
345 | if ($this->getAccept() === null) { |
||
346 | $this->setDefaultAccept(); |
||
347 | } |
||
348 | |||
349 | if ($this->getContentType() === null) { |
||
350 | $this->setDefaultContentType(); |
||
351 | } |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * @return HttpHeaders |
||
356 | */ |
||
357 | protected function createHeaders() { |
||
358 | return new HttpHeaders(); |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * @return CookieJar |
||
363 | */ |
||
364 | protected function createCookieJar() { |
||
365 | return new CookieJar($this); |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * @return IUrl |
||
370 | */ |
||
371 | protected function createUrl() { |
||
372 | return new Url(); |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * Get default accept header. |
||
377 | * |
||
378 | * @return string |
||
379 | */ |
||
380 | protected function setDefaultAccept() {} |
||
381 | |||
382 | /** |
||
383 | * @return string |
||
384 | */ |
||
385 | protected function setDefaultContentType() {} |
||
386 | |||
387 | /** |
||
388 | * @return IHttpData |
||
389 | */ |
||
390 | protected function createData() { |
||
391 | $matcher = $this->getContentTypeDataMatcher(); |
||
392 | |||
393 | return $matcher->createDataForContentType($this, $this->getContentType()); |
||
394 | } |
||
395 | |||
396 | /** |
||
397 | * @return HttpBasicAuth |
||
398 | */ |
||
399 | protected function createBasicAuth() { |
||
400 | return new HttpBasicAuth($this); |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * @return IContentTypeDataMatcher |
||
405 | */ |
||
406 | protected function createContentTypeDataMatcher() { |
||
407 | return new ContentTypeDataMatcher(); |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * @return ServerGlobal |
||
412 | */ |
||
413 | protected function createServerGlobal() { |
||
414 | return new ServerGlobal(); |
||
415 | } |
||
416 | } |
||
417 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.