From f18ed3a53130a8908cd54ec1288c74366e147145 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Wed, 26 Mar 2025 13:57:53 +0200 Subject: [PATCH] ref --- .../Espo/Core/Currency/CalculatorUtil.php | 33 +++++++++++++++++-- application/Espo/Core/Currency/Converter.php | 4 +++ application/Espo/Core/Field/Currency.php | 7 ++-- .../ExtGroup/CurrencyGroup/ConvertType.php | 2 ++ 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/application/Espo/Core/Currency/CalculatorUtil.php b/application/Espo/Core/Currency/CalculatorUtil.php index 9e1fca6062..1a76a51be6 100644 --- a/application/Espo/Core/Currency/CalculatorUtil.php +++ b/application/Espo/Core/Currency/CalculatorUtil.php @@ -35,6 +35,11 @@ class CalculatorUtil { private const SCALE = 14; + /** + * @param numeric-string $arg1 + * @param numeric-string $arg2 + * @return numeric-string + */ public static function add(string $arg1, string $arg2): string { if (!function_exists('bcadd')) { @@ -50,6 +55,11 @@ class CalculatorUtil ); } + /** + * @param numeric-string $arg1 + * @param numeric-string $arg2 + * @return numeric-string + */ public static function subtract(string $arg1, string $arg2): string { if (!function_exists('bcsub')) { @@ -65,6 +75,11 @@ class CalculatorUtil ); } + /** + * @param numeric-string $arg1 + * @param numeric-string $arg2 + * @return numeric-string + */ public static function multiply(string $arg1, string $arg2): string { if (!function_exists('bcmul')) { @@ -80,6 +95,11 @@ class CalculatorUtil ); } + /** + * @param numeric-string $arg1 + * @param numeric-string $arg2 + * @return numeric-string + */ public static function divide(string $arg1, string $arg2): string { if (!function_exists('bcdiv')) { @@ -88,20 +108,23 @@ class CalculatorUtil ); } - /** @var ?string $result */ $result = bcdiv( $arg1, $arg2, self::SCALE ); - if ($result === null) { + if ($result === null) { /** @phpstan-ignore-line */ throw new DivisionByZeroError(); } return $result; } + /** + * @param numeric-string $arg + * @return numeric-string + */ public static function round(string $arg, int $precision = 0): string { if (!function_exists('bcadd')) { @@ -114,6 +137,8 @@ class CalculatorUtil $addition = '-' . $addition; } + assert(is_numeric($addition)); + return bcadd( $arg, $addition, @@ -121,6 +146,10 @@ class CalculatorUtil ); } + /** + * @param numeric-string $arg1 + * @param numeric-string $arg2 + */ public static function compare(string $arg1, string $arg2): int { if (!function_exists('bccomp')) { diff --git a/application/Espo/Core/Currency/Converter.php b/application/Espo/Core/Currency/Converter.php index 37ee09199b..2dafa9082b 100644 --- a/application/Espo/Core/Currency/Converter.php +++ b/application/Espo/Core/Currency/Converter.php @@ -100,6 +100,10 @@ class Converter return $this->convert($value, $targetCurrencyCode); } + /** + * @param numeric-string $amount + * @return numeric-string + */ private function convertAmount(string $amount, float $rate, float $targetRate): string { return CalculatorUtil::divide( diff --git a/application/Espo/Core/Field/Currency.php b/application/Espo/Core/Field/Currency.php index 7b14ac1c6c..b5d733b10d 100644 --- a/application/Espo/Core/Field/Currency.php +++ b/application/Espo/Core/Field/Currency.php @@ -39,11 +39,12 @@ use InvalidArgumentException; */ class Currency { + /** @var numeric-string */ private string $amount; private string $code; /** - * @param string|float $amount An amount. + * @param numeric-string|float $amount An amount. * @param string $code A currency code. * @throws RuntimeException */ @@ -67,6 +68,8 @@ class Currency /** * Get an amount as string. + * + * @return numeric-string */ public function getAmountAsString(): string { @@ -194,7 +197,7 @@ class Currency /** * Create from an amount and code. * - * @param string|float $amount An amount. + * @param numeric-string|float $amount An amount. * @param string $code A currency code. * @throws RuntimeException */ diff --git a/application/Espo/Core/Formula/Functions/ExtGroup/CurrencyGroup/ConvertType.php b/application/Espo/Core/Formula/Functions/ExtGroup/CurrencyGroup/ConvertType.php index 5b6a31499e..cbf1b9d7ca 100644 --- a/application/Espo/Core/Formula/Functions/ExtGroup/CurrencyGroup/ConvertType.php +++ b/application/Espo/Core/Formula/Functions/ExtGroup/CurrencyGroup/ConvertType.php @@ -70,6 +70,8 @@ class ConvertType implements Func throw BadArgumentType::create(3, 'string'); } + assert(is_numeric($amount)); + $value = Currency::create($amount, $fromCode); $convertedValue = $this->converter->convert($value, $toCode);