OpenMage / magento-lts

Official OpenMage LTS codebase | Migrate easily from Magento Community Edition in minutes! Download the source code for free or contribute to OpenMage LTS | Security vulnerability patches, bug fixes, performance improvements and more.
https://www.openmage.org
Open Software License 3.0
866 stars 438 forks source link

Deprecated functionality: strlen(): Passing null to parameter #1 - Abstract.php #4076

Closed ADDISON74 closed 15 hours ago

ADDISON74 commented 6 days ago

I am getting this error with PHP 8.3 in the system.log file:

Deprecated functionality: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/html/app/code/core/Mage/Wishlist/Controller/Abstract.php on line 88

/app/code/core/Mage/Wishlist/Controller/Abstract.php on line 88

In order to reproduce this issue log into a customer account in the Frontend then add any product to the Wishlist. You will be redirected to the customer account. A part of the URL is wishlist/index/index/wishlist_id/[PRODUCT_ID]/. Press the [Add All to the Cart] button. In my case I am getting an error message because the product has custom options and I have to choose them. If you take a look into the /var/log directory you will see a nice system.log file, having 371 kB as size and inside the same line 1745 times.

ADDISON74 commented 5 days ago

This is before Magento 1.8.1.0

$qtys = $this->getRequest()->getParam('qty');

This is after Magento 1.8.1.0

$qtysString = $this->getRequest()->getParam('qty');
$qtys =  array_filter(json_decode($qtysString), 'strlen');

This PR #2335 changed strlen to \strlen. I don't know what was its scope.

$qtys = array_filter(json_decode($qtysString), '\strlen');
ADDISON74 commented 5 days ago

Before PHP 8.1 this line would remove empty values from an array like null, false, '', but 0 and '0' will be kept. Starting with PHP 8.1 a deprecated error is issued as I mentioned in the initial report. After doing a research on the Internet, there are a few implementations to solve this issue:

This one is readable

$qtys = array_filter(json_decode($qtysString), function ($qtysString) { return strlen($qtysString ?? ''); });

A shorter version

$qtys = array_filter(json_decode($qtysString), fn($qtysString) => strlen($qtysString ?? ''));

Suppress the warning with @

$qtys = @array_filter(json_decode($qtysString), '\strlen');

I would like to get some feedback which implementation should be used.

Please note there is another file with this issue

https://github.com/OpenMage/magento-lts/blob/2b907ad6ed5edd66a419f7671beb6d47d2d8d6b5/app/code/core/Mage/ImportExport/Model/Import/Entity/Product.php#L1973C4-L1980C6

fballiano commented 5 days ago

arrow functions became available in php7.4 so all and all I think I like the shorter version :-)

ADDISON74 commented 15 hours ago

Fixed in #4083.