Skip to content

Commit

Permalink
feat: update PSR message interface (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
rancoud authored Dec 7, 2024
1 parent 75f3d41 commit fd5b6aa
Show file tree
Hide file tree
Showing 26 changed files with 185 additions and 574 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"symfony/process": "^5.2"
},
"provide": {
"psr/http-message-implementation": "1.0",
"psr/http-message-implementation": "2.0",
"psr/http-factory-implementation": "1.0"
},
"scripts": {
Expand Down
6 changes: 3 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Message/Factory/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function createUriFromArray(array $server): Uri
}

if (isset($server['SERVER_PORT'])) {
$uri = $uri->withPort($server['SERVER_PORT']);
$uri = $uri->withPort((int) $server['SERVER_PORT']);
}

if (isset($server['REQUEST_URI'])) {
Expand Down
58 changes: 25 additions & 33 deletions src/Message/MessageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function getProtocolVersion(): string
*
* @return self
*/
public function withProtocolVersion($version): self
public function withProtocolVersion(string $version): self
{
$this->validateProtocolVersion($version);

Expand Down Expand Up @@ -69,12 +69,8 @@ public function getHeaders(): array
*
* @return bool
*/
public function hasHeader($name): bool
public function hasHeader(string $name): bool
{
if (!\is_string($name)) {
throw new \InvalidArgumentException('Header name must be a string');
}

return isset($this->headerNames[\mb_strtolower($name)]);
}

Expand All @@ -85,12 +81,8 @@ public function hasHeader($name): bool
*
* @return array
*/
public function getHeader($name): array
public function getHeader(string $name): array
{
if (!\is_string($name)) {
throw new \InvalidArgumentException('Header name must be a string');
}

$name = \mb_strtolower($name);

if (!isset($this->headerNames[$name])) {
Expand All @@ -109,7 +101,7 @@ public function getHeader($name): array
*
* @return string
*/
public function getHeaderLine($name): string
public function getHeaderLine(string $name): string
{
return \implode(', ', $this->getHeader($name));
}
Expand All @@ -122,9 +114,9 @@ public function getHeaderLine($name): string
*
* @return self
*/
public function withHeader($name, $value): self
public function withHeader(string $name, $value): self
{
if (!\is_string($name) || $name === '') {
if ($name === '') {
throw new \InvalidArgumentException('Header name must be non-empty string');
}

Expand All @@ -149,9 +141,9 @@ public function withHeader($name, $value): self
*
* @return self
*/
public function withAddedHeader($name, $value): self
public function withAddedHeader(string $name, $value): self
{
if (!\is_string($name) || $name === '') {
if ($name === '') {
throw new \InvalidArgumentException('Header name must be non-empty string');
}

Expand All @@ -168,9 +160,9 @@ public function withAddedHeader($name, $value): self
*
* @return self
*/
public function withoutHeader($name): self
public function withoutHeader(string $name): self
{
if (!\is_string($name) || $name === '') {
if ($name === '') {
throw new \InvalidArgumentException('Header name must be non-empty string');
}

Expand Down Expand Up @@ -240,8 +232,8 @@ protected function setHeaders(array $headers): void
}

/**
* @param string $header
* @param mixed $values
* @param string|array $header
* @param mixed $values
*
* @throws \InvalidArgumentException
*
Expand Down Expand Up @@ -278,35 +270,35 @@ protected function validateAndTrimHeader($header, $values): array
}

/**
* Because of PHP 8.4.
* @param string $protocolVersion
*
* @param $string
* @param string $characters
* @throws \InvalidArgumentException
*
* @return string
*/
protected function trim($string, string $characters = " \n\r\t\v\0"): string
protected function validateProtocolVersion(string $protocolVersion): string
{
if (\PHP_MAJOR_VERSION >= 8 && \PHP_MINOR_VERSION >= 4) {
return \mb_trim((string) $string, $characters);
if (!\in_array($protocolVersion, static::$validProtocols, true)) {
throw new \InvalidArgumentException('Protocol Version must be ' . \implode(' or ', static::$validProtocols));
}

return \trim((string) $string, $characters);
return $protocolVersion;
}

/**
* @param string $protocolVersion
* Because of PHP 8.4.
*
* @throws \InvalidArgumentException
* @param $string
* @param string $characters
*
* @return string
*/
protected function validateProtocolVersion(string $protocolVersion): string
protected function trim($string, string $characters = " \n\r\t\v\0"): string
{
if (!\in_array($protocolVersion, static::$validProtocols, true)) {
throw new \InvalidArgumentException('Protocol Version must be ' . \implode(' or ', static::$validProtocols));
if (\PHP_MAJOR_VERSION >= 8 && \PHP_MINOR_VERSION >= 4) {
return \mb_trim((string) $string, $characters);
}

return $protocolVersion;
return \trim((string) $string, $characters);
}
}
18 changes: 5 additions & 13 deletions src/Message/RequestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ public function getRequestTarget(): string
}

/**
* @param $requestTarget
* @param string $requestTarget
*
* @throws \InvalidArgumentException
*
* @return self
*/
public function withRequestTarget($requestTarget): self
public function withRequestTarget(string $requestTarget): self
{
if (\preg_match('#\s#', $requestTarget)) {
throw new \InvalidArgumentException('Invalid request target provided; cannot contain whitespace');
Expand All @@ -129,7 +129,7 @@ public function getMethod(): string
*
* @return self
*/
public function withMethod($method): self
public function withMethod(string $method): self
{
$method = $this->filterMethod($method);

Expand All @@ -155,12 +155,8 @@ public function getUri(): UriInterface
*
* @return self
*/
public function withUri(UriInterface $uri, $preserveHost = false): self
public function withUri(UriInterface $uri, bool $preserveHost = false): self
{
if (!\is_bool($preserveHost)) {
throw new \InvalidArgumentException('Preserve Host must be a boolean');
}

if ($this->uri === $uri) {
return $this;
}
Expand All @@ -182,12 +178,8 @@ public function withUri(UriInterface $uri, $preserveHost = false): self
*
* @return string
*/
protected function filterMethod($method): string
protected function filterMethod(string $method): string
{
if (!\is_string($method)) {
throw new \InvalidArgumentException('Method must be a string');
}

if (!\in_array($method, static::$methods, true)) {
throw new \InvalidArgumentException(\sprintf('Method %s is invalid', $method));
}
Expand Down
6 changes: 1 addition & 5 deletions src/Message/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,8 @@ public function getStatusCode(): int
*
* @return Response
*/
public function withStatus($code, $reasonPhrase = ''): self
public function withStatus(int $code, string $reasonPhrase = ''): self
{
if (!\is_int($code)) {
throw new \InvalidArgumentException('Status code has to be an integer');
}

if (!isset(static::PHRASES[$code])) {
throw new \InvalidArgumentException('Status code has to be an integer between 100 and 799');
}
Expand Down
18 changes: 3 additions & 15 deletions src/Message/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,8 @@ public function getAttributes(): array
*
* @return mixed|null
*/
public function getAttribute($name, $default = null)
public function getAttribute(string $name, $default = null)
{
if (!\is_string($name)) {
throw new \InvalidArgumentException('Name must be a string');
}

if (!\array_key_exists($name, $this->attributes)) {
return $default;
}
Expand All @@ -203,12 +199,8 @@ public function getAttribute($name, $default = null)
*
* @return self
*/
public function withAttribute($name, $value): self
public function withAttribute(string $name, $value): self
{
if (!\is_string($name)) {
throw new \InvalidArgumentException('Name must be a string');
}

$new = clone $this;
$new->attributes[$name] = $value;

Expand All @@ -222,12 +214,8 @@ public function withAttribute($name, $value): self
*
* @return self
*/
public function withoutAttribute($name): self
public function withoutAttribute(string $name): self
{
if (!\is_string($name)) {
throw new \InvalidArgumentException('Name must be a string');
}

if (!\array_key_exists($name, $this->attributes)) {
return $this;
}
Expand Down
42 changes: 6 additions & 36 deletions src/Message/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,8 @@ public function isSeekable(): bool
* @throws \InvalidArgumentException
* @throws \RuntimeException
*/
public function seek($offset, $whence = \SEEK_SET): void
public function seek(int $offset, int $whence = \SEEK_SET): void
{
if (!\is_int($offset)) {
throw new \InvalidArgumentException('Offset must be a int');
}

if (!\is_int($whence)) {
throw new \InvalidArgumentException('Whence must be a int');
}

if (!$this->seekable) {
throw new \RuntimeException('Stream is not seekable');
}
Expand Down Expand Up @@ -208,12 +200,8 @@ public function isWritable(): bool
*
* @return int
*/
public function write($string)
public function write(string $string): int
{
if (!\is_string($string)) {
throw new \InvalidArgumentException('Data must be a string');
}

if (!$this->writable) {
throw new \RuntimeException('Cannot write to a non-writable stream');
}
Expand Down Expand Up @@ -241,19 +229,15 @@ public function isReadable(): bool
}

/**
* @param $length
* @param int $length
*
* @throws \InvalidArgumentException
* @throws \RuntimeException
*
* @return string
*/
public function read($length): string
public function read(int $length): string
{
if (!\is_int($length)) {
throw new \InvalidArgumentException('Length must be a int');
}

if (!$this->readable) {
throw new \RuntimeException('Cannot read from non-readable stream');
}
Expand Down Expand Up @@ -318,14 +302,10 @@ public function getContents(): string
*
* @throws \InvalidArgumentException
*
* @return array|null
* @return array|mixed|null
*/
public function getMetadata($key = null)
public function getMetadata(?string $key = null)
{
if (!$this->isStringOrNull($key)) {
throw new \InvalidArgumentException('Key must be a string or NULL');
}

if (!isset($this->stream)) {
return $key ? null : [];
}
Expand Down Expand Up @@ -428,14 +408,4 @@ public function __destruct()
{
$this->close();
}

/**
* @param $param
*
* @return bool
*/
protected function isStringOrNull($param): bool
{
return $param === null || \is_string($param);
}
}
Loading

0 comments on commit fd5b6aa

Please sign in to comment.