Skip to content

Commit

Permalink
Merge pull request #3071 from takaram/reduce-clone
Browse files Browse the repository at this point in the history
Optimize `setUnitNoOverflow()`
  • Loading branch information
kylekatarnls authored Oct 7, 2024
2 parents 50c0e29 + 335aa70 commit cda6063
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/Carbon/Traits/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -1702,11 +1702,10 @@ public function setDaysFromStartOfWeek(int $numberOfDays, WeekDay|int|null $week
public function setUnitNoOverflow(string $valueUnit, int $value, string $overflowUnit): static
{
try {
$original = $this->avoidMutation();
$start = $this->avoidMutation()->startOf($overflowUnit);
$end = $this->avoidMutation()->endOf($overflowUnit);
/** @var static $date */
$date = $this->$valueUnit($value);
$start = $original->avoidMutation()->startOf($overflowUnit);
$end = $original->avoidMutation()->endOf($overflowUnit);

if ($date < $start) {
$date = $date->setDateTimeFrom($start);
Expand Down

0 comments on commit cda6063

Please sign in to comment.