From 31322c437ebd6c31f7841e556b3763ce8a05989b Mon Sep 17 00:00:00 2001 From: Ng Kiat Siong Date: Sun, 2 Feb 2025 18:27:33 +0800 Subject: [PATCH] Fixed null deprecation in UnserializeArray.php (#4394) * Fixed issue #4352 null deprecation in UnserializeArray.php * Added @ to suppress warnings and notices * Suppress PHPMD ErrorControlOperator * Fix suppress PHPMD ErrorControlOperator * Update app/code/core/Mage/Core/Helper/UnserializeArray.php phpstan fix * Update app/code/core/Mage/Core/Helper/UnserializeArray.php Co-authored-by: Sven Reichel * updated test result * sonar-1 --------- Co-authored-by: Sven Reichel --- app/code/core/Mage/Core/Helper/UnserializeArray.php | 4 +++- tests/unit/Mage/Core/Helper/UnserializeArrayTest.php | 8 +++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/code/core/Mage/Core/Helper/UnserializeArray.php b/app/code/core/Mage/Core/Helper/UnserializeArray.php index d81119d0873..42a48606d0e 100644 --- a/app/code/core/Mage/Core/Helper/UnserializeArray.php +++ b/app/code/core/Mage/Core/Helper/UnserializeArray.php @@ -26,11 +26,13 @@ class Mage_Core_Helper_UnserializeArray * @param string $str * @return array * @throws Exception + * @SuppressWarnings("PHPMD.ErrorControlOperator") */ public function unserialize($str) { try { - $result = unserialize($str, ['allowed_classes' => false]); + $str = is_null($str) ? '' : $str; + $result = @unserialize($str, ['allowed_classes' => false]); if ($result === false && $str !== serialize(false)) { throw new Exception('Error unserializing data.'); } diff --git a/tests/unit/Mage/Core/Helper/UnserializeArrayTest.php b/tests/unit/Mage/Core/Helper/UnserializeArrayTest.php index 8803897b14a..458e49c1f42 100644 --- a/tests/unit/Mage/Core/Helper/UnserializeArrayTest.php +++ b/tests/unit/Mage/Core/Helper/UnserializeArrayTest.php @@ -50,16 +50,18 @@ public function testUnserialize($expectedTesult, $string): void public function provideUnserialize(): Generator { + $errorMessage = 'Error unserializing data.'; + yield 'null' => [ - 'Error unserializing data.', + $errorMessage, null, ]; yield 'empty string' => [ - 'Error unserializing data.', + $errorMessage, '', ]; yield 'random string' => [ - 'unserialize(): Error at offset 0 of 3 bytes', + $errorMessage, 'abc', ]; yield 'valid' => [