Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ parameters:
message: "#with generic class DatePeriod but does not specify its types: TDate, TEnd, TRecurrences$#"
count: 1
path: src/ChronosDatePeriod.php
-
identifier: method.childReturnType
path: src/Chronos.php
12 changes: 7 additions & 5 deletions src/Chronos.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use DateTimeInterface;
use DateTimeZone;
use InvalidArgumentException;
use ReturnTypeWillChange;
use RuntimeException;
use Stringable;

Expand Down Expand Up @@ -1011,11 +1012,12 @@ public function modify(string $modifier): static
*
* @param \DateTimeInterface $target Target instance
* @param bool $absolute Whether the interval is forced to be positive
* @return \DateInterval
* @return \Cake\Chronos\ChronosInterval
*/
public function diff(DateTimeInterface $target, bool $absolute = false): DateInterval
#[ReturnTypeWillChange]
public function diff(DateTimeInterface $target, bool $absolute = false): ChronosInterval
{
return parent::diff($target, $absolute);
return new ChronosInterval(parent::diff($target, $absolute));
}

/**
Expand Down Expand Up @@ -2778,9 +2780,9 @@ public function secondsUntilEndOfDay(): int
* Convenience method for getting the remaining time from a given time.
*
* @param \DateTimeInterface $other The date to get the remaining time from.
* @return \DateInterval|bool The DateInterval object representing the difference between the two dates or FALSE on failure.
* @return \Cake\Chronos\ChronosInterval The ChronosInterval object representing the difference between the two dates.
*/
public static function fromNow(DateTimeInterface $other): DateInterval|bool
public static function fromNow(DateTimeInterface $other): ChronosInterval
{
$timeNow = new static();

Expand Down
6 changes: 3 additions & 3 deletions src/ChronosDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,11 +407,11 @@ public function setISODate(int $year, int $week, int $dayOfWeek = 1): static
*
* @param \Cake\Chronos\ChronosDate $target Target instance
* @param bool $absolute Whether the interval is forced to be positive
* @return \DateInterval
* @return \Cake\Chronos\ChronosInterval
*/
public function diff(ChronosDate $target, bool $absolute = false): DateInterval
public function diff(ChronosDate $target, bool $absolute = false): ChronosInterval
{
return $this->native->diff($target->native, $absolute);
return new ChronosInterval($this->native->diff($target->native, $absolute));
}

/**
Expand Down
34 changes: 25 additions & 9 deletions tests/TestCase/ChronosIntervalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,32 @@
namespace Cake\Chronos\Test\TestCase;

use Cake\Chronos\Chronos;
use Cake\Chronos\ChronosDate;
use Cake\Chronos\ChronosInterval;
use DateInterval;

class ChronosIntervalTest extends TestCase
{
public function testChronosDiffReturnsChronosInterval(): void
{
$start = new Chronos('2020-01-01');
$end = new Chronos('2020-01-11');
$diff = $start->diff($end);

$this->assertInstanceOf(ChronosInterval::class, $diff);
$this->assertSame(10, $diff->d);
}

public function testChronosDateDiffReturnsChronosInterval(): void
{
$start = ChronosDate::create(2020, 1, 1);
$end = ChronosDate::create(2020, 1, 11);
$diff = $start->diff($end);

$this->assertInstanceOf(ChronosInterval::class, $diff);
$this->assertSame(10, $diff->d);
}

public function testCreateFromSpec(): void
{
$interval = ChronosInterval::create('P1Y2M3D');
Expand Down Expand Up @@ -88,10 +109,8 @@ public function testToIso8601StringNegative(): void
{
$past = new Chronos('2020-01-01');
$future = new Chronos('2021-02-02');
$diff = $past->diff($future);
$diff->invert = 1;
$interval = $future->diff($past);

$interval = ChronosInterval::instance($diff);
$this->assertStringStartsWith('-P', $interval->toIso8601String());
}

Expand Down Expand Up @@ -123,9 +142,8 @@ public function testTotalDaysFromDiff(): void
{
$start = new Chronos('2020-01-01');
$end = new Chronos('2020-01-11');
$diff = $start->diff($end);
$interval = $start->diff($end);

$interval = ChronosInterval::instance($diff);
$this->assertSame(10, $interval->totalDays());
}

Expand All @@ -136,9 +154,8 @@ public function testIsNegative(): void

$past = new Chronos('2020-01-01');
$future = new Chronos('2020-01-02');
$diff = $future->diff($past);
$interval = $future->diff($past);

$interval = ChronosInterval::instance($diff);
$this->assertTrue($interval->isNegative());
}

Expand Down Expand Up @@ -296,9 +313,8 @@ public function testToDateStringNegative(): void
{
$past = new Chronos('2020-01-01');
$future = new Chronos('2020-01-02');
$diff = $future->diff($past);
$interval = $future->diff($past);

$interval = ChronosInterval::instance($diff);
$this->assertStringStartsWith('-', $interval->toDateString());
}

Expand Down
Loading