From dea699237cbadbb137b875b5df68d38fa1e53f84 Mon Sep 17 00:00:00 2001 From: Dorian Villet Date: Thu, 21 Mar 2024 10:23:59 +0100 Subject: [PATCH] Year::atMonth(): deprecate int argument, add support for Month enum. --- src/Year.php | 18 ++++++++++++------ tests/YearTest.php | 2 ++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/Year.php b/src/Year.php index 0802a49..2045412 100644 --- a/src/Year.php +++ b/src/Year.php @@ -239,14 +239,20 @@ public function atDay(int $dayOfYear): LocalDate /** * Combines this year with a month to create a YearMonth. - * - * @param int $month The month-of-year to use, from 1 to 12. - * - * @throws DateTimeException If the month is invalid. */ - public function atMonth(int $month): YearMonth + public function atMonth(int|Month $month): YearMonth { - return YearMonth::of($this->year, $month); + if (is_int($month)) { + // usually we don't use trigger_error() for deprecations, but we can't rely on @deprecated for a parameter type change; + // maybe we should revisit using trigger_error() unconditionally for deprecations in the future. + trigger_error('Passing an integer to Year::atMonth() is deprecated, pass a Month instance instead.', E_USER_DEPRECATED); + + Field\MonthOfYear::check($month); + + $month = Month::from($month); + } + + return YearMonth::of($this->year, $month->value); } /** diff --git a/tests/YearTest.php b/tests/YearTest.php index 79a88c5..5265ec0 100644 --- a/tests/YearTest.php +++ b/tests/YearTest.php @@ -7,6 +7,7 @@ use Brick\DateTime\Clock\FixedClock; use Brick\DateTime\DateTimeException; use Brick\DateTime\Instant; +use Brick\DateTime\Month; use Brick\DateTime\MonthDay; use Brick\DateTime\TimeZone; use Brick\DateTime\Year; @@ -432,6 +433,7 @@ public function testAtInvalidDayThrowsException(): void public function testAtMonth(): void { self::assertYearMonthIs(2014, 7, Year::of(2014)->atMonth(7)); + self::assertYearMonthIs(2014, 7, Year::of(2014)->atMonth(Month::JULY)); } /**