Skip to content

Fix #1215: Replace removed money_format() with NumberFormatter#1607

Open
mambax7 wants to merge 2 commits intoXOOPS:masterfrom
mambax7:fix/1215-money-format-php8
Open

Fix #1215: Replace removed money_format() with NumberFormatter#1607
mambax7 wants to merge 2 commits intoXOOPS:masterfrom
mambax7:fix/1215-money-format-php8

Conversation

@mambax7
Copy link
Collaborator

@mambax7 mambax7 commented Feb 17, 2026

Summary

  • Replaces the deprecated/removed money_format() PHP function (removed in PHP 8.0) with NumberFormatter::formatCurrency() from the intl extension in the English locale class
  • The $format parameter is retained for backward compatibility but is no longer used, as NumberFormatter handles locale-specific formatting internally
  • setlocale(LC_MONETARY) call removed as it is not needed by NumberFormatter

Test plan

  • Verify currency formatting still works in any XOOPS page that calls XoopsLocal::money_format()
  • Confirm no PHP deprecation/error on PHP 8.0+

Fixes #1215

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Refactor
    • Improved currency formatting to use a more reliable, modern approach with graceful fallback when advanced locale support isn't available. Preserves the existing public API and behavior, ensures consistent USD formatting, and reduces errors in edge cases.

money_format() was removed in PHP 8.0. Replace the English locale
implementation with NumberFormatter::formatCurrency() from the
intl extension, which is the standard PHP approach for locale-aware
currency formatting.

The $format parameter is kept for backward compatibility but ignored,
as NumberFormatter handles locale-specific formatting internally.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings February 17, 2026 02:10
@coderabbitai
Copy link

coderabbitai bot commented Feb 17, 2026

Walkthrough

Replaced the deprecated PHP money_format() usage in htdocs/language/english/locale.php with a dual-path implementation: use NumberFormatter (intl) for en_US currency formatting when available, otherwise fall back to a simple number_format() USD string. Function signature remains unchanged; docblock updated.

Changes

Cohort / File(s) Summary
Locale money formatting
htdocs/language/english/locale.php
Reimplemented money_format($format, $number) to avoid the removed PHP 8 money_format(): lazily initialize an \NumberFormatter for en_US currency when the intl extension is present, use formatCurrency(), and fall back to number_format() producing a USD-formatted string if intl is unavailable or formatting fails. Docblock updated (parameter types and return type) and an unused parameter suppression added.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically references the issue being fixed (#1215) and describes the main change: replacing the removed money_format() function with NumberFormatter.
Linked Issues check ✅ Passed The PR implements the core requirement from #1215: replacing the removed PHP 8.0 money_format() with NumberFormatter, providing a supported currency formatting approach while maintaining backward compatibility.
Out of Scope Changes check ✅ Passed All changes in the PR are directly scoped to addressing issue #1215—updating the money_format() implementation in the English locale file with no extraneous modifications.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@htdocs/language/english/locale.php`:
- Line 72: Import the NumberFormatter class and remove the inline FQN: add a
top-level use NumberFormatter; statement (after the opening PHP tag / namespace)
and change the instantiation that currently uses \NumberFormatter (the $fmt =
new \NumberFormatter(...) call) to use the short class name (new
NumberFormatter(...)) so the file follows PSR-12 and resolves the PHPMD
MissingImport hint.
- Around line 66-68: Update the docblock for the function in locale.php that
declares the unused parameter $format so IDEs and PHPMD stop flagging it: mark
the parameter as deprecated in the `@param` line (e.g. "@param string $format
Deprecated - kept for BC") or add an `@internal` note, and additionally add a
PHPMD suppression annotation (e.g.
"@SuppressWarnings(PHPMD.UnusedFormalParameter)") or an equivalent
PhpStorm/PSalm suppression in the docblock; ensure you reference the same
$format parameter name so tools pick up the intent.
- Around line 72-74: The call to NumberFormatter::formatCurrency in locale.php
can return false on failure, so update the code around
$fmt->formatCurrency((float)$number, 'USD') to check the result and ensure a
string is returned; if formatCurrency returns false, either log/raise an error
(e.g., throw an exception) or return a safe fallback string (like a plain
numeric formatting with a USD suffix), and propagate that change from the
function so its return type remains string—locate the $fmt variable and the
formatCurrency call to implement the check and fallback handling.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the English locale implementation to avoid PHP 8+ incompatibility from the removed money_format() function by switching to intl’s NumberFormatter.

Changes:

  • Replace money_format() usage with NumberFormatter::formatCurrency() in the English locale.
  • Remove the per-call setlocale(LC_MONETARY, ...) usage and update the method PHPDoc to reflect the new behavior.

- Add extension_loaded('intl') guard with number_format() fallback for
  environments where the intl extension is not installed
- Cache NumberFormatter in a static variable to avoid repeated ICU
  initialization on every call
- Handle formatCurrency() returning false with the same fallback
- Clarify $format docblock: 'legacy money_format()-style format string'
- Add @SuppressWarnings(PHPMD.UnusedFormalParameter) for $format

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@sonarqubecloud
Copy link

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@htdocs/language/english/locale.php`:
- Around line 74-86: The static analyzer flags the inline FQN "\NumberFormatter"
as a MissingImport; to fix, add a top-level "use NumberFormatter;" import and
update usage in the function (the static $fmt initialization and any other
references to \NumberFormatter) to use the imported class name "NumberFormatter"
instead of the leading-backslash FQN so the code follows PSR-12 import
conventions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"money_format" removed in PHP 8.0

2 participants

Comments