Skip to content

Commit

Permalink
[REF] Deprecate passing a blank currecny to CRM_Utils_Money::format a…
Browse files Browse the repository at this point in the history
…nd also update function call to remove at least one usage of money_format
  • Loading branch information
seamuslee001 committed Dec 3, 2020
1 parent fb19945 commit e9dc523
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
10 changes: 9 additions & 1 deletion CRM/Core/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -1039,7 +1039,15 @@ public static function deprecatedFunctionWarning($newMethod, $oldMethod = NULL)
$callerClass = $dbt[1]['class'] ?? NULL;
$oldMethod = "{$callerClass}::{$callerFunction}";
}
Civi::log()->warning("Deprecated function $oldMethod, use $newMethod.", ['civi.tag' => 'deprecated']);
self::deprecatedWarning("Deprecated function $oldMethod, use $newMethod.");
}

/**
* Output a deprecated notice about a deprecated call path, rather than deprecating a whole function.
* @param string $message
*/
public static function deprecatedWarning($message) {
Civi::log()->warning($message, ['civi.tag' => 'deprecated']);
}

}
Expand Down
21 changes: 11 additions & 10 deletions CRM/Utils/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,15 @@ public static function format($amount, $currency = NULL, $format = NULL, $onlyNu
}

if (!empty($valueFormat) && $valueFormat !== '%!i') {
CRM_Core_Error::deprecatedFunctionWarning('Having a Money Value format other than %!i is deprecated, please report this on the GitLab Issue https://lab.civicrm.org/dev/core/-/issues/1494 with the relevant moneyValueFormat you use.');
CRM_Core_Error::deprecatedWarning('Having a Money Value format other than %!i is deprecated, please report this on the GitLab Issue https://lab.civicrm.org/dev/core/-/issues/1494 with the relevant moneyValueFormat you use.');
}

if (!$currency) {
$currency = $config->defaultCurrency;
}

if ($onlyNumber) {
// money_format() exists only in certain PHP install (CRM-650)
if (is_numeric($amount) and function_exists('money_format')) {
$amount = money_format($valueFormat, $amount);
}
$amount = self::formatLocaleNumericRoundedByCurrency($amount, $currency);
return $amount;
}

Expand All @@ -85,16 +86,16 @@ public static function format($amount, $currency = NULL, $format = NULL, $onlyNu
]);
}

if (!$currency) {
$currency = $config->defaultCurrency;
}

// ensure $currency is a valid currency code
// for backwards-compatibility, also accept one space instead of a currency
if ($currency != ' ' && !array_key_exists($currency, self::$_currencySymbols)) {
throw new CRM_Core_Exception("Invalid currency \"{$currency}\"");
}

if ($currency === ' ') {
CRM_Core_Error::deprecatedWarning('Passing empty currency to CRM_Utils_Money::format is deprecated if you need it for display without currency call CRM_Utils_Money::formatLocaleNumericRounded');
}

$amount = self::formatNumericByFormat($amount, $valueFormat);
// If it contains tags, means that HTML was passed and the
// amount is already converted properly,
Expand Down Expand Up @@ -181,7 +182,7 @@ public static function equals($value1, $value2, $currency) {
*/
protected static function formatLocaleNumeric($amount) {
if (CRM_Core_Config::singleton()->moneyvalueformat !== '%!i') {
CRM_Core_Error::deprecatedFunctionWarning('Having a Money Value format other than !%i is deprecated, please report this on GitLab with the relevant moneyValueFormat you use.');
CRM_Core_Error::deprecatedWarning('Having a Money Value format other than !%i is deprecated, please report this on GitLab with the relevant moneyValueFormat you use.');
}
return self::formatNumericByFormat($amount, CRM_Core_Config::singleton()->moneyvalueformat);
}
Expand Down
7 changes: 0 additions & 7 deletions tests/phpunit/CRM/Utils/MoneyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,6 @@ public function testFormatLocaleNumericRoundedByOptionalPrecision($thousandSepar
$this->assertEquals($result, $expected);
}

/**
* Test that using the space character as a currency works
*/
public function testSpaceCurrency() {
$this->assertEquals(' 8,950.37', CRM_Utils_Money::format(8950.37, ' '));
}

/**
* Test that passing an invalid currency throws an error
*/
Expand Down

0 comments on commit e9dc523

Please sign in to comment.