From 088615ddaf4f8d3817e6e12e9e818e163b8c07c3 Mon Sep 17 00:00:00 2001 From: Amrouche Hamza Date: Sun, 7 Jul 2019 09:01:32 +0200 Subject: [PATCH] [Translation] deprecate passing a null locale --- .../Translation/MessageCatalogue.php | 4 +++ .../Tests/MessageCatalogueTest.php | 11 +++++++ .../Translation/Tests/TranslatorTest.php | 29 ++++++++++++++++++- .../Component/Translation/Translator.php | 4 +++ 4 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Translation/MessageCatalogue.php b/src/Symfony/Component/Translation/MessageCatalogue.php index 19afb903f7a7..8bd8fc563c31 100644 --- a/src/Symfony/Component/Translation/MessageCatalogue.php +++ b/src/Symfony/Component/Translation/MessageCatalogue.php @@ -32,6 +32,10 @@ class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterf */ public function __construct(?string $locale, array $messages = []) { + if (null === $locale) { + @trigger_error(sprintf('Passing "null" to the first argument of the "%s" method has been deprecated since Symfony 4.4 and will throw an error in 5.0.', __METHOD__), E_USER_DEPRECATED); + } + $this->locale = $locale; $this->messages = $messages; } diff --git a/src/Symfony/Component/Translation/Tests/MessageCatalogueTest.php b/src/Symfony/Component/Translation/Tests/MessageCatalogueTest.php index cf0dd1a24c2b..3113271462ff 100644 --- a/src/Symfony/Component/Translation/Tests/MessageCatalogueTest.php +++ b/src/Symfony/Component/Translation/Tests/MessageCatalogueTest.php @@ -23,6 +23,17 @@ public function testGetLocale() $this->assertEquals('en', $catalogue->getLocale()); } + /** + * @group legacy + * @expectedDeprecation Passing "null" to the first argument of the "Symfony\Component\Translation\MessageCatalogue::__construct" method has been deprecated since Symfony 4.4 and will throw an error in 5.0. + */ + public function testGetNullLocale() + { + $catalogue = new MessageCatalogue(null); + + $this->assertNull($catalogue->getLocale()); + } + public function testGetDomains() { $catalogue = new MessageCatalogue('en', ['domain1' => [], 'domain2' => [], 'domain2+intl-icu' => [], 'domain3+intl-icu' => []]); diff --git a/src/Symfony/Component/Translation/Tests/TranslatorTest.php b/src/Symfony/Component/Translation/Tests/TranslatorTest.php index 51c4a0a048be..aa7ef00f7174 100644 --- a/src/Symfony/Component/Translation/Tests/TranslatorTest.php +++ b/src/Symfony/Component/Translation/Tests/TranslatorTest.php @@ -184,12 +184,25 @@ public function testAddResourceInvalidLocales($locale) */ public function testAddResourceValidLocales($locale) { + if (null === $locale) { + $this->markTestSkipped('null is not a valid locale'); + } $translator = new Translator('fr'); $translator->addResource('array', ['foo' => 'foofoo'], $locale); // no assertion. this method just asserts that no exception is thrown $this->addToAssertionCount(1); } + /** + * @group legacy + * @expectedDeprecation Passing "null" to the third argument of the "Symfony\Component\Translation\Translator::addResource" method has been deprecated since Symfony 4.4 and will throw an error in 5.0. + */ + public function testAddResourceNull() + { + $translator = new Translator('fr'); + $translator->addResource('array', ['foo' => 'foofoo'], null); + } + public function testAddResourceAfterTrans() { $translator = new Translator('fr'); @@ -367,10 +380,13 @@ public function testTransInvalidLocale($locale) } /** - * @dataProvider getValidLocalesTests + * @dataProvider getValidLocalesTests */ public function testTransValidLocale($locale) { + if (null === $locale) { + $this->markTestSkipped('null is not a valid locale'); + } $translator = new Translator($locale); $translator->addLoader('array', new ArrayLoader()); $translator->addResource('array', ['test' => 'OK'], $locale); @@ -379,6 +395,17 @@ public function testTransValidLocale($locale) $this->assertEquals('OK', $translator->trans('test', [], null, $locale)); } + /** + * @group legacy + * @expectedDeprecation Passing "null" to the third argument of the "Symfony\Component\Translation\Translator::addResource" method has been deprecated since Symfony 4.4 and will throw an error in 5.0. + */ + public function testTransNullLocale() + { + $translator = new Translator(null); + $translator->addLoader('array', new ArrayLoader()); + $translator->addResource('array', ['test' => 'OK'], null); + } + /** * @dataProvider getFlattenedTransTests */ diff --git a/src/Symfony/Component/Translation/Translator.php b/src/Symfony/Component/Translation/Translator.php index 9846c8338bb1..cee7058ba441 100644 --- a/src/Symfony/Component/Translation/Translator.php +++ b/src/Symfony/Component/Translation/Translator.php @@ -132,6 +132,10 @@ public function addResource($format, $resource, $locale, $domain = null) $domain = 'messages'; } + if (null === $locale) { + @trigger_error(sprintf('Passing "null" to the third argument of the "%s" method has been deprecated since Symfony 4.4 and will throw an error in 5.0.', __METHOD__), E_USER_DEPRECATED); + } + $this->assertValidLocale($locale); $this->resources[$locale][] = [$format, $resource, $domain];