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

Remove deprecations when using int for the month argument in Year/YearMonth/YearWeek #103

Merged
merged 1 commit into from
Mar 31, 2024
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
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
Loading