From c6222283fa3f4ac679f8b9ced9a4e23f163e80d0 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Fri, 30 Dec 2022 01:23:10 +0100 Subject: [PATCH] Migrate codebase to PHP 8.1 (#94) --- .github/workflows/continuous-integration.yml | 2 +- composer.json | 14 ++++----- phpcs.xml.dist | 31 +------------------ .../Exception/ExceptionInterface.php | 2 ++ .../Exception/InvalidArgumentException.php | 6 ++-- .../Exception/UnexpectedValueException.php | 14 +++++---- src/Doctrine/Instantiator/Instantiator.php | 31 +++++++------------ .../Instantiator/InstantiatorInterface.php | 6 ++-- .../InstantiatorPerformanceBench.php | 5 +-- .../InvalidArgumentExceptionTest.php | 12 ++++--- .../UnexpectedValueExceptionTest.php | 8 +++-- .../InstantiatorTest/InstantiatorTest.php | 12 ++----- .../AbstractClassAsset.php | 2 ++ .../ArrayObjectAsset.php | 2 ++ .../InstantiatorTestAsset/ExceptionAsset.php | 2 ++ .../FinalExceptionAsset.php | 2 ++ .../InstantiatorTestAsset/PharAsset.php | 2 ++ .../PharExceptionAsset.php | 2 ++ .../SerializableArrayObjectAsset.php | 4 ++- .../SerializableFinalInternalChildAsset.php | 1 + .../InstantiatorTestAsset/SimpleEnumAsset.php | 2 ++ .../SimpleSerializableAsset.php | 6 ++-- .../SimpleTraitAsset.php | 2 ++ .../UnCloneableAsset.php | 2 ++ .../UnserializeExceptionArrayObjectAsset.php | 2 ++ .../WakeUpNoticesAsset.php | 2 ++ .../InstantiatorTestAsset/XMLReaderAsset.php | 2 ++ 27 files changed, 88 insertions(+), 90 deletions(-) diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 8e50868..989f5f7 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -14,5 +14,5 @@ jobs: name: "PHPUnit" uses: "doctrine/.github/.github/workflows/continuous-integration.yml@3.0.0" with: - php-versions: '["7.1", "7.2", "7.3", "7.4", "8.0", "8.1", "8.2"]' + php-versions: '["8.1", "8.2"]' composer-root-version: "1.4" diff --git a/composer.json b/composer.json index fab8172..179145e 100644 --- a/composer.json +++ b/composer.json @@ -16,17 +16,17 @@ } ], "require": { - "php": "^7.1 || ^8.0" + "php": "^8.1" }, "require-dev": { "ext-phar": "*", "ext-pdo": "*", - "doctrine/coding-standard": "^9 || ^11", - "phpbench/phpbench": "^0.16 || ^1", - "phpstan/phpstan": "^1.4", - "phpstan/phpstan-phpunit": "^1", - "phpunit/phpunit": "^7.5 || ^8.5 || ^9.5", - "vimeo/psalm": "^4.30 || ^5.4" + "doctrine/coding-standard": "^11", + "phpbench/phpbench": "^1.2", + "phpstan/phpstan": "^1.9.4", + "phpstan/phpstan-phpunit": "^1.3", + "phpunit/phpunit": "^9.5.27", + "vimeo/psalm": "^5.4" }, "autoload": { "psr-4": { diff --git a/phpcs.xml.dist b/phpcs.xml.dist index d735c3e..76833e8 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -9,35 +9,15 @@ - + src tests - - - - - - - - - - - - src/* - tests/DoctrineTest/InstantiatorTestAsset/SerializableArrayObjectAsset.php - tests/DoctrineTest/InstantiatorTestAsset/SimpleSerializableAsset.php - - - - */src/* - - tests/DoctrineTest/InstantiatorTestAsset/AbstractClassAsset.php @@ -51,13 +31,4 @@ src/Doctrine/Instantiator/Exception/ExceptionInterface.php src/Doctrine/Instantiator/InstantiatorInterface.php - - - - tests/DoctrineTest/InstantiatorTestAsset/SimpleEnumAsset.php - - - - tests/DoctrineTest/InstantiatorTestAsset/SimpleEnumAsset.php - diff --git a/src/Doctrine/Instantiator/Exception/ExceptionInterface.php b/src/Doctrine/Instantiator/Exception/ExceptionInterface.php index e6a5195..1e59192 100644 --- a/src/Doctrine/Instantiator/Exception/ExceptionInterface.php +++ b/src/Doctrine/Instantiator/Exception/ExceptionInterface.php @@ -1,5 +1,7 @@ getName() + $reflectionClass->getName(), )); } @@ -44,7 +46,7 @@ public static function fromEnum(string $className): self { return new self(sprintf( 'The provided class "%s" is an enum, and cannot be instantiated', - $className + $className, )); } } diff --git a/src/Doctrine/Instantiator/Exception/UnexpectedValueException.php b/src/Doctrine/Instantiator/Exception/UnexpectedValueException.php index 4e55ac5..4f70ded 100644 --- a/src/Doctrine/Instantiator/Exception/UnexpectedValueException.php +++ b/src/Doctrine/Instantiator/Exception/UnexpectedValueException.php @@ -1,5 +1,7 @@ getName() + $reflectionClass->getName(), ), 0, - $exception + $exception, ); } @@ -42,7 +44,7 @@ public static function fromUncleanUnSerialization( string $errorString, int $errorCode, string $errorFile, - int $errorLine + int $errorLine, ): self { return new self( sprintf( @@ -50,10 +52,10 @@ public static function fromUncleanUnSerialization( . 'in file "%s" at line "%d"', $reflectionClass->getName(), $errorFile, - $errorLine + $errorLine, ), 0, - new Exception($errorString, $errorCode) + new Exception($errorString, $errorCode), ); } } diff --git a/src/Doctrine/Instantiator/Instantiator.php b/src/Doctrine/Instantiator/Instantiator.php index 10067a0..f803f89 100644 --- a/src/Doctrine/Instantiator/Instantiator.php +++ b/src/Doctrine/Instantiator/Instantiator.php @@ -1,5 +1,7 @@ $className * - * @return object * @phpstan-return T * * @throws ExceptionInterface * * @template T of object */ - public function instantiate($className) + public function instantiate(string $className): object { if (isset(self::$cachedCloneables[$className])) { /** @phpstan-var T */ @@ -84,12 +80,11 @@ public function instantiate($className) * * @phpstan-param class-string $className * - * @return object * @phpstan-return T * * @template T of object */ - private function buildAndCacheFromFactory(string $className) + private function buildAndCacheFromFactory(string $className): object { $factory = self::$cachedInstantiators[$className] = $this->buildFactory($className); $instance = $factory(); @@ -127,14 +122,12 @@ private function buildFactory(string $className): callable '%s:%d:"%s":0:{}', is_subclass_of($className, Serializable::class) ? self::SERIALIZATION_FORMAT_USE_UNSERIALIZER : self::SERIALIZATION_FORMAT_AVOID_UNSERIALIZER, strlen($className), - $className + $className, ); $this->checkIfUnSerializationIsSupported($reflectionClass, $serializedString); - return static function () use ($serializedString) { - return unserialize($serializedString); - }; + return static fn () => unserialize($serializedString); } /** @@ -153,7 +146,7 @@ private function getReflectionClass(string $className): ReflectionClass throw InvalidArgumentException::fromNonExistingClass($className); } - if (PHP_VERSION_ID >= 80100 && enum_exists($className, false)) { + if (enum_exists($className, false)) { throw InvalidArgumentException::fromEnum($className); } @@ -181,7 +174,7 @@ private function checkIfUnSerializationIsSupported(ReflectionClass $reflectionCl $message, $code, $file, - $line + $line, ); return true; diff --git a/src/Doctrine/Instantiator/InstantiatorInterface.php b/src/Doctrine/Instantiator/InstantiatorInterface.php index 10508b5..c6ebe35 100644 --- a/src/Doctrine/Instantiator/InstantiatorInterface.php +++ b/src/Doctrine/Instantiator/InstantiatorInterface.php @@ -1,5 +1,7 @@ $className * - * @return object * @phpstan-return T * * @throws ExceptionInterface * * @template T of object */ - public function instantiate($className); + public function instantiate(string $className): object; } diff --git a/tests/DoctrineTest/InstantiatorPerformance/InstantiatorPerformanceBench.php b/tests/DoctrineTest/InstantiatorPerformance/InstantiatorPerformanceBench.php index 1d6857e..295d49d 100644 --- a/tests/DoctrineTest/InstantiatorPerformance/InstantiatorPerformanceBench.php +++ b/tests/DoctrineTest/InstantiatorPerformance/InstantiatorPerformanceBench.php @@ -1,5 +1,7 @@ getMessage() + $exception->getMessage(), ); } @@ -45,9 +47,9 @@ public function testFromNonExistingTypeWithInterface(): void self::assertSame( sprintf( 'The provided type "%s" is an interface, and cannot be instantiated', - InstantiatorInterface::class + InstantiatorInterface::class, ), - $exception->getMessage() + $exception->getMessage(), ); } @@ -59,9 +61,9 @@ public function testFromAbstractClass(): void self::assertSame( sprintf( 'The provided class "%s" is abstract, and cannot be instantiated', - AbstractClassAsset::class + AbstractClassAsset::class, ), - $exception->getMessage() + $exception->getMessage(), ); } } diff --git a/tests/DoctrineTest/InstantiatorTest/Exception/UnexpectedValueExceptionTest.php b/tests/DoctrineTest/InstantiatorTest/Exception/UnexpectedValueExceptionTest.php index b64fdd2..9e1fff1 100644 --- a/tests/DoctrineTest/InstantiatorTest/Exception/UnexpectedValueExceptionTest.php +++ b/tests/DoctrineTest/InstantiatorTest/Exception/UnexpectedValueExceptionTest.php @@ -1,5 +1,7 @@ getMessage() + $exception->getMessage(), ); } @@ -40,9 +42,9 @@ public function testFromUncleanUnSerialization(): void sprintf( 'Could not produce an instance of "%s" ' . 'via un-serialization, since an error was triggered in file "bar" at line "456"', - AbstractClassAsset::class + AbstractClassAsset::class, ), - $exception->getMessage() + $exception->getMessage(), ); $previous = $exception->getPrevious(); diff --git a/tests/DoctrineTest/InstantiatorTest/InstantiatorTest.php b/tests/DoctrineTest/InstantiatorTest/InstantiatorTest.php index 3e2a2c2..3760e3b 100644 --- a/tests/DoctrineTest/InstantiatorTest/InstantiatorTest.php +++ b/tests/DoctrineTest/InstantiatorTest/InstantiatorTest.php @@ -1,5 +1,7 @@ [InstantiatorInterface::class]; yield 'abstract class' => [AbstractClassAsset::class]; yield 'trait' => [SimpleTraitAsset::class]; - - if (PHP_VERSION_ID < 80100) { - return; - } - yield 'enum' => [SimpleEnumAsset::class]; } } diff --git a/tests/DoctrineTest/InstantiatorTestAsset/AbstractClassAsset.php b/tests/DoctrineTest/InstantiatorTestAsset/AbstractClassAsset.php index 4981d75..bf0bb88 100644 --- a/tests/DoctrineTest/InstantiatorTestAsset/AbstractClassAsset.php +++ b/tests/DoctrineTest/InstantiatorTestAsset/AbstractClassAsset.php @@ -1,5 +1,7 @@