From 1bf8c003109315fae9b66120931833b30b0cf587 Mon Sep 17 00:00:00 2001 From: smiley Date: Thu, 20 Jul 2023 00:37:17 +0200 Subject: [PATCH 1/3] PHP8+ "static" return type --- composer.json | 2 +- src/MessageInterface.php | 10 +++++----- src/RequestInterface.php | 6 +++--- src/ResponseInterface.php | 2 +- src/ServerRequestInterface.php | 12 ++++++------ src/UriInterface.php | 14 +++++++------- 6 files changed, 23 insertions(+), 23 deletions(-) diff --git a/composer.json b/composer.json index c66e5ab..b9b3598 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ } ], "require": { - "php": "^7.2 || ^8.0" + "php": "^8.0" }, "autoload": { "psr-4": { diff --git a/src/MessageInterface.php b/src/MessageInterface.php index a83c985..0bbdf84 100644 --- a/src/MessageInterface.php +++ b/src/MessageInterface.php @@ -38,7 +38,7 @@ public function getProtocolVersion(): string; * @param string $version HTTP protocol version * @return static */ - public function withProtocolVersion(string $version): MessageInterface; + public function withProtocolVersion(string $version): static; /** * Retrieves all message header values. @@ -129,7 +129,7 @@ public function getHeaderLine(string $name): string; * @return static * @throws \InvalidArgumentException for invalid header names or values. */ - public function withHeader(string $name, $value): MessageInterface; + public function withHeader(string $name, $value): static; /** * Return an instance with the specified header appended with the given value. @@ -147,7 +147,7 @@ public function withHeader(string $name, $value): MessageInterface; * @return static * @throws \InvalidArgumentException for invalid header names or values. */ - public function withAddedHeader(string $name, $value): MessageInterface; + public function withAddedHeader(string $name, $value): static; /** * Return an instance without the specified header. @@ -161,7 +161,7 @@ public function withAddedHeader(string $name, $value): MessageInterface; * @param string $name Case-insensitive header field name to remove. * @return static */ - public function withoutHeader(string $name): MessageInterface; + public function withoutHeader(string $name): static; /** * Gets the body of the message. @@ -183,5 +183,5 @@ public function getBody(): StreamInterface; * @return static * @throws \InvalidArgumentException When the body is not valid. */ - public function withBody(StreamInterface $body): MessageInterface; + public function withBody(StreamInterface $body): static; } diff --git a/src/RequestInterface.php b/src/RequestInterface.php index 33f85e5..88c5f76 100644 --- a/src/RequestInterface.php +++ b/src/RequestInterface.php @@ -58,7 +58,7 @@ public function getRequestTarget(): string; * @param string $requestTarget * @return static */ - public function withRequestTarget(string $requestTarget): RequestInterface; + public function withRequestTarget(string $requestTarget): static; /** @@ -83,7 +83,7 @@ public function getMethod(): string; * @return static * @throws \InvalidArgumentException for invalid HTTP methods. */ - public function withMethod(string $method): RequestInterface; + public function withMethod(string $method): static; /** * Retrieves the URI instance. @@ -126,5 +126,5 @@ public function getUri(): UriInterface; * @param bool $preserveHost Preserve the original state of the Host header. * @return static */ - public function withUri(UriInterface $uri, bool $preserveHost = false): RequestInterface; + public function withUri(UriInterface $uri, bool $preserveHost = false): static; } diff --git a/src/ResponseInterface.php b/src/ResponseInterface.php index e9299a9..6a95631 100644 --- a/src/ResponseInterface.php +++ b/src/ResponseInterface.php @@ -49,7 +49,7 @@ public function getStatusCode(): int; * @return static * @throws \InvalidArgumentException For invalid status code arguments. */ - public function withStatus(int $code, string $reasonPhrase = ''): ResponseInterface; + public function withStatus(int $code, string $reasonPhrase = ''): static; /** * Gets the response reason phrase associated with the status code. diff --git a/src/ServerRequestInterface.php b/src/ServerRequestInterface.php index 8625d0e..a11ac71 100644 --- a/src/ServerRequestInterface.php +++ b/src/ServerRequestInterface.php @@ -82,7 +82,7 @@ public function getCookieParams(): array; * @param array $cookies Array of key/value pairs representing cookies. * @return static */ - public function withCookieParams(array $cookies): ServerRequestInterface; + public function withCookieParams(array $cookies): static; /** * Retrieve query string arguments. @@ -120,7 +120,7 @@ public function getQueryParams(): array; * $_GET. * @return static */ - public function withQueryParams(array $query): ServerRequestInterface; + public function withQueryParams(array $query): static; /** * Retrieve normalized file upload data. @@ -147,7 +147,7 @@ public function getUploadedFiles(): array; * @return static * @throws \InvalidArgumentException if an invalid structure is provided. */ - public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface; + public function withUploadedFiles(array $uploadedFiles): static; /** * Retrieve any parameters provided in the request body. @@ -194,7 +194,7 @@ public function getParsedBody(); * @throws \InvalidArgumentException if an unsupported argument type is * provided. */ - public function withParsedBody($data): ServerRequestInterface; + public function withParsedBody($data): static; /** * Retrieve attributes derived from the request. @@ -241,7 +241,7 @@ public function getAttribute(string $name, $default = null); * @param mixed $value The value of the attribute. * @return static */ - public function withAttribute(string $name, $value): ServerRequestInterface; + public function withAttribute(string $name, $value): static; /** * Return an instance that removes the specified derived request attribute. @@ -257,5 +257,5 @@ public function withAttribute(string $name, $value): ServerRequestInterface; * @param string $name The attribute name. * @return static */ - public function withoutAttribute(string $name): ServerRequestInterface; + public function withoutAttribute(string $name): static; } diff --git a/src/UriInterface.php b/src/UriInterface.php index 15e2cf2..7c9b238 100644 --- a/src/UriInterface.php +++ b/src/UriInterface.php @@ -189,7 +189,7 @@ public function getFragment(): string; * @return static A new instance with the specified scheme. * @throws \InvalidArgumentException for invalid or unsupported schemes. */ - public function withScheme(string $scheme): UriInterface; + public function withScheme(string $scheme): static; /** * Return an instance with the specified user information. @@ -205,7 +205,7 @@ public function withScheme(string $scheme): UriInterface; * @param null|string $password The password associated with $user. * @return static A new instance with the specified user information. */ - public function withUserInfo(string $user, ?string $password = null): UriInterface; + public function withUserInfo(string $user, ?string $password = null): static; /** * Return an instance with the specified host. @@ -219,7 +219,7 @@ public function withUserInfo(string $user, ?string $password = null): UriInterfa * @return static A new instance with the specified host. * @throws \InvalidArgumentException for invalid hostnames. */ - public function withHost(string $host): UriInterface; + public function withHost(string $host): static; /** * Return an instance with the specified port. @@ -238,7 +238,7 @@ public function withHost(string $host): UriInterface; * @return static A new instance with the specified port. * @throws \InvalidArgumentException for invalid ports. */ - public function withPort(?int $port): UriInterface; + public function withPort(?int $port): static; /** * Return an instance with the specified path. @@ -262,7 +262,7 @@ public function withPort(?int $port): UriInterface; * @return static A new instance with the specified path. * @throws \InvalidArgumentException for invalid paths. */ - public function withPath(string $path): UriInterface; + public function withPath(string $path): static; /** * Return an instance with the specified query string. @@ -279,7 +279,7 @@ public function withPath(string $path): UriInterface; * @return static A new instance with the specified query string. * @throws \InvalidArgumentException for invalid query strings. */ - public function withQuery(string $query): UriInterface; + public function withQuery(string $query): static; /** * Return an instance with the specified URI fragment. @@ -295,7 +295,7 @@ public function withQuery(string $query): UriInterface; * @param string $fragment The fragment to use with the new instance. * @return static A new instance with the specified fragment. */ - public function withFragment(string $fragment): UriInterface; + public function withFragment(string $fragment): static; /** * Return the string representation as a URI reference. From bdc54b476c808b0f03c2db9d1c0394f24b70b177 Mon Sep 17 00:00:00 2001 From: smiley Date: Thu, 20 Jul 2023 00:56:40 +0200 Subject: [PATCH 2/3] PHP8+ "mixed" and union types --- src/MessageInterface.php | 4 ++-- src/ServerRequestInterface.php | 8 ++++---- src/StreamInterface.php | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/MessageInterface.php b/src/MessageInterface.php index 0bbdf84..40567a6 100644 --- a/src/MessageInterface.php +++ b/src/MessageInterface.php @@ -129,7 +129,7 @@ public function getHeaderLine(string $name): string; * @return static * @throws \InvalidArgumentException for invalid header names or values. */ - public function withHeader(string $name, $value): static; + public function withHeader(string $name, array|string $value): static; /** * Return an instance with the specified header appended with the given value. @@ -147,7 +147,7 @@ public function withHeader(string $name, $value): static; * @return static * @throws \InvalidArgumentException for invalid header names or values. */ - public function withAddedHeader(string $name, $value): static; + public function withAddedHeader(string $name, array|string $value): static; /** * Return an instance without the specified header. diff --git a/src/ServerRequestInterface.php b/src/ServerRequestInterface.php index a11ac71..9b42e5c 100644 --- a/src/ServerRequestInterface.php +++ b/src/ServerRequestInterface.php @@ -164,7 +164,7 @@ public function withUploadedFiles(array $uploadedFiles): static; * @return null|array|object The deserialized body parameters, if any. * These will typically be an array or object. */ - public function getParsedBody(); + public function getParsedBody(): null|array|object; /** * Return an instance with the specified body parameters. @@ -194,7 +194,7 @@ public function getParsedBody(); * @throws \InvalidArgumentException if an unsupported argument type is * provided. */ - public function withParsedBody($data): static; + public function withParsedBody(null|array|object $data): static; /** * Retrieve attributes derived from the request. @@ -224,7 +224,7 @@ public function getAttributes(): array; * @param mixed $default Default value to return if the attribute does not exist. * @return mixed */ - public function getAttribute(string $name, $default = null); + public function getAttribute(string $name, mixed $default = null): mixed; /** * Return an instance with the specified derived request attribute. @@ -241,7 +241,7 @@ public function getAttribute(string $name, $default = null); * @param mixed $value The value of the attribute. * @return static */ - public function withAttribute(string $name, $value): static; + public function withAttribute(string $name, mixed $value): static; /** * Return an instance that removes the specified derived request attribute. diff --git a/src/StreamInterface.php b/src/StreamInterface.php index a62aabb..50404d4 100644 --- a/src/StreamInterface.php +++ b/src/StreamInterface.php @@ -41,7 +41,7 @@ public function close(): void; * * @return resource|null Underlying PHP stream, if any */ - public function detach(); + public function detach(): mixed; /** * Get the size of the stream if known. @@ -154,5 +154,5 @@ public function getContents(): string; * provided. Returns a specific key value if a key is provided and the * value is found, or null if the key is not found. */ - public function getMetadata(?string $key = null); + public function getMetadata(?string $key = null): mixed; } From 4383e8967424c50bfe95492d4fbfa2815a245dde Mon Sep 17 00:00:00 2001 From: smiley Date: Tue, 25 Jul 2023 19:26:37 +0200 Subject: [PATCH 3/3] use Stringable interface, leave __toString() docblock for informational purpose --- src/StreamInterface.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/StreamInterface.php b/src/StreamInterface.php index 50404d4..cccd028 100644 --- a/src/StreamInterface.php +++ b/src/StreamInterface.php @@ -2,6 +2,8 @@ namespace Psr\Http\Message; +use Stringable; + /** * Describes a data stream. * @@ -9,7 +11,7 @@ * a wrapper around the most common operations, including serialization of * the entire stream to a string. */ -interface StreamInterface +interface StreamInterface extends Stringable { /** * Reads all data from the stream into a string, from the beginning to end. @@ -25,7 +27,7 @@ interface StreamInterface * @see http://php.net/manual/en/language.oop5.magic.php#object.tostring * @return string */ - public function __toString(): string; +# public function __toString(): string; /** * Closes the stream and any underlying resources.