From f255e60e5bf4f36040a215f18f9e11a39c425422 Mon Sep 17 00:00:00 2001 From: Ihor Sviziev Date: Thu, 1 May 2025 17:47:09 +0200 Subject: [PATCH 1/2] Remove legacy compatibility code for PHP versions < 8.1 --- app/bootstrap.php | 15 ++----------- dev/tests/static/framework/bootstrap.php | 9 -------- dev/tests/unit/framework/bootstrap.php | 12 +---------- .../DB/DataConverter/SerializedToJson.php | 21 +++---------------- .../DB/Test/Unit/DB/Statement/MysqlTest.php | 5 +---- 5 files changed, 7 insertions(+), 55 deletions(-) diff --git a/app/bootstrap.php b/app/bootstrap.php index a7aea8094f816..2e99d752b06cc 100644 --- a/app/bootstrap.php +++ b/app/bootstrap.php @@ -13,9 +13,8 @@ } #ini_set('display_errors', 1); -/* PHP version validation */ -if (!defined('PHP_VERSION_ID') || PHP_VERSION_ID < 80100) { - if (PHP_SAPI == 'cli') { +if (PHP_VERSION_ID < 80100) { + if (PHP_SAPI === 'cli') { echo 'Magento supports PHP 8.1.0 or later. ' . 'Please read https://experienceleague.adobe.com/docs/commerce-operations/installation-guide/system-requirements.html'; } else { @@ -31,16 +30,6 @@ exit(1); } -// PHP 8 compatibility. Define constants that are not present in PHP < 8.0 -if (!defined('PHP_VERSION_ID') || PHP_VERSION_ID < 80000) { - if (!defined('T_NAME_QUALIFIED')) { - define('T_NAME_QUALIFIED', 24001); - } - if (!defined('T_NAME_FULLY_QUALIFIED')) { - define('T_NAME_FULLY_QUALIFIED', 24002); - } -} - require_once __DIR__ . '/autoload.php'; // Sets default autoload mappings, may be overridden in Bootstrap::create \Magento\Framework\App\Bootstrap::populateAutoloader(BP, []); diff --git a/dev/tests/static/framework/bootstrap.php b/dev/tests/static/framework/bootstrap.php index 28f8824221f5e..36a2966879a42 100644 --- a/dev/tests/static/framework/bootstrap.php +++ b/dev/tests/static/framework/bootstrap.php @@ -21,15 +21,6 @@ define('TESTS_TEMP_DIR', dirname(__DIR__) . '/tmp'); } -// PHP 8 compatibility. Define constants that are not present in PHP < 8.0 -if (!defined('PHP_VERSION_ID') || PHP_VERSION_ID < 80000) { - if (!defined('T_NAME_QUALIFIED')) { - define('T_NAME_QUALIFIED', 24001); - } - if (!defined('T_NAME_FULLY_QUALIFIED')) { - define('T_NAME_FULLY_QUALIFIED', 24002); - } -} setCustomErrorHandler(); diff --git a/dev/tests/unit/framework/bootstrap.php b/dev/tests/unit/framework/bootstrap.php index 31eab08d1221a..24902cfda984b 100644 --- a/dev/tests/unit/framework/bootstrap.php +++ b/dev/tests/unit/framework/bootstrap.php @@ -10,16 +10,6 @@ define('TESTS_TEMP_DIR', dirname(__DIR__) . '/tmp'); } -// PHP 8 compatibility. Define constants that are not present in PHP < 8.0 -if (!defined('PHP_VERSION_ID') || PHP_VERSION_ID < 80000) { - if (!defined('T_NAME_QUALIFIED')) { - define('T_NAME_QUALIFIED', 24001); - } - if (!defined('T_NAME_FULLY_QUALIFIED')) { - define('T_NAME_FULLY_QUALIFIED', 24002); - } -} - require_once __DIR__ . '/autoload.php'; setCustomErrorHandler(); @@ -59,7 +49,7 @@ function ($errNo, $errStr, $errFile, $errLine) { E_USER_DEPRECATED => 'User Deprecated', ]; - $errName = isset($errorNames[$errNo]) ? $errorNames[$errNo] : ""; + $errName = $errorNames[$errNo] ?? ""; throw new \PHPUnit\Framework\Exception( sprintf("%s: %s in %s:%s.", $errName, $errStr, $errFile, $errLine), diff --git a/lib/internal/Magento/Framework/DB/DataConverter/SerializedToJson.php b/lib/internal/Magento/Framework/DB/DataConverter/SerializedToJson.php index bc31cb91ecede..9655e530527bf 100644 --- a/lib/internal/Magento/Framework/DB/DataConverter/SerializedToJson.php +++ b/lib/internal/Magento/Framework/DB/DataConverter/SerializedToJson.php @@ -95,15 +95,7 @@ protected function unserializeValue($value) /** * Encode value with json encoder. * - * In PHP version < 7.1.0 serialize() uses PG(serialize_precision) which set to 17 be default. - * Since json_encode() uses EG(precision) which set to 14 be default, json_encode() removes lower digits of - * fraction parts and destroys original value even if PHP's float could hold more precise float value. - * To prevent this issue EG(precision) is set to 17 to be equal with PG(serialize_precision) during - * data converting from serialized format to JSON. - * - * In PHP version >= 7.1.0 serialize() and json_encode() use PG(serialize_precision) which set to -1 be default. - * Setting -1 uses better algorithm for rounding float numbers. - * But for data consistency during converting process PG(serialize_precision) is set to 17. + * For data consistency during converting process PG(serialize_precision) is set to 17. * * @param string $value * @return string @@ -111,20 +103,13 @@ protected function unserializeValue($value) */ protected function encodeJson($value) { - $storedPrecision = ini_get('precision'); $storedSerializePrecision = ini_get('serialize_precision'); - if (PHP_VERSION_ID < 70100) { - // In PHP version < 7.1.0 json_encode() uses EG(precision). - ini_set('precision', 17); - } else { - // In PHP version >= 7.1.0 json_encode() uses PG(serialize_precision). - ini_set('serialize_precision', 17); - } + // In PHP 8.1+ json_encode() uses PG(serialize_precision) + ini_set('serialize_precision', 17); $value = $this->json->serialize($value); - ini_set('precision', $storedPrecision); ini_set('serialize_precision', $storedSerializePrecision); if (json_last_error()) { diff --git a/lib/internal/Magento/Framework/DB/Test/Unit/DB/Statement/MysqlTest.php b/lib/internal/Magento/Framework/DB/Test/Unit/DB/Statement/MysqlTest.php index 45efccba28ca9..4ab1a1804c62d 100644 --- a/lib/internal/Magento/Framework/DB/Test/Unit/DB/Statement/MysqlTest.php +++ b/lib/internal/Magento/Framework/DB/Test/Unit/DB/Statement/MysqlTest.php @@ -189,10 +189,7 @@ private function setQueryStringForPdoStmtMock(string $query): void /* * In PHP 8.1 $queryString is a Typed property, thus it should be initialized before the 1st call. * But it's not automatically initialized in case of Mocking, so we do it here. - * Note: In PHP < 8.1 such assignment prohibited. */ - if (PHP_VERSION_ID >= 80100) { - $this->pdoStatementMock->queryString = $query; - } + $this->pdoStatementMock->queryString = $query; } } From 83398c6a5204f50a10d4516b27be8f3eaf936359 Mon Sep 17 00:00:00 2001 From: Ihor Sviziev Date: Fri, 2 May 2025 10:34:36 +0200 Subject: [PATCH 2/2] Remove legacy compatibility code for PHP versions < 8.1 Fix static test failures --- .../Magento/Framework/DB/DataConverter/SerializedToJson.php | 4 ++-- .../Magento/Framework/DB/Test/Unit/DB/Statement/MysqlTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/internal/Magento/Framework/DB/DataConverter/SerializedToJson.php b/lib/internal/Magento/Framework/DB/DataConverter/SerializedToJson.php index 9655e530527bf..620cc1f0806e1 100644 --- a/lib/internal/Magento/Framework/DB/DataConverter/SerializedToJson.php +++ b/lib/internal/Magento/Framework/DB/DataConverter/SerializedToJson.php @@ -1,7 +1,7 @@