From acd07120d5f616b9ab4f9b86f0548be34cc000e2 Mon Sep 17 00:00:00 2001 From: Slawomir Dolzycki-Uchto Date: Thu, 7 Dec 2023 13:24:28 +0100 Subject: [PATCH 1/3] IBX-5827: Replaced deprecated string interpolation (PHP 8.2+) (#386) * IBX-5827: Replaced deprecated string interpolation (PHP 8.2+) * IBX-5827: Added custom ErrorHandler to hide deprecations in PHP 8.2 * fix: Split test into separate cases * fix: Support PHP 7.3 * fix: Coding Standards * fix: Code review --- composer.json | 3 +- .../Features/Context/UserContext.php | 2 +- eZ/Publish/API/Repository/Tests/BaseTest.php | 2 +- .../Tests/ContentTypeServiceTest.php | 2 +- .../Core/FieldType/GatewayBasedStorage.php | 2 +- .../Persistence/Cache/AbstractHandler.php | 4 +- .../Cache/AbstractInMemoryHandler.php | 2 +- .../Core/Persistence/Cache/ContentHandler.php | 4 +- .../Php82HideDeprecationsErrorHandler.php | 24 ++++++++++ .../Php82HideDeprecationsErrorHandlerTest.php | 48 +++++++++++++++++++ 10 files changed, 83 insertions(+), 10 deletions(-) create mode 100644 src/contracts/MVC/Symfony/ErrorHandler/Php82HideDeprecationsErrorHandler.php create mode 100644 tests/lib/MVC/Symfony/ErrorHandler/Php82HideDeprecationsErrorHandlerTest.php diff --git a/composer.json b/composer.json index 208d172a7d..8cbab703f1 100644 --- a/composer.json +++ b/composer.json @@ -69,7 +69,8 @@ "matthiasnoback/symfony-dependency-injection-test": "^4.1", "symfony/phpunit-bridge": "^5.1", "symfony/proxy-manager-bridge": "^5.3", - "phpstan/phpstan": "^1.2" + "phpstan/phpstan": "^1.2", + "symfony/runtime": "^5.3.0" }, "conflict": { "friendsofphp/php-cs-fixer": "3.5.0", diff --git a/eZ/Bundle/EzPublishCoreBundle/Features/Context/UserContext.php b/eZ/Bundle/EzPublishCoreBundle/Features/Context/UserContext.php index 2f4f835741..885ac2569b 100644 --- a/eZ/Bundle/EzPublishCoreBundle/Features/Context/UserContext.php +++ b/eZ/Bundle/EzPublishCoreBundle/Features/Context/UserContext.php @@ -544,7 +544,7 @@ public function assertUserWithNameExistsWithFields($username, TableNode $table) */ private function findNonExistingUserEmail($username = 'User') { - $email = "${username}@ez.no"; + $email = "{$username}@ez.no"; if ($this->checkUserExistenceByEmail($email)) { return $email; } diff --git a/eZ/Publish/API/Repository/Tests/BaseTest.php b/eZ/Publish/API/Repository/Tests/BaseTest.php index fec0373452..ef4b0ecd76 100644 --- a/eZ/Publish/API/Repository/Tests/BaseTest.php +++ b/eZ/Publish/API/Repository/Tests/BaseTest.php @@ -171,7 +171,7 @@ protected function getSetupFactory(): SetupFactory if (null === $this->setupFactory) { if (false === ($setupClass = getenv('setupFactory'))) { $setupClass = LegacySetupFactory::class; - putenv("setupFactory=${setupClass}"); + putenv("setupFactory={$setupClass}"); } if (false === getenv('fixtureDir')) { diff --git a/eZ/Publish/API/Repository/Tests/ContentTypeServiceTest.php b/eZ/Publish/API/Repository/Tests/ContentTypeServiceTest.php index 79f9e425c5..b76bab4f72 100644 --- a/eZ/Publish/API/Repository/Tests/ContentTypeServiceTest.php +++ b/eZ/Publish/API/Repository/Tests/ContentTypeServiceTest.php @@ -841,7 +841,7 @@ public function testCreateContentTypeStructValues(array $data) $this->assertEquals( $typeCreate->$propertyName, $contentType->$propertyName, - "Did not assert that property '${propertyName}' is equal on struct and resulting value object" + "Did not assert that property '{$propertyName}' is equal on struct and resulting value object" ); break; } diff --git a/eZ/Publish/Core/FieldType/GatewayBasedStorage.php b/eZ/Publish/Core/FieldType/GatewayBasedStorage.php index 6e0eedf5b1..261c115029 100644 --- a/eZ/Publish/Core/FieldType/GatewayBasedStorage.php +++ b/eZ/Publish/Core/FieldType/GatewayBasedStorage.php @@ -87,7 +87,7 @@ protected function getGateway(array $context) ); if (!isset($this->gateways[$context['identifier']])) { - throw new \OutOfBoundsException("No gateway for ${context['identifier']} available."); + throw new \OutOfBoundsException("No gateway for {$context['identifier']} available."); } $gateway = $this->gateways[$context['identifier']]; diff --git a/eZ/Publish/Core/Persistence/Cache/AbstractHandler.php b/eZ/Publish/Core/Persistence/Cache/AbstractHandler.php index f5509d6c0f..9daaec953a 100644 --- a/eZ/Publish/Core/Persistence/Cache/AbstractHandler.php +++ b/eZ/Publish/Core/Persistence/Cache/AbstractHandler.php @@ -66,11 +66,11 @@ public function __construct( /** * Helper for getting multiple cache items in one call and do the id extraction for you. * - * Cache items must be stored with a key in the following format "${keyPrefix}${id}", like "ez-content-info-${id}", + * Cache items must be stored with a key in the following format "{$keyPrefix}{$id}", like "ez-content-info-{$id}", * in order for this method to be able to prefix key on id's and also extract key prefix afterwards. * * It also optionally supports a key suffixs, for use on a variable argument that affects all lookups, - * like translations, i.e. "ez-content-${id}-${translationKey}" where $keySuffixes = [$id => "-${translationKey}"]. + * like translations, i.e. "ez-content-{$id}-{$translationKey}" where $keySuffixes = [$id => "-{$translationKey}"]. * * @param array $ids * @param string $keyPrefix E.g "ez-content-" diff --git a/eZ/Publish/Core/Persistence/Cache/AbstractInMemoryHandler.php b/eZ/Publish/Core/Persistence/Cache/AbstractInMemoryHandler.php index 3cbd1dcb7c..7424f45d26 100644 --- a/eZ/Publish/Core/Persistence/Cache/AbstractInMemoryHandler.php +++ b/eZ/Publish/Core/Persistence/Cache/AbstractInMemoryHandler.php @@ -183,7 +183,7 @@ final protected function getListCacheValue( * Load items from in-memory cache, symfony cache pool or backend in that order. * If not cached the returned objects will be placed in cache. * - * Cache items must be stored with a key in the following format "${keyPrefix}${id}", like "ez-content-info-${id}", + * Cache items must be stored with a key in the following format "{$keyPrefix}{$id}", like "ez-content-info-{$id}", * in order for this method to be able to prefix key on id's and also extract key prefix afterwards. * * @param array $ids diff --git a/eZ/Publish/Core/Persistence/Cache/ContentHandler.php b/eZ/Publish/Core/Persistence/Cache/ContentHandler.php index 4e77e1ddf6..eacaa7fcd8 100644 --- a/eZ/Publish/Core/Persistence/Cache/ContentHandler.php +++ b/eZ/Publish/Core/Persistence/Cache/ContentHandler.php @@ -128,7 +128,7 @@ public function copy($contentId, $versionNo = null, $newOwnerId = null) */ public function load($contentId, $versionNo = null, array $translations = null) { - $keySuffix = $versionNo ? "-${versionNo}-" : '-'; + $keySuffix = $versionNo ? "-{$versionNo}-" : '-'; $keySuffix .= empty($translations) ? self::ALL_TRANSLATIONS_KEY : implode('|', $translations); return $this->getCacheValue( @@ -235,7 +235,7 @@ function () use ($remoteId) { */ public function loadVersionInfo($contentId, $versionNo = null) { - $keySuffix = $versionNo ? "-${versionNo}" : ''; + $keySuffix = $versionNo ? "-{$versionNo}" : ''; $cacheItem = $this->cache->getItem( $this->cacheIdentifierGenerator->generateKey( self::CONTENT_VERSION_INFO_IDENTIFIER, diff --git a/src/contracts/MVC/Symfony/ErrorHandler/Php82HideDeprecationsErrorHandler.php b/src/contracts/MVC/Symfony/ErrorHandler/Php82HideDeprecationsErrorHandler.php new file mode 100644 index 0000000000..d9521c6c68 --- /dev/null +++ b/src/contracts/MVC/Symfony/ErrorHandler/Php82HideDeprecationsErrorHandler.php @@ -0,0 +1,24 @@ + 80200) { + error_reporting(E_ALL & ~E_DEPRECATED); + } + } +} diff --git a/tests/lib/MVC/Symfony/ErrorHandler/Php82HideDeprecationsErrorHandlerTest.php b/tests/lib/MVC/Symfony/ErrorHandler/Php82HideDeprecationsErrorHandlerTest.php new file mode 100644 index 0000000000..a4eadebe86 --- /dev/null +++ b/tests/lib/MVC/Symfony/ErrorHandler/Php82HideDeprecationsErrorHandlerTest.php @@ -0,0 +1,48 @@ += 8.2.0 + */ +final class Php82HideDeprecationsErrorHandlerTest extends TestCase +{ + /** @var int */ + private $originalErrorReporting; + + protected function setUp(): void + { + $this->originalErrorReporting = error_reporting(); + } + + protected function tearDown(): void + { + error_reporting($this->originalErrorReporting); + restore_error_handler(); + } + + public function testRegisterDebug(): void + { + Php82HideDeprecationsErrorHandler::register(true); + $errorReporting = error_reporting(); + + $this->assertSame(E_ALL & ~E_DEPRECATED, $errorReporting); + } + + public function testRegisterNoDebug(): void + { + Php82HideDeprecationsErrorHandler::register(false); + $errorReporting = error_reporting(); + + $this->assertSame(E_ALL & ~E_DEPRECATED, $errorReporting); + } +} From e8a5aac3e98df3fee1b1ed6fa409a991e6a9a14b Mon Sep 17 00:00:00 2001 From: Nattfarinn Date: Thu, 7 Dec 2023 14:35:34 +0100 Subject: [PATCH 2/3] IBX-5827: Adapted code to Ibexa coding standards --- src/bundle/IO/Command/MigrateFilesCommand.php | 2 +- .../ErrorHandler/Php82HideDeprecationsErrorHandlerTest.php | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/bundle/IO/Command/MigrateFilesCommand.php b/src/bundle/IO/Command/MigrateFilesCommand.php index f5c56f04dc..213369d40f 100644 --- a/src/bundle/IO/Command/MigrateFilesCommand.php +++ b/src/bundle/IO/Command/MigrateFilesCommand.php @@ -127,7 +127,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int } $output->writeln([ - "Migrating from '$fromHandlers[0],$fromHandlers[1]' to '$toHandlers[0],$toHandlers[1]'", + "Migrating from '{$fromHandlers[0]},{$fromHandlers[1]}' to '{$toHandlers[0]},{$toHandlers[1]}'", '', ]); diff --git a/tests/lib/MVC/Symfony/ErrorHandler/Php82HideDeprecationsErrorHandlerTest.php b/tests/lib/MVC/Symfony/ErrorHandler/Php82HideDeprecationsErrorHandlerTest.php index a4eadebe86..d66552b029 100644 --- a/tests/lib/MVC/Symfony/ErrorHandler/Php82HideDeprecationsErrorHandlerTest.php +++ b/tests/lib/MVC/Symfony/ErrorHandler/Php82HideDeprecationsErrorHandlerTest.php @@ -16,8 +16,7 @@ */ final class Php82HideDeprecationsErrorHandlerTest extends TestCase { - /** @var int */ - private $originalErrorReporting; + private int $originalErrorReporting; protected function setUp(): void { From 488c86d6d5d8321be8516e93acab89026dd88835 Mon Sep 17 00:00:00 2001 From: Nattfarinn Date: Mon, 11 Dec 2023 13:23:45 +0100 Subject: [PATCH 3/3] Coding Standards: Update PHPStan baseline --- phpstan-baseline.neon | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index c63300cc05..52e0ea3382 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -15625,6 +15625,11 @@ parameters: count: 1 path: src/lib/Persistence/Legacy/Content/FieldValue/Converter/ImageConverter.php + - + message: "#^Cannot call method addAttribute\\(\\) on SimpleXMLElement\\|null\\.$#" + count: 1 + path: src/lib/Persistence/Legacy/Content/FieldValue/Converter/ImageConverter.php + - message: "#^Cannot call method getAttribute\\(\\) on DOMElement\\|null\\.$#" count: 6 @@ -20215,11 +20220,6 @@ parameters: count: 1 path: src/lib/Repository/ContentService.php - - - message: "#^Unreachable statement \\- code above always terminates\\.$#" - count: 1 - path: src/lib/Repository/ContentService.php - - message: "#^Method Ibexa\\\\Core\\\\Repository\\\\ContentTypeService\\:\\:__construct\\(\\) has parameter \\$settings with no value type specified in iterable type array\\.$#" count: 1