diff --git a/phpstan.neon b/phpstan.neon index 02711ac..1d3977a 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -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 diff --git a/src/Chronos.php b/src/Chronos.php index 2cb3e0c..baa67b1 100644 --- a/src/Chronos.php +++ b/src/Chronos.php @@ -20,6 +20,7 @@ use DateTimeInterface; use DateTimeZone; use InvalidArgumentException; +use ReturnTypeWillChange; use RuntimeException; use Stringable; @@ -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)); } /** @@ -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(); diff --git a/src/ChronosDate.php b/src/ChronosDate.php index 3d80089..902314f 100644 --- a/src/ChronosDate.php +++ b/src/ChronosDate.php @@ -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)); } /** diff --git a/tests/TestCase/ChronosIntervalTest.php b/tests/TestCase/ChronosIntervalTest.php index ffefedc..2875a0d 100644 --- a/tests/TestCase/ChronosIntervalTest.php +++ b/tests/TestCase/ChronosIntervalTest.php @@ -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'); @@ -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()); } @@ -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()); } @@ -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()); } @@ -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()); }