From c7973cdfbf68a33d9911fa96f9772e30886ad548 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Sat, 4 Nov 2023 13:02:16 +0100 Subject: [PATCH] Fix a deprecation in IntlFormatter --- src/Intl/IntlFormatter.php | 14 ++++++++++++++ tests/Intl/IntlFormatterTest.php | 25 +++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/Intl/IntlFormatter.php b/src/Intl/IntlFormatter.php index ef46d1e061..427c4edc18 100644 --- a/src/Intl/IntlFormatter.php +++ b/src/Intl/IntlFormatter.php @@ -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)))); } diff --git a/tests/Intl/IntlFormatterTest.php b/tests/Intl/IntlFormatterTest.php index c144bde5f1..7b4d9f4e4d 100644 --- a/tests/Intl/IntlFormatterTest.php +++ b/tests/Intl/IntlFormatterTest.php @@ -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.'); @@ -157,7 +174,6 @@ public static function provideFormatDateTime() public static function provideFormatNumber() { - yield ['0', null, []]; yield ['0', 0, []]; yield ['0', 0.0, []]; @@ -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)