Skip to content

Commit

Permalink
Allow to compare different sub-classes of Carbon
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Jan 23, 2024
1 parent 616a9d6 commit e61a85b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Carbon/CarbonPeriod.php
Original file line number Diff line number Diff line change
Expand Up @@ -2413,7 +2413,7 @@ protected function resolveCarbon($date = null)
/**
* Resolve passed arguments or DatePeriod to a CarbonPeriod object.
*/
protected function resolveCarbonPeriod(mixed $period, mixed ...$arguments): static
protected function resolveCarbonPeriod(mixed $period, mixed ...$arguments): self
{
if ($period instanceof self) {
return $period;
Expand Down
8 changes: 2 additions & 6 deletions src/Carbon/Traits/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -2789,7 +2789,7 @@ public function __call(string $method, array $parameters): mixed
* Return the Carbon instance passed through, a now instance in the same timezone
* if null given or parse the input if string given.
*/
protected function resolveCarbon(DateTimeInterface|string|null $date): static
protected function resolveCarbon(DateTimeInterface|string|null $date): self
{
if (!$date) {
return $this->nowWithSameTz();
Expand All @@ -2806,12 +2806,8 @@ protected function resolveCarbon(DateTimeInterface|string|null $date): static
* Return the Carbon instance passed through, a now instance in UTC
* if null given or parse the input if string given (using current timezone
* then switching to UTC).
*
* @param Carbon|DateTimeInterface|string|null $date
*
* @return static
*/
protected function resolveUTC(DateTimeInterface|string|null $date = null): static
protected function resolveUTC(DateTimeInterface|string|null $date = null): self
{
if (!$date) {
return $this->transmitFactory(static fn () => static::now('UTC'));
Expand Down
11 changes: 11 additions & 0 deletions tests/Carbon/ConstructTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use DateTime;
use DateTimeInterface;
use DateTimeZone;
use SubCarbon;
use Tests\AbstractTestCase;

class ConstructTest extends AbstractTestCase
Expand Down Expand Up @@ -199,4 +200,14 @@ public function testFloatTimestamp()
$date = new Carbon(123.5);
$this->assertSame('Thursday 1 January 1970 00:02:03.500000', $date->format('l j F Y H:i:s.u'));
}

public function testDifferentType()
{
require_once __DIR__.'/../Fixtures/SubCarbon.php';

$subCarbon = new SubCarbon('2024-01-24 00:00');
$carbon = new Carbon('2024-01-24 00:00');
$this->assertTrue($subCarbon->equalTo($carbon));
$this->assertTrue($carbon->equalTo($subCarbon));
}
}
11 changes: 11 additions & 0 deletions tests/CarbonImmutable/ComparisonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Carbon\CarbonImmutable as Carbon;
use DateTime;
use SubCarbon;
use Tests\AbstractTestCase;

class ComparisonTest extends AbstractTestCase
Expand Down Expand Up @@ -304,4 +305,14 @@ public function testFarthestWithFarDates()

$this->assertSame('06.300000', $baseDate->farthest($closestDate, $farthestDate)->format('s.u'));
}

public function testDifferentType()
{
require_once __DIR__.'/../Fixtures/SubCarbon.php';

$subCarbon = new SubCarbon('2024-01-24 00:00');
$carbon = new Carbon('2024-01-24 00:00');
$this->assertTrue($subCarbon->equalTo($carbon));
$this->assertTrue($carbon->equalTo($subCarbon));
}
}

0 comments on commit e61a85b

Please sign in to comment.