Skip to content

Commit

Permalink
Allow Symfony 7
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Nov 18, 2023
1 parent 96d7882 commit 1175d7d
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 58 deletions.
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 0 additions & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 3 additions & 7 deletions src/Carbon/AbstractTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]);
Expand Down Expand Up @@ -356,12 +354,10 @@ public function setLocale($locale)
}

if (!$this->loadMessagesFromFile($locale) && !$this->initializing) {
return false;
return;
}

parent::setLocale($locale);

return true;
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/Carbon/CarbonInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 11 additions & 6 deletions src/Carbon/Traits/Localization.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/Carbon/TranslatorImmutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -82,7 +82,7 @@ public function resetMessages($locale = null)
/**
* @codeCoverageIgnore
*/
public function setFallbackLocales(array $locales)
public function setFallbackLocales(array $locales): void
{
$this->disallowMutation(__METHOD__);

Expand Down
1 change: 0 additions & 1 deletion tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
45 changes: 30 additions & 15 deletions tests/Carbon/LocalizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

Expand All @@ -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());
}

/**
Expand All @@ -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()
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -494,7 +509,7 @@ public function testCustomWeekStart()

$translator->resetMessages('ru');

$this->assertTrue(Carbon::setLocale('en'));
Carbon::setLocale('en');
}

public function testAddAndRemoveDirectory()
Expand All @@ -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()
Expand Down
44 changes: 29 additions & 15 deletions tests/CarbonImmutable/LocalizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

Expand All @@ -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());
}

/**
Expand All @@ -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()
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -473,7 +487,7 @@ public function testCustomWeekStart()

$translator->resetMessages('ru');

$this->assertTrue(Carbon::setLocale('en'));
Carbon::setLocale('en');
}

public function testAddAndRemoveDirectory()
Expand All @@ -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()
Expand Down
Loading

0 comments on commit 1175d7d

Please sign in to comment.