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

LocalDateTime::of() / LocalDateTime::withMonth() / ZonedDateTime::withMonth() allows Month enum #105

Closed
wants to merge 2 commits into from
Closed
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: 9 additions & 9 deletions src/LocalDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@
}

/**
* @param int $year The year, from MIN_YEAR to MAX_YEAR.
* @param int $month The month-of-year, from 1 (January) to 12 (December).
* @param int $day The day-of-month, from 1 to 31.
* @param int $hour The hour-of-day, from 0 to 23.
* @param int $minute The minute-of-hour, from 0 to 59.
* @param int $second The second-of-minute, from 0 to 59.
* @param int $nano The nano-of-second, from 0 to 999,999,999.
* @param int $year The year, from MIN_YEAR to MAX_YEAR.
* @param int|Month $month The month-of-year, from 1 (January) to 12 (December).
* @param int $day The day-of-month, from 1 to 31.
* @param int $hour The hour-of-day, from 0 to 23.
* @param int $minute The minute-of-hour, from 0 to 59.
* @param int $second The second-of-minute, from 0 to 59.
* @param int $nano The nano-of-second, from 0 to 999,999,999.
*
* @throws DateTimeException If the date or time is not valid.
*/
public static function of(int $year, int $month, int $day, int $hour = 0, int $minute = 0, int $second = 0, int $nano = 0): LocalDateTime
public static function of(int $year, Month|int $month, int $day, int $hour = 0, int $minute = 0, int $second = 0, int $nano = 0): LocalDateTime
{
$date = LocalDate::of($year, $month, $day);

Check failure on line 46 in src/LocalDateTime.php

View workflow job for this annotation

GitHub Actions / Psalm

PossiblyInvalidArgument

src/LocalDateTime.php:46:38: PossiblyInvalidArgument: Argument 2 of Brick\DateTime\LocalDate::of expects int, but possibly different type Brick\DateTime\Month|int provided (see https://psalm.dev/092)
$time = LocalTime::of($hour, $minute, $second, $nano);

return new LocalDateTime($date, $time);
Expand Down Expand Up @@ -306,9 +306,9 @@
*
* @throws DateTimeException If the month is invalid.
*/
public function withMonth(int $month): LocalDateTime
public function withMonth(Month|int $month): LocalDateTime
{
$date = $this->date->withMonth($month);

Check failure on line 311 in src/LocalDateTime.php

View workflow job for this annotation

GitHub Actions / Psalm

PossiblyInvalidArgument

src/LocalDateTime.php:311:40: PossiblyInvalidArgument: Argument 1 of Brick\DateTime\LocalDate::withMonth expects int, but possibly different type Brick\DateTime\Month|int provided (see https://psalm.dev/092)

if ($date === $this->date) {
return $this;
Expand Down
2 changes: 1 addition & 1 deletion src/ZonedDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ public function withYear(int $year): ZonedDateTime
/**
* Returns a copy of this ZonedDateTime with the month-of-year altered.
*/
public function withMonth(int $month): ZonedDateTime
public function withMonth(Month|int $month): ZonedDateTime
{
return ZonedDateTime::of($this->localDateTime->withMonth($month), $this->timeZone);
}
Expand Down
15 changes: 11 additions & 4 deletions tests/LocalDateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Brick\DateTime\LocalDate;
use Brick\DateTime\LocalDateTime;
use Brick\DateTime\LocalTime;
use Brick\DateTime\Month;
use Brick\DateTime\Parser\DateTimeParseException;
use Brick\DateTime\Period;
use Brick\DateTime\TimeZone;
Expand All @@ -32,9 +33,12 @@ class LocalDateTimeTest extends AbstractTestCase
{
public function testOf(): void
{
$date = LocalDate::of(2001, 12, 23);
$time = LocalTime::of(12, 34, 56, 987654321);

$date = LocalDate::of(2001, 12, 23);
self::assertLocalDateTimeIs(2001, 12, 23, 12, 34, 56, 987654321, new LocalDateTime($date, $time));

$date = LocalDate::of(2001, Month::DECEMBER, 23);
self::assertLocalDateTimeIs(2001, 12, 23, 12, 34, 56, 987654321, new LocalDateTime($date, $time));
}

Expand Down Expand Up @@ -349,10 +353,13 @@ public static function providerWithInvalidYearThrowsException(): array
#[DataProvider('providerWithMonth')]
public function testWithMonth(int $year, int $month, int $day, int $newMonth, int $expectedDay): void
{
$date = LocalDate::of($year, $month, $day);
$time = LocalTime::of(1, 2, 3, 123456789);
$localDateTime = $date->atTime($time)->withMonth($newMonth);
self::assertLocalDateTimeIs($year, $newMonth, $expectedDay, 1, 2, 3, 123456789, $localDateTime);

$date = LocalDate::of($year, $month, $day);
self::assertLocalDateTimeIs($year, $newMonth, $expectedDay, 1, 2, 3, 123456789, $date->atTime($time)->withMonth($newMonth));

$date = LocalDate::of($year, Month::from($month), $day);
self::assertLocalDateTimeIs($year, $newMonth, $expectedDay, 1, 2, 3, 123456789, $date->atTime($time)->withMonth($newMonth));
}

public static function providerWithMonth(): array
Expand Down
2 changes: 2 additions & 0 deletions tests/ZonedDateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Brick\DateTime\LocalDate;
use Brick\DateTime\LocalDateTime;
use Brick\DateTime\LocalTime;
use Brick\DateTime\Month;
use Brick\DateTime\Parser\DateTimeParseException;
use Brick\DateTime\Period;
use Brick\DateTime\TimeZone;
Expand Down Expand Up @@ -581,6 +582,7 @@ public function testWithYear(): void
public function testWithMonth(): void
{
self::assertIs(ZonedDateTime::class, '2000-07-20T12:34:56.123456789-07:00[America/Los_Angeles]', $this->getTestZonedDateTime()->withMonth(7));
self::assertIs(ZonedDateTime::class, '2000-07-20T12:34:56.123456789-07:00[America/Los_Angeles]', $this->getTestZonedDateTime()->withMonth(Month::JULY));
}

public function testWithDay(): void
Expand Down
Loading