Skip to content

Commit

Permalink
minor #6004 Fix a deprecation in IntlFormatter (javiereguiluz)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 4.x branch.

Discussion
----------

Fix a deprecation in IntlFormatter

Fixes this direct deprecation:

```
Remaining self deprecation notices (1)

  1x: NumberFormatter::format(): Passing null to parameter #1 ($num) of type int|float is deprecated
    1x in IntlFormatterTest::testFormatNumber from EasyCorp\Bundle\EasyAdminBundle\Tests\Intl
``

Commits
-------

c7973cd Fix a deprecation in IntlFormatter
  • Loading branch information
javiereguiluz committed Nov 4, 2023
2 parents 015e5f8 + c7973cd commit ced0bfc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/Intl/IntlFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,22 @@ public function formatCurrency($amount, string $currency, array $attrs = [], ?st
return $formattedCurrency;
}

/**
* @param int|float $number
*/
public function formatNumber($number, array $attrs = [], string $style = 'decimal', string $type = 'default', ?string $locale = null): string
{
if (null === $number) {
trigger_deprecation(
'easycorp/easyadmin-bundle',
'4.8.5',
'Passing null values to "%s()" method is deprecated and will throw an exception in EasyAdmin 5.0.0.',
__METHOD__,
);

return '0';
}

if (!isset(self::NUMBER_TYPES[$type])) {
throw new RuntimeError(sprintf('The type "%s" does not exist, known types are: "%s".', $type, implode('", "', array_keys(self::NUMBER_TYPES))));
}
Expand Down
25 changes: 23 additions & 2 deletions tests/Intl/IntlFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,24 @@ public function testFormatDateTime(?string $expectedResult, ?\DateTimeInterface
/**
* @dataProvider provideFormatNumber
*/
public function testFormatNumber(?string $expectedResult, int|float|null $number, array $attrs)
public function testFormatNumber(?string $expectedResult, int|float $number, array $attrs)
{
if (\PHP_VERSION_ID < 80200) {
$this->markTestSkipped('PHP 8.2 or higher is required to run this test.');
}

$intlFormatter = new IntlFormatter();
$formattedNumber = $intlFormatter->formatNumber($number, $attrs);

$this->assertSame($expectedResult, $formattedNumber);
}

/**
* @dataProvider provideLegacyFormatNumber
*
* @group legacy
*/
public function testLegacyFormatNumber(?string $expectedResult, int|float|null $number, array $attrs)
{
if (\PHP_VERSION_ID < 80200) {
$this->markTestSkipped('PHP 8.2 or higher is required to run this test.');
Expand Down Expand Up @@ -157,7 +174,6 @@ public static function provideFormatDateTime()

public static function provideFormatNumber()
{
yield ['0', null, []];
yield ['0', 0, []];
yield ['0', 0.0, []];

Expand All @@ -172,6 +188,11 @@ public static function provideFormatNumber()
yield ['1 234,560', 1234.56, ['fraction_digit' => 3, 'decimal_separator' => ',', 'grouping_separator' => ' ']];
}

public static function provideLegacyFormatNumber()
{
yield ['0', null, []];
}

private function normalizeWhiteSpaces(string $string): string
{
return u($string)
Expand Down

0 comments on commit ced0bfc

Please sign in to comment.