Skip to content

Commit 6a31c27

Browse files
Allow psr/http-message v2 (#234)
1 parent 07c455a commit 6a31c27

11 files changed

+142
-62
lines changed

.github/workflows/static.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ jobs:
6161
php-version: 8.1
6262
extensions: apcu, redis
6363
coverage: none
64-
tools: vimeo/psalm:4.29.0
64+
tools: vimeo/psalm:5.9
6565

6666
- name: Download dependencies
6767
uses: ramsey/composer-install@v2

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
],
1717
"require": {
1818
"php": ">=7.2",
19-
"psr/http-message": "^1.0",
19+
"psr/http-message": "^1.1 || ^2.0",
2020
"php-http/message-factory": "^1.0",
2121
"psr/http-factory": "^1.0"
2222
},
2323
"require-dev": {
2424
"phpunit/phpunit": "^7.5 || 8.5 || 9.4",
25-
"php-http/psr7-integration-tests": "^1.0",
25+
"php-http/psr7-integration-tests": "^1.0@dev",
2626
"http-interop/http-factory-tests": "^0.9",
2727
"symfony/error-handler": "^4.4"
2828
},

phpstan-baseline.neon

-5
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ parameters:
2020
count: 1
2121
path: src/ServerRequest.php
2222

23-
-
24-
message: "#^Parameter \\#1 \\$callback of function set_error_handler expects \\(callable\\(int, string, string, int\\)\\: bool\\)\\|null, 'var_dump' given\\.$#"
25-
count: 1
26-
path: src/Stream.php
27-
2823
-
2924
message: "#^Result of && is always false\\.$#"
3025
count: 1

src/MessageTrait.php

+21-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Nyholm\Psr7;
66

7+
use Psr\Http\Message\MessageInterface;
78
use Psr\Http\Message\StreamInterface;
89

910
/**
@@ -34,7 +35,10 @@ public function getProtocolVersion(): string
3435
return $this->protocol;
3536
}
3637

37-
public function withProtocolVersion($version): self
38+
/**
39+
* @return static
40+
*/
41+
public function withProtocolVersion($version): MessageInterface
3842
{
3943
if (!\is_scalar($version)) {
4044
throw new \InvalidArgumentException('Protocol version must be a string');
@@ -81,7 +85,10 @@ public function getHeaderLine($header): string
8185
return \implode(', ', $this->getHeader($header));
8286
}
8387

84-
public function withHeader($header, $value): self
88+
/**
89+
* @return static
90+
*/
91+
public function withHeader($header, $value): MessageInterface
8592
{
8693
$value = $this->validateAndTrimHeader($header, $value);
8794
$normalized = \strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
@@ -96,7 +103,10 @@ public function withHeader($header, $value): self
96103
return $new;
97104
}
98105

99-
public function withAddedHeader($header, $value): self
106+
/**
107+
* @return static
108+
*/
109+
public function withAddedHeader($header, $value): MessageInterface
100110
{
101111
if (!\is_string($header) || '' === $header) {
102112
throw new \InvalidArgumentException('Header name must be an RFC 7230 compatible string');
@@ -108,7 +118,10 @@ public function withAddedHeader($header, $value): self
108118
return $new;
109119
}
110120

111-
public function withoutHeader($header): self
121+
/**
122+
* @return static
123+
*/
124+
public function withoutHeader($header): MessageInterface
112125
{
113126
if (!\is_string($header)) {
114127
throw new \InvalidArgumentException('Header name must be an RFC 7230 compatible string');
@@ -135,7 +148,10 @@ public function getBody(): StreamInterface
135148
return $this->stream;
136149
}
137150

138-
public function withBody(StreamInterface $body): self
151+
/**
152+
* @return static
153+
*/
154+
public function withBody(StreamInterface $body): MessageInterface
139155
{
140156
if ($body === $this->stream) {
141157
return $this;

src/RequestTrait.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Nyholm\Psr7;
66

7+
use Psr\Http\Message\RequestInterface;
78
use Psr\Http\Message\UriInterface;
89

910
/**
@@ -40,7 +41,10 @@ public function getRequestTarget(): string
4041
return $target;
4142
}
4243

43-
public function withRequestTarget($requestTarget): self
44+
/**
45+
* @return static
46+
*/
47+
public function withRequestTarget($requestTarget): RequestInterface
4448
{
4549
if (!\is_string($requestTarget)) {
4650
throw new \InvalidArgumentException('Request target must be a string');
@@ -61,7 +65,10 @@ public function getMethod(): string
6165
return $this->method;
6266
}
6367

64-
public function withMethod($method): self
68+
/**
69+
* @return static
70+
*/
71+
public function withMethod($method): RequestInterface
6572
{
6673
if (!\is_string($method)) {
6774
throw new \InvalidArgumentException('Method must be a string');
@@ -78,7 +85,10 @@ public function getUri(): UriInterface
7885
return $this->uri;
7986
}
8087

81-
public function withUri(UriInterface $uri, $preserveHost = false): self
88+
/**
89+
* @return static
90+
*/
91+
public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface
8292
{
8393
if ($uri === $this->uri) {
8494
return $this;

src/Response.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ public function getReasonPhrase(): string
6767
return $this->reasonPhrase;
6868
}
6969

70-
public function withStatus($code, $reasonPhrase = ''): self
70+
/**
71+
* @return static
72+
*/
73+
public function withStatus($code, $reasonPhrase = ''): ResponseInterface
7174
{
7275
if (!\is_int($code) && !\is_string($code)) {
7376
throw new \InvalidArgumentException('Status code has to be an integer');

src/ServerRequest.php

+12-6
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function getUploadedFiles(): array
8181
/**
8282
* @return static
8383
*/
84-
public function withUploadedFiles(array $uploadedFiles)
84+
public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface
8585
{
8686
$new = clone $this;
8787
$new->uploadedFiles = $uploadedFiles;
@@ -97,7 +97,7 @@ public function getCookieParams(): array
9797
/**
9898
* @return static
9999
*/
100-
public function withCookieParams(array $cookies)
100+
public function withCookieParams(array $cookies): ServerRequestInterface
101101
{
102102
$new = clone $this;
103103
$new->cookieParams = $cookies;
@@ -113,7 +113,7 @@ public function getQueryParams(): array
113113
/**
114114
* @return static
115115
*/
116-
public function withQueryParams(array $query)
116+
public function withQueryParams(array $query): ServerRequestInterface
117117
{
118118
$new = clone $this;
119119
$new->queryParams = $query;
@@ -132,7 +132,7 @@ public function getParsedBody()
132132
/**
133133
* @return static
134134
*/
135-
public function withParsedBody($data)
135+
public function withParsedBody($data): ServerRequestInterface
136136
{
137137
if (!\is_array($data) && !\is_object($data) && null !== $data) {
138138
throw new \InvalidArgumentException('First parameter to withParsedBody MUST be object, array or null');
@@ -165,7 +165,10 @@ public function getAttribute($attribute, $default = null)
165165
return $this->attributes[$attribute];
166166
}
167167

168-
public function withAttribute($attribute, $value): self
168+
/**
169+
* @return static
170+
*/
171+
public function withAttribute($attribute, $value): ServerRequestInterface
169172
{
170173
if (!\is_string($attribute)) {
171174
throw new \InvalidArgumentException('Attribute name must be a string');
@@ -177,7 +180,10 @@ public function withAttribute($attribute, $value): self
177180
return $new;
178181
}
179182

180-
public function withoutAttribute($attribute): self
183+
/**
184+
* @return static
185+
*/
186+
public function withoutAttribute($attribute): ServerRequestInterface
181187
{
182188
if (!\is_string($attribute)) {
183189
throw new \InvalidArgumentException('Attribute name must be a string');

src/Stream.php

+2-31
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
namespace Nyholm\Psr7;
66

77
use Psr\Http\Message\StreamInterface;
8-
use Symfony\Component\Debug\ErrorHandler as SymfonyLegacyErrorHandler;
9-
use Symfony\Component\ErrorHandler\ErrorHandler as SymfonyErrorHandler;
108

119
/**
1210
* @author Michael Dowling and contributors to guzzlehttp/psr7
@@ -17,6 +15,8 @@
1715
*/
1816
class Stream implements StreamInterface
1917
{
18+
use StreamTrait;
19+
2020
/** @var resource|null A resource reference */
2121
private $stream;
2222

@@ -102,35 +102,6 @@ public function __destruct()
102102
$this->close();
103103
}
104104

105-
/**
106-
* @return string
107-
*/
108-
public function __toString()
109-
{
110-
try {
111-
if ($this->isSeekable()) {
112-
$this->seek(0);
113-
}
114-
115-
return $this->getContents();
116-
} catch (\Throwable $e) {
117-
if (\PHP_VERSION_ID >= 70400) {
118-
throw $e;
119-
}
120-
121-
if (\is_array($errorHandler = \set_error_handler('var_dump'))) {
122-
$errorHandler = $errorHandler[0] ?? null;
123-
}
124-
\restore_error_handler();
125-
126-
if ($e instanceof \Error || $errorHandler instanceof SymfonyErrorHandler || $errorHandler instanceof SymfonyLegacyErrorHandler) {
127-
return \trigger_error((string) $e, \E_USER_ERROR);
128-
}
129-
130-
return '';
131-
}
132-
}
133-
134105
public function close(): void
135106
{
136107
if (isset($this->stream)) {

src/StreamTrait.php

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Nyholm\Psr7;
6+
7+
use Psr\Http\Message\StreamInterface;
8+
use Symfony\Component\Debug\ErrorHandler as SymfonyLegacyErrorHandler;
9+
use Symfony\Component\ErrorHandler\ErrorHandler as SymfonyErrorHandler;
10+
11+
if (\PHP_VERSION_ID >= 70400 || (new \ReflectionMethod(StreamInterface::class, '__toString'))->hasReturnType()) {
12+
/**
13+
* @internal
14+
*/
15+
trait StreamTrait
16+
{
17+
public function __toString(): string
18+
{
19+
if ($this->isSeekable()) {
20+
$this->seek(0);
21+
}
22+
23+
return $this->getContents();
24+
}
25+
}
26+
} else {
27+
/**
28+
* @internal
29+
*/
30+
trait StreamTrait
31+
{
32+
/**
33+
* @return string
34+
*/
35+
public function __toString()
36+
{
37+
try {
38+
if ($this->isSeekable()) {
39+
$this->seek(0);
40+
}
41+
42+
return $this->getContents();
43+
} catch (\Throwable $e) {
44+
if (\is_array($errorHandler = \set_error_handler('var_dump'))) {
45+
$errorHandler = $errorHandler[0] ?? null;
46+
}
47+
\restore_error_handler();
48+
49+
if ($e instanceof \Error || $errorHandler instanceof SymfonyErrorHandler || $errorHandler instanceof SymfonyLegacyErrorHandler) {
50+
return \trigger_error((string) $e, \E_USER_ERROR);
51+
}
52+
53+
return '';
54+
}
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)