Skip to content

Commit

Permalink
update signature and remove type checkers
Browse files Browse the repository at this point in the history
  • Loading branch information
rancoud committed Dec 6, 2024
1 parent 00bc212 commit 65ad2eb
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 73 deletions.
4 changes: 2 additions & 2 deletions src/Message/RequestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
36 changes: 5 additions & 31 deletions src/Message/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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 : [];
}
Expand Down Expand Up @@ -424,14 +408,4 @@ public function __destruct()
{
$this->close();
}

/**
* @param $param
*
* @return bool
*/
protected function isStringOrNull($param): bool
{
return $param === null || \is_string($param);
}
}
9 changes: 9 additions & 0 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
40 changes: 0 additions & 40 deletions tests/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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+');
Expand Down

0 comments on commit 65ad2eb

Please sign in to comment.