Skip to content

Commit

Permalink
Use comparison methods instead of operators
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Oct 6, 2024
1 parent dfe7a32 commit 84c1cb0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
14 changes: 7 additions & 7 deletions src/Carbon/Traits/Comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function eq(DateTimeInterface|string $date): bool
*/
public function equalTo(DateTimeInterface|string $date): bool
{
return $this == $this->resolveCarbon($date);
return $this->getPreciseTimestamp() == $this->resolveCarbon($date)->getPreciseTimestamp();
}

/**
Expand Down Expand Up @@ -140,7 +140,7 @@ public function gt(DateTimeInterface|string $date): bool
*/
public function greaterThan(DateTimeInterface|string $date): bool
{
return $this > $this->resolveCarbon($date);
return $this->getPreciseTimestamp() > $this->resolveCarbon($date)->getPreciseTimestamp();
}

/**
Expand Down Expand Up @@ -189,7 +189,7 @@ public function gte(DateTimeInterface|string $date): bool
*/
public function greaterThanOrEqualTo(DateTimeInterface|string $date): bool
{
return $this >= $this->resolveCarbon($date);
return $this->getPreciseTimestamp() >= $this->resolveCarbon($date)->getPreciseTimestamp();
}

/**
Expand Down Expand Up @@ -221,7 +221,7 @@ public function lt(DateTimeInterface|string $date): bool
*/
public function lessThan(DateTimeInterface|string $date): bool
{
return $this < $this->resolveCarbon($date);
return $this->getPreciseTimestamp() < $this->resolveCarbon($date)->getPreciseTimestamp();
}

/**
Expand Down Expand Up @@ -270,7 +270,7 @@ public function lte(DateTimeInterface|string $date): bool
*/
public function lessThanOrEqualTo(DateTimeInterface|string $date): bool
{
return $this <= $this->resolveCarbon($date);
return $this->getPreciseTimestamp() <= $this->resolveCarbon($date)->getPreciseTimestamp();
}

/**
Expand Down Expand Up @@ -300,10 +300,10 @@ public function between(DateTimeInterface|string $date1, DateTimeInterface|strin
}

if ($equal) {
return $this >= $date1 && $this <= $date2;
return $this->greaterThanOrEqualTo($date1) && $this->lessThanOrEqualTo($date2);
}

return $this > $date1 && $this < $date2;
return $this->greaterThan($date1) && $this->lessThan($date2);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Carbon/Traits/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -1708,9 +1708,9 @@ public function setUnitNoOverflow(string $valueUnit, int $value, string $overflo
$start = $original->avoidMutation()->startOf($overflowUnit);
$end = $original->avoidMutation()->endOf($overflowUnit);

if ($date < $start) {
if ($date->lessThan($start)) {
$date = $date->setDateTimeFrom($start);
} elseif ($date > $end) {
} elseif ($date->greaterThan($end)) {
$date = $date->setDateTimeFrom($end);
}

Expand Down
7 changes: 4 additions & 3 deletions tests/Carbon/SettersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\TestWith;
use Tests\AbstractTestCase;
use function PHPUnit\Framework\greaterThan;

class SettersTest extends AbstractTestCase
{
Expand Down Expand Up @@ -676,7 +677,7 @@ public function testSetUnitNoOverflow()
$start = $original->copy()->startOf($overflowUnit);
$end = $original->copy()->endOf($overflowUnit);

if ($date < $start || $date > $end) {
if ($date->lessThan($start) || $date->greaterThan($end)) {
$results['failure'][] = [
'year' => $year,
'month' => $month,
Expand Down Expand Up @@ -821,7 +822,7 @@ public function testAddUnitNoOverflow()
$start = $original->copy()->startOf($overflowUnit);
$end = $original->copy()->endOf($overflowUnit);

if ($date < $start || $date > $end) {
if ($date->lessThan($start) || $date->greaterThan($end)) {
$results['failure'][] = [
'year' => $year,
'month' => $month,
Expand Down Expand Up @@ -930,7 +931,7 @@ public function testSubUnitNoOverflow()
$start = $original->copy()->startOf($overflowUnit);
$end = $original->copy()->endOf($overflowUnit);

if ($date < $start || $date > $end) {
if ($date->lessThan($start) || $date->greaterThan($end)) {
$results['failure'][] = [
'year' => $year,
'month' => $month,
Expand Down

0 comments on commit 84c1cb0

Please sign in to comment.