Skip to content

Commit 99ac1e5

Browse files
authored
Support the version 2.x of psr/http-message. (#5602)
1 parent 56c14a1 commit 99ac1e5

File tree

11 files changed

+93
-141
lines changed

11 files changed

+93
-141
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"hyperf/engine": "^1.9|^2.7",
1515
"hyperf/utils": "~3.0.0",
1616
"laminas/laminas-mime": "^2.7",
17-
"psr/http-message": "^1.0"
17+
"psr/http-message": "^1.0|^2.0"
1818
},
1919
"suggest": {
2020
"psr/container": "Required to replace RequestParserInterface."

src/Base/MessageTrait.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ trait MessageTrait
4040
*
4141
* @return string HTTP protocol version
4242
*/
43-
public function getProtocolVersion()
43+
public function getProtocolVersion(): string
4444
{
4545
return $this->protocol;
4646
}
@@ -54,9 +54,8 @@ public function getProtocolVersion()
5454
* new protocol version.
5555
*
5656
* @param string $version HTTP protocol version
57-
* @return static
5857
*/
59-
public function withProtocolVersion($version)
58+
public function withProtocolVersion($version): static
6059
{
6160
if ($this->protocol === $version) {
6261
return $this;
@@ -162,10 +161,9 @@ public function getHeaderLine($name): string
162161
*
163162
* @param string $name case-insensitive header field name
164163
* @param string|string[] $value header value(s)
165-
* @return static
166164
* @throws InvalidArgumentException for invalid header names or values
167165
*/
168-
public function withHeader($name, $value)
166+
public function withHeader($name, $value): static
169167
{
170168
if (! is_array($value)) {
171169
$value = [$value];
@@ -204,10 +202,9 @@ public function withHeaders(array $headers): static
204202
*
205203
* @param string $name case-insensitive header field name to add
206204
* @param string|string[] $value header value(s)
207-
* @return static
208205
* @throws InvalidArgumentException for invalid header names or values
209206
*/
210-
public function withAddedHeader($name, $value)
207+
public function withAddedHeader($name, $value): static
211208
{
212209
if (! is_array($value)) {
213210
$value = [$value];
@@ -236,9 +233,8 @@ public function withAddedHeader($name, $value)
236233
* the named header.
237234
*
238235
* @param string $name case-insensitive header field name to remove
239-
* @return static
240236
*/
241-
public function withoutHeader($name)
237+
public function withoutHeader($name): static
242238
{
243239
$normalized = strtolower($name);
244240

@@ -259,7 +255,7 @@ public function withoutHeader($name)
259255
*
260256
* @return StreamInterface returns the body as a stream
261257
*/
262-
public function getBody()
258+
public function getBody(): StreamInterface
263259
{
264260
if (! $this->stream) {
265261
$this->stream = new SwooleStream('');
@@ -276,10 +272,9 @@ public function getBody()
276272
* new body stream.
277273
*
278274
* @param StreamInterface $body body
279-
* @return static
280275
* @throws InvalidArgumentException when the body is not valid
281276
*/
282-
public function withBody(StreamInterface $body)
277+
public function withBody(StreamInterface $body): static
283278
{
284279
if ($body === $this->stream) {
285280
return $this;

src/Base/Request.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,8 @@ public function __construct(
7272
* withRequestTarget() below).
7373
* If no URI is available, and no request-target has been specifically
7474
* provided, this method MUST return the string "/".
75-
*
76-
* @return string
7775
*/
78-
public function getRequestTarget()
76+
public function getRequestTarget(): string
7977
{
8078
if ($this->requestTarget !== null) {
8179
return $this->requestTarget;
@@ -105,9 +103,8 @@ public function getRequestTarget()
105103
* @see http://tools.ietf.org/html/rfc7230#section-5.3 (for the various
106104
* request-target forms allowed in request messages)
107105
* @param mixed $requestTarget
108-
* @return static
109106
*/
110-
public function withRequestTarget($requestTarget)
107+
public function withRequestTarget($requestTarget): static
111108
{
112109
if (preg_match('#\s#', $requestTarget)) {
113110
throw new InvalidArgumentException('Invalid request target provided; cannot contain whitespace');
@@ -123,7 +120,7 @@ public function withRequestTarget($requestTarget)
123120
*
124121
* @return string returns the request method
125122
*/
126-
public function getMethod()
123+
public function getMethod(): string
127124
{
128125
return $this->method;
129126
}
@@ -138,10 +135,9 @@ public function getMethod()
138135
* changed request method.
139136
*
140137
* @param string $method case-sensitive method
141-
* @return static
142138
* @throws InvalidArgumentException for invalid HTTP methods
143139
*/
144-
public function withMethod($method)
140+
public function withMethod($method): static
145141
{
146142
$method = strtoupper($method);
147143
$methods = ['GET', 'POST', 'PATCH', 'PUT', 'DELETE', 'HEAD'];
@@ -190,9 +186,8 @@ public function getUri(): UriInterface
190186
* @see http://tools.ietf.org/html/rfc3986#section-4.3
191187
* @param UriInterface $uri new request URI to use
192188
* @param bool $preserveHost preserve the original state of the Host header
193-
* @return static
194189
*/
195-
public function withUri(UriInterface $uri, $preserveHost = false): self
190+
public function withUri(UriInterface $uri, $preserveHost = false): static
196191
{
197192
if ($uri === $this->uri) {
198193
return $this;

src/Base/Response.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class Response implements ResponseInterface
9292

9393
private array $attributes = [];
9494

95-
public function __toString()
95+
public function __toString(): string
9696
{
9797
return (string) $this->getBody();
9898
}
@@ -140,10 +140,9 @@ public function getAttribute($name, $default = null)
140140
*
141141
* @param string $name the attribute name
142142
* @param mixed $value the value of the attribute
143-
* @return static
144143
* @see getAttributes()
145144
*/
146-
public function withAttribute($name, $value)
145+
public function withAttribute($name, $value): static
147146
{
148147
$clone = clone $this;
149148
$clone->attributes[$name] = $value;
@@ -177,10 +176,9 @@ public function getStatusCode(): int
177176
* @param string $reasonPhrase the reason phrase to use with the
178177
* provided status code; if none is provided, implementations MAY
179178
* use the defaults as suggested in the HTTP specification
180-
* @return static
181179
* @throws InvalidArgumentException for invalid status code arguments
182180
*/
183-
public function withStatus($code, $reasonPhrase = '')
181+
public function withStatus($code, $reasonPhrase = ''): static
184182
{
185183
$clone = clone $this;
186184
$clone->statusCode = (int) $code;
@@ -202,10 +200,9 @@ public static function getReasonPhraseByCode(int $code): string
202200
/**
203201
* Return an instance with the specified charset content type.
204202
*
205-
* @return static
206203
* @throws InvalidArgumentException
207204
*/
208-
public function withCharset(string $charset)
205+
public function withCharset(string $charset): static
209206
{
210207
return $this->withAddedHeader('Content-Type', sprintf('charset=%s', $charset));
211208
}
@@ -232,7 +229,7 @@ public function getCharset(): string
232229
return $this->charset;
233230
}
234231

235-
public function setCharset(string $charset): Response
232+
public function setCharset(string $charset): static
236233
{
237234
$this->charset = $charset;
238235
return $this;

src/Server/Request.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,8 @@ public static function loadFromSwooleRequest(Swoole\Http\Request $swooleRequest)
7878
* Retrieves data related to the incoming request environment,
7979
* typically derived from PHP's $_SERVER superglobal. The data IS NOT
8080
* REQUIRED to originate from $_SERVER.
81-
*
82-
* @return array
8381
*/
84-
public function getServerParams()
82+
public function getServerParams(): array
8583
{
8684
return $this->serverParams;
8785
}
@@ -171,9 +169,8 @@ public function addQueryParam(string $name, mixed $value): static
171169
*
172170
* @param array $query array of query string arguments, typically from
173171
* $_GET
174-
* @return static
175172
*/
176-
public function withQueryParams(array $query)
173+
public function withQueryParams(array $query): static
177174
{
178175
$clone = clone $this;
179176
$clone->queryParams = $query;
@@ -190,7 +187,7 @@ public function withQueryParams(array $query)
190187
* @return array an array tree of UploadedFileInterface instances; an empty
191188
* array MUST be returned if no data is present
192189
*/
193-
public function getUploadedFiles()
190+
public function getUploadedFiles(): array
194191
{
195192
return $this->uploadedFiles;
196193
}
@@ -202,10 +199,9 @@ public function getUploadedFiles()
202199
* updated body parameters.
203200
*
204201
* @param array $uploadedFiles an array tree of UploadedFileInterface instances
205-
* @return static
206202
* @throws InvalidArgumentException if an invalid structure is provided
207203
*/
208-
public function withUploadedFiles(array $uploadedFiles)
204+
public function withUploadedFiles(array $uploadedFiles): static
209205
{
210206
$clone = clone $this;
211207
$clone->uploadedFiles = $uploadedFiles;
@@ -274,11 +270,10 @@ public function getBodyParams(): mixed
274270
*
275271
* @param null|array|object $data The deserialized body data. This will
276272
* typically be in an array or object.
277-
* @return static
278273
* @throws InvalidArgumentException if an unsupported argument type is
279274
* provided
280275
*/
281-
public function withParsedBody($data)
276+
public function withParsedBody($data): static
282277
{
283278
$clone = clone $this;
284279
$clone->parsedBody = $data;
@@ -305,7 +300,7 @@ public function withBodyParams(mixed $data): static
305300
*
306301
* @return array attributes derived from the request
307302
*/
308-
public function getAttributes()
303+
public function getAttributes(): array
309304
{
310305
return $this->attributes;
311306
}
@@ -338,10 +333,9 @@ public function getAttribute($name, $default = null)
338333
*
339334
* @param string $name the attribute name
340335
* @param mixed $value the value of the attribute
341-
* @return static
342336
* @see getAttributes()
343337
*/
344-
public function withAttribute($name, $value)
338+
public function withAttribute($name, $value): static
345339
{
346340
$clone = clone $this;
347341
$clone->attributes[$name] = $value;
@@ -357,10 +351,9 @@ public function withAttribute($name, $value)
357351
* the attribute.
358352
*
359353
* @param string $name the attribute name
360-
* @return static
361354
* @see getAttributes()
362355
*/
363-
public function withoutAttribute($name)
356+
public function withoutAttribute($name): static
364357
{
365358
if (array_key_exists($name, $this->attributes) === false) {
366359
return $this;

src/Server/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function withTrailer(string $key, $value): static
6969
/**
7070
* Retrieves a specified trailer value, returns null if the value does not exists.
7171
*/
72-
public function getTrailer(string $key)
72+
public function getTrailer(string $key): mixed
7373
{
7474
return $this->trailers[$key] ?? null;
7575
}

src/Server/ResponseProxyTrait.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,78 +33,78 @@ public function getResponse(): ResponseInterface
3333
return $this->response;
3434
}
3535

36-
public function getProtocolVersion()
36+
public function getProtocolVersion(): string
3737
{
3838
return $this->getResponse()->getProtocolVersion();
3939
}
4040

41-
public function withProtocolVersion($version)
41+
public function withProtocolVersion($version): static
4242
{
4343
$this->setResponse($this->getResponse()->withProtocolVersion($version));
4444
return $this;
4545
}
4646

47-
public function getHeaders()
47+
public function getHeaders(): array
4848
{
4949
return $this->getResponse()->getHeaders();
5050
}
5151

52-
public function hasHeader($name)
52+
public function hasHeader($name): bool
5353
{
5454
return $this->getResponse()->hasHeader($name);
5555
}
5656

57-
public function getHeader($name)
57+
public function getHeader($name): array
5858
{
5959
return $this->getResponse()->getHeader($name);
6060
}
6161

62-
public function getHeaderLine($name)
62+
public function getHeaderLine($name): string
6363
{
6464
return $this->getResponse()->getHeaderLine($name);
6565
}
6666

67-
public function withHeader($name, $value)
67+
public function withHeader($name, $value): static
6868
{
6969
$this->setResponse($this->getResponse()->withHeader($name, $value));
7070
return $this;
7171
}
7272

73-
public function withAddedHeader($name, $value)
73+
public function withAddedHeader($name, $value): static
7474
{
7575
$this->setResponse($this->getResponse()->withAddedHeader($name, $value));
7676
return $this;
7777
}
7878

79-
public function withoutHeader($name)
79+
public function withoutHeader($name): static
8080
{
8181
$this->setResponse($this->getResponse()->withoutHeader($name));
8282
return $this;
8383
}
8484

85-
public function getBody()
85+
public function getBody(): StreamInterface
8686
{
8787
return $this->getResponse()->getBody();
8888
}
8989

90-
public function withBody(StreamInterface $body)
90+
public function withBody(StreamInterface $body): static
9191
{
9292
$this->setResponse($this->getResponse()->withBody($body));
9393
return $this;
9494
}
9595

96-
public function getStatusCode()
96+
public function getStatusCode(): int
9797
{
9898
return $this->getResponse()->getStatusCode();
9999
}
100100

101-
public function withStatus($code, $reasonPhrase = '')
101+
public function withStatus($code, $reasonPhrase = ''): static
102102
{
103103
$this->setResponse($this->getResponse()->withStatus($code, $reasonPhrase));
104104
return $this;
105105
}
106106

107-
public function getReasonPhrase()
107+
public function getReasonPhrase(): string
108108
{
109109
return $this->getResponse()->getReasonPhrase();
110110
}

0 commit comments

Comments
 (0)