Skip to content

Commit 5bd06ba

Browse files
committed
Improve Request DTO types
1 parent f0fef33 commit 5bd06ba

File tree

1 file changed

+82
-14
lines changed

1 file changed

+82
-14
lines changed

src/Request.php

Lines changed: 82 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,110 @@
11
<?php
22

33
/**
4-
* High-performance PHP process supervisor and load balancer written in Go. Http core.
4+
* This file is part of RoadRunner package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
58
*/
69

710
declare(strict_types=1);
811

912
namespace Spiral\RoadRunner\Http;
1013

14+
use JetBrains\PhpStorm\Immutable;
15+
16+
/**
17+
* @psalm-immutable
18+
*
19+
* @psalm-type UploadedFile = array {
20+
* error: positive-int|0,
21+
* name: string,
22+
* tmpName: string,
23+
* size: positive-int|0,
24+
* mime: string
25+
* }
26+
*
27+
* @psalm-type HeadersList = array<string, array<array-key, string>>
28+
* @psalm-type AttributesList = array<string, mixed>
29+
* @psalm-type QueryArgumentsList = array<string, string>
30+
* @psalm-type CookiesList = array<string, string>
31+
* @psalm-type UploadedFilesList = array<array-key, UploadedFile>
32+
*/
33+
#[Immutable]
1134
final class Request
1235
{
13-
public string $remoteAddr;
14-
public string $protocol;
15-
public string $method;
16-
public string $uri;
17-
public array $headers;
18-
public array $cookies;
19-
public array $uploads;
20-
public array $attributes;
21-
public array $query;
22-
public ?string $body;
23-
public bool $parsed;
36+
/**
37+
* @var string
38+
*/
39+
40+
public string $remoteAddr = '127.0.0.1';
41+
42+
/**
43+
* @var string
44+
*/
45+
public string $protocol = 'HTTP/1.0';
46+
47+
/**
48+
* @var string
49+
*/
50+
public string $method = 'GET';
51+
52+
/**
53+
* @var string
54+
*/
55+
public string $uri = 'http://localhost';
56+
57+
/**
58+
* @var HeadersList
59+
*/
60+
public array $headers = [];
61+
62+
/**
63+
* @var CookiesList
64+
*/
65+
public array $cookies = [];
66+
67+
/**
68+
* @var UploadedFilesList
69+
*/
70+
public array $uploads = [];
71+
72+
/**
73+
* @var AttributesList
74+
*/
75+
public array $attributes = [];
76+
77+
/**
78+
* @var QueryArgumentsList
79+
*/
80+
public array $query = [];
81+
82+
/**
83+
* @var string
84+
*/
85+
public string $body = '';
86+
87+
/**
88+
* @var bool
89+
*/
90+
public bool $parsed = false;
2491

2592
/**
2693
* @return string
2794
*/
2895
public function getRemoteAddr(): string
2996
{
30-
return $this->attributes['ipAddress'] ?? $this->remoteAddr ?? '127.0.0.1';
97+
return (string)($this->attributes['ipAddress'] ?? $this->remoteAddr);
3198
}
3299

33100
/**
34101
* @return array|null
102+
* @throws \JsonException
35103
*/
36104
public function getParsedBody(): ?array
37105
{
38106
if ($this->parsed) {
39-
return json_decode($this->body, true);
107+
return (array)\json_decode($this->body, true, 512, \JSON_THROW_ON_ERROR);
40108
}
41109

42110
return null;

0 commit comments

Comments
 (0)