Skip to content

Commit

Permalink
update types and simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
rancoud committed Dec 6, 2024
1 parent d1d498d commit 7ad9551
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 136 deletions.
30 changes: 15 additions & 15 deletions src/Message/MessageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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);
}
}
46 changes: 10 additions & 36 deletions src/Message/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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');
}
Expand All @@ -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
*
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down
34 changes: 17 additions & 17 deletions src/Message/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
68 changes: 0 additions & 68 deletions tests/UploadedFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
];
Expand All @@ -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');
Expand Down

0 comments on commit 7ad9551

Please sign in to comment.