Skip to content

Commit

Permalink
Merge pull request #76 from gnutix/optimize-min-max-of-methods
Browse files Browse the repository at this point in the history
[performance] Optimize minOf/maxOf methods.
  • Loading branch information
BenMorel authored Sep 15, 2023
2 parents c7c2107 + 0bf9233 commit 5ac7c43
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/LocalDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,10 @@ public static function minOf(LocalDate ...$dates): LocalDate
throw new DateTimeException(__METHOD__ . ' does not accept less than 1 parameter.');
}

$min = LocalDate::max();
$min = null;

foreach ($dates as $date) {
if ($date->isBefore($min)) {
if ($min === null || $date->isBefore($min)) {
$min = $date;
}
}
Expand All @@ -275,10 +275,10 @@ public static function maxOf(LocalDate ...$dates): LocalDate
throw new DateTimeException(__METHOD__ . ' does not accept less than 1 parameter.');
}

$max = LocalDate::min();
$max = null;

foreach ($dates as $date) {
if ($date->isAfter($max)) {
if ($max === null || $date->isAfter($max)) {
$max = $date;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/LocalDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ public static function minOf(LocalDateTime ...$times): LocalDateTime
throw new DateTimeException(__METHOD__ . ' does not accept less than 1 parameter.');
}

$min = LocalDateTime::max();
$min = null;

foreach ($times as $time) {
if ($time->isBefore($min)) {
if ($min === null || $time->isBefore($min)) {
$min = $time;
}
}
Expand All @@ -160,10 +160,10 @@ public static function maxOf(LocalDateTime ...$times): LocalDateTime
throw new DateTimeException(__METHOD__ . ' does not accept less than 1 parameter.');
}

$max = LocalDateTime::min();
$max = null;

foreach ($times as $time) {
if ($time->isAfter($max)) {
if ($max === null || $time->isAfter($max)) {
$max = $time;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/LocalTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,10 @@ public static function minOf(LocalTime ...$times): LocalTime
throw new DateTimeException(__METHOD__ . ' does not accept less than 1 parameter.');
}

$min = LocalTime::max();
$min = null;

foreach ($times as $time) {
if ($time->isBefore($min)) {
if ($min === null || $time->isBefore($min)) {
$min = $time;
}
}
Expand All @@ -240,10 +240,10 @@ public static function maxOf(LocalTime ...$times): LocalTime
throw new DateTimeException(__METHOD__ . ' does not accept less than 1 parameter.');
}

$max = LocalTime::min();
$max = null;

foreach ($times as $time) {
if ($time->isAfter($max)) {
if ($max === null || $time->isAfter($max)) {
$max = $time;
}
}
Expand Down

0 comments on commit 5ac7c43

Please sign in to comment.