From 8e341e0788b87eea0ca994916cba9fcf4193e474 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Rancoud?= Date: Mon, 21 Apr 2025 01:33:26 +0200 Subject: [PATCH 1/2] chore: clean files for PHP 8.4 --- src/Client/Client.php | 43 ++++++++++++--------------------- src/Message/Factory/Factory.php | 13 +++------- src/Message/MessageTrait.php | 11 ++------- src/Message/Request.php | 8 ++---- src/Message/RequestTrait.php | 3 --- src/Message/Response.php | 7 ++---- src/Message/ServerRequest.php | 19 ++++----------- src/Message/Stream.php | 9 +++---- src/Message/UploadedFile.php | 13 +++------- src/Message/Uri.php | 9 +++---- tests/ClientTest.php | 4 +-- tests/FactoryTest.php | 7 +----- tests/RequestTest.php | 4 +-- tests/ResponseTest.php | 4 +-- tests/ServerRequestTest.php | 4 +-- tests/StreamTest.php | 6 ++--- tests/UploadedFileTest.php | 4 +-- tests/UriTest.php | 6 ++--- 18 files changed, 49 insertions(+), 125 deletions(-) diff --git a/src/Client/Client.php b/src/Client/Client.php index f7856aa..68a523d 100644 --- a/src/Client/Client.php +++ b/src/Client/Client.php @@ -62,15 +62,15 @@ 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(); + if ($curlHandle === false) { + throw new \RuntimeException(); + } + \curl_setopt($curlHandle, \CURLOPT_RETURNTRANSFER, true); \curl_setopt($curlHandle, \CURLOPT_HEADER, true); @@ -84,8 +84,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(); @@ -97,8 +96,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') { @@ -112,20 +110,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(); @@ -150,8 +143,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 = []; @@ -167,8 +159,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) { @@ -189,12 +180,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); @@ -212,12 +201,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); diff --git a/src/Message/Factory/Factory.php b/src/Message/Factory/Factory.php index 3d478c4..98bb76a 100644 --- a/src/Message/Factory/Factory.php +++ b/src/Message/Factory/Factory.php @@ -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 { /** @@ -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'); } @@ -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); diff --git a/src/Message/MessageTrait.php b/src/Message/MessageTrait.php index 289e30b..a72abc8 100644 --- a/src/Message/MessageTrait.php +++ b/src/Message/MessageTrait.php @@ -6,9 +6,6 @@ use Psr\Http\Message\StreamInterface; -/** - * Trait MessageTrait. - */ trait MessageTrait { protected static string $patternHeaderName = "@^[!#$%&'*+.^_`|~0-9A-Za-z-]+$@"; @@ -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.'); diff --git a/src/Message/Request.php b/src/Message/Request.php index fa825ee..66fd40e 100644 --- a/src/Message/Request.php +++ b/src/Message/Request.php @@ -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); diff --git a/src/Message/RequestTrait.php b/src/Message/RequestTrait.php index dcdb27e..dbff1c2 100644 --- a/src/Message/RequestTrait.php +++ b/src/Message/RequestTrait.php @@ -6,9 +6,6 @@ use Psr\Http\Message\UriInterface; -/** - * Trait RequestTrait. - */ trait RequestTrait { public static array $methods = [ diff --git a/src/Message/Response.php b/src/Message/Response.php index 4c9f2c2..dc37434 100644 --- a/src/Message/Response.php +++ b/src/Message/Response.php @@ -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', @@ -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 ) { diff --git a/src/Message/ServerRequest.php b/src/Message/ServerRequest.php index 38c001e..0131f2e 100644 --- a/src/Message/ServerRequest.php +++ b/src/Message/ServerRequest.php @@ -9,9 +9,6 @@ use Psr\Http\Message\UploadedFileInterface; use Psr\Http\Message\UriInterface; -/** - * Class ServerRequest. - */ class ServerRequest implements ServerRequestInterface { use MessageTrait; @@ -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 = []; @@ -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 = [] ) { @@ -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; @@ -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) { diff --git a/src/Message/Stream.php b/src/Message/Stream.php index 0b3c156..69e9d0f 100644 --- a/src/Message/Stream.php +++ b/src/Message/Stream.php @@ -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, @@ -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; @@ -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 diff --git a/src/Message/UploadedFile.php b/src/Message/UploadedFile.php index d143175..c8d0163 100644 --- a/src/Message/UploadedFile.php +++ b/src/Message/UploadedFile.php @@ -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, @@ -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; @@ -47,7 +44,7 @@ class UploadedFile implements UploadedFileInterface * @throws \InvalidArgumentException */ public function __construct( - $streamOrFile, + mixed $streamOrFile, ?int $size, int $errorStatus, ?string $clientFilename = null, @@ -140,9 +137,7 @@ public function getFilename(): ?string return $this->file; } - /** - * @throws \InvalidArgumentException - */ + /** @throws \InvalidArgumentException */ protected function setStreamOrFile($streamOrFile): void { if (\is_string($streamOrFile)) { diff --git a/src/Message/Uri.php b/src/Message/Uri.php index 307c4b2..66b1a6c 100644 --- a/src/Message/Uri.php +++ b/src/Message/Uri.php @@ -6,22 +6,19 @@ use Psr\Http\Message\UriInterface; -/** - * Class Uri. - */ class Uri implements UriInterface { /** @var array */ - protected const SCHEMES = [ + protected const array SCHEMES = [ 'http' => 80, 'https' => 443, ]; /** @var string */ - protected const CHAR_UNRESERVED = 'a-zA-Z0-9_\-\.~'; + protected const string CHAR_UNRESERVED = 'a-zA-Z0-9_\-\.~'; /** @var string */ - protected const CHAR_SUB_DELIMS = '!\$&\'\(\)\*\+,;='; + protected const string CHAR_SUB_DELIMS = '!\$&\'\(\)\*\+,;='; protected string $scheme = ''; diff --git a/tests/ClientTest.php b/tests/ClientTest.php index c4ad112..1557c09 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -13,9 +13,7 @@ use Rancoud\Http\Message\Stream; use Symfony\Component\Process\Process; -/** - * @internal - */ +/** @internal */ class ClientTest extends TestCase { protected static Process $process; diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index 0bab77a..0c40615 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -10,9 +10,7 @@ use Rancoud\Http\Message\Factory\Factory; use Rancoud\Http\Message\Uri; -/** - * @internal - */ +/** @internal */ class FactoryTest extends TestCase { public function testCreateRequest(): void @@ -72,7 +70,6 @@ public function testCreateServerRequestFromArrayRaiseExceptionMethod(): void (new Factory())->createServerRequestFromArray($_SERVER); } - /** @runInSeparateProcess */ #[RunInSeparateProcess] public function testCreateServerRequestFromGlobalsWithRequestMethod(): void { @@ -81,7 +78,6 @@ public function testCreateServerRequestFromGlobalsWithRequestMethod(): void static::assertSame('POST', $r->getMethod()); } - /** @runInSeparateProcess */ #[RunInSeparateProcess] public function testCreateServerRequestFromGlobalsWithoutRequestMethod(): void { @@ -89,7 +85,6 @@ public function testCreateServerRequestFromGlobalsWithoutRequestMethod(): void static::assertSame('GET', $r->getMethod()); } - /** @runInSeparateProcess */ #[RunInSeparateProcess] public function testCreateServerRequestFromGlobalsFakeNginx(): void { diff --git a/tests/RequestTest.php b/tests/RequestTest.php index ea0e4fe..fdfc805 100644 --- a/tests/RequestTest.php +++ b/tests/RequestTest.php @@ -9,9 +9,7 @@ use Rancoud\Http\Message\Request; use Rancoud\Http\Message\Uri; -/** - * @internal - */ +/** @internal */ class RequestTest extends TestCase { public function testRequestUriMayBeString(): void diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php index b123d80..fe7d12e 100644 --- a/tests/ResponseTest.php +++ b/tests/ResponseTest.php @@ -12,9 +12,7 @@ use Rancoud\Http\Message\Factory\Factory; use Rancoud\Http\Message\Response; -/** - * @internal - */ +/** @internal */ #[\PHPUnit\Framework\Attributes\BackupGlobals(false)] #[PreserveGlobalState(false)] class ResponseTest extends TestCase diff --git a/tests/ServerRequestTest.php b/tests/ServerRequestTest.php index 6d3f566..d9fa350 100644 --- a/tests/ServerRequestTest.php +++ b/tests/ServerRequestTest.php @@ -11,9 +11,7 @@ use Rancoud\Http\Message\UploadedFile; use Rancoud\Http\Message\Uri; -/** - * @internal - */ +/** @internal */ class ServerRequestTest extends TestCase { public function testConstruct(): void diff --git a/tests/StreamTest.php b/tests/StreamTest.php index cddef3d..fb8bd64 100644 --- a/tests/StreamTest.php +++ b/tests/StreamTest.php @@ -7,9 +7,7 @@ use PHPUnit\Framework\TestCase; use Rancoud\Http\Message\Stream; -/** - * @internal - */ +/** @internal */ class StreamTest extends TestCase { public function testConstructorInitializesProperties(): void @@ -136,7 +134,7 @@ public function testCanDetachStream(): void try { $fn($stream); static::fail(); - } catch (\Exception $e) { + } catch (\Exception) { } }; diff --git a/tests/UploadedFileTest.php b/tests/UploadedFileTest.php index 9d6bea6..12e7d83 100644 --- a/tests/UploadedFileTest.php +++ b/tests/UploadedFileTest.php @@ -9,9 +9,7 @@ use Rancoud\Http\Message\Stream; use Rancoud\Http\Message\UploadedFile; -/** - * @internal - */ +/** @internal */ class UploadedFileTest extends TestCase { protected array $cleanup; diff --git a/tests/UriTest.php b/tests/UriTest.php index 584de28..630c75b 100644 --- a/tests/UriTest.php +++ b/tests/UriTest.php @@ -8,12 +8,10 @@ use PHPUnit\Framework\TestCase; use Rancoud\Http\Message\Uri; -/** - * @internal - */ +/** @internal */ class UriTest extends TestCase { - public const RFC3986_BASE = 'https://a/b/c/d;p?q'; + public const string RFC3986_BASE = 'https://a/b/c/d;p?q'; public function testParsesProvidedUri(): void { From a2a4a7ea1ca83a018068e4f89b0d796db6737b6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Rancoud?= Date: Mon, 21 Apr 2025 01:41:54 +0200 Subject: [PATCH 2/2] f --- src/Client/Client.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Client/Client.php b/src/Client/Client.php index 68a523d..bfd39b7 100644 --- a/src/Client/Client.php +++ b/src/Client/Client.php @@ -67,9 +67,12 @@ 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);