From 65ad2ebfa45a97635ef8676c972458bc63ad98b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Rancoud?= Date: Fri, 6 Dec 2024 22:20:55 +0100 Subject: [PATCH] update signature and remove type checkers --- src/Message/RequestTrait.php | 4 ++-- src/Message/Stream.php | 36 +++++--------------------------- tests/ResponseTest.php | 9 ++++++++ tests/StreamTest.php | 40 ------------------------------------ 4 files changed, 16 insertions(+), 73 deletions(-) diff --git a/src/Message/RequestTrait.php b/src/Message/RequestTrait.php index cbe1e73..a625d8a 100644 --- a/src/Message/RequestTrait.php +++ b/src/Message/RequestTrait.php @@ -96,13 +96,13 @@ public function getRequestTarget(): string } /** - * @param $requestTarget + * @param string $requestTarget * * @throws \InvalidArgumentException * * @return self */ - public function withRequestTarget($requestTarget): self + public function withRequestTarget(string $requestTarget): self { if (\preg_match('#\s#', $requestTarget)) { throw new \InvalidArgumentException('Invalid request target provided; cannot contain whitespace'); diff --git a/src/Message/Stream.php b/src/Message/Stream.php index d0008de..94c1a22 100644 --- a/src/Message/Stream.php +++ b/src/Message/Stream.php @@ -162,16 +162,8 @@ public function isSeekable(): bool * @throws \InvalidArgumentException * @throws \RuntimeException */ - public function seek($offset, $whence = \SEEK_SET): void + public function seek(int $offset, int $whence = SEEK_SET): void { - if (!\is_int($offset)) { - throw new \InvalidArgumentException('Offset must be a int'); - } - - if (!\is_int($whence)) { - throw new \InvalidArgumentException('Whence must be a int'); - } - if (!$this->seekable) { throw new \RuntimeException('Stream is not seekable'); } @@ -237,19 +229,15 @@ public function isReadable(): bool } /** - * @param $length + * @param int $length * * @throws \InvalidArgumentException * @throws \RuntimeException * * @return string */ - public function read($length): string + public function read(int $length): string { - if (!\is_int($length)) { - throw new \InvalidArgumentException('Length must be a int'); - } - if (!$this->readable) { throw new \RuntimeException('Cannot read from non-readable stream'); } @@ -314,14 +302,10 @@ public function getContents(): string * * @throws \InvalidArgumentException * - * @return array|null + * @return array|mixed|null */ - public function getMetadata($key = null) + public function getMetadata(?string $key = null) { - if (!$this->isStringOrNull($key)) { - throw new \InvalidArgumentException('Key must be a string or NULL'); - } - if (!isset($this->stream)) { return $key ? null : []; } @@ -424,14 +408,4 @@ public function __destruct() { $this->close(); } - - /** - * @param $param - * - * @return bool - */ - protected function isStringOrNull($param): bool - { - return $param === null || \is_string($param); - } } diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php index 03a322d..cd61609 100644 --- a/tests/ResponseTest.php +++ b/tests/ResponseTest.php @@ -120,6 +120,15 @@ public function testRaiseExceptionConstructWithProtocolVersion(): void new Response(200, [], null, '1000'); } + public function testRaiseWithInvalidStatusCode(): void + { + $r = new Response(); + + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Status code has to be an integer between 100 and 799'); + $r->withStatus(1); + } + public function testWithStatusCodeAndNoReason(): void { $r = (new Response())->withStatus(201); diff --git a/tests/StreamTest.php b/tests/StreamTest.php index b08f504..7844f9b 100644 --- a/tests/StreamTest.php +++ b/tests/StreamTest.php @@ -179,26 +179,6 @@ public function testCloseClearProperties(): void static::assertNull($stream->getMetadata('key')); } - public function testSeekRaiseExceptionOffset(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Offset must be a int'); - - $handle = \fopen('php://temp', 'w+'); - $stream = Stream::create($handle); - $stream->seek('string'); - } - - public function testSeekRaiseExceptionWhence(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Whence must be a int'); - - $handle = \fopen('php://temp', 'w+'); - $stream = Stream::create($handle); - $stream->seek(1, 'string'); - } - public function testSeekRaiseExceptionUnableToSeek(): void { $this->expectException(\RuntimeException::class); @@ -209,26 +189,6 @@ public function testSeekRaiseExceptionUnableToSeek(): void $stream->seek(1, 90909090); } - public function testReadRaiseExceptionLength(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Length must be a int'); - - $handle = \fopen('php://temp', 'w+'); - $stream = Stream::create($handle); - $stream->read('string'); - } - - public function testGetMetadataRaiseExceptionKey(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Key must be a string or NULL'); - - $handle = \fopen('php://temp', 'w+'); - $stream = Stream::create($handle); - $stream->getMetadata(45); - } - public function testGetMetadataKeyNonExist(): void { $handle = \fopen('php://temp', 'w+');