Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 18 additions & 28 deletions src/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,18 @@ public function sendRequest(RequestInterface $request): ResponseInterface
return $this->convertCurlSimpleResponse($infos);
}

/**
* @throws \RuntimeException
*
* @return false|resource
*/
protected function createCurlSimple(RequestInterface $request)
/** @throws \RuntimeException */
protected function createCurlSimple(RequestInterface $request): \CurlHandle
{
$curlHandle = \curl_init();

// @codeCoverageIgnoreStart
// Not possible to arrive here
if ($curlHandle === false) {
throw new \RuntimeException();
}
// @codeCoverageIgnoreEnd

\curl_setopt($curlHandle, \CURLOPT_RETURNTRANSFER, true);
\curl_setopt($curlHandle, \CURLOPT_HEADER, true);

Expand All @@ -84,8 +87,7 @@ protected function createCurlSimple(RequestInterface $request)
return $curlHandle;
}

/** @param resource $curlHandle */
protected function setProtocolVersion($curlHandle, RequestInterface $request): self
protected function setProtocolVersion(\CurlHandle $curlHandle, RequestInterface $request): self
{
$version = $request->getProtocolVersion();

Expand All @@ -97,8 +99,7 @@ protected function setProtocolVersion($curlHandle, RequestInterface $request): s
return $this;
}

/** @param resource $curlHandle */
protected function setMethod($curlHandle, RequestInterface $request): self
protected function setMethod(\CurlHandle $curlHandle, RequestInterface $request): self
{
$method = $request->getMethod();
if ($method === 'HEAD') {
Expand All @@ -112,20 +113,15 @@ protected function setMethod($curlHandle, RequestInterface $request): self
return $this;
}

/** @param resource $curlHandle */
protected function setUrl($curlHandle, RequestInterface $request): self
protected function setUrl(\CurlHandle $curlHandle, RequestInterface $request): self
{
\curl_setopt($curlHandle, \CURLOPT_URL, $request->getUri()->__toString());

return $this;
}

/**
* @param resource $curlHandle
*
* @throws \RuntimeException
*/
protected function setBody($curlHandle, RequestInterface $request): self
/** @throws \RuntimeException */
protected function setBody(\CurlHandle $curlHandle, RequestInterface $request): self
{
$body = $request->getBody();
$bodySize = $body->getSize();
Expand All @@ -150,8 +146,7 @@ protected function setBody($curlHandle, RequestInterface $request): self
return $this;
}

/** @param resource $curlHandle */
protected function setHeaders($curlHandle, RequestInterface $request): self
protected function setHeaders(\CurlHandle $curlHandle, RequestInterface $request): self
{
$headersCurl = [];

Expand All @@ -167,8 +162,7 @@ protected function setHeaders($curlHandle, RequestInterface $request): self
return $this;
}

/** @param resource $curlHandle */
protected function setSsl($curlHandle, RequestInterface $request): self
protected function setSsl(\CurlHandle $curlHandle, RequestInterface $request): self
{
if ($request->getUri()->getScheme() === 'https') {
if ($this->CAInfosPath['info'] !== null) {
Expand All @@ -189,12 +183,10 @@ protected function setSsl($curlHandle, RequestInterface $request): self
}

/**
* @param resource $curlHandle
*
* @throws NetworkException
* @throws RequestException
*/
protected function sendCurlSimple($curlHandle, RequestInterface $request): array
protected function sendCurlSimple(\CurlHandle $curlHandle, RequestInterface $request): array
{
$data = \curl_exec($curlHandle);

Expand All @@ -212,12 +204,10 @@ protected function sendCurlSimple($curlHandle, RequestInterface $request): array
}

/**
* @param resource $curlHandle
*
* @throws NetworkException
* @throws RequestException
*/
protected function parseCurlSimpleError($curlHandle, RequestInterface $request): void
protected function parseCurlSimpleError(\CurlHandle $curlHandle, RequestInterface $request): void
{
$errno = \curl_errno($curlHandle);

Expand Down
13 changes: 3 additions & 10 deletions src/Message/Factory/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
use Rancoud\Http\Message\UploadedFile;
use Rancoud\Http\Message\Uri;

/**
* Class Factory.
*/
class Factory implements RequestFactoryInterface, ResponseFactoryInterface, ServerRequestFactoryInterface, StreamFactoryInterface, UploadedFileFactoryInterface, UriFactoryInterface
{
/**
Expand All @@ -49,7 +46,7 @@ public function createResponse(int $code = 200, string $reasonPhrase = ''): Resp
*
* @throws \InvalidArgumentException
*/
public function createResponseBody(int $code = 200, $body = null): Response
public function createResponseBody(int $code = 200, mixed $body = null): Response
{
return new Response($code, [], $body, '1.1');
}
Expand Down Expand Up @@ -255,12 +252,8 @@ protected static function normalizeFiles(array $files): array
return $normalized;
}

/**
* @throws \InvalidArgumentException
*
* @return array|UploadedFile
*/
protected static function createUploadedFileFromSpec(array $value)
/** @throws \InvalidArgumentException */
protected static function createUploadedFileFromSpec(array $value): array|UploadedFile
{
if (\is_array($value['tmp_name'])) {
return static::normalizeNestedFileSpec($value);
Expand Down
11 changes: 2 additions & 9 deletions src/Message/MessageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

use Psr\Http\Message\StreamInterface;

/**
* Trait MessageTrait.
*/
trait MessageTrait
{
protected static string $patternHeaderName = "@^[!#$%&'*+.^_`|~0-9A-Za-z-]+$@";
Expand Down Expand Up @@ -176,12 +173,8 @@ protected function setHeaders(array $headers): void
}
}

/**
* @param array|string $header
*
* @throws \InvalidArgumentException
*/
protected function validateAndTrimHeader($header, $values): array
/** @throws \InvalidArgumentException */
protected function validateAndTrimHeader(array|string $header, $values): array
{
if (!\is_string($header) || \preg_match(static::$patternHeaderName, $header) !== 1) {
throw new \InvalidArgumentException('Header name must be RFC 7230 compatible string.');
Expand Down
8 changes: 2 additions & 6 deletions src/Message/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,21 @@
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;

/**
* Class Request.
*/
class Request implements RequestInterface
{
use MessageTrait;
use RequestTrait;

/**
* @param string|UriInterface $uri
* @param resource|StreamInterface|string|null $body
*
* @throws \InvalidArgumentException
*/
public function __construct(
string $method,
$uri,
string|UriInterface $uri,
array $headers = [],
$body = null,
mixed $body = null,
string $version = '1.1'
) {
$this->method = $this->filterMethod($method);
Expand Down
3 changes: 0 additions & 3 deletions src/Message/RequestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

use Psr\Http\Message\UriInterface;

/**
* Trait RequestTrait.
*/
trait RequestTrait
{
public static array $methods = [
Expand Down
7 changes: 2 additions & 5 deletions src/Message/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;

/**
* Class Response.
*/
class Response implements ResponseInterface
{
use MessageTrait;

/** @var string[] */
public const PHRASES = [
public const array PHRASES = [
100 => 'Continue',
101 => 'Switching Protocols',
102 => 'Processing',
Expand Down Expand Up @@ -188,7 +185,7 @@ class Response implements ResponseInterface
public function __construct(
int $status = 200,
array $headers = [],
$body = null,
mixed $body = null,
string $version = '1.1',
?string $reason = null
) {
Expand Down
19 changes: 5 additions & 14 deletions src/Message/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
use Psr\Http\Message\UploadedFileInterface;
use Psr\Http\Message\UriInterface;

/**
* Class ServerRequest.
*/
class ServerRequest implements ServerRequestInterface
{
use MessageTrait;
Expand All @@ -21,8 +18,7 @@ class ServerRequest implements ServerRequestInterface

protected array $cookieParams = [];

/** @var array|object|null */
protected $parsedBody;
protected array|object|null $parsedBody = null;

protected array $queryParams = [];

Expand All @@ -32,16 +28,15 @@ class ServerRequest implements ServerRequestInterface
protected array $uploadedFiles = [];

/**
* @param string|UriInterface $uri
* @param resource|StreamInterface|string|null $body
*
* @throws \InvalidArgumentException
*/
public function __construct(
string $method,
$uri,
string|UriInterface $uri,
array $headers = [],
$body = null,
mixed $body = null,
string $version = '1.1',
array $serverParams = []
) {
Expand Down Expand Up @@ -150,9 +145,7 @@ public function getAttribute(string $name, $default = null)
return $this->attributes[$name];
}

/**
* @throws \InvalidArgumentException
*/
/** @throws \InvalidArgumentException */
public function withAttribute(string $name, $value): self
{
$new = clone $this;
Expand All @@ -174,9 +167,7 @@ public function withoutAttribute(string $name): self
return $new;
}

/**
* @throws \InvalidArgumentException
*/
/** @throws \InvalidArgumentException */
protected function validateData($data): void
{
if (!\is_array($data) && !\is_object($data) && $data !== null) {
Expand Down
9 changes: 3 additions & 6 deletions src/Message/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@

use Psr\Http\Message\StreamInterface;

/**
* Class Stream.
*/
class Stream implements StreamInterface
{
/** @var array */
protected const READ_WRITE_HASH = [
protected const array READ_WRITE_HASH = [
'read' => [
'r' => true, 'w+' => true, 'r+' => true, 'x+' => true, 'c+' => true,
'rb' => true, 'w+b' => true, 'r+b' => true, 'x+b' => true,
Expand Down Expand Up @@ -284,7 +281,7 @@ public function getMetadata(?string $key = null)
*
* @throws \InvalidArgumentException
*/
public static function create($content = ''): StreamInterface
public static function create(mixed $content = ''): StreamInterface
{
if ($content instanceof StreamInterface) {
return $content;
Expand Down Expand Up @@ -336,7 +333,7 @@ public static function createFromFile(string $filename, string $mode = 'r'): Str
try {
$resource = \fopen($filename, $mode);
// @codeCoverageIgnoreStart
} catch (\Throwable $e) {
} catch (\Throwable) {
// Could not reach this statement without mocking the filesystem
throw new \RuntimeException(\sprintf('The file %s cannot be opened.', $filename));
// @codeCoverageIgnoreEnd
Expand Down
13 changes: 4 additions & 9 deletions src/Message/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;

/**
* Class UploadedFile.
*/
class UploadedFile implements UploadedFileInterface
{
/** @var int[] */
protected const ERRORS = [
protected const array ERRORS = [
\UPLOAD_ERR_OK => 1,
\UPLOAD_ERR_INI_SIZE => 1,
\UPLOAD_ERR_FORM_SIZE => 1,
Expand All @@ -25,7 +22,7 @@ class UploadedFile implements UploadedFileInterface
];

/** @var int */
protected const DEFAULT_MAX_BYTES_LENGTH = 1048576;
protected const int DEFAULT_MAX_BYTES_LENGTH = 1048576;

protected ?string $clientFilename;

Expand All @@ -47,7 +44,7 @@ class UploadedFile implements UploadedFileInterface
* @throws \InvalidArgumentException
*/
public function __construct(
$streamOrFile,
mixed $streamOrFile,
?int $size,
int $errorStatus,
?string $clientFilename = null,
Expand Down Expand Up @@ -140,9 +137,7 @@ public function getFilename(): ?string
return $this->file;
}

/**
* @throws \InvalidArgumentException
*/
/** @throws \InvalidArgumentException */
protected function setStreamOrFile($streamOrFile): void
{
if (\is_string($streamOrFile)) {
Expand Down
Loading