From 7ad95511c4fe82226bd953152a5e04db12d3395c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Rancoud?= Date: Fri, 6 Dec 2024 23:35:58 +0100 Subject: [PATCH] update types and simplify --- src/Message/MessageTrait.php | 30 ++++++++-------- src/Message/UploadedFile.php | 46 ++++++------------------ src/Message/Uri.php | 34 +++++++++--------- tests/UploadedFileTest.php | 68 ------------------------------------ 4 files changed, 42 insertions(+), 136 deletions(-) diff --git a/src/Message/MessageTrait.php b/src/Message/MessageTrait.php index fd1eadb..317d4da 100644 --- a/src/Message/MessageTrait.php +++ b/src/Message/MessageTrait.php @@ -232,8 +232,8 @@ protected function setHeaders(array $headers): void } /** - * @param string $header - * @param mixed $values + * @param string|array $header + * @param mixed $values * * @throws \InvalidArgumentException * @@ -270,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); } } diff --git a/src/Message/UploadedFile.php b/src/Message/UploadedFile.php index d930dfa..fa44ead 100644 --- a/src/Message/UploadedFile.php +++ b/src/Message/UploadedFile.php @@ -43,7 +43,7 @@ class UploadedFile implements UploadedFileInterface /** * @param StreamInterface|string|resource $streamOrFile - * @param int $size + * @param int|null $size * @param int $errorStatus * @param string|null $clientFilename * @param string|null $clientMediaType @@ -52,10 +52,10 @@ class UploadedFile implements UploadedFileInterface */ public function __construct( $streamOrFile, - $size, - $errorStatus, - $clientFilename = null, - $clientMediaType = null + ?int $size, + int $errorStatus, + ?string $clientFilename = null, + ?string $clientMediaType = null ) { $this->setError($errorStatus); $this->setSize($size); @@ -187,12 +187,8 @@ protected function setStreamOrFile($streamOrFile): void * * @throws \InvalidArgumentException */ - protected function setError($error): void + protected function setError(int $error): void { - if (!\is_int($error)) { - throw new \InvalidArgumentException('Upload file error status must be an integer'); - } - if (!isset(static::ERRORS[$error])) { throw new \InvalidArgumentException('Invalid error status for UploadedFile'); } @@ -201,29 +197,15 @@ protected function setError($error): void } /** - * @param int $size + * @param int|null $size * * @throws \InvalidArgumentException */ - protected function setSize($size): void + protected function setSize(?int $size): void { - if (!\is_int($size)) { - throw new \InvalidArgumentException('Upload file size must be an integer'); - } - $this->size = $size; } - /** - * @param $param - * - * @return bool - */ - protected function isStringOrNull($param): bool - { - return $param === null || \is_string($param); - } - /** * @param $param * @@ -239,12 +221,8 @@ protected function isStringNotEmpty($param): bool * * @throws \InvalidArgumentException */ - protected function setClientFilename($clientFilename): void + protected function setClientFilename(?string $clientFilename): void { - if (!$this->isStringOrNull($clientFilename)) { - throw new \InvalidArgumentException('Upload file client filename must be a string or null'); - } - $this->clientFilename = $clientFilename; } @@ -253,12 +231,8 @@ protected function setClientFilename($clientFilename): void * * @throws \InvalidArgumentException */ - protected function setClientMediaType($clientMediaType): void + protected function setClientMediaType(?string $clientMediaType): void { - if (!$this->isStringOrNull($clientMediaType)) { - throw new \InvalidArgumentException('Upload file client media type must be a string or null'); - } - $this->clientMediaType = $clientMediaType; } diff --git a/src/Message/Uri.php b/src/Message/Uri.php index d87d6ef..7b75a23 100644 --- a/src/Message/Uri.php +++ b/src/Message/Uri.php @@ -401,23 +401,6 @@ protected static function createUriString( return $uri; } - /** - * Because of PHP 8.4. - * - * @param $string - * @param string $characters - * - * @return string - */ - protected static function ltrim($string, string $characters = " \n\r\t\v\0"): string - { - if (\PHP_MAJOR_VERSION >= 8 && \PHP_MINOR_VERSION >= 4) { - return \mb_ltrim((string) $string, $characters); - } - - return \ltrim((string) $string, $characters); - } - /** * @param string $scheme * @param int $port @@ -558,4 +541,21 @@ protected static function getPatternForFilteringQueryAndFragment(): string { return '/(?:[^' . static::CHAR_UNRESERVED . static::CHAR_SUB_DELIMS . '%:@\/\?]++|%(?![A-Fa-f0-9]{2}))/'; } + + /** + * Because of PHP 8.4. + * + * @param $string + * @param string $characters + * + * @return string + */ + protected static function ltrim($string, string $characters = " \n\r\t\v\0"): string + { + if (\PHP_MAJOR_VERSION >= 8 && \PHP_MINOR_VERSION >= 4) { + return \mb_ltrim((string) $string, $characters); + } + + return \ltrim((string) $string, $characters); + } } diff --git a/tests/UploadedFileTest.php b/tests/UploadedFileTest.php index a883d17..c109f6d 100644 --- a/tests/UploadedFileTest.php +++ b/tests/UploadedFileTest.php @@ -50,39 +50,9 @@ public function testRaisesExceptionOnInvalidStreamOrFile($streamOrFile): void new UploadedFile($streamOrFile, 0, \UPLOAD_ERR_OK); } - public function invalidSizes(): array - { - return [ - 'null' => [null], - 'float' => [1.1], - 'array' => [[1]], - 'object' => [(object) [1]], - ]; - } - - /** - * @dataProvider invalidSizes - * - * @param $size - */ - public function testRaisesExceptionOnInvalidSize($size): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('size'); - - new UploadedFile(\fopen('php://temp', 'wb+'), $size, \UPLOAD_ERR_OK); - } - public function invalidErrorStatuses(): array { return [ - 'null' => [null], - 'true' => [true], - 'false' => [false], - 'float' => [1.1], - 'string' => ['1'], - 'array' => [[1]], - 'object' => [(object) [1]], 'negative' => [-1], 'too-big' => [9], ]; @@ -101,44 +71,6 @@ public function testRaisesExceptionOnInvalidErrorStatus($status): void new UploadedFile(\fopen('php://temp', 'wb+'), 0, $status); } - public function invalidFilenamesAndMediaTypes(): array - { - return [ - 'true' => [true], - 'false' => [false], - 'int' => [1], - 'float' => [1.1], - 'array' => [['string']], - 'object' => [(object) ['string']], - ]; - } - - /** - * @dataProvider invalidFilenamesAndMediaTypes - * - * @param $filename - */ - public function testRaisesExceptionOnInvalidClientFilename($filename): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('filename'); - - new UploadedFile(\fopen('php://temp', 'wb+'), 0, \UPLOAD_ERR_OK, $filename); - } - - /** - * @dataProvider invalidFilenamesAndMediaTypes - * - * @param $mediaType - */ - public function testRaisesExceptionOnInvalidClientMediaType($mediaType): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('media type'); - - new UploadedFile(\fopen('php://temp', 'wb+'), 0, \UPLOAD_ERR_OK, 'foobar.baz', $mediaType); - } - public function testGetStreamWithFile(): void { $stream = Stream::createFromFile(__DIR__ . \DIRECTORY_SEPARATOR . 'noise.jpg');