Skip to content

Commit

Permalink
Allow psr/http-message v2 (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas authored Apr 20, 2023
1 parent 07c455a commit 6a31c27
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 62 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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
],
"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"
},
"require-dev": {
"phpunit/phpunit": "^7.5 || 8.5 || 9.4",
"php-http/psr7-integration-tests": "^1.0",
"php-http/psr7-integration-tests": "^1.0@dev",
"http-interop/http-factory-tests": "^0.9",
"symfony/error-handler": "^4.4"
},
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
26 changes: 21 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,10 @@ public function getProtocolVersion(): string
return $this->protocol;
}

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

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

public function withAddedHeader($header, $value): self
/**
* @return static
*/
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 +118,10 @@ public function withAddedHeader($header, $value): self
return $new;
}

public function withoutHeader($header): self
/**
* @return static
*/
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 +148,10 @@ public function getBody(): StreamInterface
return $this->stream;
}

public function withBody(StreamInterface $body): self
/**
* @return static
*/
public function withBody(StreamInterface $body): MessageInterface
{
if ($body === $this->stream) {
return $this;
Expand Down
16 changes: 13 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,10 @@ public function getRequestTarget(): string
return $target;
}

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

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

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

public function withStatus($code, $reasonPhrase = ''): self
/**
* @return static
*/
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
18 changes: 12 additions & 6 deletions src/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function getUploadedFiles(): array
/**
* @return static
*/
public function withUploadedFiles(array $uploadedFiles)
public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface
{
$new = clone $this;
$new->uploadedFiles = $uploadedFiles;
Expand All @@ -97,7 +97,7 @@ public function getCookieParams(): array
/**
* @return static
*/
public function withCookieParams(array $cookies)
public function withCookieParams(array $cookies): ServerRequestInterface
{
$new = clone $this;
$new->cookieParams = $cookies;
Expand All @@ -113,7 +113,7 @@ public function getQueryParams(): array
/**
* @return static
*/
public function withQueryParams(array $query)
public function withQueryParams(array $query): ServerRequestInterface
{
$new = clone $this;
$new->queryParams = $query;
Expand All @@ -132,7 +132,7 @@ public function getParsedBody()
/**
* @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 +165,10 @@ public function getAttribute($attribute, $default = null)
return $this->attributes[$attribute];
}

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

public function withoutAttribute($attribute): self
/**
* @return static
*/
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 '';
}
}
}
}
Loading

0 comments on commit 6a31c27

Please sign in to comment.