From c45ae466ba634aee768007c4d5749e1d910f8c5e Mon Sep 17 00:00:00 2001 From: platoindebugmode Date: Mon, 9 Jun 2025 01:48:50 +0330 Subject: [PATCH] add accurate return types and handle false from NumberFormatter --- src/Illuminate/Support/Number.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Illuminate/Support/Number.php b/src/Illuminate/Support/Number.php index f4a642f7df6c..dca54875c564 100644 --- a/src/Illuminate/Support/Number.php +++ b/src/Illuminate/Support/Number.php @@ -45,7 +45,7 @@ public static function format(int|float $number, ?int $precision = null, ?int $m $formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $precision); } - return $formatter->format($number); + return $formatter->format($number) ?: (string) $number; } /** @@ -56,7 +56,7 @@ public static function format(int|float $number, ?int $precision = null, ?int $m * @param string|null $locale * @return int|float|false */ - public static function parse(string $string, ?int $type = NumberFormatter::TYPE_DOUBLE, ?string $locale = null): int|float + public static function parse(string $string, ?int $type = NumberFormatter::TYPE_DOUBLE, ?string $locale = null): int|float|false { static::ensureIntlExtensionIsInstalled(); @@ -72,7 +72,7 @@ public static function parse(string $string, ?int $type = NumberFormatter::TYPE_ * @param string|null $locale * @return int|false */ - public static function parseInt(string $string, ?string $locale = null): int + public static function parseInt(string $string, ?string $locale = null): int|false { return self::parse($string, NumberFormatter::TYPE_INT32, $locale); } @@ -84,7 +84,7 @@ public static function parseInt(string $string, ?string $locale = null): int * @param string|null $locale The locale to use * @return float|false */ - public static function parseFloat(string $string, ?string $locale = null): float + public static function parseFloat(string $string, ?string $locale = null): float|false { return self::parse($string, NumberFormatter::TYPE_DOUBLE, $locale); } @@ -98,7 +98,7 @@ public static function parseFloat(string $string, ?string $locale = null): float * @param int|null $until * @return string */ - public static function spell(int|float $number, ?string $locale = null, ?int $after = null, ?int $until = null) + public static function spell(int|float $number, ?string $locale = null, ?int $after = null, ?int $until = null): string { static::ensureIntlExtensionIsInstalled(); @@ -112,7 +112,7 @@ public static function spell(int|float $number, ?string $locale = null, ?int $af $formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::SPELLOUT); - return $formatter->format($number); + return $formatter->format($number) ?: (string) $number; } /** @@ -122,13 +122,13 @@ public static function spell(int|float $number, ?string $locale = null, ?int $af * @param string|null $locale * @return string */ - public static function ordinal(int|float $number, ?string $locale = null) + public static function ordinal(int|float $number, ?string $locale = null): string { static::ensureIntlExtensionIsInstalled(); $formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::ORDINAL); - return $formatter->format($number); + return $formatter->format($number) ?: (string) $number; } /** @@ -138,7 +138,7 @@ public static function ordinal(int|float $number, ?string $locale = null) * @param string|null $locale * @return string */ - public static function spellOrdinal(int|float $number, ?string $locale = null) + public static function spellOrdinal(int|float $number, ?string $locale = null): string { static::ensureIntlExtensionIsInstalled(); @@ -146,7 +146,7 @@ public static function spellOrdinal(int|float $number, ?string $locale = null) $formatter->setTextAttribute(NumberFormatter::DEFAULT_RULESET, '%spellout-ordinal'); - return $formatter->format($number); + return $formatter->format($number) ?: (string) $number; } /**