diff --git a/src/Carbon/AbstractTranslator.php b/src/Carbon/AbstractTranslator.php index 48441e7c29..9c8a430b7b 100644 --- a/src/Carbon/AbstractTranslator.php +++ b/src/Carbon/AbstractTranslator.php @@ -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); } /** @@ -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; } /** diff --git a/src/Carbon/CarbonInterval.php b/src/Carbon/CarbonInterval.php index 8f18c45b9e..3f7bef2b76 100644 --- a/src/Carbon/CarbonInterval.php +++ b/src/Carbon/CarbonInterval.php @@ -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)) { @@ -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); } /** diff --git a/src/Carbon/CarbonTimeZone.php b/src/Carbon/CarbonTimeZone.php index fdb31b98ed..9ecd33981e 100644 --- a/src/Carbon/CarbonTimeZone.php +++ b/src/Carbon/CarbonTimeZone.php @@ -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; } /** @@ -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; } /** diff --git a/src/Carbon/Traits/Comparison.php b/src/Carbon/Traits/Comparison.php index 9476ebecc5..5e30243a89 100644 --- a/src/Carbon/Traits/Comparison.php +++ b/src/Carbon/Traits/Comparison.php @@ -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; } /**