diff --git a/src/LocalDate.php b/src/LocalDate.php index 667027e..f0939c1 100644 --- a/src/LocalDate.php +++ b/src/LocalDate.php @@ -470,6 +470,13 @@ public function plusDays(int $days): LocalDate return $this; } + // Performance optimization for a common use case. + if ($days === 1) { + return $this->day >= 28 && $this->day === $this->getLengthOfMonth() + ? new self($this->year + intdiv($this->month, 12), ($this->month % 12) + 1, 1) + : new self($this->year, $this->month, $this->day + 1); + } + return LocalDate::ofEpochDay($this->toEpochDay() + $days); } diff --git a/tests/LocalDateTest.php b/tests/LocalDateTest.php index 30c71d4..b2faa29 100644 --- a/tests/LocalDateTest.php +++ b/tests/LocalDateTest.php @@ -798,6 +798,12 @@ public function providerPlusDays(): array [2012, 1, 1, 366, 2013, 1, 1], [2012, 1, 2, -1, 2012, 1, 1], [2012, 1, 1, -1, 2011, 12, 31], + [2014, 2, 1, 1, 2014, 2, 2], + [2014, 2, 15, 1, 2014, 2, 16], + [2014, 2, 28, 1, 2014, 3, 1], + [2012, 2, 28, 1, 2012, 2, 29], + [2012, 2, 29, 1, 2012, 3, 1], + [2014, 12, 31, 1, 2015, 1, 1], ]; }