From 8e8513916f5a93719452c729c036b768275bfdc7 Mon Sep 17 00:00:00 2001 From: Gary Lockett Date: Fri, 2 Jun 2023 16:10:46 +0100 Subject: [PATCH] run rector to upgrade old PHP code Signed-off-by: Gary Lockett --- psalm-baseline.xml | 10 +--------- src/Exception/InvalidForwardedHeaderNameException.php | 5 ++--- src/Exception/InvalidProxyAddressException.php | 5 ++--- src/HeaderSecurity.php | 7 +++---- src/Response/HtmlResponse.php | 5 ++--- src/Response/RedirectResponse.php | 5 ++--- src/Response/TextResponse.php | 5 ++--- src/Response/XmlResponse.php | 5 ++--- src/ServerRequestFactory.php | 2 +- src/UploadedFile.php | 3 +-- test/Response/JsonResponseTest.php | 5 +++-- test/StreamTest.php | 6 +++--- test/UploadedFileTest.php | 3 +-- 13 files changed, 25 insertions(+), 41 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index aafe7966..ffc4b341 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,5 +1,5 @@ - + null|callable @@ -146,14 +146,6 @@ json_encode - - - gettype($uri) - - - is_object($uri) - - $body diff --git a/src/Exception/InvalidForwardedHeaderNameException.php b/src/Exception/InvalidForwardedHeaderNameException.php index 5a3bc3ba..318ce8e6 100644 --- a/src/Exception/InvalidForwardedHeaderNameException.php +++ b/src/Exception/InvalidForwardedHeaderNameException.php @@ -6,8 +6,7 @@ use Laminas\Diactoros\ServerRequestFilter\FilterUsingXForwardedHeaders; -use function gettype; -use function is_object; +use function get_debug_type; use function is_string; use function sprintf; @@ -16,7 +15,7 @@ class InvalidForwardedHeaderNameException extends RuntimeException implements Ex public static function forHeader(mixed $name): self { if (! is_string($name)) { - $name = sprintf('(value of type %s)', is_object($name) ? $name::class : gettype($name)); + $name = sprintf('(value of type %s)', get_debug_type($name)); } return new self(sprintf( diff --git a/src/Exception/InvalidProxyAddressException.php b/src/Exception/InvalidProxyAddressException.php index 771c7bef..f9bec776 100644 --- a/src/Exception/InvalidProxyAddressException.php +++ b/src/Exception/InvalidProxyAddressException.php @@ -4,15 +4,14 @@ namespace Laminas\Diactoros\Exception; -use function gettype; -use function is_object; +use function get_debug_type; use function sprintf; class InvalidProxyAddressException extends RuntimeException implements ExceptionInterface { public static function forInvalidProxyArgument(mixed $proxy): self { - $type = is_object($proxy) ? $proxy::class : gettype($proxy); + $type = get_debug_type($proxy); return new self(sprintf( 'Invalid proxy of type "%s" provided;' . ' must be a valid IPv4 or IPv6 address, optionally with a subnet mask provided' diff --git a/src/HeaderSecurity.php b/src/HeaderSecurity.php index d12486ae..0951eb99 100644 --- a/src/HeaderSecurity.php +++ b/src/HeaderSecurity.php @@ -4,10 +4,9 @@ namespace Laminas\Diactoros; -use function gettype; +use function get_debug_type; use function in_array; use function is_numeric; -use function is_object; use function is_string; use function ord; use function preg_match; @@ -128,7 +127,7 @@ public static function assertValid(mixed $value): void if (! is_string($value) && ! is_numeric($value)) { throw new Exception\InvalidArgumentException(sprintf( 'Invalid header value type; must be a string or numeric; received %s', - is_object($value) ? $value::class : gettype($value) + get_debug_type($value) )); } if (! self::isValid($value)) { @@ -151,7 +150,7 @@ public static function assertValidName(mixed $name): void if (! is_string($name)) { throw new Exception\InvalidArgumentException(sprintf( 'Invalid header name type; expected string; received %s', - is_object($name) ? $name::class : gettype($name) + get_debug_type($name) )); } if (! preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/D', $name)) { diff --git a/src/Response/HtmlResponse.php b/src/Response/HtmlResponse.php index 746f3df3..0e0aadb7 100644 --- a/src/Response/HtmlResponse.php +++ b/src/Response/HtmlResponse.php @@ -9,8 +9,7 @@ use Laminas\Diactoros\Stream; use Psr\Http\Message\StreamInterface; -use function gettype; -use function is_object; +use function get_debug_type; use function is_string; use function sprintf; @@ -60,7 +59,7 @@ private function createBody($html): StreamInterface if (! is_string($html)) { throw new Exception\InvalidArgumentException(sprintf( 'Invalid content (%s) provided to %s', - is_object($html) ? $html::class : gettype($html), + get_debug_type($html), self::class )); } diff --git a/src/Response/RedirectResponse.php b/src/Response/RedirectResponse.php index 0f42ebc9..a6d3bae3 100644 --- a/src/Response/RedirectResponse.php +++ b/src/Response/RedirectResponse.php @@ -8,8 +8,7 @@ use Laminas\Diactoros\Response; use Psr\Http\Message\UriInterface; -use function gettype; -use function is_object; +use function get_debug_type; use function is_string; use function sprintf; @@ -36,7 +35,7 @@ public function __construct($uri, int $status = 302, array $headers = []) throw new Exception\InvalidArgumentException(sprintf( 'Uri provided to %s MUST be a string or Psr\Http\Message\UriInterface instance; received "%s"', self::class, - is_object($uri) ? $uri::class : gettype($uri) + get_debug_type($uri) )); } diff --git a/src/Response/TextResponse.php b/src/Response/TextResponse.php index 60298491..6e9ff642 100644 --- a/src/Response/TextResponse.php +++ b/src/Response/TextResponse.php @@ -9,8 +9,7 @@ use Laminas\Diactoros\Stream; use Psr\Http\Message\StreamInterface; -use function gettype; -use function is_object; +use function get_debug_type; use function is_string; use function sprintf; @@ -60,7 +59,7 @@ private function createBody($text): StreamInterface if (! is_string($text)) { throw new Exception\InvalidArgumentException(sprintf( 'Invalid content (%s) provided to %s', - is_object($text) ? $text::class : gettype($text), + get_debug_type($text), self::class )); } diff --git a/src/Response/XmlResponse.php b/src/Response/XmlResponse.php index 8a4d4237..a1efe563 100644 --- a/src/Response/XmlResponse.php +++ b/src/Response/XmlResponse.php @@ -9,8 +9,7 @@ use Laminas\Diactoros\Stream; use Psr\Http\Message\StreamInterface; -use function gettype; -use function is_object; +use function get_debug_type; use function is_string; use function sprintf; @@ -62,7 +61,7 @@ private function createBody($xml): StreamInterface if (! is_string($xml)) { throw new Exception\InvalidArgumentException(sprintf( 'Invalid content (%s) provided to %s', - is_object($xml) ? $xml::class : gettype($xml), + get_debug_type($xml), self::class )); } diff --git a/src/ServerRequestFactory.php b/src/ServerRequestFactory.php index ca176e14..86903d6d 100644 --- a/src/ServerRequestFactory.php +++ b/src/ServerRequestFactory.php @@ -54,7 +54,7 @@ public static function fromGlobals( ?array $files = null, ?FilterServerRequestInterface $requestFilter = null ): ServerRequestInterface { - $requestFilter = $requestFilter ?? FilterUsingXForwardedHeaders::trustReservedSubnets(); + $requestFilter ??= FilterUsingXForwardedHeaders::trustReservedSubnets(); $server = normalizeServer( $server ?? $_SERVER, diff --git a/src/UploadedFile.php b/src/UploadedFile.php index 354b0428..14c711c2 100644 --- a/src/UploadedFile.php +++ b/src/UploadedFile.php @@ -50,8 +50,7 @@ class UploadedFile implements UploadedFileInterface private bool $moved = false; - /** @var null|StreamInterface */ - private $stream; + private ?StreamInterface $stream = null; /** * @param string|resource|StreamInterface $streamOrFile diff --git a/test/Response/JsonResponseTest.php b/test/Response/JsonResponseTest.php index 3dc24344..28869aa1 100644 --- a/test/Response/JsonResponseTest.php +++ b/test/Response/JsonResponseTest.php @@ -19,6 +19,7 @@ use const JSON_HEX_QUOT; use const JSON_HEX_TAG; use const JSON_PRETTY_PRINT; +use const JSON_THROW_ON_ERROR; use const JSON_UNESCAPED_SLASHES; class JsonResponseTest extends TestCase @@ -139,7 +140,7 @@ public function testConstructorRewindsBodyStream(): void $json = ['test' => 'data']; $response = new JsonResponse($json); - $actual = json_decode($response->getBody()->getContents(), true); + $actual = json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR); $this->assertSame($json, $actual); } @@ -158,7 +159,7 @@ public function testWithPayload(): void $this->assertNotSame($response, $newResponse); $this->assertSame($json, $newResponse->getPayload()); - $decodedBody = json_decode($newResponse->getBody()->getContents(), true); + $decodedBody = json_decode($newResponse->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR); $this->assertSame($json, $decodedBody); } diff --git a/test/StreamTest.php b/test/StreamTest.php index 2280004c..e98a7ca2 100644 --- a/test/StreamTest.php +++ b/test/StreamTest.php @@ -27,6 +27,7 @@ use function fwrite; use function imagecreate; use function is_resource; +use function is_string; use function shmop_open; use function stream_get_meta_data; use function sys_get_temp_dir; @@ -38,8 +39,7 @@ final class StreamTest extends TestCase { - /** @var string|null|false */ - private $tmpnam; + private null|string|bool $tmpnam; private Stream $stream; @@ -51,7 +51,7 @@ protected function setUp(): void protected function tearDown(): void { - if ($this->tmpnam && file_exists($this->tmpnam)) { + if (is_string($this->tmpnam) && $this->tmpnam !== '' && file_exists($this->tmpnam)) { unlink($this->tmpnam); } } diff --git a/test/UploadedFileTest.php b/test/UploadedFileTest.php index fd2149c6..8129b7c3 100644 --- a/test/UploadedFileTest.php +++ b/test/UploadedFileTest.php @@ -33,8 +33,7 @@ final class UploadedFileTest extends TestCase { - /** @var false|null|string */ - private $orgFile; + private null|string|bool $orgFile; /** @var mixed */ private $tmpFile;