Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Year::atMonth(): deprecate int argument, add support for Month enum. #95

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading