Skip to content

Commit

Permalink
- force native functions
Browse files Browse the repository at this point in the history
- code style
kodeart committed May 3, 2021
1 parent 80ddeb3 commit 1a8e037
Showing 9 changed files with 28 additions and 26 deletions.
22 changes: 11 additions & 11 deletions AcceptHeaderNegotiator.php
Original file line number Diff line number Diff line change
@@ -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']);
}
2 changes: 1 addition & 1 deletion Client/EncodingTrait.php
Original file line number Diff line number Diff line change
@@ -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;
}
8 changes: 6 additions & 2 deletions Client/Psr18Exception.php
Original file line number Diff line number Diff line change
@@ -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;
}
7 changes: 3 additions & 4 deletions ClientRequest.php
Original file line number Diff line number Diff line change
@@ -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']);
}

}
3 changes: 1 addition & 2 deletions HeaderTrait.php
Original file line number Diff line number Diff line change
@@ -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(
1 change: 0 additions & 1 deletion MessageTrait.php
Original file line number Diff line number Diff line change
@@ -14,7 +14,6 @@

use Psr\Http\Message\StreamInterface;


trait MessageTrait
{
protected string $protocolVersion = '1.1';
3 changes: 1 addition & 2 deletions ServerRequest.php
Original file line number Diff line number Diff line change
@@ -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))
);
}

2 changes: 1 addition & 1 deletion ServerResponse.php
Original file line number Diff line number Diff line change
@@ -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);
6 changes: 4 additions & 2 deletions functions.php
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 1a8e037

Please sign in to comment.