Skip to content

Commit

Permalink
Optimize performances of minOf/maxOf methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
gnutix committed Sep 14, 2023
1 parent c7c2107 commit 1d93d18
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/LocalDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use DateTimeInterface;
use JsonSerializable;

use function array_shift;
use function intdiv;
use function min;
use function sprintf;
Expand Down Expand Up @@ -249,7 +250,7 @@ public static function minOf(LocalDate ...$dates): LocalDate
throw new DateTimeException(__METHOD__ . ' does not accept less than 1 parameter.');
}

$min = LocalDate::max();
$min = array_shift($dates);

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

$max = LocalDate::min();
$max = array_shift($dates);

foreach ($dates as $date) {
if ($date->isAfter($max)) {
Expand Down
5 changes: 3 additions & 2 deletions src/LocalDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use DateTimeInterface;
use JsonSerializable;

use function array_shift;
use function intdiv;

/**
Expand Down Expand Up @@ -134,7 +135,7 @@ public static function minOf(LocalDateTime ...$times): LocalDateTime
throw new DateTimeException(__METHOD__ . ' does not accept less than 1 parameter.');
}

$min = LocalDateTime::max();
$min = array_shift($times);

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

$max = LocalDateTime::min();
$max = array_shift($times);

foreach ($times as $time) {
if ($time->isAfter($max)) {
Expand Down
5 changes: 3 additions & 2 deletions src/LocalTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use DateTimeInterface;
use JsonSerializable;

use function array_shift;
use function intdiv;
use function rtrim;
use function sprintf;
Expand Down Expand Up @@ -214,7 +215,7 @@ public static function minOf(LocalTime ...$times): LocalTime
throw new DateTimeException(__METHOD__ . ' does not accept less than 1 parameter.');
}

$min = LocalTime::max();
$min = array_shift($times);

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

$max = LocalTime::min();
$max = array_shift($times);

foreach ($times as $time) {
if ($time->isAfter($max)) {
Expand Down

0 comments on commit 1d93d18

Please sign in to comment.