Skip to content

[12.x] Fix incorrect return types and handle NumberFormatter failure in Number class #55957

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: 12.x
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/Illuminate/Support/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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();

Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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();

Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -138,15 +138,15 @@ 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();

$formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::SPELLOUT);

$formatter->setTextAttribute(NumberFormatter::DEFAULT_RULESET, '%spellout-ordinal');

return $formatter->format($number);
return $formatter->format($number) ?: (string) $number;
}

/**
Expand Down