Skip to content

Commit

Permalink
Fix immutable return for setUnitNoOverflow
Browse files Browse the repository at this point in the history
Fix #3102
  • Loading branch information
kylekatarnls committed Nov 7, 2024
1 parent 10ac0aa commit 9d1cc38
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Carbon/Traits/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -2965,7 +2965,7 @@ private static function floorZeroPad(int|float $value, int $length): string
private function mutateIfMutable(CarbonInterface $date): CarbonInterface
{
return $this instanceof DateTimeImmutable
? $this
? $date
: $this->modify('@'.$date->rawFormat('U.u'))->setTimezone($date->getTimezone());
}
}
8 changes: 8 additions & 0 deletions tests/Carbon/SettersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,14 @@ public function testMidDayAtSetter()
$this->assertSame(12, $d->getMidDayAt());
}

public function testSetUnitNoOverflowFebruary()
{
$d = Carbon::parse('2024-02-29')->setUnitNoOverFlow('day', 31, 'month');

$this->assertInstanceOf(Carbon::class, $d);
$this->assertSame('2024-02-29 23:59:59.999999', $d->format('Y-m-d H:i:s.u'));
}

public function testSetUnitNoOverflow()
{
$results = [
Expand Down
8 changes: 8 additions & 0 deletions tests/CarbonImmutable/SettersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,12 @@ public function testSetter()
$d->setMidDayAt(12);
$this->assertSame(12, $d->getMidDayAt());
}

public function testSetUnitNoOverflowFebruary()
{
$d = Carbon::parse('2024-02-29')->setUnitNoOverFlow('day', 31, 'month');

$this->assertInstanceOf(Carbon::class, $d);
$this->assertSame('2024-02-29 23:59:59.999999', $d->format('Y-m-d H:i:s.u'));
}
}

0 comments on commit 9d1cc38

Please sign in to comment.