Skip to content

Commit

Permalink
Fix Month enum argument deprecations within the library itself.
Browse files Browse the repository at this point in the history
  • Loading branch information
gnutix committed Mar 27, 2024
1 parent dbc3696 commit ad1c9a4
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 50 deletions.
3 changes: 2 additions & 1 deletion src/Field/WeekOfYear.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Brick\DateTime\DateTimeException;
use Brick\DateTime\DayOfWeek;
use Brick\DateTime\LocalDate;
use Brick\DateTime\Month;

/**
* The week-of-year field.
Expand Down Expand Up @@ -51,7 +52,7 @@ public static function check(int $weekOfYear, ?int $year = null): void
*/
public static function is53WeekYear(int $year): bool
{
$date = LocalDate::of($year, 1, 1);
$date = LocalDate::of($year, Month::JANUARY, 1);
$dayOfWeek = $date->getDayOfWeek();

return $dayOfWeek === DayOfWeek::THURSDAY
Expand Down
49 changes: 35 additions & 14 deletions src/LocalDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
use Stringable;

use function intdiv;
use function is_int;
use function min;
use function str_pad;
use function trigger_error;

use const E_USER_DEPRECATED;
use const STR_PAD_LEFT;

/**
Expand Down Expand Up @@ -65,16 +68,24 @@ private function __construct(
/**
* Obtains an instance of `LocalDate` from a year, month and day.
*
* @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 $year The year, from MIN_YEAR to MAX_YEAR.
* @param int $day The day-of-month, from 1 to 31.
*
* @throws DateTimeException If the date is not valid.
*/
public static function of(int $year, int $month, int $day): LocalDate
public static function of(int $year, Month|int $month, int $day): LocalDate
{
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 LocalDateTime::of() is deprecated, pass a Month instance instead.', E_USER_DEPRECATED);

Field\MonthOfYear::check($month);
} else {
$month = $month->value;
}

Field\Year::check($year);
Field\MonthOfYear::check($month);
Field\DayOfMonth::check($day, $month, $year);

return new LocalDate($year, $month, $day);
Expand Down Expand Up @@ -104,7 +115,7 @@ public static function ofYearDay(int $year, int $dayOfYear): LocalDate

$dayOfMonth = $dayOfYear - $monthOfYear->getFirstDayOfYear($isLeap) + 1;

return LocalDate::of($year, $monthOfYear->value, $dayOfMonth);
return LocalDate::of($year, $monthOfYear, $dayOfMonth);
}

/**
Expand All @@ -117,7 +128,9 @@ public static function from(DateTimeParseResult $result): LocalDate
$month = (int) $result->getField(Field\MonthOfYear::NAME);
$day = (int) $result->getField(Field\DayOfMonth::NAME);

return LocalDate::of($year, $month, $day);
Field\MonthOfYear::check($month);

return LocalDate::of($year, Month::from($month), $day);
}

/**
Expand Down Expand Up @@ -216,7 +229,7 @@ public static function min(): LocalDate
return $min;
}

return $min = LocalDate::of(self::MIN_YEAR, 1, 1);
return $min = LocalDate::of(self::MIN_YEAR, Month::JANUARY, 1);
}

/**
Expand All @@ -233,7 +246,7 @@ public static function max(): LocalDate
return $max;
}

return $max = LocalDate::of(self::MAX_YEAR, 12, 31);
return $max = LocalDate::of(self::MAX_YEAR, Month::DECEMBER, 31);
}

/**
Expand Down Expand Up @@ -325,7 +338,7 @@ public function getDayOfMonth(): int

public function getYearMonth(): YearMonth
{
return YearMonth::of($this->year, $this->month);
return YearMonth::of($this->year, Month::from($this->month));
}

public function getDayOfWeek(): DayOfWeek
Expand Down Expand Up @@ -382,14 +395,22 @@ public function withYear(int $year): LocalDate
*
* @throws DateTimeException If the month is invalid.
*/
public function withMonth(int $month): LocalDate
public function withMonth(Month|int $month): LocalDate
{
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 LocalDate::withMonth() is deprecated, pass a Month instance instead.', E_USER_DEPRECATED);

Field\MonthOfYear::check($month);
} else {
$month = $month->value;
}

if ($month === $this->month) {
return $this;
}

Field\MonthOfYear::check($month);

return $this->resolvePreviousValid($this->year, $month, $this->day);
}

Expand Down Expand Up @@ -808,7 +829,7 @@ public function __toString(): string
private function resolvePreviousValid(int $year, int $month, int $day): LocalDate
{
if ($day > 28) {
$day = min($day, YearMonth::of($year, $month)->getLengthOfMonth());
$day = min($day, YearMonth::of($year, Month::from($month))->getLengthOfMonth());
}

return new LocalDate($year, $month, $day);
Expand Down
21 changes: 18 additions & 3 deletions src/LocalDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
use Stringable;

use function intdiv;
use function is_int;
use function trigger_error;

use const E_USER_DEPRECATED;

/**
* A date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30.
Expand All @@ -32,7 +36,6 @@ public function __construct(

/**
* @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.
Expand All @@ -41,8 +44,14 @@ public function __construct(
*
* @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
{
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 LocalDateTime::of() is deprecated, pass a Month instance instead.', E_USER_DEPRECATED);
}

$date = LocalDate::of($year, $month, $day);
$time = LocalTime::of($hour, $minute, $second, $nano);

Expand Down Expand Up @@ -306,8 +315,14 @@ public function withYear(int $year): LocalDateTime
*
* @throws DateTimeException If the month is invalid.
*/
public function withMonth(int $month): LocalDateTime
public function withMonth(Month|int $month): LocalDateTime
{
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 LocalDateTime::withMonth() is deprecated, pass a Month instance instead.', E_USER_DEPRECATED);
}

$date = $this->date->withMonth($month);

if ($date === $this->date) {
Expand Down
2 changes: 1 addition & 1 deletion src/LocalTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ public function toSecondOfDay(): int
*/
public function toNativeDateTime(): DateTime
{
return $this->atDate(LocalDate::of(0, 1, 1))->toNativeDateTime();
return $this->atDate(LocalDate::of(0, Month::JANUARY, 1))->toNativeDateTime();
}

/**
Expand Down
47 changes: 35 additions & 12 deletions src/MonthDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
use JsonSerializable;
use Stringable;

use function is_int;
use function trigger_error;

use const E_USER_DEPRECATED;

/**
* A month-day in the ISO-8601 calendar system, such as `--12-03`.
*/
Expand All @@ -31,14 +36,22 @@ private function __construct(
/**
* Obtains an instance of MonthDay.
*
* @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 $day The day-of-month, from 1 to 31.
*
* @throws DateTimeException If the month-day is not valid.
*/
public static function of(int $month, int $day): MonthDay
public static function of(Month|int $month, int $day): MonthDay
{
Field\MonthOfYear::check($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 MonthDay::of() is deprecated, pass a Month instance instead.', E_USER_DEPRECATED);

Field\MonthOfYear::check($month);
} else {
$month = $month->value;
}

Field\DayOfMonth::check($day, $month);

return new MonthDay($month, $day);
Expand All @@ -50,10 +63,12 @@ public static function of(int $month, int $day): MonthDay
*/
public static function from(DateTimeParseResult $result): MonthDay
{
return MonthDay::of(
(int) $result->getField(Field\MonthOfYear::NAME),
(int) $result->getField(Field\DayOfMonth::NAME),
);
$month = (int) $result->getField(Field\MonthOfYear::NAME);
$day = (int) $result->getField(Field\DayOfMonth::NAME);

Field\MonthOfYear::check($month);

return MonthDay::of(Month::from($month), $day);
}

/**
Expand Down Expand Up @@ -187,14 +202,22 @@ public function isValidYear(int $year): bool
*
* @throws DateTimeException If the month is invalid.
*/
public function withMonth(int $month): MonthDay
public function withMonth(Month|int $month): MonthDay
{
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 MonthDay::withMonth() is deprecated, pass a Month instance instead.', E_USER_DEPRECATED);

Field\MonthOfYear::check($month);
} else {
$month = $month->value;
}

if ($month === $this->month) {
return $this;
}

Field\MonthOfYear::check($month);

$lastDay = Field\MonthOfYear::getLength($month);

return new MonthDay($month, ($lastDay < $this->day) ? $lastDay : $this->day);
Expand Down Expand Up @@ -230,7 +253,7 @@ public function withDay(int $day): MonthDay
*/
public function atYear(int $year): LocalDate
{
return LocalDate::of($year, $this->month, $this->isValidYear($year) ? $this->day : 28);
return LocalDate::of($year, Month::from($this->month), $this->isValidYear($year) ? $this->day : 28);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Year.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public function atMonth(int|Month $month): YearMonth
$month = Month::from($month);
}

return YearMonth::of($this->year, $month->value);
return YearMonth::of($this->year, $month);
}

/**
Expand All @@ -274,8 +274,8 @@ public function atMonthDay(MonthDay $monthDay): LocalDate
public function toLocalDateRange(): LocalDateRange
{
return LocalDateRange::of(
$this->atMonth(1)->getFirstDay(),
$this->atMonth(12)->getLastDay(),
$this->atMonth(Month::JANUARY)->getFirstDay(),
$this->atMonth(Month::DECEMBER)->getLastDay(),
);
}

Expand Down
13 changes: 8 additions & 5 deletions src/YearMonth.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,13 @@ public static function of(int $year, int|Month $month): YearMonth
*/
public static function from(DateTimeParseResult $result): YearMonth
{
return YearMonth::of(
(int) $result->getField(Field\Year::NAME),
(int) $result->getField(Field\MonthOfYear::NAME),
);
$year = (int) $result->getField(Field\Year::NAME);
$month = (int) $result->getField(Field\MonthOfYear::NAME);

Field\Year::check($year);
Field\MonthOfYear::check($month);

return new YearMonth($year, $month);
}

/**
Expand Down Expand Up @@ -253,7 +256,7 @@ public function getLastDay(): LocalDate
*/
public function atDay(int $day): LocalDate
{
return LocalDate::of($this->year, $this->month, $day);
return LocalDate::of($this->year, Month::from($this->month), $day);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/YearMonthRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ public static function from(DateTimeParseResult $result): YearMonthRange
if ($result->hasField(Field\Year::NAME)) {
$end = YearMonth::from($result);
} else {
$end = $start->withMonth((int) $result->getField(Field\MonthOfYear::NAME));
$month = (int) $result->getField(Field\MonthOfYear::NAME);

Field\MonthOfYear::check($month);

$end = $start->withMonth(Month::from($month));
}

return YearMonthRange::of($start, $end);
Expand Down
2 changes: 1 addition & 1 deletion src/YearWeek.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public function atDay(DayOfWeek|int $dayOfWeek): LocalDate
$dayOfWeek = DayOfWeek::from($dayOfWeek);
}

$correction = LocalDate::of($this->year, 1, 4)->getDayOfWeek()->value + 3;
$correction = LocalDate::of($this->year, Month::JANUARY, 4)->getDayOfWeek()->value + 3;
$dayOfYear = $this->week * 7 + $dayOfWeek->value - $correction;
$maxDaysOfYear = Field\Year::isLeap($this->year) ? 366 : 365;

Expand Down
12 changes: 11 additions & 1 deletion src/ZonedDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
use Stringable;

use function intdiv;
use function is_int;
use function trigger_error;

use const E_USER_DEPRECATED;

/**
* A date-time with a time-zone in the ISO-8601 calendar system.
Expand Down Expand Up @@ -347,8 +351,14 @@ 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
{
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 ZonedDateTime::withMonth() is deprecated, pass a Month instance instead.', E_USER_DEPRECATED);
}

return ZonedDateTime::of($this->localDateTime->withMonth($month), $this->timeZone);
}

Expand Down
Loading

0 comments on commit ad1c9a4

Please sign in to comment.