Skip to content

Commit

Permalink
Allow psr/http-message v2
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Apr 17, 2023
1 parent 07c455a commit 2b30957
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 73 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
php-version: 8.1
extensions: apcu, redis
coverage: none
tools: vimeo/psalm:4.29.0
tools: vimeo/psalm:5.9

- name: Download dependencies
uses: ramsey/composer-install@v2
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
],
"require": {
"php": ">=7.2",
"psr/http-message": "^1.0",
"psr/http-message": "^1.1 || ^2.0",
"php-http/message-factory": "^1.0",
"psr/http-factory": "^1.0"
},
Expand Down
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ parameters:
count: 1
path: src/ServerRequest.php

-
message: "#^Parameter \\#1 \\$callback of function set_error_handler expects \\(callable\\(int, string, string, int\\)\\: bool\\)\\|null, 'var_dump' given\\.$#"
count: 1
path: src/Stream.php

-
message: "#^Result of && is always false\\.$#"
count: 1
Expand Down
11 changes: 6 additions & 5 deletions src/MessageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Nyholm\Psr7;

use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\StreamInterface;

/**
Expand Down Expand Up @@ -34,7 +35,7 @@ public function getProtocolVersion(): string
return $this->protocol;
}

public function withProtocolVersion($version): self
public function withProtocolVersion($version): MessageInterface
{
if (!\is_scalar($version)) {
throw new \InvalidArgumentException('Protocol version must be a string');
Expand Down Expand Up @@ -81,7 +82,7 @@ public function getHeaderLine($header): string
return \implode(', ', $this->getHeader($header));
}

public function withHeader($header, $value): self
public function withHeader($header, $value): MessageInterface
{
$value = $this->validateAndTrimHeader($header, $value);
$normalized = \strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
Expand All @@ -96,7 +97,7 @@ public function withHeader($header, $value): self
return $new;
}

public function withAddedHeader($header, $value): self
public function withAddedHeader($header, $value): MessageInterface
{
if (!\is_string($header) || '' === $header) {
throw new \InvalidArgumentException('Header name must be an RFC 7230 compatible string');
Expand All @@ -108,7 +109,7 @@ public function withAddedHeader($header, $value): self
return $new;
}

public function withoutHeader($header): self
public function withoutHeader($header): MessageInterface
{
if (!\is_string($header)) {
throw new \InvalidArgumentException('Header name must be an RFC 7230 compatible string');
Expand All @@ -135,7 +136,7 @@ public function getBody(): StreamInterface
return $this->stream;
}

public function withBody(StreamInterface $body): self
public function withBody(StreamInterface $body): MessageInterface
{
if ($body === $this->stream) {
return $this;
Expand Down
7 changes: 4 additions & 3 deletions src/RequestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Nyholm\Psr7;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface;

/**
Expand Down Expand Up @@ -40,7 +41,7 @@ public function getRequestTarget(): string
return $target;
}

public function withRequestTarget($requestTarget): self
public function withRequestTarget($requestTarget): RequestInterface
{
if (!\is_string($requestTarget)) {
throw new \InvalidArgumentException('Request target must be a string');
Expand All @@ -61,7 +62,7 @@ public function getMethod(): string
return $this->method;
}

public function withMethod($method): self
public function withMethod($method): RequestInterface
{
if (!\is_string($method)) {
throw new \InvalidArgumentException('Method must be a string');
Expand All @@ -78,7 +79,7 @@ public function getUri(): UriInterface
return $this->uri;
}

public function withUri(UriInterface $uri, $preserveHost = false): self
public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface
{
if ($uri === $this->uri) {
return $this;
Expand Down
2 changes: 1 addition & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function getReasonPhrase(): string
return $this->reasonPhrase;
}

public function withStatus($code, $reasonPhrase = ''): self
public function withStatus($code, $reasonPhrase = ''): ResponseInterface
{
if (!\is_int($code) && !\is_string($code)) {
throw new \InvalidArgumentException('Status code has to be an integer');
Expand Down
24 changes: 6 additions & 18 deletions src/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ public function getUploadedFiles(): array
return $this->uploadedFiles;
}

/**
* @return static
*/
public function withUploadedFiles(array $uploadedFiles)
public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface
{
$new = clone $this;
$new->uploadedFiles = $uploadedFiles;
Expand All @@ -94,10 +91,7 @@ public function getCookieParams(): array
return $this->cookieParams;
}

/**
* @return static
*/
public function withCookieParams(array $cookies)
public function withCookieParams(array $cookies): ServerRequestInterface
{
$new = clone $this;
$new->cookieParams = $cookies;
Expand All @@ -110,10 +104,7 @@ public function getQueryParams(): array
return $this->queryParams;
}

/**
* @return static
*/
public function withQueryParams(array $query)
public function withQueryParams(array $query): ServerRequestInterface
{
$new = clone $this;
$new->queryParams = $query;
Expand All @@ -129,10 +120,7 @@ public function getParsedBody()
return $this->parsedBody;
}

/**
* @return static
*/
public function withParsedBody($data)
public function withParsedBody($data): ServerRequestInterface
{
if (!\is_array($data) && !\is_object($data) && null !== $data) {
throw new \InvalidArgumentException('First parameter to withParsedBody MUST be object, array or null');
Expand Down Expand Up @@ -165,7 +153,7 @@ public function getAttribute($attribute, $default = null)
return $this->attributes[$attribute];
}

public function withAttribute($attribute, $value): self
public function withAttribute($attribute, $value): ServerRequestInterface
{
if (!\is_string($attribute)) {
throw new \InvalidArgumentException('Attribute name must be a string');
Expand All @@ -177,7 +165,7 @@ public function withAttribute($attribute, $value): self
return $new;
}

public function withoutAttribute($attribute): self
public function withoutAttribute($attribute): ServerRequestInterface
{
if (!\is_string($attribute)) {
throw new \InvalidArgumentException('Attribute name must be a string');
Expand Down
33 changes: 2 additions & 31 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
namespace Nyholm\Psr7;

use Psr\Http\Message\StreamInterface;
use Symfony\Component\Debug\ErrorHandler as SymfonyLegacyErrorHandler;
use Symfony\Component\ErrorHandler\ErrorHandler as SymfonyErrorHandler;

/**
* @author Michael Dowling and contributors to guzzlehttp/psr7
Expand All @@ -17,6 +15,8 @@
*/
class Stream implements StreamInterface
{
use StreamTrait;

/** @var resource|null A resource reference */
private $stream;

Expand Down Expand Up @@ -102,35 +102,6 @@ public function __destruct()
$this->close();
}

/**
* @return string
*/
public function __toString()
{
try {
if ($this->isSeekable()) {
$this->seek(0);
}

return $this->getContents();
} catch (\Throwable $e) {
if (\PHP_VERSION_ID >= 70400) {
throw $e;
}

if (\is_array($errorHandler = \set_error_handler('var_dump'))) {
$errorHandler = $errorHandler[0] ?? null;
}
\restore_error_handler();

if ($e instanceof \Error || $errorHandler instanceof SymfonyErrorHandler || $errorHandler instanceof SymfonyLegacyErrorHandler) {
return \trigger_error((string) $e, \E_USER_ERROR);
}

return '';
}
}

public function close(): void
{
if (isset($this->stream)) {
Expand Down
57 changes: 57 additions & 0 deletions src/StreamTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Nyholm\Psr7;

use Psr\Http\Message\StreamInterface;
use Symfony\Component\Debug\ErrorHandler as SymfonyLegacyErrorHandler;
use Symfony\Component\ErrorHandler\ErrorHandler as SymfonyErrorHandler;

if (\PHP_VERSION_ID >= 70400 || (new \ReflectionMethod(StreamInterface::class, '__toString'))->hasReturnType()) {
/**
* @internal
*/
trait StreamTrait
{
public function __toString(): string
{
if ($this->isSeekable()) {
$this->seek(0);
}

return $this->getContents();
}
}
} else {
/**
* @internal
*/
trait StreamTrait
{
/**
* @return string
*/
public function __toString()
{
try {
if ($this->isSeekable()) {
$this->seek(0);
}

return $this->getContents();
} catch (\Throwable $e) {
if (\is_array($errorHandler = \set_error_handler('var_dump'))) {
$errorHandler = $errorHandler[0] ?? null;
}
\restore_error_handler();

if ($e instanceof \Error || $errorHandler instanceof SymfonyErrorHandler || $errorHandler instanceof SymfonyLegacyErrorHandler) {
return trigger_error((string) $e, \E_USER_ERROR);
}

return '';
}
}
}
}
14 changes: 7 additions & 7 deletions src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function getFragment(): string
return $this->fragment;
}

public function withScheme($scheme): self
public function withScheme($scheme): UriInterface
{
if (!\is_string($scheme)) {
throw new \InvalidArgumentException('Scheme must be a string');
Expand All @@ -157,7 +157,7 @@ public function withScheme($scheme): self
return $new;
}

public function withUserInfo($user, $password = null): self
public function withUserInfo($user, $password = null): UriInterface
{
if (!\is_string($user)) {
throw new \InvalidArgumentException('User must be a string');
Expand All @@ -182,7 +182,7 @@ public function withUserInfo($user, $password = null): self
return $new;
}

public function withHost($host): self
public function withHost($host): UriInterface
{
if (!\is_string($host)) {
throw new \InvalidArgumentException('Host must be a string');
Expand All @@ -198,7 +198,7 @@ public function withHost($host): self
return $new;
}

public function withPort($port): self
public function withPort($port): UriInterface
{
if ($this->port === $port = $this->filterPort($port)) {
return $this;
Expand All @@ -210,7 +210,7 @@ public function withPort($port): self
return $new;
}

public function withPath($path): self
public function withPath($path): UriInterface
{
if ($this->path === $path = $this->filterPath($path)) {
return $this;
Expand All @@ -222,7 +222,7 @@ public function withPath($path): self
return $new;
}

public function withQuery($query): self
public function withQuery($query): UriInterface
{
if ($this->query === $query = $this->filterQueryAndFragment($query)) {
return $this;
Expand All @@ -234,7 +234,7 @@ public function withQuery($query): self
return $new;
}

public function withFragment($fragment): self
public function withFragment($fragment): UriInterface
{
if ($this->fragment === $fragment = $this->filterQueryAndFragment($fragment)) {
return $this;
Expand Down
3 changes: 2 additions & 1 deletion tests/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Nyholm\Psr7\Stream;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\StreamInterface;
use Symfony\Component\ErrorHandler\ErrorHandler as SymfonyErrorHandler;

/**
Expand Down Expand Up @@ -171,7 +172,7 @@ public function testCanDetachStream()
$throws(function ($stream) {
(string) $stream;
});
} else {
} elseif (!(new \ReflectionMethod(StreamInterface::class, '__toString'))->hasReturnType()) {
$this->assertSame('', (string) $stream);

SymfonyErrorHandler::register();
Expand Down

0 comments on commit 2b30957

Please sign in to comment.