From d7ac8bc6c4d3d48abedbe41eafcde437c3651cf0 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 11 Oct 2022 04:21:43 +0700 Subject: [PATCH] Apply PHP 8.0 Syntax and constructor promotion Signed-off-by: Abdul Malik Ikhsan --- src/CallbackStream.php | 3 ++- .../InvalidForwardedHeaderNameException.php | 3 +-- .../InvalidProxyAddressException.php | 3 +-- src/HeaderSecurity.php | 5 ++-- src/MessageTrait.php | 3 +-- src/PhpInputStream.php | 4 ++- src/RelativeStream.php | 10 +++---- src/Response/ArraySerializer.php | 1 - src/Response/JsonResponse.php | 18 +++---------- src/ServerRequest.php | 23 ++++------------ src/ServerRequestFactory.php | 9 +++---- .../FilterUsingXForwardedHeaders.php | 27 +++++-------------- src/ServerRequestFilter/IPRange.php | 8 +++--- src/Stream.php | 7 ++--- src/UploadedFile.php | 21 ++++----------- src/Uri.php | 18 +++++++------ src/functions/marshal_headers_from_sapi.php | 8 +++--- src/functions/marshal_uri_from_sapi.php | 4 +-- test/CallbackStreamTest.php | 4 +-- test/MessageTraitTest.php | 18 +++++-------- test/PhpInputStreamTest.php | 4 +-- test/RequestTest.php | 14 ++++------ test/Response/HtmlResponseTest.php | 3 +-- test/Response/JsonResponseTest.php | 3 +-- test/Response/RedirectResponseTest.php | 3 +-- test/Response/TextResponseTest.php | 3 +-- test/Response/XmlResponseTest.php | 3 +-- test/ResponseTest.php | 11 +++----- test/ServerRequestFactoryTest.php | 15 +++++------ test/ServerRequestTest.php | 2 +- test/StreamTest.php | 5 ++-- test/UploadedFileTest.php | 6 ++--- test/UriTest.php | 12 +++------ 33 files changed, 101 insertions(+), 180 deletions(-) diff --git a/src/CallbackStream.php b/src/CallbackStream.php index 4027e665..cf344a15 100644 --- a/src/CallbackStream.php +++ b/src/CallbackStream.php @@ -5,6 +5,7 @@ namespace Laminas\Diactoros; use Psr\Http\Message\StreamInterface; +use Stringable; use function array_key_exists; @@ -13,7 +14,7 @@ /** * Implementation of PSR HTTP streams */ -class CallbackStream implements StreamInterface +class CallbackStream implements StreamInterface, Stringable { /** @var callable|null */ protected $callback; diff --git a/src/Exception/InvalidForwardedHeaderNameException.php b/src/Exception/InvalidForwardedHeaderNameException.php index c010a429..5a3bc3ba 100644 --- a/src/Exception/InvalidForwardedHeaderNameException.php +++ b/src/Exception/InvalidForwardedHeaderNameException.php @@ -13,8 +13,7 @@ class InvalidForwardedHeaderNameException extends RuntimeException implements ExceptionInterface { - /** @param mixed $name */ - public static function forHeader($name): self + public static function forHeader(mixed $name): self { if (! is_string($name)) { $name = sprintf('(value of type %s)', is_object($name) ? $name::class : gettype($name)); diff --git a/src/Exception/InvalidProxyAddressException.php b/src/Exception/InvalidProxyAddressException.php index 43fd224e..771c7bef 100644 --- a/src/Exception/InvalidProxyAddressException.php +++ b/src/Exception/InvalidProxyAddressException.php @@ -10,8 +10,7 @@ class InvalidProxyAddressException extends RuntimeException implements ExceptionInterface { - /** @param mixed $proxy */ - public static function forInvalidProxyArgument($proxy): self + public static function forInvalidProxyArgument(mixed $proxy): self { $type = is_object($proxy) ? $proxy::class : gettype($proxy); return new self(sprintf( diff --git a/src/HeaderSecurity.php b/src/HeaderSecurity.php index 99ea5185..08a5e3c3 100644 --- a/src/HeaderSecurity.php +++ b/src/HeaderSecurity.php @@ -123,7 +123,7 @@ public static function isValid($value): bool * @param mixed $value Value to be tested. This method asserts it is a string or number. * @throws Exception\InvalidArgumentException For invalid values. */ - public static function assertValid($value): void + public static function assertValid(mixed $value): void { if (! is_string($value) && ! is_numeric($value)) { throw new Exception\InvalidArgumentException(sprintf( @@ -144,10 +144,9 @@ public static function assertValid($value): void * * @see http://tools.ietf.org/html/rfc7230#section-3.2 * - * @param mixed $name * @throws Exception\InvalidArgumentException */ - public static function assertValidName($name): void + public static function assertValidName(mixed $name): void { if (! is_string($name)) { throw new Exception\InvalidArgumentException(sprintf( diff --git a/src/MessageTrait.php b/src/MessageTrait.php index 3945b69d..c4dac43f 100644 --- a/src/MessageTrait.php +++ b/src/MessageTrait.php @@ -375,10 +375,9 @@ private function validateProtocolVersion($version): void } /** - * @param mixed $values * @return string[] */ - private function filterHeaderValue($values): array + private function filterHeaderValue(mixed $values): array { if (! is_array($values)) { $values = [$values]; diff --git a/src/PhpInputStream.php b/src/PhpInputStream.php index 6cd5ae65..cef45d62 100644 --- a/src/PhpInputStream.php +++ b/src/PhpInputStream.php @@ -4,12 +4,14 @@ namespace Laminas\Diactoros; +use Stringable; + use function stream_get_contents; /** * Caching version of php://input */ -class PhpInputStream extends Stream +class PhpInputStream extends Stream implements Stringable { private string $cache = ''; diff --git a/src/RelativeStream.php b/src/RelativeStream.php index 2811d4a0..0c12833e 100644 --- a/src/RelativeStream.php +++ b/src/RelativeStream.php @@ -5,6 +5,7 @@ namespace Laminas\Diactoros; use Psr\Http\Message\StreamInterface; +use Stringable; use const SEEK_SET; @@ -14,16 +15,13 @@ * * @see AbstractSerializer::splitStream() */ -final class RelativeStream implements StreamInterface +final class RelativeStream implements StreamInterface, Stringable { - private StreamInterface $decoratedStream; - private int $offset; - public function __construct(StreamInterface $decoratedStream, ?int $offset) + public function __construct(private StreamInterface $decoratedStream, ?int $offset) { - $this->decoratedStream = $decoratedStream; - $this->offset = (int) $offset; + $this->offset = (int) $offset; } /** diff --git a/src/Response/ArraySerializer.php b/src/Response/ArraySerializer.php index b3ecc111..0828e798 100644 --- a/src/Response/ArraySerializer.php +++ b/src/Response/ArraySerializer.php @@ -68,7 +68,6 @@ public static function fromArray(array $serializedResponse): Response } /** - * @param array $data * @return mixed * @throws Exception\DeserializationException */ diff --git a/src/Response/JsonResponse.php b/src/Response/JsonResponse.php index 3ae8697b..8dcad1b2 100644 --- a/src/Response/JsonResponse.php +++ b/src/Response/JsonResponse.php @@ -47,8 +47,6 @@ class JsonResponse extends Response /** @var mixed */ private $payload; - private int $encodingOptions; - /** * Create a JSON response with the given data. * @@ -71,10 +69,9 @@ public function __construct( $data, int $status = 200, array $headers = [], - int $encodingOptions = self::DEFAULT_JSON_FLAGS + private int $encodingOptions = self::DEFAULT_JSON_FLAGS ) { $this->setPayload($data); - $this->encodingOptions = $encodingOptions; $json = $this->jsonEncode($data, $this->encodingOptions); $body = $this->createBodyFromJson($json); @@ -92,10 +89,7 @@ public function getPayload() return $this->payload; } - /** - * @param mixed $data - */ - public function withPayload($data): JsonResponse + public function withPayload(mixed $data): JsonResponse { $new = clone $this; $new->setPayload($data); @@ -126,10 +120,9 @@ private function createBodyFromJson(string $json): Stream /** * Encode the provided data to JSON. * - * @param mixed $data * @throws Exception\InvalidArgumentException If unable to encode the $data to JSON. */ - private function jsonEncode($data, int $encodingOptions): string + private function jsonEncode(mixed $data, int $encodingOptions): string { if (is_resource($data)) { throw new Exception\InvalidArgumentException('Cannot JSON encode resources'); @@ -151,10 +144,7 @@ private function jsonEncode($data, int $encodingOptions): string return $json; } - /** - * @param mixed $data - */ - private function setPayload($data): void + private function setPayload(mixed $data): void { if (is_object($data)) { $data = clone $data; diff --git a/src/ServerRequest.php b/src/ServerRequest.php index 1304522b..8fdfd449 100644 --- a/src/ServerRequest.php +++ b/src/ServerRequest.php @@ -35,15 +35,6 @@ class ServerRequest implements ServerRequestInterface private array $attributes = []; - private array $cookieParams = []; - - /** @var null|array|object */ - private $parsedBody; - - private array $queryParams = []; - - private array $serverParams; - private array $uploadedFiles; /** @@ -53,22 +44,22 @@ class ServerRequest implements ServerRequestInterface * @param null|string $method HTTP method for the request, if any. * @param string|resource|StreamInterface $body Message body, if any. * @param array $headers Headers for the message, if any. - * @param array $cookies Cookies for the message, if any. + * @param array $cookieParams Cookies for the message, if any. * @param array $queryParams Query params for the message, if any. * @param null|array|object $parsedBody The deserialized body parameters, if any. * @param string $protocol HTTP protocol version. * @throws Exception\InvalidArgumentException For any invalid value. */ public function __construct( - array $serverParams = [], + private array $serverParams = [], array $uploadedFiles = [], $uri = null, ?string $method = null, $body = 'php://input', array $headers = [], - array $cookies = [], - array $queryParams = [], - $parsedBody = null, + private array $cookieParams = [], + private array $queryParams = [], + private $parsedBody = null, string $protocol = '1.1' ) { $this->validateUploadedFiles($uploadedFiles); @@ -78,11 +69,7 @@ public function __construct( } $this->initialize($uri, $method, $body, $headers); - $this->serverParams = $serverParams; $this->uploadedFiles = $uploadedFiles; - $this->cookieParams = $cookies; - $this->queryParams = $queryParams; - $this->parsedBody = $parsedBody; $this->protocol = $protocol; } diff --git a/src/ServerRequestFactory.php b/src/ServerRequestFactory.php index 4d74bf3c..6835758d 100644 --- a/src/ServerRequestFactory.php +++ b/src/ServerRequestFactory.php @@ -22,8 +22,8 @@ use function preg_match; use function preg_replace; use function sprintf; +use function str_contains; use function strlen; -use function strpos; use function strrpos; use function strtolower; use function substr; @@ -158,7 +158,7 @@ private static function marshalUriFromSapi(array $server, array $headers): Uri // URI fragment $fragment = ''; - if (strpos($path, '#') !== false) { + if (str_contains($path, '#')) { [$path, $fragment] = explode('#', $path, 2); } @@ -267,10 +267,7 @@ private static function marshalRequestPath(array $server): string return $origPathInfo; } - /** - * @param mixed $https - */ - private static function marshalHttpsValue($https): bool + private static function marshalHttpsValue(mixed $https): bool { if (is_bool($https)) { return $https; diff --git a/src/ServerRequestFilter/FilterUsingXForwardedHeaders.php b/src/ServerRequestFilter/FilterUsingXForwardedHeaders.php index 4d44a85a..2327646d 100644 --- a/src/ServerRequestFilter/FilterUsingXForwardedHeaders.php +++ b/src/ServerRequestFilter/FilterUsingXForwardedHeaders.php @@ -12,7 +12,7 @@ use function filter_var; use function in_array; use function is_string; -use function strpos; +use function str_contains; use function strtolower; use const FILTER_FLAG_IPV4; @@ -41,24 +41,14 @@ final class FilterUsingXForwardedHeaders implements FilterServerRequestInterface self::HEADER_PROTO, ]; - /** @var list */ - private array $trustedHeaders; - - /** @var list */ - private array $trustedProxies; - /** * Only allow construction via named constructors * * @param list $trustedProxies * @param list $trustedHeaders */ - private function __construct( - array $trustedProxies = [], - array $trustedHeaders = [] - ) { - $this->trustedProxies = $trustedProxies; - $this->trustedHeaders = $trustedHeaders; + private function __construct(private array $trustedProxies = [], private array $trustedHeaders = []) + { } public function __invoke(ServerRequestInterface $request): ServerRequestInterface @@ -79,7 +69,7 @@ public function __invoke(ServerRequestInterface $request): ServerRequestInterfac $uri = $originalUri = $request->getUri(); foreach ($this->trustedHeaders as $headerName) { $header = $request->getHeaderLine($headerName); - if ('' === $header || false !== strpos($header, ',')) { + if ('' === $header || str_contains($header, ',')) { // Reject empty headers and/or headers with multiple values continue; } @@ -225,10 +215,7 @@ private static function normalizeProxiesList(array $proxyCIDRList): array return $proxyCIDRList; } - /** - * @param mixed $cidr - */ - private static function validateProxyCIDR($cidr): bool + private static function validateProxyCIDR(mixed $cidr): bool { if (! is_string($cidr) || '' === $cidr) { return false; @@ -236,12 +223,12 @@ private static function validateProxyCIDR($cidr): bool $address = $cidr; $mask = null; - if (false !== strpos($cidr, '/')) { + if (str_contains($cidr, '/')) { [$address, $mask] = explode('/', $cidr, 2); $mask = (int) $mask; } - if (false !== strpos($address, ':')) { + if (str_contains($address, ':')) { // is IPV6 return filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) && ( diff --git a/src/ServerRequestFilter/IPRange.php b/src/ServerRequestFilter/IPRange.php index a306ffd8..cdf16d0b 100644 --- a/src/ServerRequestFilter/IPRange.php +++ b/src/ServerRequestFilter/IPRange.php @@ -10,9 +10,9 @@ use function ip2long; use function pack; use function sprintf; +use function str_contains; use function str_pad; use function str_repeat; -use function strpos; use function substr_compare; use function unpack; @@ -29,7 +29,7 @@ private function __construct() /** @psalm-pure */ public static function matches(string $ip, string $cidr): bool { - if (false !== strpos($ip, ':')) { + if (str_contains($ip, ':')) { return self::matchesIPv6($ip, $cidr); } @@ -42,7 +42,7 @@ public static function matchesIPv4(string $ip, string $cidr): bool $mask = 32; $subnet = $cidr; - if (false !== strpos($cidr, '/')) { + if (str_contains($cidr, '/')) { [$subnet, $mask] = explode('/', $cidr, 2); $mask = (int) $mask; } @@ -72,7 +72,7 @@ public static function matchesIPv6(string $ip, string $cidr): bool $mask = 128; $subnet = $cidr; - if (false !== strpos($cidr, '/')) { + if (str_contains($cidr, '/')) { [$subnet, $mask] = explode('/', $cidr, 2); $mask = (int) $mask; } diff --git a/src/Stream.php b/src/Stream.php index 29c1931b..fb2f5dab 100644 --- a/src/Stream.php +++ b/src/Stream.php @@ -7,6 +7,7 @@ use GdImage; use Psr\Http\Message\StreamInterface; use RuntimeException; +use Stringable; use Throwable; use function array_key_exists; @@ -34,7 +35,7 @@ /** * Implementation of PSR HTTP streams */ -class Stream implements StreamInterface +class Stream implements StreamInterface, Stringable { /** * A list of allowed stream resource types that are allowed to instantiate a Stream @@ -72,7 +73,7 @@ public function __toString(): string } return $this->getContents(); - } catch (RuntimeException $e) { + } catch (RuntimeException) { return ''; } } @@ -352,7 +353,7 @@ private function setStream($stream, string $mode = 'r'): void * @param mixed $resource Stream resource. * @psalm-assert-if-true resource $resource */ - private function isValidStreamResourceType($resource): bool + private function isValidStreamResourceType(mixed $resource): bool { if (is_resource($resource)) { return in_array(get_resource_type($resource), self::ALLOWED_STREAM_RESOURCE_TYPES, true); diff --git a/src/UploadedFile.php b/src/UploadedFile.php index 500cf35e..50407365 100644 --- a/src/UploadedFile.php +++ b/src/UploadedFile.php @@ -17,7 +17,7 @@ use function is_string; use function is_writable; use function move_uploaded_file; -use function strpos; +use function str_starts_with; use function unlink; use const PHP_SAPI; @@ -44,18 +44,12 @@ class UploadedFile implements UploadedFileInterface UPLOAD_ERR_EXTENSION => 'A PHP extension stopped the file upload.', ]; - private ?string $clientFilename; - - private ?string $clientMediaType; - private int $error; private ?string $file = null; private bool $moved = false; - private int $size; - /** @var null|StreamInterface */ private $stream; @@ -65,10 +59,10 @@ class UploadedFile implements UploadedFileInterface */ public function __construct( $streamOrFile, - int $size, + private int $size, int $errorStatus, - ?string $clientFilename = null, - ?string $clientMediaType = null + private ?string $clientFilename = null, + private ?string $clientMediaType = null ) { if ($errorStatus === UPLOAD_ERR_OK) { if (is_string($streamOrFile)) { @@ -86,17 +80,12 @@ public function __construct( } } - $this->size = $size; - if (0 > $errorStatus || 8 < $errorStatus) { throw new Exception\InvalidArgumentException( 'Invalid error status for UploadedFile; must be an UPLOAD_ERR_* constant' ); } $this->error = $errorStatus; - - $this->clientFilename = $clientFilename; - $this->clientMediaType = $clientMediaType; } /** @@ -161,7 +150,7 @@ public function moveTo($targetPath): void $sapi = PHP_SAPI; switch (true) { - case empty($sapi) || 0 === strpos($sapi, 'cli') || 0 === strpos($sapi, 'phpdbg') || ! $this->file: + case empty($sapi) || str_starts_with($sapi, 'cli') || str_starts_with($sapi, 'phpdbg') || ! $this->file: // Non-SAPI environment, or no filename present $this->writeFile($targetPath); diff --git a/src/Uri.php b/src/Uri.php index 728b06d3..b2183187 100644 --- a/src/Uri.php +++ b/src/Uri.php @@ -6,6 +6,7 @@ use Psr\Http\Message\UriInterface; use SensitiveParameter; +use Stringable; use function array_keys; use function explode; @@ -22,8 +23,9 @@ use function preg_replace_callback; use function rawurlencode; use function sprintf; +use function str_contains; use function str_split; -use function strpos; +use function str_starts_with; use function strtolower; use function substr; @@ -37,7 +39,7 @@ * state of the current instance and return a new instance that contains the * changed state. */ -class Uri implements UriInterface +class Uri implements UriInterface, Stringable { /** * Sub-delimiters used in user info, query strings and fragments. @@ -346,13 +348,13 @@ public function withPath($path): UriInterface ); } - if (strpos($path, '?') !== false) { + if (str_contains($path, '?')) { throw new Exception\InvalidArgumentException( 'Invalid path provided; must not contain a query string' ); } - if (strpos($path, '#') !== false) { + if (str_contains($path, '#')) { throw new Exception\InvalidArgumentException( 'Invalid path provided; must not contain a URI fragment' ); @@ -382,7 +384,7 @@ public function withQuery($query): UriInterface ); } - if (strpos($query, '#') !== false) { + if (str_contains($query, '#')) { throw new Exception\InvalidArgumentException( 'Query string must not include a URI fragment' ); @@ -473,7 +475,7 @@ private static function createUriString( $uri .= '//' . $authority; } - if ('' !== $path && '/' !== substr($path, 0, 1)) { + if ('' !== $path && ! str_starts_with($path, '/')) { $path = '/' . $path; } @@ -602,7 +604,7 @@ private function filterInvalidUtf8(string $string): string */ private function filterQuery(string $query): string { - if ('' !== $query && strpos($query, '?') === 0) { + if ('' !== $query && str_starts_with($query, '?')) { $query = substr($query, 1); } @@ -642,7 +644,7 @@ private function splitQueryValue(string $value): array */ private function filterFragment(string $fragment): string { - if ('' !== $fragment && strpos($fragment, '#') === 0) { + if ('' !== $fragment && str_starts_with($fragment, '#')) { $fragment = '%23' . substr($fragment, 1); } diff --git a/src/functions/marshal_headers_from_sapi.php b/src/functions/marshal_headers_from_sapi.php index 8a6c1293..7fd09996 100644 --- a/src/functions/marshal_headers_from_sapi.php +++ b/src/functions/marshal_headers_from_sapi.php @@ -6,7 +6,7 @@ use function array_key_exists; use function is_string; -use function strpos; +use function str_starts_with; use function strtolower; use function strtr; use function substr; @@ -26,7 +26,7 @@ function marshalHeadersFromSapi(array $server): array ]; return isset($contentHeaders[$key]); } - : static fn(string $key): bool => strpos($key, 'CONTENT_') === 0; + : static fn(string $key): bool => str_starts_with($key, 'CONTENT_'); $headers = []; foreach ($server as $key => $value) { @@ -40,7 +40,7 @@ function marshalHeadersFromSapi(array $server): array // Apache prefixes environment variables with REDIRECT_ // if they are added by rewrite rules - if (strpos($key, 'REDIRECT_') === 0) { + if (str_starts_with($key, 'REDIRECT_')) { $key = substr($key, 9); // We will not overwrite existing variables with the @@ -50,7 +50,7 @@ function marshalHeadersFromSapi(array $server): array } } - if (strpos($key, 'HTTP_') === 0) { + if (str_starts_with($key, 'HTTP_')) { $name = strtr(strtolower(substr($key, 5)), '_', '-'); $headers[$name] = $value; continue; diff --git a/src/functions/marshal_uri_from_sapi.php b/src/functions/marshal_uri_from_sapi.php index 3e78c9dd..124cdff4 100644 --- a/src/functions/marshal_uri_from_sapi.php +++ b/src/functions/marshal_uri_from_sapi.php @@ -16,8 +16,8 @@ use function preg_match; use function preg_replace; use function sprintf; +use function str_contains; use function strlen; -use function strpos; use function strrpos; use function strtolower; use function substr; @@ -218,7 +218,7 @@ function marshalUriFromSapi(array $server, array $headers): Uri // URI fragment $fragment = ''; - if (strpos($path, '#') !== false) { + if (str_contains($path, '#')) { [$path, $fragment] = explode('#', $path, 2); } diff --git a/test/CallbackStreamTest.php b/test/CallbackStreamTest.php index e5f927ac..358dff46 100644 --- a/test/CallbackStreamTest.php +++ b/test/CallbackStreamTest.php @@ -15,7 +15,7 @@ final class CallbackStreamTest extends TestCase { public function testToString(): void { - $stream = new CallbackStream(static fn() => 'foobarbaz'); + $stream = new CallbackStream(static fn(): string => 'foobarbaz'); $ret = $stream->__toString(); $this->assertSame('foobarbaz', $ret); @@ -138,7 +138,7 @@ public function testRead(): void public function testGetContents(): void { - $stream = new CallbackStream(static fn() => 'foobarbaz'); + $stream = new CallbackStream(static fn(): string => 'foobarbaz'); $ret = $stream->getContents(); $this->assertSame('foobarbaz', $ret); diff --git a/test/MessageTraitTest.php b/test/MessageTraitTest.php index cb6bebc5..8a643b33 100644 --- a/test/MessageTraitTest.php +++ b/test/MessageTraitTest.php @@ -54,9 +54,8 @@ public function invalidProtocolVersionProvider(): array /** * @dataProvider invalidProtocolVersionProvider - * @param mixed $version */ - public function testWithProtocolVersionRaisesExceptionForInvalidVersion($version): void + public function testWithProtocolVersionRaisesExceptionForInvalidVersion(mixed $version): void { $request = new Request(); @@ -194,9 +193,8 @@ public function invalidGeneralHeaderValues(): array /** * @dataProvider invalidGeneralHeaderValues - * @param mixed $value */ - public function testWithHeaderRaisesExceptionForInvalidNestedHeaderValue($value): void + public function testWithHeaderRaisesExceptionForInvalidNestedHeaderValue(mixed $value): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Invalid header value'); @@ -218,9 +216,8 @@ public function invalidHeaderValues(): array /** * @dataProvider invalidHeaderValues - * @param mixed $value */ - public function testWithHeaderRaisesExceptionForInvalidValueType($value): void + public function testWithHeaderRaisesExceptionForInvalidValueType(mixed $value): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Invalid header value'); @@ -239,9 +236,8 @@ public function testWithHeaderReplacesDifferentCapitalization(): void /** * @dataProvider invalidGeneralHeaderValues - * @param mixed $value */ - public function testWithAddedHeaderRaisesExceptionForNonStringNonArrayValue($value): void + public function testWithAddedHeaderRaisesExceptionForNonStringNonArrayValue(mixed $value): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('must be a string'); @@ -423,9 +419,8 @@ public function invalidArrayHeaderValues(): array /** * @dataProvider invalidArrayHeaderValues * @group 99 - * @param mixed $value */ - public function testWithHeaderShouldRaiseExceptionForInvalidHeaderValuesInArrays($value): void + public function testWithHeaderShouldRaiseExceptionForInvalidHeaderValuesInArrays(mixed $value): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('header value type'); @@ -437,9 +432,8 @@ public function testWithHeaderShouldRaiseExceptionForInvalidHeaderValuesInArrays /** * @dataProvider invalidHeaderValueTypes * @group 99 - * @param mixed $value */ - public function testWithHeaderShouldRaiseExceptionForInvalidHeaderScalarValues($value): void + public function testWithHeaderShouldRaiseExceptionForInvalidHeaderScalarValues(mixed $value): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('header value type'); diff --git a/test/PhpInputStreamTest.php b/test/PhpInputStreamTest.php index cfaef868..454902d3 100644 --- a/test/PhpInputStreamTest.php +++ b/test/PhpInputStreamTest.php @@ -12,9 +12,9 @@ final class PhpInputStreamTest extends TestCase { - protected string $file; + private string $file; - protected PhpInputStream $stream; + private PhpInputStream $stream; protected function setUp(): void { diff --git a/test/RequestTest.php b/test/RequestTest.php index 7c4a8892..0c2df7e4 100644 --- a/test/RequestTest.php +++ b/test/RequestTest.php @@ -14,7 +14,7 @@ final class RequestTest extends TestCase { - protected Request $request; + private Request $request; protected function setUp(): void { @@ -44,9 +44,8 @@ public function invalidMethod() /** * @dataProvider invalidMethod - * @param mixed $method */ - public function testWithInvalidMethod($method): void + public function testWithInvalidMethod(mixed $method): void { $this->expectException(InvalidArgumentException::class); /** @psalm-suppress MixedArgument */ @@ -132,9 +131,8 @@ public function invalidRequestUri(): array /** * @dataProvider invalidRequestUri - * @param mixed $uri */ - public function testConstructorRaisesExceptionForInvalidUri($uri): void + public function testConstructorRaisesExceptionForInvalidUri(mixed $uri): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Invalid URI'); @@ -207,9 +205,8 @@ public function invalidRequestBody(): array /** * @dataProvider invalidRequestBody - * @param mixed $body */ - public function testConstructorRaisesExceptionForInvalidBody($body): void + public function testConstructorRaisesExceptionForInvalidBody(mixed $body): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('stream'); @@ -233,11 +230,10 @@ public function invalidHeaderTypes(): array /** * @dataProvider invalidHeaderTypes * @group 99 - * @param mixed $headers * @param non-empty-string $contains */ public function testConstructorRaisesExceptionForInvalidHeaders( - $headers, + mixed $headers, string $contains = 'header value type' ): void { $this->expectException(InvalidArgumentException::class); diff --git a/test/Response/HtmlResponseTest.php b/test/Response/HtmlResponseTest.php index 614ff1c0..d914264e 100644 --- a/test/Response/HtmlResponseTest.php +++ b/test/Response/HtmlResponseTest.php @@ -70,9 +70,8 @@ public function invalidHtmlContent(): array /** * @dataProvider invalidHtmlContent - * @param mixed $body */ - public function testRaisesExceptionForNonStringNonStreamBodyContent($body): void + public function testRaisesExceptionForNonStringNonStreamBodyContent(mixed $body): void { $this->expectException(InvalidArgumentException::class); diff --git a/test/Response/JsonResponseTest.php b/test/Response/JsonResponseTest.php index 7acf5e76..3dc24344 100644 --- a/test/Response/JsonResponseTest.php +++ b/test/Response/JsonResponseTest.php @@ -58,9 +58,8 @@ public function scalarValuesForJSON() /** * @dataProvider scalarValuesForJSON - * @param mixed $value */ - public function testScalarValuePassedToConstructorJsonEncodesDirectly($value): void + public function testScalarValuePassedToConstructorJsonEncodesDirectly(mixed $value): void { $response = new JsonResponse($value); $this->assertSame(200, $response->getStatusCode()); diff --git a/test/Response/RedirectResponseTest.php b/test/Response/RedirectResponseTest.php index f695348a..f6917ccb 100644 --- a/test/Response/RedirectResponseTest.php +++ b/test/Response/RedirectResponseTest.php @@ -64,9 +64,8 @@ public function invalidUris(): array /** * @dataProvider invalidUris - * @param mixed $uri */ - public function testConstructorRaisesExceptionOnInvalidUri($uri): void + public function testConstructorRaisesExceptionOnInvalidUri(mixed $uri): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Uri'); diff --git a/test/Response/TextResponseTest.php b/test/Response/TextResponseTest.php index 7baf1c1f..f4a67dc3 100644 --- a/test/Response/TextResponseTest.php +++ b/test/Response/TextResponseTest.php @@ -70,9 +70,8 @@ public function invalidContent(): array /** * @dataProvider invalidContent - * @param mixed $body */ - public function testRaisesExceptionForNonStringNonStreamBodyContent($body): void + public function testRaisesExceptionForNonStringNonStreamBodyContent(mixed $body): void { $this->expectException(InvalidArgumentException::class); diff --git a/test/Response/XmlResponseTest.php b/test/Response/XmlResponseTest.php index 8ba501a6..bca99c12 100644 --- a/test/Response/XmlResponseTest.php +++ b/test/Response/XmlResponseTest.php @@ -72,9 +72,8 @@ public function invalidContent(): array /** * @dataProvider invalidContent - * @param mixed $body */ - public function testRaisesExceptionforNonStringNonStreamBodyContent($body): void + public function testRaisesExceptionforNonStringNonStreamBodyContent(mixed $body): void { $this->expectException(InvalidArgumentException::class); diff --git a/test/ResponseTest.php b/test/ResponseTest.php index d3d9954d..ab0afb73 100644 --- a/test/ResponseTest.php +++ b/test/ResponseTest.php @@ -35,7 +35,7 @@ final class ResponseTest extends TestCase { - protected Response $response; + private Response $response; protected function setUp(): void { @@ -184,9 +184,8 @@ public function invalidReasonPhrases(): array /** * @dataProvider invalidReasonPhrases - * @param mixed $invalidReasonPhrase */ - public function testWithStatusRaisesAnExceptionForNonStringReasonPhrases($invalidReasonPhrase): void + public function testWithStatusRaisesAnExceptionForNonStringReasonPhrases(mixed $invalidReasonPhrase): void { $this->expectException(InvalidArgumentException::class); @@ -244,9 +243,8 @@ public function validStatusCodes(): array /** * @dataProvider invalidStatusCodes - * @param mixed $code */ - public function testCannotSetInvalidStatusCode($code): void + public function testCannotSetInvalidStatusCode(mixed $code): void { $this->expectException(InvalidArgumentException::class); @@ -285,9 +283,8 @@ public function invalidResponseBody(): array /** * @dataProvider invalidResponseBody - * @param mixed $body */ - public function testConstructorRaisesExceptionForInvalidBody($body): void + public function testConstructorRaisesExceptionForInvalidBody(mixed $body): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('stream'); diff --git a/test/ServerRequestFactoryTest.php b/test/ServerRequestFactoryTest.php index f912393e..2bdddfe5 100644 --- a/test/ServerRequestFactoryTest.php +++ b/test/ServerRequestFactoryTest.php @@ -21,8 +21,8 @@ use function Laminas\Diactoros\normalizeServer; use function Laminas\Diactoros\normalizeUploadedFiles; use function sprintf; +use function str_contains; use function str_replace; -use function strpos; use function strtolower; final class ServerRequestFactoryTest extends TestCase @@ -290,7 +290,7 @@ public function httpsDisableParamProvider(): iterable $param = array_shift($data); foreach (['lowercase-off', 'uppercase-off'] as $type) { $key = sprintf('%s-%s', $key, $type); - $value = false !== strpos($type, 'lowercase') ? 'off' : 'OFF'; + $value = str_contains($type, 'lowercase') ? 'off' : 'OFF'; yield $key => [$param, $value]; } } @@ -526,7 +526,7 @@ public function testCookieHeaderVariations(string $cookieHeader, array $expected public function testNormalizeServerUsesMixedCaseAuthorizationHeaderFromApacheWhenPresent(): void { - $server = normalizeServer([], static fn() => ['Authorization' => 'foobar']); + $server = normalizeServer([], static fn(): array => ['Authorization' => 'foobar']); $this->assertArrayHasKey('HTTP_AUTHORIZATION', $server); $this->assertSame('foobar', $server['HTTP_AUTHORIZATION']); @@ -534,7 +534,7 @@ public function testNormalizeServerUsesMixedCaseAuthorizationHeaderFromApacheWhe public function testNormalizeServerUsesLowerCaseAuthorizationHeaderFromApacheWhenPresent(): void { - $server = normalizeServer([], static fn() => ['authorization' => 'foobar']); + $server = normalizeServer([], static fn(): array => ['authorization' => 'foobar']); $this->assertArrayHasKey('HTTP_AUTHORIZATION', $server); $this->assertSame('foobar', $server['HTTP_AUTHORIZATION']); @@ -544,7 +544,7 @@ public function testNormalizeServerReturnsArrayUnalteredIfApacheHeadersDoNotCont { $expected = ['FOO_BAR' => 'BAZ']; - $server = normalizeServer($expected, static fn() => []); + $server = normalizeServer($expected, static fn(): array => []); $this->assertSame($expected, $server); } @@ -731,11 +731,8 @@ public function testReturnsFilteredRequestBasedOnRequestFilterProvided(): void { $expectedRequest = new ServerRequest(); $filter = new class ($expectedRequest) implements FilterServerRequestInterface { - private ServerRequestInterface $request; - - public function __construct(ServerRequestInterface $request) + public function __construct(private ServerRequestInterface $request) { - $this->request = $request; } public function __invoke(ServerRequestInterface $request): ServerRequestInterface diff --git a/test/ServerRequestTest.php b/test/ServerRequestTest.php index 5cf58b16..9e046c33 100644 --- a/test/ServerRequestTest.php +++ b/test/ServerRequestTest.php @@ -13,7 +13,7 @@ final class ServerRequestTest extends TestCase { - protected ServerRequest $request; + private ServerRequest $request; protected function setUp(): void { diff --git a/test/StreamTest.php b/test/StreamTest.php index 2cf7bb5d..1b72fb31 100644 --- a/test/StreamTest.php +++ b/test/StreamTest.php @@ -39,7 +39,7 @@ final class StreamTest extends TestCase /** @var string|null|false */ private $tmpnam; - protected Stream $stream; + private Stream $stream; protected function setUp(): void { @@ -483,9 +483,8 @@ public function invalidResources(): array /** * @dataProvider invalidResources - * @param mixed $resource */ - public function testAttachWithNonStringNonResourceRaisesException($resource): void + public function testAttachWithNonStringNonResourceRaisesException(mixed $resource): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Invalid stream'); diff --git a/test/UploadedFileTest.php b/test/UploadedFileTest.php index 19816088..ff869bd9 100644 --- a/test/UploadedFileTest.php +++ b/test/UploadedFileTest.php @@ -76,9 +76,8 @@ public function invalidStreams(): array /** * @dataProvider invalidStreams - * @param mixed $streamOrFile */ - public function testRaisesExceptionOnInvalidStreamOrFile($streamOrFile): void + public function testRaisesExceptionOnInvalidStreamOrFile(mixed $streamOrFile): void { $this->expectException(InvalidArgumentException::class); @@ -186,9 +185,8 @@ public function invalidMovePaths(): array /** * @dataProvider invalidMovePaths - * @param mixed $path */ - public function testMoveRaisesExceptionForInvalidPath($path): void + public function testMoveRaisesExceptionForInvalidPath(mixed $path): void { $stream = new Stream('php://temp', 'wb+'); $stream->write('Foo bar!'); diff --git a/test/UriTest.php b/test/UriTest.php index e53303d1..52065fd0 100644 --- a/test/UriTest.php +++ b/test/UriTest.php @@ -189,9 +189,8 @@ public function invalidPorts(): array /** * @dataProvider invalidPorts - * @param mixed $port */ - public function testWithPortRaisesExceptionForInvalidPorts($port): void + public function testWithPortRaisesExceptionForInvalidPorts(mixed $port): void { $uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz'); @@ -236,9 +235,8 @@ public function invalidPaths(): array /** * @dataProvider invalidPaths - * @param mixed $path */ - public function testWithPathRaisesExceptionForInvalidPaths($path): void + public function testWithPathRaisesExceptionForInvalidPaths(mixed $path): void { $uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz'); @@ -273,9 +271,8 @@ public function invalidQueryStrings(): array /** * @dataProvider invalidQueryStrings - * @param mixed $query */ - public function testWithQueryRaisesExceptionForInvalidQueryStrings($query): void + public function testWithQueryRaisesExceptionForInvalidQueryStrings(mixed $query): void { $uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz'); @@ -622,9 +619,8 @@ public function invalidStringComponentValues(): array /** * @dataProvider invalidStringComponentValues * @param 'withScheme'|'withUserInfo'|'withHost'|'withPath'|'withQuery'|'withFragment' $method - * @param mixed $value */ - public function testPassingInvalidValueToWithMethodRaisesException(string $method, $value): void + public function testPassingInvalidValueToWithMethodRaisesException(string $method, mixed $value): void { $uri = new Uri('https://example.com/');