Skip to content

Commit e1268cd

Browse files
authored
Merge pull request #3103 from briannesbitt/fix/issue-3102-setUnitNoOverflow-immutable
Fix immutable return for setUnitNoOverflow
2 parents d481d8d + 9d1cc38 commit e1268cd

3 files changed

Lines changed: 17 additions & 1 deletion

File tree

src/Carbon/Traits/Date.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2965,7 +2965,7 @@ private static function floorZeroPad(int|float $value, int $length): string
29652965
private function mutateIfMutable(CarbonInterface $date): CarbonInterface
29662966
{
29672967
return $this instanceof DateTimeImmutable
2968-
? $this
2968+
? $date
29692969
: $this->modify('@'.$date->rawFormat('U.u'))->setTimezone($date->getTimezone());
29702970
}
29712971
}

tests/Carbon/SettersTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,14 @@ public function testMidDayAtSetter()
629629
$this->assertSame(12, $d->getMidDayAt());
630630
}
631631

632+
public function testSetUnitNoOverflowFebruary()
633+
{
634+
$d = Carbon::parse('2024-02-29')->setUnitNoOverFlow('day', 31, 'month');
635+
636+
$this->assertInstanceOf(Carbon::class, $d);
637+
$this->assertSame('2024-02-29 23:59:59.999999', $d->format('Y-m-d H:i:s.u'));
638+
}
639+
632640
public function testSetUnitNoOverflow()
633641
{
634642
$results = [

tests/CarbonImmutable/SettersTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,4 +423,12 @@ public function testSetter()
423423
$d->setMidDayAt(12);
424424
$this->assertSame(12, $d->getMidDayAt());
425425
}
426+
427+
public function testSetUnitNoOverflowFebruary()
428+
{
429+
$d = Carbon::parse('2024-02-29')->setUnitNoOverFlow('day', 31, 'month');
430+
431+
$this->assertInstanceOf(Carbon::class, $d);
432+
$this->assertSame('2024-02-29 23:59:59.999999', $d->format('Y-m-d H:i:s.u'));
433+
}
426434
}

0 commit comments

Comments
 (0)