Skip to content

Commit

Permalink
Remove deprecations when using int for the month argument in Year/Yea…
Browse files Browse the repository at this point in the history
…rMonth/YearWeek
  • Loading branch information
gnutix committed Mar 30, 2024
1 parent 8389451 commit 8bf0821
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 31 deletions.
10 changes: 1 addition & 9 deletions src/Year.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@

use function is_int;
use function str_pad;
use function trigger_error;

use const E_USER_DEPRECATED;
use const STR_PAD_LEFT;

/**
Expand Down Expand Up @@ -243,16 +241,10 @@ public function atDay(int $dayOfYear): LocalDate
public function atMonth(int|Month $month): YearMonth
{
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);
return YearMonth::of($this->year, $month);
}

/**
Expand Down
16 changes: 3 additions & 13 deletions src/YearMonth.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@

use function is_int;
use function str_pad;
use function trigger_error;

use const E_USER_DEPRECATED;
use const STR_PAD_LEFT;

/**
Expand Down Expand Up @@ -46,16 +44,12 @@ public static function of(int $year, int|Month $month): YearMonth
Field\Year::check($year);

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 YearMonth::of() second argument is deprecated, pass a Month instance instead.', E_USER_DEPRECATED);

Field\MonthOfYear::check($month);

return new YearMonth($year, $month);
} else {
$month = $month->value;
}

return new YearMonth($year, $month->value);
return new YearMonth($year, $month);
}

/**
Expand Down Expand Up @@ -216,10 +210,6 @@ public function withYear(int $year): YearMonth
public function withMonth(int|Month $month): YearMonth
{
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 YearMonth::withMonth() is deprecated, pass a Month instance instead.', E_USER_DEPRECATED);

Field\MonthOfYear::check($month);
} else {
$month = $month->value;
Expand Down
12 changes: 3 additions & 9 deletions src/YearWeek.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@

use function is_int;
use function str_pad;
use function trigger_error;

use const E_USER_DEPRECATED;
use const STR_PAD_LEFT;

/**
Expand Down Expand Up @@ -197,17 +195,13 @@ public function withWeek(int $week): YearWeek
public function atDay(DayOfWeek|int $dayOfWeek): LocalDate
{
if (is_int($dayOfWeek)) {
// 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 YearWeek::atDay() is deprecated, pass a DayOfWeek instance instead.', E_USER_DEPRECATED);

Field\DayOfWeek::check($dayOfWeek);

$dayOfWeek = DayOfWeek::from($dayOfWeek);
} else {
$dayOfWeek = $dayOfWeek->value;
}

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

if ($dayOfYear > $maxDaysOfYear) {
Expand Down

0 comments on commit 8bf0821

Please sign in to comment.