Fix PHP 8.5 deprecation: normalize locale before using as array key in Currency#40816
Conversation
|
Hi @ajmalbuffy. Thank you for your contribution!
Allowed build names are:
You can find more information about the builds here For more details, review the Code Contributions documentation. |
|
Thank you for your contribution! The Magento core engineering team is working on the issue which you have addressed in this PR. Team will cherry pick the commits from your PR and may do further implementation to cover few more scenarios as needed. We will reach out to you if we need more information. For now, you can pause work on this PR. Thank you once again! |
Problem
When executing currency resolution in Magento, PHP 8.5 emits a deprecation warning:
Deprecated: Using null as an array offset is deprecated.The issue originates from
Magento\Framework\Locale\Currency::getCurrency()wheregetLocale()result was used directly as an array key without being normalized to a string first.Fix
Extract locale into a
$localevariable, normalized to a string at the top of the methodand reuse it consistently throughout, replacing all 4 direct
getLocale()calls.Testing
Verified deprecation warning appears before fix and disappears after fix on PHP 8.5.6
Fixes #40814
Description (*)
On PHP 8.5, a deprecation warning is triggered in
Magento\Framework\Locale\Currency::getCurrency()because the return value of
getLocale()was used directly as an array key without being normalizedto a string first. PHP 8.5 deprecates using null as an array offset.
The fix extracts the locale into a
$localevariable, normalized to string using(string)castwith a null coalescing fallback, and reuses it consistently throughout the method, replacing all
direct
getLocale()calls.Related Pull Requests
None
Fixed Issues (if relevant)
Manual testing scenarios (*)
getCurrency('USD').Deprecated: Using null as an array offset is deprecated.$locale = (string) ($this->_localeResolver->getLocale() ?? '');Questions or comments
This fix also improves performance slightly by resolving the locale once per method call
instead of calling
getLocale()multiple times.Contribution checklist (*)