From 1a8e0377e7e7578f3303da2c2ebd50699d57ebd8 Mon Sep 17 00:00:00 2001 From: Mihail Binev Date: Mon, 3 May 2021 19:45:02 +0200 Subject: [PATCH] - force native functions - code style --- AcceptHeaderNegotiator.php | 22 +++++++++++----------- Client/EncodingTrait.php | 2 +- Client/Psr18Exception.php | 8 ++++++-- ClientRequest.php | 7 +++---- HeaderTrait.php | 3 +-- MessageTrait.php | 1 - ServerRequest.php | 3 +-- ServerResponse.php | 2 +- functions.php | 6 ++++-- 9 files changed, 28 insertions(+), 26 deletions(-) diff --git a/AcceptHeaderNegotiator.php b/AcceptHeaderNegotiator.php index f2b71bb..c32aeee 100644 --- a/AcceptHeaderNegotiator.php +++ b/AcceptHeaderNegotiator.php @@ -70,7 +70,7 @@ public function matches(string $accepts): array */ private function parse(string $header): \Generator { - foreach (explode(',', $header) as $header) { + foreach (\explode(',', $header) as $header) { yield new class($header) extends AcceptHeader {}; } } @@ -92,22 +92,22 @@ public function __construct(string $header) { $this->header = $header; - $header = preg_replace('/[[:space:]]/', '', $header); - $bits = explode(';', $header); - $type = array_shift($bits); - if (!empty($type) && !preg_match('~^(\*|[a-z0-9._]+)([/|_\-])?(\*|[a-z0-9.\-_+]+)?$~i', $type, $matches)) { - throw new \InvalidArgumentException(sprintf('"%s" is not a valid Access header', $header), + $header = \preg_replace('/[[:space:]]/', '', $header); + $bits = \explode(';', $header); + $type = \array_shift($bits); + if (!empty($type) && !\preg_match('~^(\*|[a-z0-9._]+)([/|_\-])?(\*|[a-z0-9.\-_+]+)?$~i', $type, $matches)) { + throw new \InvalidArgumentException(\sprintf('"%s" is not a valid Access header', $header), HttpStatus::NOT_ACCEPTABLE); } $this->separator = $matches[2] ?? '/'; - [$type, $subtype] = explode($this->separator, $type, 2) + [1 => '*']; + [$type, $subtype] = \explode($this->separator, $type, 2) + [1 => '*']; if ('*' === $type && '*' !== $subtype) { // @see https://tools.ietf.org/html/rfc7231#section-5.3.2 - throw new \InvalidArgumentException(sprintf('"%s" is not a valid Access header', $header), + throw new \InvalidArgumentException(\sprintf('"%s" is not a valid Access header', $header), HttpStatus::NOT_ACCEPTABLE); } // @see https://tools.ietf.org/html/rfc7540#section-8.1.2 - $this->type = trim(strtolower($type)); + $this->type = \trim(\strtolower($type)); /* * Uses a simple heuristic to check if subtype is part of * some convoluted media type like "vnd.api-v1+json". @@ -117,9 +117,9 @@ public function __construct(string $header) * type like "vnd.whatever". The web world is a big mess * but this module can handle the Dunning-Kruger effect. */ - $this->subtype = trim(explode('+', $subtype)[1] ?? $subtype); + $this->subtype = \trim(\explode('+', $subtype)[1] ?? $subtype); $this->catchAll = ('*' === $this->type) && ('*' === $this->subtype); - parse_str(join('&', $bits), $this->params); + \parse_str(\join('&', $bits), $this->params); $this->quality = (float)($this->params['q'] ?? 1); unset($this->params['q']); } diff --git a/Client/EncodingTrait.php b/Client/EncodingTrait.php index dcac2ba..b759137 100644 --- a/Client/EncodingTrait.php +++ b/Client/EncodingTrait.php @@ -21,7 +21,7 @@ trait EncodingTrait public function withEncoding(int $type): static { - if (in_array($type, [0, PHP_QUERY_RFC1738, PHP_QUERY_RFC3986], true)) { + if (\in_array($type, [0, PHP_QUERY_RFC1738, PHP_QUERY_RFC3986], true)) { $this->encoding = $type; return $this; } diff --git a/Client/Psr18Exception.php b/Client/Psr18Exception.php index 228c8f7..c36003b 100644 --- a/Client/Psr18Exception.php +++ b/Client/Psr18Exception.php @@ -9,8 +9,12 @@ class Psr18Exception extends \Exception implements RequestExceptionInterface, Ne { private RequestInterface $request; - public function __construct(string $message, int $code, RequestInterface $request, \Throwable $previous = null) - { + public function __construct( + string $message, + int $code, + RequestInterface $request, + \Throwable $previous = null + ) { parent::__construct($message, $code, $previous); $this->request = $request; } diff --git a/ClientRequest.php b/ClientRequest.php index e63eb18..3208acc 100644 --- a/ClientRequest.php +++ b/ClientRequest.php @@ -53,7 +53,7 @@ public function __construct( public function getMethod(): string { - return strtoupper($this->method); + return \strtoupper($this->method); } public function withMethod($method): ClientRequest @@ -105,7 +105,7 @@ public function withRequestTarget($requestTarget): static public function getPath(): string { - return str_replace($_SERVER['SCRIPT_NAME'], '', $this->uri->getPath()) ?: '/'; + return \str_replace($_SERVER['SCRIPT_NAME'], '', $this->uri->getPath()) ?: '/'; } public function getBaseUri(): string @@ -187,9 +187,8 @@ protected function getPhpError(int $status, ?string $message = null): Response 'title' => StatusCode::CODE[$status], 'detail' => $message ?? \error_get_last()['message'], 'instance' => (string)$this->getUri(), - 'type' => 'https://httpstatuses.com/' . (string)$status, + 'type' => 'https://httpstatuses.com/' . $status, 'status' => $status, ]), $status, ['Content-type' => 'application/problem+json']); } - } diff --git a/HeaderTrait.php b/HeaderTrait.php index fd0dfa5..281c345 100644 --- a/HeaderTrait.php +++ b/HeaderTrait.php @@ -14,7 +14,6 @@ use Koded\Http\Interfaces\HttpStatus; - trait HeaderTrait { /** @@ -210,7 +209,7 @@ protected function normalizeHeaderValue(string $name, string|array $value): arra $value = (array)$value; } try { - if (empty($value = \array_map(function($v) { + if (empty($value = \array_map(function($v): string { return \trim(\preg_replace('/\s+/', ' ', $v)); }, $value))) { throw new \InvalidArgumentException( diff --git a/MessageTrait.php b/MessageTrait.php index 8bc6dc5..8046c00 100644 --- a/MessageTrait.php +++ b/MessageTrait.php @@ -14,7 +14,6 @@ use Psr\Http\Message\StreamInterface; - trait MessageTrait { protected string $protocolVersion = '1.1'; diff --git a/ServerRequest.php b/ServerRequest.php index f6ea47d..6411850 100644 --- a/ServerRequest.php +++ b/ServerRequest.php @@ -15,7 +15,6 @@ use Koded\Http\Interfaces\Request; use Psr\Http\Message\ServerRequestInterface; - class ServerRequest extends ClientRequest implements Request { use CookieTrait, FilesTrait, ValidatableTrait; @@ -89,7 +88,7 @@ public function withParsedBody($data): static return $instance; } throw new \InvalidArgumentException( - \sprintf('Unsupported data provided (%s), Expects NULL, array or iterable', gettype($data)) + \sprintf('Unsupported data provided (%s), Expects NULL, array or iterable', \gettype($data)) ); } diff --git a/ServerResponse.php b/ServerResponse.php index 9d12f1b..1441c22 100644 --- a/ServerResponse.php +++ b/ServerResponse.php @@ -35,7 +35,7 @@ class ServerResponse implements Response, \JsonSerializable * @param int $statusCode [optional] * @param array $headers [optional] */ - public function __construct($content = '', int $statusCode = HttpStatus::OK, array $headers = []) + public function __construct(mixed $content = '', int $statusCode = HttpStatus::OK, array $headers = []) { $this->setStatus($this, $statusCode); $this->setHeaders($headers); diff --git a/functions.php b/functions.php index 7220d71..f2c5172 100644 --- a/functions.php +++ b/functions.php @@ -57,7 +57,10 @@ function create_stream(mixed $resource, string $mode = 'r+b'): StreamInterface * * @return int The total count of bytes copied */ -function stream_copy(StreamInterface $source, StreamInterface $destination, int $length = 8192): int +function stream_copy( + StreamInterface $source, + StreamInterface $destination, + int $length = 8192): int { $bytes = 0; while (false === $source->eof()) { @@ -110,7 +113,6 @@ function normalize_files_array(array $files): array } return $file; }; - return $sane($files); }