diff --git a/src/Message/Factory/Factory.php b/src/Message/Factory/Factory.php index f4e75c1..b0b79ad 100644 --- a/src/Message/Factory/Factory.php +++ b/src/Message/Factory/Factory.php @@ -182,7 +182,7 @@ public function createUriFromArray(array $server): Uri } if (isset($server['SERVER_PORT'])) { - $uri = $uri->withPort($server['SERVER_PORT']); + $uri = $uri->withPort((int) $server['SERVER_PORT']); } if (isset($server['REQUEST_URI'])) { diff --git a/src/Message/MessageTrait.php b/src/Message/MessageTrait.php index 6094af1..fd1eadb 100644 --- a/src/Message/MessageTrait.php +++ b/src/Message/MessageTrait.php @@ -40,7 +40,7 @@ public function getProtocolVersion(): string * * @return self */ - public function withProtocolVersion($version): self + public function withProtocolVersion(string $version): self { $this->validateProtocolVersion($version); @@ -69,12 +69,8 @@ public function getHeaders(): array * * @return bool */ - public function hasHeader($name): bool + public function hasHeader(string $name): bool { - if (!\is_string($name)) { - throw new \InvalidArgumentException('Header name must be a string'); - } - return isset($this->headerNames[\mb_strtolower($name)]); } @@ -85,12 +81,8 @@ public function hasHeader($name): bool * * @return array */ - public function getHeader($name): array + public function getHeader(string $name): array { - if (!\is_string($name)) { - throw new \InvalidArgumentException('Header name must be a string'); - } - $name = \mb_strtolower($name); if (!isset($this->headerNames[$name])) { @@ -109,7 +101,7 @@ public function getHeader($name): array * * @return string */ - public function getHeaderLine($name): string + public function getHeaderLine(string $name): string { return \implode(', ', $this->getHeader($name)); } @@ -122,9 +114,9 @@ public function getHeaderLine($name): string * * @return self */ - public function withHeader($name, $value): self + public function withHeader(string $name, $value): self { - if (!\is_string($name) || $name === '') { + if ($name === '') { throw new \InvalidArgumentException('Header name must be non-empty string'); } @@ -149,9 +141,9 @@ public function withHeader($name, $value): self * * @return self */ - public function withAddedHeader($name, $value): self + public function withAddedHeader(string $name, $value): self { - if (!\is_string($name) || $name === '') { + if ($name === '') { throw new \InvalidArgumentException('Header name must be non-empty string'); } @@ -168,9 +160,9 @@ public function withAddedHeader($name, $value): self * * @return self */ - public function withoutHeader($name): self + public function withoutHeader(string $name): self { - if (!\is_string($name) || $name === '') { + if ($name === '') { throw new \InvalidArgumentException('Header name must be non-empty string'); } diff --git a/src/Message/RequestTrait.php b/src/Message/RequestTrait.php index b53db83..cbe1e73 100644 --- a/src/Message/RequestTrait.php +++ b/src/Message/RequestTrait.php @@ -129,7 +129,7 @@ public function getMethod(): string * * @return self */ - public function withMethod($method): self + public function withMethod(string $method): self { $method = $this->filterMethod($method); @@ -155,12 +155,8 @@ public function getUri(): UriInterface * * @return self */ - public function withUri(UriInterface $uri, $preserveHost = false): self + public function withUri(UriInterface $uri, bool $preserveHost = false): self { - if (!\is_bool($preserveHost)) { - throw new \InvalidArgumentException('Preserve Host must be a boolean'); - } - if ($this->uri === $uri) { return $this; } @@ -182,12 +178,8 @@ public function withUri(UriInterface $uri, $preserveHost = false): self * * @return string */ - protected function filterMethod($method): string + protected function filterMethod(string $method): string { - if (!\is_string($method)) { - throw new \InvalidArgumentException('Method must be a string'); - } - if (!\in_array($method, static::$methods, true)) { throw new \InvalidArgumentException(\sprintf('Method %s is invalid', $method)); } diff --git a/src/Message/Response.php b/src/Message/Response.php index 6d11e20..8fc5067 100644 --- a/src/Message/Response.php +++ b/src/Message/Response.php @@ -233,12 +233,8 @@ public function getStatusCode(): int * * @return Response */ - public function withStatus($code, $reasonPhrase = ''): self + public function withStatus(int $code, string $reasonPhrase = ''): self { - if (!\is_int($code)) { - throw new \InvalidArgumentException('Status code has to be an integer'); - } - if (!isset(static::PHRASES[$code])) { throw new \InvalidArgumentException('Status code has to be an integer between 100 and 799'); } diff --git a/src/Message/ServerRequest.php b/src/Message/ServerRequest.php index dcdbaad..fcc81d9 100644 --- a/src/Message/ServerRequest.php +++ b/src/Message/ServerRequest.php @@ -182,12 +182,8 @@ public function getAttributes(): array * * @return mixed|null */ - public function getAttribute($name, $default = null) + public function getAttribute(string $name, $default = null) { - if (!\is_string($name)) { - throw new \InvalidArgumentException('Name must be a string'); - } - if (!\array_key_exists($name, $this->attributes)) { return $default; } @@ -203,12 +199,8 @@ public function getAttribute($name, $default = null) * * @return self */ - public function withAttribute($name, $value): self + public function withAttribute(string $name, $value): self { - if (!\is_string($name)) { - throw new \InvalidArgumentException('Name must be a string'); - } - $new = clone $this; $new->attributes[$name] = $value; @@ -222,12 +214,8 @@ public function withAttribute($name, $value): self * * @return self */ - public function withoutAttribute($name): self + public function withoutAttribute(string $name): self { - if (!\is_string($name)) { - throw new \InvalidArgumentException('Name must be a string'); - } - if (!\array_key_exists($name, $this->attributes)) { return $this; } diff --git a/src/Message/Stream.php b/src/Message/Stream.php index bcdf355..d0008de 100644 --- a/src/Message/Stream.php +++ b/src/Message/Stream.php @@ -208,12 +208,8 @@ public function isWritable(): bool * * @return int */ - public function write($string) + public function write(string $string): int { - if (!\is_string($string)) { - throw new \InvalidArgumentException('Data must be a string'); - } - if (!$this->writable) { throw new \RuntimeException('Cannot write to a non-writable stream'); } diff --git a/src/Message/UploadedFile.php b/src/Message/UploadedFile.php index dd5be84..d930dfa 100644 --- a/src/Message/UploadedFile.php +++ b/src/Message/UploadedFile.php @@ -90,7 +90,7 @@ public function getStream(): StreamInterface * @throws \InvalidArgumentException * @throws \RuntimeException */ - public function moveTo($targetPath): void + public function moveTo(string $targetPath): void { $this->validateActive(); diff --git a/src/Message/Uri.php b/src/Message/Uri.php index 422e06e..d87d6ef 100644 --- a/src/Message/Uri.php +++ b/src/Message/Uri.php @@ -139,7 +139,7 @@ public function getFragment(): string * * @return self */ - public function withScheme($scheme): self + public function withScheme(string $scheme): self { $scheme = $this->filterScheme($scheme); @@ -162,7 +162,7 @@ public function withScheme($scheme): self * * @return self */ - public function withUserInfo($user, $password = null): self + public function withUserInfo(string $user, ?string $password = null): self { $info = $this->filterUser($user); $password = $this->filterPass($password); @@ -188,7 +188,7 @@ public function withUserInfo($user, $password = null): self * * @return self */ - public function withHost($host): self + public function withHost(string $host): self { $host = $this->filterHost($host); @@ -209,7 +209,7 @@ public function withHost($host): self * * @return self */ - public function withPort($port): self + public function withPort(?int $port): self { $port = $this->filterPort($port); @@ -230,7 +230,7 @@ public function withPort($port): self * * @return self */ - public function withPath($path): self + public function withPath(string $path): self { $path = $this->filterPath($path); @@ -251,7 +251,7 @@ public function withPath($path): self * * @return self */ - public function withQuery($query): self + public function withQuery(string $query): self { $query = $this->filterQueryAndFragment($query); @@ -272,7 +272,7 @@ public function withQuery($query): self * * @return self */ - public function withFragment($fragment): self + public function withFragment(string $fragment): self { $fragment = $this->filterQueryAndFragment($fragment); @@ -436,12 +436,8 @@ protected static function isNonStandardPort(string $scheme, int $port): bool * * @return string */ - protected function filterScheme($scheme): string + protected function filterScheme(string $scheme): string { - if (!\is_string($scheme)) { - throw new \InvalidArgumentException('Scheme must be a string'); - } - return \mb_strtolower($scheme); } @@ -452,12 +448,8 @@ protected function filterScheme($scheme): string * * @return string */ - protected function filterUser($user): string + protected function filterUser(string $user): string { - if (!\is_string($user)) { - throw new \InvalidArgumentException('User must be a string'); - } - return $user; } @@ -468,12 +460,8 @@ protected function filterUser($user): string * * @return string */ - protected function filterPass($pass): ?string + protected function filterPass(?string $pass): ?string { - if ($pass !== null && !\is_string($pass)) { - throw new \InvalidArgumentException('Password must be a string or NULL'); - } - return $pass; } @@ -484,12 +472,8 @@ protected function filterPass($pass): ?string * * @return string */ - protected function filterHost($host): string + protected function filterHost(string $host): string { - if (!\is_string($host)) { - throw new \InvalidArgumentException('Host must be a string'); - } - return \mb_strtolower($host); } @@ -500,13 +484,12 @@ protected function filterHost($host): string * * @return int|null */ - protected function filterPort($port): ?int + protected function filterPort(?int $port): ?int { if ($port === null) { return null; } - $port = (int) $port; if ($port < 1 || $port > 65535) { throw new \InvalidArgumentException(\sprintf('Invalid port: %d. Must be between 1 and 65535', $port)); } @@ -525,12 +508,8 @@ protected function filterPort($port): ?int * * @return string */ - protected function filterPath($path): string + protected function filterPath(string $path): string { - if (!\is_string($path)) { - throw new \InvalidArgumentException('Path must be a string'); - } - return \preg_replace_callback( static::getPatternForFilteringPath(), [__CLASS__, 'rawurlencodeMatchZero'], @@ -545,12 +524,8 @@ protected function filterPath($path): string * * @return string */ - protected function filterQueryAndFragment($str): string + protected function filterQueryAndFragment(string $str): string { - if (!\is_string($str)) { - throw new \InvalidArgumentException('Query and fragment must be a string'); - } - return \preg_replace_callback( static::getPatternForFilteringQueryAndFragment(), [__CLASS__, 'rawurlencodeMatchZero'], diff --git a/tests/RequestTest.php b/tests/RequestTest.php index 7b0a844..ba18548 100644 --- a/tests/RequestTest.php +++ b/tests/RequestTest.php @@ -189,24 +189,6 @@ public function testWithMethod(): void static::assertSame('PATCH', $r->getMethod()); } - public function testWithUriPreserveHostMustHaveCorrectType(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Preserve Host must be a boolean'); - - $r = new Request('GET', 'https://foo.com:8124/bar'); - $r->withUri(new Uri(), null); - } - - public function testWithMethodMustHaveCorrectType(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Method must be a string'); - - $r = new Request('GET', 'https://foo.com:8124/bar'); - $r->withMethod(null); - } - public function testWithMethodMustBeValidMethod(): void { $this->expectException(\InvalidArgumentException::class); diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php index 899cfb3..03a322d 100644 --- a/tests/ResponseTest.php +++ b/tests/ResponseTest.php @@ -46,17 +46,6 @@ public function testConstructStatusCantBeNumericString(): void new Response('-404.4'); } - public function testWithStatusCantBeNumericString(): void - { - $r = new Response(404); - static::assertSame(404, $r->getStatusCode()); - static::assertSame('Not Found', $r->getReasonPhrase()); - - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Status code has to be an integer'); - $r->withStatus('201'); - } - public function testCanConstructWithHeaders(): void { $r = new Response(200, ['Foo' => 'Bar']); @@ -269,33 +258,6 @@ public function trimmedHeaderValues(): array ]; } - public function testHasHeaderMustHaveCorrectType(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Header name must be a string'); - - $r = new Response(); - static::assertSame($r, $r->hasHeader(null)); - } - - public function testGetHeaderMustHaveCorrectType(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Header name must be a string'); - - $r = new Response(); - static::assertSame($r, $r->getHeader(null)); - } - - public function testGetHeaderLineMustHaveCorrectType(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Header name must be a string'); - - $r = new Response(); - static::assertSame($r, $r->getHeaderLine(null)); - } - public function testWithHeaderNameMustHaveCorrectType(): void { $this->expectException(\InvalidArgumentException::class); @@ -389,24 +351,6 @@ public function testHeaderValuesAreTrimmed(Response $r): void static::assertSame(['Foo'], $r->getHeader('OWS')); } - public function testWithStatusCodeMustHaveCorrectType(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Status code has to be an integer'); - - $r = new Response(); - $r->withStatus([]); - } - - public function testWithStatusReasonPhraseMustHaveCorrectType(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Status code has to be an integer between 100 and 799'); - - $r = new Response(); - $r->withStatus(9, []); - } - /** * @runInSeparateProcess */ diff --git a/tests/ServerRequestTest.php b/tests/ServerRequestTest.php index 4ef0aac..ea987bf 100644 --- a/tests/ServerRequestTest.php +++ b/tests/ServerRequestTest.php @@ -546,30 +546,6 @@ public function testNullAttribute(): void static::assertSame('different-default', $requestWithoutAttribute->getAttribute('name', 'different-default')); } - public function testGetAttributeMustHaveCorrectType(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Name must be a string'); - - (new ServerRequest('GET', '/'))->getAttribute([]); - } - - public function testWithAttributeMustHaveCorrectType(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Name must be a string'); - - (new ServerRequest('GET', '/'))->withAttribute([], null); - } - - public function testWithoutAttributeMustHaveCorrectType(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Name must be a string'); - - (new ServerRequest('GET', '/'))->withoutAttribute([]); - } - public function testWithParsedBodyMustHaveCorrectType(): void { $this->expectException(\InvalidArgumentException::class); diff --git a/tests/StreamTest.php b/tests/StreamTest.php index f40902b..b08f504 100644 --- a/tests/StreamTest.php +++ b/tests/StreamTest.php @@ -209,16 +209,6 @@ public function testSeekRaiseExceptionUnableToSeek(): void $stream->seek(1, 90909090); } - public function testWriteRaiseExceptionData(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Data must be a string'); - - $handle = \fopen('php://temp', 'w+'); - $stream = Stream::create($handle); - $stream->write(0); - } - public function testReadRaiseExceptionLength(): void { $this->expectException(\InvalidArgumentException::class); diff --git a/tests/UploadedFileTest.php b/tests/UploadedFileTest.php index 8a7054c..a883d17 100644 --- a/tests/UploadedFileTest.php +++ b/tests/UploadedFileTest.php @@ -184,14 +184,7 @@ public function testSuccessful(): void public function invalidMovePaths(): array { return [ - 'null' => [null], - 'true' => [true], - 'false' => [false], - 'int' => [1], - 'float' => [1.1], 'empty' => [''], - 'array' => [['filename']], - 'object' => [(object) ['filename']], ]; } diff --git a/tests/UriTest.php b/tests/UriTest.php index 195933a..3a44278 100644 --- a/tests/UriTest.php +++ b/tests/UriTest.php @@ -137,62 +137,6 @@ public function testParseUriPortCannotBeZero(): void new Uri('//example.com:-1'); } - public function testSchemeMustHaveCorrectType(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Scheme must be a string'); - - (new Uri())->withScheme([]); - } - - public function testUserInfoUserMustHaveCorrectType(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('User must be a string'); - - (new Uri())->withUserInfo([]); - } - - public function testUserInfoPasswordMustHaveCorrectType(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Password must be a string or NULL'); - - (new Uri())->withUserInfo('', []); - } - - public function testHostMustHaveCorrectType(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Host must be a string'); - - (new Uri())->withHost([]); - } - - public function testPathMustHaveCorrectType(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Path must be a string'); - - (new Uri())->withPath([]); - } - - public function testQueryMustHaveCorrectType(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Query and fragment must be a string'); - - (new Uri())->withQuery([]); - } - - public function testFragmentMustHaveCorrectType(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Query and fragment must be a string'); - - (new Uri())->withFragment([]); - } - public function testCanParseFalseyUriParts(): void { $uri = new Uri('0://0:0@0/0?0#0'); @@ -353,14 +297,6 @@ public function testStandardPortIsNullIfSchemeChanges(): void static::assertNull($uri->getPort()); } - public function testPortPassedAsStringIsCastedToInt(): void - { - $uri = (new Uri('//example.com'))->withPort('8080'); - - static::assertSame(8080, $uri->getPort(), 'Port is returned as integer'); - static::assertSame('example.com:8080', $uri->getAuthority()); - } - public function testPortCanBeRemoved(): void { $uri = (new Uri('http://example.com:8080'))->withPort(null);