From 437fde9d7e2a422692fceb32de5703a176d19e4c Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Sun, 14 Dec 2025 15:51:36 +0700 Subject: [PATCH] refactor(helpers): Remove unused currency and percentage formatting functions from StringHelpers.php and add NumberHelpers.php for improved organization and maintainability. --- app/Helpers/NumberHelpers.php | 68 +++++++++++++++++++++++++++++++++++ app/Helpers/StringHelpers.php | 65 --------------------------------- composer.json | 3 +- 3 files changed, 70 insertions(+), 66 deletions(-) create mode 100644 app/Helpers/NumberHelpers.php diff --git a/app/Helpers/NumberHelpers.php b/app/Helpers/NumberHelpers.php new file mode 100644 index 0000000..e140eb1 --- /dev/null +++ b/app/Helpers/NumberHelpers.php @@ -0,0 +1,68 @@ +replace('Rp', '') + ->replace('.', '') + ->replace(' ', '') + ->__toString(); + + return (int) $cleaned; + } +} + +if (! function_exists('formatPercentage')) { + /** + * Format value as percentage + * + * @param int $precision Precision (default: 2) + * @param int $maxPrecision Max precision (default: 2) + * @param string $locale Locale for formatting (default: 'id_ID') + */ + function formatPercentage(int|float $value, int $precision = 2, int $maxPrecision = 2, string $locale = 'id_ID'): string + { + return Number::percentage($value, $precision, $maxPrecision, $locale); + } +} + +if (! function_exists('formatDiscount')) { + /** + * Format discount based on voucher type (percentage or fixed amount) + */ + function formatDiscount(int|float $amount, VoucherType $type, string $currency = 'Rp'): string + { + return $type === VoucherType::PERCENTAGE + ? formatPercentage($amount) + : formatCurrencyNumber($amount, $currency); + } +} diff --git a/app/Helpers/StringHelpers.php b/app/Helpers/StringHelpers.php index cd2027a..665d550 100644 --- a/app/Helpers/StringHelpers.php +++ b/app/Helpers/StringHelpers.php @@ -1,72 +1,7 @@ replace('Rp', '') - ->replace('.', '') - ->replace(' ', '') - ->__toString(); - - return (int) $cleaned; - } -} - -if (! function_exists('formatPercentage')) { - /** - * Format value as percentage - * - * @param int $precision Precision (default: 2) - * @param int $maxPrecision Max precision (default: 2) - * @param string $locale Locale for formatting (default: 'id_ID') - */ - function formatPercentage(int|float $value, int $precision = 2, int $maxPrecision = 2, string $locale = 'id_ID'): string - { - return Number::percentage($value, $precision, $maxPrecision, $locale); - } -} - -if (! function_exists('formatDiscount')) { - /** - * Format discount based on voucher type (percentage or fixed amount) - */ - function formatDiscount(int|float $amount, VoucherType $type, string $currency = 'Rp'): string - { - return $type === VoucherType::PERCENTAGE - ? formatPercentage($amount) - : formatCurrencyNumber($amount, $currency); - } -} - if (! function_exists('generateReferralCode')) { /** * Generate referral code from username and unique number diff --git a/composer.json b/composer.json index 42f8ab4..7536c79 100644 --- a/composer.json +++ b/composer.json @@ -52,7 +52,8 @@ "files": [ "app/Helpers/DateTimeHelpers.php", "app/Helpers/StringHelpers.php", - "app/Helpers/ColorHelpers.php" + "app/Helpers/ColorHelpers.php", + "app/Helpers/NumberHelpers.php" ] }, "autoload-dev": {