From 1175d7d48aeb5d772b5c268e783df5a6a8336653 Mon Sep 17 00:00:00 2001 From: kylekatarnls Date: Sat, 18 Nov 2023 18:57:50 +0100 Subject: [PATCH] Allow Symfony 7 --- .github/workflows/tests.yml | 1 + composer.json | 2 +- phpstan.neon | 4 -- src/Carbon/AbstractTranslator.php | 10 ++--- src/Carbon/CarbonInterface.php | 4 +- src/Carbon/Traits/Localization.php | 17 +++++--- src/Carbon/TranslatorImmutable.php | 6 +-- tests/AbstractTestCase.php | 1 - tests/Carbon/LocalizationTest.php | 45 ++++++++++++++------- tests/CarbonImmutable/LocalizationTest.php | 44 +++++++++++++------- tests/Localization/LocalizationTestCase.php | 12 ++++-- 11 files changed, 88 insertions(+), 58 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 7e405a0a2c..680135e10d 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -125,6 +125,7 @@ jobs: composer update --prefer-dist --no-progress --prefer-${{ matrix.setup || 'stable' }} ${{ matrix.classmap-authoritative && '--classmap-authoritative' || '' }}${{ matrix.php >= 8.2 && ' --ignore-platform-reqs' || '' }}; - name: Run test suite + continue-on-error: ${{ matrix.laravel || 'false' }} run: | if [[ "${{ matrix.laravel }}" != 'true' && "${{ matrix.coverage }}" = 'true' ]]; then php -d memory_limit=-1 -d zend.enable_gc=0 -d error_reporting=-1 vendor/phpunit/phpunit/phpunit --coverage-clover=clover.xml --coverage-text; diff --git a/composer.json b/composer.json index 04eaca1659..7a259c0155 100644 --- a/composer.json +++ b/composer.json @@ -46,7 +46,7 @@ "psr/clock": "^1.0", "symfony/clock": "^6.3", "symfony/polyfill-mbstring": "^1.0", - "symfony/translation": "^4.4.18 || ^5.2.1|| ^6.0" + "symfony/translation": "^4.4.18 || ^5.2.1|| ^6.0 || ^7.0" }, "require-dev": { "doctrine/dbal": "^3.6.3 || ^4.0", diff --git a/phpstan.neon b/phpstan.neon index 148482f1f7..a59b5e7d96 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -33,10 +33,6 @@ parameters: - '#^Call to an undefined method DatePeriod::[a-zA-Z]+\(\)\.$#' - '#^Call to an undefined method DateInterval::(spec|optimize)\(\)\.$#' - '#^Method class@anonymous/tests/Carbon/TestingAidsTest\.php:\d+::modify\(\) should return class@anonymous/tests/Carbon/TestingAidsTest\.php:\d+ but returns \(DateTimeImmutable\|false\)\.$#' - - - message: '#^Result of method Symfony\\Contracts\\Translation\\LocaleAwareInterface::setLocale\(\) \(void\) is used\.$#' - paths: - - src/Carbon/Traits/Localization.php - message: '#^Undefined variable: \$this$#' paths: diff --git a/src/Carbon/AbstractTranslator.php b/src/Carbon/AbstractTranslator.php index beb44db5cc..33608504d6 100644 --- a/src/Carbon/AbstractTranslator.php +++ b/src/Carbon/AbstractTranslator.php @@ -302,10 +302,8 @@ public function getMessages($locale = null) * Set the current translator locale and indicate if the source locale file exists * * @param string $locale locale ex. en - * - * @return bool */ - public function setLocale($locale) + public function setLocale($locale): void { $locale = preg_replace_callback('/[-_]([a-z]{2,}|\d{2,})/', function ($matches) { // _2-letters or YUE is a region, _3+-letters is a variant @@ -321,7 +319,7 @@ public function setLocale($locale) $previousLocale = $this->getLocale(); if ($previousLocale === $locale && isset($this->messages[$locale])) { - return true; + return; } unset(static::$singletons[$previousLocale]); @@ -356,12 +354,10 @@ public function setLocale($locale) } if (!$this->loadMessagesFromFile($locale) && !$this->initializing) { - return false; + return; } parent::setLocale($locale); - - return true; } /** diff --git a/src/Carbon/CarbonInterface.php b/src/Carbon/CarbonInterface.php index 9023d97dee..3842cc17fb 100644 --- a/src/Carbon/CarbonInterface.php +++ b/src/Carbon/CarbonInterface.php @@ -3693,10 +3693,8 @@ public function setLocalTranslator(TranslatorInterface $translator); * Pass 'auto' as locale to use closest language from the current LC_TIME locale. * * @param string $locale locale ex. en - * - * @return bool */ - public static function setLocale(string $locale): bool; + public static function setLocale(string $locale): void; /** * @deprecated To avoid conflict between different third-party libraries, static setters should not be used. diff --git a/src/Carbon/Traits/Localization.php b/src/Carbon/Traits/Localization.php index 924aa07637..0d6155de9a 100644 --- a/src/Carbon/Traits/Localization.php +++ b/src/Carbon/Traits/Localization.php @@ -465,15 +465,13 @@ public static function getLocale(): string /** * Set the current translator locale and indicate if the source locale file exists. - * Pass 'auto' as locale to use closest language from the current LC_TIME locale. + * Pass 'auto' as locale to use the closest language to the current LC_TIME locale. * * @param string $locale locale ex. en - * - * @return bool */ - public static function setLocale(string $locale): bool + public static function setLocale(string $locale): void { - return static::getLocaleAwareTranslator()->setLocale($locale) !== false; + static::getLocaleAwareTranslator()->setLocale($locale); } /** @@ -531,7 +529,14 @@ public static function getFallbackLocale() public static function executeWithLocale($locale, $func) { $currentLocale = static::getLocale(); - $result = $func(static::setLocale($locale) ? static::getLocale() : false, static::translator()); + static::setLocale($locale); + $newLocale = static::getLocale(); + $result = $func( + $newLocale === 'en' && strtolower(substr((string) $locale, 0, 2)) !== 'en' + ? false + : $newLocale, + static::translator(), + ); static::setLocale($currentLocale); return $result; diff --git a/src/Carbon/TranslatorImmutable.php b/src/Carbon/TranslatorImmutable.php index 8370feda08..237e553598 100644 --- a/src/Carbon/TranslatorImmutable.php +++ b/src/Carbon/TranslatorImmutable.php @@ -35,11 +35,11 @@ public function setDirectories(array $directories) return parent::setDirectories($directories); } - public function setLocale($locale) + public function setLocale($locale): void { $this->disallowMutation(__METHOD__); - return parent::setLocale($locale); + parent::setLocale($locale); } /** @@ -82,7 +82,7 @@ public function resetMessages($locale = null) /** * @codeCoverageIgnore */ - public function setFallbackLocales(array $locales) + public function setFallbackLocales(array $locales): void { $this->disallowMutation(__METHOD__); diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php index 9335d5626d..5c647f9809 100644 --- a/tests/AbstractTestCase.php +++ b/tests/AbstractTestCase.php @@ -319,7 +319,6 @@ protected function areSameLocales($a, $b) if ($aliases === null) { $property = new ReflectionProperty(Translator::class, 'aliases'); - $property->setAccessible(true); $aliases = $property->getValue(Translator::get()); } diff --git a/tests/Carbon/LocalizationTest.php b/tests/Carbon/LocalizationTest.php index 2a75e32d36..674c25b8f0 100644 --- a/tests/Carbon/LocalizationTest.php +++ b/tests/Carbon/LocalizationTest.php @@ -310,7 +310,7 @@ public static function dataForLocales(): Generator */ public function testSetLocale(string $locale) { - $this->assertTrue(Carbon::setLocale($locale)); + Carbon::setLocale($locale); $this->assertTrue($this->areSameLocales($locale, Carbon::getLocale())); } @@ -334,7 +334,9 @@ public function testSetTranslator(string $locale) public function testSetLocaleWithKnownLocale() { - $this->assertTrue(Carbon::setLocale('fr')); + Carbon::setLocale('fr'); + + $this->assertSame('fr', Carbon::getLocale()); } /** @@ -357,17 +359,27 @@ public static function dataForTestSetLocaleWithMalformedLocale(): Generator */ public function testSetLocaleWithMalformedLocale(string $malformedLocale) { - $this->assertTrue(Carbon::setLocale($malformedLocale)); + Carbon::setLocale($malformedLocale); + $split = preg_split('/[-_]/', $malformedLocale); + + $this->assertSame( + strtolower($split[0]).(\count($split) === 1 ? '' : '_'.strtoupper($split[1])), + Carbon::getLocale(), + ); } public function testSetLocaleWithNonExistingLocale() { - $this->assertFalse(Carbon::setLocale('pt-XX')); + Carbon::setLocale('pt-XX'); + + $this->assertSame('pt', Carbon::getLocale()); } public function testSetLocaleWithUnknownLocale() { - $this->assertFalse(Carbon::setLocale('zz')); + Carbon::setLocale('zz'); + + $this->assertSame('en', Carbon::getLocale()); } public function testCustomTranslation() @@ -425,7 +437,8 @@ public function testAddCustomTranslation() 'day' => '1 boring day|%count% boring days', ]; - $this->assertTrue(Carbon::setLocale('en')); + Carbon::setLocale('en'); + $this->assertSame('en', Carbon::getLocale()); /** @var Translator $translator */ $translator = Carbon::getTranslator(); $translator->setMessages('en', $enBoring); @@ -452,7 +465,8 @@ public function testAddCustomTranslation() $this->assertArrayHasKey('en_Boring', $messages); $this->assertSame($enBoring, $messages['en_Boring']); - $this->assertTrue(Carbon::setLocale('en_Boring')); + Carbon::setLocale('en_Boring'); + $this->assertSame('en_Boring', Carbon::getLocale()); $diff = Carbon::create(2018, 1, 1, 0, 0, 0) ->diffForHumans(Carbon::create(2018, 1, 4, 4, 0, 0), true, false, 2); @@ -464,12 +478,13 @@ public function testAddCustomTranslation() $this->assertSame([], $translator->getMessages()); - $this->assertTrue(Carbon::setLocale('en')); + Carbon::setLocale('en'); + $this->assertSame('en', Carbon::getLocale()); } public function testCustomWeekStart() { - $this->assertTrue(Carbon::setLocale('ru')); + Carbon::setLocale('ru'); /** @var Translator $translator */ $translator = Carbon::getTranslator(); @@ -494,7 +509,7 @@ public function testCustomWeekStart() $translator->resetMessages('ru'); - $this->assertTrue(Carbon::setLocale('en')); + Carbon::setLocale('en'); } public function testAddAndRemoveDirectory() @@ -508,24 +523,24 @@ public function testAddAndRemoveDirectory() $translator = Carbon::getTranslator(); Carbon::setLocale('en'); - $this->assertFalse(Carbon::setLocale('foo')); + Carbon::setLocale('foo'); $this->assertSame('Saturday', Carbon::parse('2018-07-07 00:00:00')->isoFormat('dddd')); $translator->addDirectory($directory); - $this->assertTrue(Carbon::setLocale('foo')); + Carbon::setLocale('foo'); $this->assertSame('samedi', Carbon::parse('2018-07-07 00:00:00')->isoFormat('dddd')); Carbon::setLocale('en'); $translator->removeDirectory($directory); - $this->assertFalse(Carbon::setLocale('bar')); + Carbon::setLocale('bar'); $this->assertSame('Saturday', Carbon::parse('2018-07-07 00:00:00')->isoFormat('dddd')); - $this->assertTrue(Carbon::setLocale('foo')); + Carbon::setLocale('foo'); $this->assertSame('samedi', Carbon::parse('2018-07-07 00:00:00')->isoFormat('dddd')); - $this->assertTrue(Carbon::setLocale('en')); + Carbon::setLocale('en'); } public function testLocaleHasShortUnits() diff --git a/tests/CarbonImmutable/LocalizationTest.php b/tests/CarbonImmutable/LocalizationTest.php index bd97f89c65..9a948a2d8a 100644 --- a/tests/CarbonImmutable/LocalizationTest.php +++ b/tests/CarbonImmutable/LocalizationTest.php @@ -318,7 +318,7 @@ public static function dataForLocales(): array */ public function testSetLocale(string $locale) { - $this->assertTrue(Carbon::setLocale($locale)); + Carbon::setLocale($locale); $this->assertTrue($this->areSameLocales($locale, Carbon::getLocale())); } @@ -341,7 +341,8 @@ public function testSetTranslator(string $locale) public function testSetLocaleWithKnownLocale() { - $this->assertTrue(Carbon::setLocale('fr')); + Carbon::setLocale('fr'); + $this->assertSame('fr', Carbon::getLocale()); } /** @@ -366,17 +367,27 @@ public static function dataForTestSetLocaleWithMalformedLocale(): array */ public function testSetLocaleWithMalformedLocale(string $malformedLocale) { - $this->assertTrue(Carbon::setLocale($malformedLocale)); + Carbon::setLocale($malformedLocale); + $split = preg_split('/[-_]/', $malformedLocale); + + $this->assertSame( + strtolower($split[0]).(\count($split) === 1 ? '' : '_'.strtoupper($split[1])), + Carbon::getLocale(), + ); } public function testSetLocaleWithNonExistingLocale() { - $this->assertFalse(Carbon::setLocale('pt-XX')); + Carbon::setLocale('pt-XX'); + + $this->assertSame('pt', Carbon::getLocale()); } public function testSetLocaleWithUnknownLocale() { - $this->assertFalse(Carbon::setLocale('zz')); + Carbon::setLocale('zz'); + + $this->assertSame('en', Carbon::getLocale()); } public function testCustomTranslation() @@ -404,7 +415,8 @@ public function testAddCustomTranslation() 'day' => '1 boring day|%count% boring days', ]; - $this->assertTrue(Carbon::setLocale('en')); + Carbon::setLocale('en'); + $this->assertSame('en', Carbon::getLocale()); /** @var Translator $translator */ $translator = Carbon::getTranslator(); $translator->setMessages('en', $enBoring); @@ -431,7 +443,8 @@ public function testAddCustomTranslation() $this->assertArrayHasKey('en_Boring', $messages); $this->assertSame($enBoring, $messages['en_Boring']); - $this->assertTrue(Carbon::setLocale('en_Boring')); + Carbon::setLocale('en_Boring'); + $this->assertSame('en_Boring', Carbon::getLocale()); $diff = Carbon::create(2018, 1, 1, 0, 0, 0) ->diffForHumans(Carbon::create(2018, 1, 4, 4, 0, 0), true, false, 2); @@ -443,12 +456,13 @@ public function testAddCustomTranslation() $this->assertSame([], $translator->getMessages()); - $this->assertTrue(Carbon::setLocale('en')); + Carbon::setLocale('en'); + $this->assertSame('en', Carbon::getLocale()); } public function testCustomWeekStart() { - $this->assertTrue(Carbon::setLocale('ru')); + Carbon::setLocale('ru'); /** @var Translator $translator */ $translator = Carbon::getTranslator(); @@ -473,7 +487,7 @@ public function testCustomWeekStart() $translator->resetMessages('ru'); - $this->assertTrue(Carbon::setLocale('en')); + Carbon::setLocale('en'); } public function testAddAndRemoveDirectory() @@ -487,24 +501,24 @@ public function testAddAndRemoveDirectory() $translator = Carbon::getTranslator(); Carbon::setLocale('en'); - $this->assertFalse(Carbon::setLocale('foo')); + Carbon::setLocale('foo'); $this->assertSame('Saturday', Carbon::parse('2018-07-07 00:00:00')->isoFormat('dddd')); $translator->addDirectory($directory); - $this->assertTrue(Carbon::setLocale('foo')); + Carbon::setLocale('foo'); $this->assertSame('samedi', Carbon::parse('2018-07-07 00:00:00')->isoFormat('dddd')); Carbon::setLocale('en'); $translator->removeDirectory($directory); - $this->assertFalse(Carbon::setLocale('bar')); + Carbon::setLocale('bar'); $this->assertSame('Saturday', Carbon::parse('2018-07-07 00:00:00')->isoFormat('dddd')); - $this->assertTrue(Carbon::setLocale('foo')); + Carbon::setLocale('foo'); $this->assertSame('samedi', Carbon::parse('2018-07-07 00:00:00')->isoFormat('dddd')); - $this->assertTrue(Carbon::setLocale('en')); + Carbon::setLocale('en'); } public function testLocaleHasShortUnits() diff --git a/tests/Localization/LocalizationTestCase.php b/tests/Localization/LocalizationTestCase.php index 80e13c851f..a6d799187a 100644 --- a/tests/Localization/LocalizationTestCase.php +++ b/tests/Localization/LocalizationTestCase.php @@ -329,15 +329,21 @@ protected function setUp(): void { parent::setUp(); - if (!Carbon::setLocale(static::LOCALE) || !$this->areSameLocales(Carbon::getLocale(), static::LOCALE)) { + Carbon::setLocale(static::LOCALE); + + if (!$this->areSameLocales(Carbon::getLocale(), static::LOCALE)) { throw new InvalidArgumentException('Locale '.static::LOCALE.' not found'); } - if (!CarbonImmutable::setLocale(static::LOCALE) || !$this->areSameLocales(CarbonImmutable::getLocale(), static::LOCALE)) { + CarbonImmutable::setLocale(static::LOCALE); + + if (!$this->areSameLocales(CarbonImmutable::getLocale(), static::LOCALE)) { throw new InvalidArgumentException('Locale '.static::LOCALE.' not found'); } - if (!CarbonInterval::setLocale(static::LOCALE) || !$this->areSameLocales(CarbonInterval::getLocale(), static::LOCALE)) { + CarbonInterval::setLocale(static::LOCALE); + + if (!$this->areSameLocales(CarbonInterval::getLocale(), static::LOCALE)) { throw new InvalidArgumentException('Locale '.static::LOCALE.' not found'); } }