From 0de82ed60de184e000987cc460644fe8c5baee0c Mon Sep 17 00:00:00 2001 From: Gary Lockett Date: Fri, 2 Jun 2023 16:09:36 +0100 Subject: [PATCH 1/2] add rector PHP as a dependency with configuration Signed-off-by: Gary Lockett --- .gitattributes | 1 + composer.json | 1 + composer.lock | 125 ++++++++++++++++++++++++++++++++++++++++++++++++- phpcs.xml | 1 + psalm.xml.dist | 1 + rector.php | 19 ++++++++ 6 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 rector.php diff --git a/.gitattributes b/.gitattributes index ed26f59d..ef421eb6 100644 --- a/.gitattributes +++ b/.gitattributes @@ -10,6 +10,7 @@ /phpunit.xml.dist export-ignore /psalm-baseline.xml export-ignore /psalm.xml.dist export-ignore +/rector.php export-ignore /renovate.json export-ignore /test/ export-ignore /psalm/ export-ignore diff --git a/composer.json b/composer.json index 371b97de..a4f19316 100644 --- a/composer.json +++ b/composer.json @@ -48,6 +48,7 @@ "php-http/psr7-integration-tests": "^1.3", "phpunit/phpunit": "^9.5.28", "psalm/plugin-phpunit": "^0.18.4", + "rector/rector": "^0.17.0", "vimeo/psalm": "^5.9" }, "provide": { diff --git a/composer.lock b/composer.lock index 9f009192..dde367f6 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f5e2677fceaed39db15d93cacd111d2a", + "content-hash": "72fc5e2cc181ac465ae72dfc7f56a937", "packages": [ { "name": "psr/http-factory", @@ -1562,6 +1562,68 @@ }, "time": "2022-05-05T11:32:40+00:00" }, + { + "name": "phpstan/phpstan", + "version": "1.10.15", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan.git", + "reference": "762c4dac4da6f8756eebb80e528c3a47855da9bd" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/762c4dac4da6f8756eebb80e528c3a47855da9bd", + "reference": "762c4dac4da6f8756eebb80e528c3a47855da9bd", + "shasum": "" + }, + "require": { + "php": "^7.2|^8.0" + }, + "conflict": { + "phpstan/phpstan-shim": "*" + }, + "bin": [ + "phpstan", + "phpstan.phar" + ], + "type": "library", + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPStan - PHP Static Analysis Tool", + "keywords": [ + "dev", + "static analysis" + ], + "support": { + "docs": "https://phpstan.org/user-guide/getting-started", + "forum": "https://github.com/phpstan/phpstan/discussions", + "issues": "https://github.com/phpstan/phpstan/issues", + "security": "https://github.com/phpstan/phpstan/security/policy", + "source": "https://github.com/phpstan/phpstan-src" + }, + "funding": [ + { + "url": "https://github.com/ondrejmirtes", + "type": "github" + }, + { + "url": "https://github.com/phpstan", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/phpstan/phpstan", + "type": "tidelift" + } + ], + "time": "2023-05-09T15:28:01+00:00" + }, { "name": "phpunit/php-code-coverage", "version": "9.2.26", @@ -2146,6 +2208,67 @@ }, "time": "2021-07-14T16:46:02+00:00" }, + { + "name": "rector/rector", + "version": "0.17.0", + "source": { + "type": "git", + "url": "https://github.com/rectorphp/rector.git", + "reference": "d8da002b107c9b64d464bb48101290d4d078df4b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/rectorphp/rector/zipball/d8da002b107c9b64d464bb48101290d4d078df4b", + "reference": "d8da002b107c9b64d464bb48101290d4d078df4b", + "shasum": "" + }, + "require": { + "php": "^7.2|^8.0", + "phpstan/phpstan": "^1.10.15" + }, + "conflict": { + "rector/rector-doctrine": "*", + "rector/rector-downgrade-php": "*", + "rector/rector-phpunit": "*", + "rector/rector-symfony": "*" + }, + "bin": [ + "bin/rector" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "0.15-dev" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Instant Upgrade and Automated Refactoring of any PHP code", + "keywords": [ + "automation", + "dev", + "migration", + "refactoring" + ], + "support": { + "issues": "https://github.com/rectorphp/rector/issues", + "source": "https://github.com/rectorphp/rector/tree/0.17.0" + }, + "funding": [ + { + "url": "https://github.com/tomasvotruba", + "type": "github" + } + ], + "time": "2023-06-01T09:42:59+00:00" + }, { "name": "sebastian/cli-parser", "version": "1.0.1", diff --git a/phpcs.xml b/phpcs.xml index 6ce2a0b0..2d3e8228 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -13,6 +13,7 @@ + rector.php src test diff --git a/psalm.xml.dist b/psalm.xml.dist index 28ed3276..885fedb7 100644 --- a/psalm.xml.dist +++ b/psalm.xml.dist @@ -10,6 +10,7 @@ errorBaseline="psalm-baseline.xml" > + diff --git a/rector.php b/rector.php new file mode 100644 index 00000000..8fb06624 --- /dev/null +++ b/rector.php @@ -0,0 +1,19 @@ +paths([ + __DIR__ . '/src', + __DIR__ . '/test', + ]); + + // Define set list to upgrade PHP + $rectorConfig->sets([ + LevelSetList::UP_TO_PHP_80, + ]); +}; From 8e8513916f5a93719452c729c036b768275bfdc7 Mon Sep 17 00:00:00 2001 From: Gary Lockett Date: Fri, 2 Jun 2023 16:10:46 +0100 Subject: [PATCH 2/2] 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;