From 9e17ba92ed83596adddef43b7f3208f11600e319 Mon Sep 17 00:00:00 2001 From: Phil Bates Date: Mon, 15 Jan 2024 16:26:09 +0000 Subject: [PATCH] [10.x] Officially support floats in trans_choice This already works fine with no further changes needed, so I'm not sure if there's a reason that floats were never officially supported. This is how it is currently, which you can see works perfectly fine: Given: // lang/en/foo.php return [ 'hours' => 'A total of :hours hour|A total of :hours hours', ]; Then: trans_choice('foo.hours', 1, ['hours' => 1]) === 'A total of 1 hour' trans_choice('foo.hours', 1.0, ['hours' => 1.0]) === 'A total of 1 hour' trans_choice('foo.hours', 1.1, ['hours' => 1.1]) === 'A total of 1.1 hours' trans_choice('foo.hours', 0.9, ['hours' => 0.9]) === 'A total of 0.9 hours' However, when running phpstan & larastan on a Laravel project that passes a float to trans_choice when wanting to display text similar to those examples ("A total of X hour[s]") it results in an error because the only documented allowed types are \Countable|int|array. --- src/Illuminate/Contracts/Translation/Translator.php | 2 +- src/Illuminate/Foundation/helpers.php | 2 +- .../Translation/PotentiallyTranslatedString.php | 2 +- src/Illuminate/Translation/Translator.php | 2 +- tests/Translation/TranslationTranslatorTest.php | 12 +++++++++++- 5 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Contracts/Translation/Translator.php b/src/Illuminate/Contracts/Translation/Translator.php index 6eae4915d5a1..ded1a8b864f9 100644 --- a/src/Illuminate/Contracts/Translation/Translator.php +++ b/src/Illuminate/Contracts/Translation/Translator.php @@ -18,7 +18,7 @@ public function get($key, array $replace = [], $locale = null); * Get a translation according to an integer value. * * @param string $key - * @param \Countable|int|array $number + * @param \Countable|int|float|array $number * @param array $replace * @param string|null $locale * @return string diff --git a/src/Illuminate/Foundation/helpers.php b/src/Illuminate/Foundation/helpers.php index e4c15edca725..c698bbdfaa8e 100644 --- a/src/Illuminate/Foundation/helpers.php +++ b/src/Illuminate/Foundation/helpers.php @@ -929,7 +929,7 @@ function trans($key = null, $replace = [], $locale = null) * Translates the given message based on a count. * * @param string $key - * @param \Countable|int|array $number + * @param \Countable|int|float|array $number * @param array $replace * @param string|null $locale * @return string diff --git a/src/Illuminate/Translation/PotentiallyTranslatedString.php b/src/Illuminate/Translation/PotentiallyTranslatedString.php index f46db3522429..efcccca28331 100644 --- a/src/Illuminate/Translation/PotentiallyTranslatedString.php +++ b/src/Illuminate/Translation/PotentiallyTranslatedString.php @@ -57,7 +57,7 @@ public function translate($replace = [], $locale = null) /** * Translates the string based on a count. * - * @param \Countable|int|array $number + * @param \Countable|int|float|array $number * @param array $replace * @param string|null $locale * @return $this diff --git a/src/Illuminate/Translation/Translator.php b/src/Illuminate/Translation/Translator.php index f9f8b49cb11c..634a102d54c7 100755 --- a/src/Illuminate/Translation/Translator.php +++ b/src/Illuminate/Translation/Translator.php @@ -183,7 +183,7 @@ public function get($key, array $replace = [], $locale = null, $fallback = true) * Get a translation according to an integer value. * * @param string $key - * @param \Countable|int|array $number + * @param \Countable|int|float|array $number * @param array $replace * @param string|null $locale * @return string diff --git a/tests/Translation/TranslationTranslatorTest.php b/tests/Translation/TranslationTranslatorTest.php index 8601f1493ea7..07ec429aab77 100755 --- a/tests/Translation/TranslationTranslatorTest.php +++ b/tests/Translation/TranslationTranslatorTest.php @@ -123,7 +123,7 @@ public function testGetMethodProperlyLoadsAndRetrievesItemForGlobalNamespace() $this->assertSame('breeze bar', $t->get('foo.bar', ['foo' => 'bar'])); } - public function testChoiceMethodProperlyLoadsAndRetrievesItem() + public function testChoiceMethodProperlyLoadsAndRetrievesItemForAnInt() { $t = $this->getMockBuilder(Translator::class)->onlyMethods(['get'])->setConstructorArgs([$this->getLoader(), 'en'])->getMock(); $t->expects($this->once())->method('get')->with($this->equalTo('foo'), $this->equalTo(['replace']), $this->equalTo('en'))->willReturn('line'); @@ -133,6 +133,16 @@ public function testChoiceMethodProperlyLoadsAndRetrievesItem() $t->choice('foo', 10, ['replace']); } + public function testChoiceMethodProperlyLoadsAndRetrievesItemForAFloat() + { + $t = $this->getMockBuilder(Translator::class)->onlyMethods(['get'])->setConstructorArgs([$this->getLoader(), 'en'])->getMock(); + $t->expects($this->once())->method('get')->with($this->equalTo('foo'), $this->equalTo(['replace']), $this->equalTo('en'))->willReturn('line'); + $t->setSelector($selector = m::mock(MessageSelector::class)); + $selector->shouldReceive('choose')->once()->with('line', 1.2, 'en')->andReturn('choiced'); + + $t->choice('foo', 1.2, ['replace']); + } + public function testChoiceMethodProperlyCountsCollectionsAndLoadsAndRetrievesItem() { $t = $this->getMockBuilder(Translator::class)->onlyMethods(['get'])->setConstructorArgs([$this->getLoader(), 'en'])->getMock();