url-normalizer icon indicating copy to clipboard operation
url-normalizer copied to clipboard

Updating to PHP 8.1 causes a "PHP Deprecated" message to show up

Open wahabmirjan opened this issue 2 years ago • 0 comments

I use Normalizer in my code, and after updating to PHP 8.1, I started getting this message

[16-Nov-2022 16:24:31 UTC] PHP Deprecated: Automatic conversion of false to array is deprecated in /var/www/dev-server/wp-content/plugins/my-plugin/vendors/url-normalizer/Normalizer.php on line 375

After further investigating, it seems that the issue is in the mbParseUrl($url) function. PHP 8.1 now does not like the following:

$result = false;
/// some code here
$result[$key] = urldecode(str_replace($replacements, $entities, $value));
/// some more code
return $result;

In other words, you cannot implicitly convert a boolean to an array. So here is a suggested solution

$result = [];
/// some code here
$result[$key] = urldecode(str_replace($replacements, $entities, $value));
/// some more code
return (empty($result)) ? false : $result;

This is obviously one solution. Another solution is:

$result = false;
/// some code here
if(!is_array($result)) $result = [];
$result[$key] = urldecode(str_replace($replacements, $entities, $value));
/// some more code
return $result;

Anyways, those are suggested solutions. Thanks.

wahabmirjan avatar Nov 16 '22 16:11 wahabmirjan