Skip to content

Commit

Permalink
Year::atMonth(): deprecate int argument, add support for Month enum.
Browse files Browse the repository at this point in the history
  • Loading branch information
gnutix committed Mar 21, 2024
1 parent 4682c4e commit dea6992
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/Year.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/YearTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}

/**
Expand Down

0 comments on commit dea6992

Please sign in to comment.