Skip to content

Commit

Permalink
avoid recursion when value within bound
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptouuuu committed Nov 23, 2024
1 parent 005777c commit 8c7f920
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Period.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ public static function year(int $year): self
*/
public static function month(int $month): self
{
if ($month < 12) {
return new self(0, $month, 0, 0, 0, 0, 0);
}

/** @var int<0, max> */
$year = (int) ($month / 12);
$month = $month % 12;
Expand Down Expand Up @@ -153,6 +157,10 @@ public static function day(int $day): self
*/
public static function hour(int $hour): self
{
if ($hour < 24) {
return new self(0, 0, 0, $hour, 0, 0, 0);
}

/** @var int<0, max> */
$day = (int) ($hour / 24);
$hour = $hour % 24;
Expand All @@ -175,6 +183,10 @@ public static function hour(int $hour): self
*/
public static function minute(int $minute): self
{
if ($minute < 60) {
return new self(0, 0, 0, 0, $minute, 0, 0);
}

/** @var int<0, max> */
$hour = (int) ($minute / 60);
$hour = self::hour($hour);
Expand All @@ -198,6 +210,10 @@ public static function minute(int $minute): self
*/
public static function second(int $second): self
{
if ($second < 60) {
return new self(0, 0, 0, 0, 0, $second, 0);
}

/** @var int<0, max> */
$minute = (int) ($second / 60);
$minute = self::minute($minute);
Expand All @@ -221,6 +237,10 @@ public static function second(int $second): self
*/
public static function millisecond(int $millisecond): self
{
if ($millisecond < 1_000) {
return new self(0, 0, 0, 0, 0, 0, $millisecond);
}

/** @var int<0, max> */
$second = (int) ($millisecond / 1000);
$second = self::second($second);
Expand Down

0 comments on commit 8c7f920

Please sign in to comment.