Skip to content

Commit

Permalink
Merge pull request #2599 from lucasmichot/conditions
Browse files Browse the repository at this point in the history
Simplify some conditions
  • Loading branch information
kylekatarnls authored Apr 25, 2022
2 parents 0ccd195 + 0406228 commit 97a34af
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 42 deletions.
16 changes: 6 additions & 10 deletions src/Carbon/AbstractTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,7 @@ protected function translate(?string $id, array $parameters = [], ?string $domai
*/
protected function loadMessagesFromFile($locale)
{
if (isset($this->messages[$locale])) {
return true;
}

return $this->resetMessages($locale);
return isset($this->messages[$locale]) || $this->resetMessages($locale);
}

/**
Expand Down Expand Up @@ -359,13 +355,13 @@ public function setLocale($locale)
parent::setLocale($macroLocale);
}

if ($this->loadMessagesFromFile($locale) || $this->initializing) {
parent::setLocale($locale);

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

return false;
parent::setLocale($locale);

return true;
}

/**
Expand Down
20 changes: 9 additions & 11 deletions src/Carbon/CarbonInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -1435,11 +1435,9 @@ protected function getForHumansParameters($syntax = null, $short = false, $parts
];
}

if ($altNumbers) {
if ($altNumbers !== true) {
$language = new Language($this->locale);
$altNumbers = \in_array($language->getCode(), (array) $altNumbers, true);
}
if ($altNumbers && $altNumbers !== true) {
$language = new Language($this->locale);
$altNumbers = \in_array($language->getCode(), (array) $altNumbers, true);
}

if (\is_array($join)) {
Expand Down Expand Up @@ -1825,15 +1823,15 @@ public function __toString()
{
$format = $this->localToStringFormat;

if ($format) {
if ($format instanceof Closure) {
return $format($this);
}
if (!$format) {
return $this->forHumans();
}

return $this->format($format);
if ($format instanceof Closure) {
return $format($this);
}

return $this->forHumans();
return $this->format($format);
}

/**
Expand Down
24 changes: 12 additions & 12 deletions src/Carbon/CarbonTimeZone.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ public static function instance($object = null, $objectDump = null)
$tz = static::getDateTimeZoneFromName($object);
}

if ($tz === false) {
if (Carbon::isStrictModeEnabled()) {
throw new InvalidTimeZoneException('Unknown or bad timezone ('.($objectDump ?: $object).')');
}
if ($tz !== false) {
return new static($tz->getName());
}

return false;
if (Carbon::isStrictModeEnabled()) {
throw new InvalidTimeZoneException('Unknown or bad timezone ('.($objectDump ?: $object).')');
}

return new static($tz->getName());
return false;
}

/**
Expand Down Expand Up @@ -231,15 +231,15 @@ public function toRegionTimeZone(DateTimeInterface $date = null)
{
$tz = $this->toRegionName($date);

if ($tz === false) {
if (Carbon::isStrictModeEnabled()) {
throw new InvalidTimeZoneException('Unknown timezone for offset '.$this->getOffset($date ?: Carbon::now($this)).' seconds.');
}
if ($tz !== false) {
return new static($tz);
}

return false;
if (Carbon::isStrictModeEnabled()) {
throw new InvalidTimeZoneException('Unknown timezone for offset '.$this->getOffset($date ?: Carbon::now($this)).' seconds.');
}

return new static($tz);
return false;
}

/**
Expand Down
18 changes: 9 additions & 9 deletions src/Carbon/Traits/Comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -621,19 +621,19 @@ public function isSameUnit($unit, $date = null)
'microsecond' => 'Y-m-d H:i:s.u',
];

if (!isset($units[$unit])) {
if (isset($this->$unit)) {
return $this->resolveCarbon($date)->$unit === $this->$unit;
}
if (isset($units[$unit])) {
return $this->isSameAs($units[$unit], $date);
}

if ($this->localStrictModeEnabled ?? static::isStrictModeEnabled()) {
throw new BadComparisonUnitException($unit);
}
if (isset($this->$unit)) {
return $this->resolveCarbon($date)->$unit === $this->$unit;
}

return false;
if ($this->localStrictModeEnabled ?? static::isStrictModeEnabled()) {
throw new BadComparisonUnitException($unit);
}

return $this->isSameAs($units[$unit], $date);
return false;
}

/**
Expand Down

0 comments on commit 97a34af

Please sign in to comment.