Skip to content

Commit

Permalink
Use unmodified English for period conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Feb 13, 2022
1 parent cc32408 commit a01b257
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 10 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@
},
"config": {
"process-timeout": 0,
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"phpstan/extension-installer": true
}
},
"extra": {
"branch-alias": {
Expand Down
5 changes: 0 additions & 5 deletions phpmd.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@
<exclude name="CouplingBetweenObjects" />
<exclude name="CountInLoopExpression" />
</rule>
<rule ref="rulesets/design.xml/CouplingBetweenObjects">
<properties>
<property name="maximum" value="25" />
</properties>
</rule>
<rule ref="rulesets/naming.xml/ShortVariable">
<properties>
<property name="exceptions" value="ci,id,to,tz" />
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ parameters:
- '#^PHPDoc tag @mixin contains invalid type Carbon\\Traits\\DeprecatedProperties\.$#'
- '#^Implementing PHPStan\\Reflection\\Php\\BuiltinMethodReflection is not covered by backward compatibility promise\.#'
- '#^Calling PHPStan\\Reflection\\Php\\PhpMethodReflectionFactory::create\(\) is not covered by backward compatibility promise\.#'
- "#^Method Carbon\\\\TranslatorImmutable::disallowMutation\\(\\) should always throw an exception or terminate script execution but doesn't do that\\.$#"
excludePaths:
- '*/src/Carbon/Laravel/ServiceProvider.php'
- '*/src/Carbon/PHPStan/Macro.php'
Expand Down
7 changes: 4 additions & 3 deletions src/Carbon/AbstractTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ abstract class AbstractTranslator extends Translation\Translator
public static function get($locale = null)
{
$locale = $locale ?: 'en';
$key = static::class === Translator::class ? $locale : static::class . '|' . $locale;

if (!isset(static::$singletons[$locale])) {
static::$singletons[$locale] = new static($locale);
if (!isset(static::$singletons[$key])) {
static::$singletons[$key] = new static($locale);
}

return static::$singletons[$locale];
return static::$singletons[$key];
}

public function __construct($locale, MessageFormatterInterface $formatter = null, $cacheDir = null, $debug = false)
Expand Down
7 changes: 6 additions & 1 deletion src/Carbon/CarbonPeriod.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@
* @method $this floorMicroseconds(float $precision = 1) Truncate the current instance microsecond with given precision.
* @method $this ceilMicrosecond(float $precision = 1) Ceil the current instance microsecond with given precision.
* @method $this ceilMicroseconds(float $precision = 1) Ceil the current instance microsecond with given precision.
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class CarbonPeriod implements Iterator, Countable, JsonSerializable
{
Expand Down Expand Up @@ -2179,7 +2181,10 @@ public function ceilUnit($unit, $precision = 1)
*/
public function round($precision = null, $function = 'round')
{
return $this->roundWith($precision ?? (string) $this->getDateInterval(), $function);
return $this->roundWith(
$precision ?? (string) $this->getDateInterval()->setLocalTranslator(TranslatorImmutable::get('en')),
$function
);
}

/**
Expand Down
137 changes: 137 additions & 0 deletions src/Carbon/TranslatorImmutable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

/**
* This file is part of the Carbon package.
*
* (c) Brian Nesbitt <brian@nesbot.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Carbon;

use Carbon\Exceptions\ImmutableException;
use Symfony\Component\Config\ConfigCacheFactoryInterface;
use Symfony\Component\Translation\Formatter\MessageFormatterInterface;

/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
class TranslatorImmutable extends Translator
{
/** @var bool */
private $constructed = false;

public function __construct($locale, MessageFormatterInterface $formatter = null, $cacheDir = null, $debug = false)
{
parent::__construct($locale, $formatter, $cacheDir, $debug);
$this->constructed = true;
}

/**
* @return never
*/
public function setDirectories(array $directories)
{
$this->disallowMutation(__METHOD__);
}

/**
* @return never
*/
public function setLocale($locale)
{
$this->disallowMutation(__METHOD__);
}

/**
* @return never
*/
public function setMessages($locale, $messages)
{
$this->disallowMutation(__METHOD__);
}

/**
* @return never
*/
public function setTranslations($messages)
{
$this->disallowMutation(__METHOD__);
}

/**
* @return never
*/
public function setConfigCacheFactory(ConfigCacheFactoryInterface $configCacheFactory)
{
$this->disallowMutation(__METHOD__);
}

/**
* @return never
*/
public function resetMessages($locale = null)
{
$this->disallowMutation(__METHOD__);
}

/**
* @return never
*/
public function setFallbackLocales(array $locales)
{
$this->disallowMutation(__METHOD__);
}

/**
* @return never
*/
public function setAliases(array $aliases): void
{
$this->disallowMutation(__METHOD__);
}

/**
* @return never
*/
public function setCatalogues(array $catalogues): void
{
$this->disallowMutation(__METHOD__);
}

/**
* @return never
*/
public function setInitializing(bool $initializing): void
{
$this->disallowMutation(__METHOD__);
}

/**
* @return never
*/
public static function setSingletons(array $singletons): void
{
self::disallowStaticMutation(__METHOD__);
}

/**
* @return never
*/
private function disallowMutation($method)
{
if ($this->constructed) {
self::disallowStaticMutation($method);
}
}

/**
* @return never
*/
private static function disallowStaticMutation($method)
{
throw new ImmutableException($method . ' not allowed on ' . static::class);
}
}
39 changes: 39 additions & 0 deletions tests/CarbonPeriod/ToDatePeriodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

namespace Tests\CarbonPeriod;

use Carbon\CarbonInterval;
use Carbon\CarbonPeriod;
use Carbon\Translator;
use DatePeriod;
use Tests\AbstractTestCase;

Expand Down Expand Up @@ -58,4 +60,41 @@ public function testToArrayIsNotEmptyArray()
$this->assertSame('2021-01-05', $newInstance->getStartDate()->format('Y-m-d'));
$this->assertSame(3, $newInstance->getRecurrences());
}

public function testWithIntervalLocalized()
{
CarbonInterval::setLocale('fr');
$period = CarbonPeriod::create('2021-01-05', 3);
$result = $period->floor()->toDatePeriod();

$this->assertSame(DatePeriod::class, \get_class($result));
$this->assertSame('2021-01-05', $result->getStartDate()->format('Y-m-d'));
$this->assertNull($result->getEndDate());

if (method_exists($result, 'getRecurrences')) {
$this->assertSame(3, $result->getRecurrences());
}

CarbonInterval::setLocale('en');
}

public function testWithModifiedEnglish()
{
$translator = Translator::get('en');
$translator->setTranslations([
'day' => ':count boring day|:count boring days',
]);
$period = CarbonPeriod::create('2021-01-05', 3);
$result = $period->floor()->toDatePeriod();

$this->assertSame(DatePeriod::class, \get_class($result));
$this->assertSame('2021-01-05', $result->getStartDate()->format('Y-m-d'));
$this->assertNull($result->getEndDate());

if (method_exists($result, 'getRecurrences')) {
$this->assertSame(3, $result->getRecurrences());
}

$translator->resetMessages();
}
}

0 comments on commit a01b257

Please sign in to comment.