From a73f7a3f2e36caa62d1a06207b59c6d5b713857d Mon Sep 17 00:00:00 2001 From: breno Date: Mon, 18 Sep 2023 11:05:15 -0400 Subject: [PATCH 1/5] add halfOfYear, isFirstHalfOfYear, IsSecondHalfOfYear; improve getters typehint --- src/Chronos.php | 50 ++++++++++++++++++------- src/ChronosDate.php | 40 ++++++++++++++++---- tests/TestCase/Date/GettersTest.php | 41 ++++++++++++++++++++ tests/TestCase/Date/IsTest.php | 20 ++++++++++ tests/TestCase/DateTime/GettersTest.php | 19 ++++++++++ tests/TestCase/DateTime/IsTest.php | 21 +++++++++++ tests/TestCase/DateTime/IssetTest.php | 1 + 7 files changed, 171 insertions(+), 21 deletions(-) create mode 100644 tests/TestCase/Date/GettersTest.php diff --git a/src/Chronos.php b/src/Chronos.php index 969802b7..267f96bf 100644 --- a/src/Chronos.php +++ b/src/Chronos.php @@ -29,23 +29,24 @@ * * @property-read int $year * @property-read int $yearIso - * @property-read int $month - * @property-read int $day - * @property-read int $hour - * @property-read int $minute - * @property-read int $second - * @property-read int $micro - * @property-read int $microsecond + * @property-read int<1, 12> $month + * @property-read int<1, 31> $day + * @property-read int<0, 23> $hour + * @property-read int<0, 59> $minute + * @property-read int<0, 59> $second + * @property-read int<0, 999999> $micro + * @property-read int<0, 999999> $microsecond * @property-read int $timestamp seconds since the Unix Epoch * @property-read \DateTimeZone $timezone the current timezone * @property-read \DateTimeZone $tz alias of timezone - * @property-read int $dayOfWeek 1 (for Monday) through 7 (for Sunday) - * @property-read int $dayOfYear 0 through 365 - * @property-read int $weekOfMonth 1 through 5 - * @property-read int $weekOfYear ISO-8601 week number of year, weeks starting on Monday - * @property-read int $daysInMonth number of days in the given month + * @property-read int<1, 7> $dayOfWeek 1 (for Monday) through 7 (for Sunday) + * @property-read int<0, 365> $dayOfYear 0 through 365 + * @property-read int<1, 5> $weekOfMonth 1 through 5 + * @property-read int<1, 53> $weekOfYear ISO-8601 week number of year, weeks starting on Monday + * @property-read int<1, 31> $daysInMonth number of days in the given month * @property-read int $age does a diffInYears() with default parameters - * @property-read int $quarter the quarter of this instance, 1 - 4 + * @property-read int<1, 4> $quarter the quarter of this instance, 1 - 4 + * @property-read int<1, 2> $halfOfYear the half of year of this instance, 1 - 2 * @property-read int $offset the timezone offset in seconds from UTC * @property-read int $offsetHours the timezone offset in hours from UTC * @property-read bool $dst daylight savings time indicator, true if DST, false otherwise @@ -2055,6 +2056,26 @@ public function isLastYear(): bool return $this->year === static::now($this->tz)->subYears(1)->year; } + /** + * Determines if the instance is within the first half of year + * + * @return bool + */ + public function isFirstHalfOfYear(): bool + { + return $this->halfOfYear === 1; + } + + /** + * Determines if the instance is within the second half of year + * + * @return bool + */ + public function isSecondHalfOfYear(): bool + { + return $this->halfOfYear === 2; + } + /** * Determines if the instance is in the future, ie. greater (after) than now * @@ -2592,6 +2613,9 @@ public function __get(string $name): string|float|int|bool|DateTimeZone case $name === 'quarter': return (int)ceil($this->month / 3); + case $name === 'halfOfYear': + return $this->month <= 6 ? 1 : 2; + case $name === 'offset': return $this->getOffset(); diff --git a/src/ChronosDate.php b/src/ChronosDate.php index 941b5158..b9a194dd 100644 --- a/src/ChronosDate.php +++ b/src/ChronosDate.php @@ -30,15 +30,16 @@ * * @property-read int $year * @property-read int $yearIso - * @property-read int $month - * @property-read int $day - * @property-read int $dayOfWeek 1 (for Monday) through 7 (for Sunday) - * @property-read int $dayOfYear 0 through 365 - * @property-read int $weekOfMonth 1 through 5 - * @property-read int $weekOfYear ISO-8601 week number of year, weeks starting on Monday - * @property-read int $daysInMonth number of days in the given month + * @property-read int<1, 12> $month + * @property-read int<1, 31> $day + * @property-read int<1, 7> $dayOfWeek 1 (for Monday) through 7 (for Sunday) + * @property-read int<0, 365> $dayOfYear 0 through 365 + * @property-read int<1, 5> $weekOfMonth 1 through 5 + * @property-read int<1, 53> $weekOfYear ISO-8601 week number of year, weeks starting on Monday + * @property-read int<1, 31> $daysInMonth number of days in the given month * @property-read int $age does a diffInYears() with default parameters - * @property-read int $quarter the quarter of this instance, 1 - 4 + * @property-read int<1, 4> $quarter the quarter of this instance, 1 - 4 + * @property-read int<1, 2> $halfOfYear the half of year of this instance, 1 - 2 * @psalm-immutable * @psalm-consistent-constructor */ @@ -1225,6 +1226,26 @@ public function isLastYear(DateTimeZone|string|null $timezone = null): bool return $this->year === static::now($timezone)->subYears(1)->year; } + /** + * Determines if the instance is within the first half of year + * + * @return bool + */ + public function isFirstHalfOfYear(): bool + { + return $this->halfOfYear === 1; + } + + /** + * Determines if the instance is within the second half of year + * + * @return bool + */ + public function isSecondHalfOfYear(): bool + { + return $this->halfOfYear === 2; + } + /** * Determines if the instance is in the future, ie. greater (after) than now * @@ -1566,6 +1587,9 @@ public function __get(string $name): string|float|int|bool case $name === 'quarter': return (int)ceil($this->month / 3); + case $name === 'halfOfYear': + return $this->month <= 6 ? 1 : 2; + default: throw new InvalidArgumentException(sprintf("Unknown getter '%s'", $name)); } diff --git a/tests/TestCase/Date/GettersTest.php b/tests/TestCase/Date/GettersTest.php new file mode 100644 index 00000000..62521d3d --- /dev/null +++ b/tests/TestCase/Date/GettersTest.php @@ -0,0 +1,41 @@ + + * @link https://cakephp.org CakePHP(tm) Project + * @license https://www.opensource.org/licenses/mit-license.php MIT License + */ + +namespace Cake\Chronos\Test\TestCase\Date; + +use Cake\Chronos\ChronosDate; +use Cake\Chronos\Test\TestCase\TestCase; +use PHPUnit\Framework\Attributes\TestWith; + +class GettersTest extends TestCase +{ + #[TestWith([1, 1])] + #[TestWith([2, 1])] + #[TestWith([3, 1])] + #[TestWith([4, 1])] + #[TestWith([5, 1])] + #[TestWith([6, 1])] + #[TestWith([7, 2])] + #[TestWith([8, 2])] + #[TestWith([9, 2])] + #[TestWith([10, 2])] + #[TestWith([11, 2])] + #[TestWith([12, 2])] + public function testHalfOfYear(int $month, int $expectedHalfOfYear): void + { + $d = ChronosDate::create(year: 2012, month: $month, day: 1); + $this->assertSame($expectedHalfOfYear, $d->halfOfYear); + } +} diff --git a/tests/TestCase/Date/IsTest.php b/tests/TestCase/Date/IsTest.php index abe00902..07a4d35a 100644 --- a/tests/TestCase/Date/IsTest.php +++ b/tests/TestCase/Date/IsTest.php @@ -17,6 +17,7 @@ use Cake\Chronos\Chronos; use Cake\Chronos\ChronosDate; use Cake\Chronos\Test\TestCase\TestCase; +use PHPUnit\Framework\Attributes\TestWith; class IsTest extends TestCase { @@ -364,4 +365,23 @@ public function testIsWithinNext() $this->assertTrue((new Chronos('+1 week'))->isWithinNext('7 day')); $this->assertTrue((new Chronos('+1 month'))->isWithinNext('1 month')); } + + #[TestWith([1, true, false])] + #[TestWith([2, true, false])] + #[TestWith([3, true, false])] + #[TestWith([4, true, false])] + #[TestWith([5, true, false])] + #[TestWith([6, true, false])] + #[TestWith([7, false, true])] + #[TestWith([8, false, true])] + #[TestWith([9, false, true])] + #[TestWith([10, false, true])] + #[TestWith([11, false, true])] + #[TestWith([12, false, true])] + public function testIsFirstOrSecondHalfOfYear(int $month, bool $isFirstHalfOfYear, bool $isSecondHalfOfYear): void + { + $d = Chronos::createFromDate(2023, $month, 1); + $this->assertSame($isFirstHalfOfYear, $d->isFirstHalfOfYear()); + $this->assertSame($isSecondHalfOfYear, $d->isSecondHalfOfYear()); + } } diff --git a/tests/TestCase/DateTime/GettersTest.php b/tests/TestCase/DateTime/GettersTest.php index 419bcd0c..cf07a6a2 100644 --- a/tests/TestCase/DateTime/GettersTest.php +++ b/tests/TestCase/DateTime/GettersTest.php @@ -18,6 +18,7 @@ use Cake\Chronos\Chronos; use Cake\Chronos\Test\TestCase\TestCase; use InvalidArgumentException; +use PHPUnit\Framework\Attributes\TestWith; class GettersTest extends TestCase { @@ -162,6 +163,24 @@ public function testGetQuarterFirstLast() $this->assertSame(4, $d->quarter); } + #[TestWith([1, 1])] + #[TestWith([2, 1])] + #[TestWith([3, 1])] + #[TestWith([4, 1])] + #[TestWith([5, 1])] + #[TestWith([6, 1])] + #[TestWith([7, 2])] + #[TestWith([8, 2])] + #[TestWith([9, 2])] + #[TestWith([10, 2])] + #[TestWith([11, 2])] + #[TestWith([12, 2])] + public function testHalfOfYear(int $month, int $expectedHalfOfYear): void + { + $d = Chronos::createFromDate(2012, $month, 1); + $this->assertSame($expectedHalfOfYear, $d->halfOfYear); + } + public function testGetLocalTrue() { // Default timezone has been set to America/Toronto in TestCase.php diff --git a/tests/TestCase/DateTime/IsTest.php b/tests/TestCase/DateTime/IsTest.php index 4ce8ef0d..9b430ec7 100644 --- a/tests/TestCase/DateTime/IsTest.php +++ b/tests/TestCase/DateTime/IsTest.php @@ -16,7 +16,9 @@ namespace Cake\Chronos\Test\TestCase\DateTime; use Cake\Chronos\Chronos; +use Cake\Chronos\ChronosDate; use Cake\Chronos\Test\TestCase\TestCase; +use PHPUnit\Framework\Attributes\TestWith; class IsTest extends TestCase { @@ -440,4 +442,23 @@ public function testIsWithinNext() $this->assertTrue((new Chronos('+1 second'))->isWithinNext('1 minute')); $this->assertTrue((new Chronos('+1 month'))->isWithinNext('1 month')); } + + #[TestWith([1, true, false])] + #[TestWith([2, true, false])] + #[TestWith([3, true, false])] + #[TestWith([4, true, false])] + #[TestWith([5, true, false])] + #[TestWith([6, true, false])] + #[TestWith([7, false, true])] + #[TestWith([8, false, true])] + #[TestWith([9, false, true])] + #[TestWith([10, false, true])] + #[TestWith([11, false, true])] + #[TestWith([12, false, true])] + public function testIsFirstOrSecondHalfOfYear(int $month, bool $isFirstHalfOfYear, bool $isSecondHalfOfYear): void + { + $d = ChronosDate::create(2023, $month, 1); + $this->assertSame($isFirstHalfOfYear, $d->isFirstHalfOfYear()); + $this->assertSame($isSecondHalfOfYear, $d->isSecondHalfOfYear()); + } } diff --git a/tests/TestCase/DateTime/IssetTest.php b/tests/TestCase/DateTime/IssetTest.php index 3d8c6d54..3d16c533 100644 --- a/tests/TestCase/DateTime/IssetTest.php +++ b/tests/TestCase/DateTime/IssetTest.php @@ -47,6 +47,7 @@ public function testIssetReturnTrueForProperties() 'timezoneName', 'tz', 'tzName', + 'halfOfYear', ]; foreach ($properties as $property) { From 67e4d2d71c1e426ea2fb6fadf31c6743484398e5 Mon Sep 17 00:00:00 2001 From: breno Date: Mon, 18 Sep 2023 16:37:23 -0400 Subject: [PATCH 2/5] Revert "add halfOfYear, isFirstHalfOfYear, IsSecondHalfOfYear; improve getters typehint" This reverts commit a73f7a3f2e36caa62d1a06207b59c6d5b713857d. --- src/Chronos.php | 50 +++++++------------------ src/ChronosDate.php | 40 ++++---------------- tests/TestCase/Date/GettersTest.php | 41 -------------------- tests/TestCase/Date/IsTest.php | 20 ---------- tests/TestCase/DateTime/GettersTest.php | 19 ---------- tests/TestCase/DateTime/IsTest.php | 21 ----------- tests/TestCase/DateTime/IssetTest.php | 1 - 7 files changed, 21 insertions(+), 171 deletions(-) delete mode 100644 tests/TestCase/Date/GettersTest.php diff --git a/src/Chronos.php b/src/Chronos.php index 267f96bf..969802b7 100644 --- a/src/Chronos.php +++ b/src/Chronos.php @@ -29,24 +29,23 @@ * * @property-read int $year * @property-read int $yearIso - * @property-read int<1, 12> $month - * @property-read int<1, 31> $day - * @property-read int<0, 23> $hour - * @property-read int<0, 59> $minute - * @property-read int<0, 59> $second - * @property-read int<0, 999999> $micro - * @property-read int<0, 999999> $microsecond + * @property-read int $month + * @property-read int $day + * @property-read int $hour + * @property-read int $minute + * @property-read int $second + * @property-read int $micro + * @property-read int $microsecond * @property-read int $timestamp seconds since the Unix Epoch * @property-read \DateTimeZone $timezone the current timezone * @property-read \DateTimeZone $tz alias of timezone - * @property-read int<1, 7> $dayOfWeek 1 (for Monday) through 7 (for Sunday) - * @property-read int<0, 365> $dayOfYear 0 through 365 - * @property-read int<1, 5> $weekOfMonth 1 through 5 - * @property-read int<1, 53> $weekOfYear ISO-8601 week number of year, weeks starting on Monday - * @property-read int<1, 31> $daysInMonth number of days in the given month + * @property-read int $dayOfWeek 1 (for Monday) through 7 (for Sunday) + * @property-read int $dayOfYear 0 through 365 + * @property-read int $weekOfMonth 1 through 5 + * @property-read int $weekOfYear ISO-8601 week number of year, weeks starting on Monday + * @property-read int $daysInMonth number of days in the given month * @property-read int $age does a diffInYears() with default parameters - * @property-read int<1, 4> $quarter the quarter of this instance, 1 - 4 - * @property-read int<1, 2> $halfOfYear the half of year of this instance, 1 - 2 + * @property-read int $quarter the quarter of this instance, 1 - 4 * @property-read int $offset the timezone offset in seconds from UTC * @property-read int $offsetHours the timezone offset in hours from UTC * @property-read bool $dst daylight savings time indicator, true if DST, false otherwise @@ -2056,26 +2055,6 @@ public function isLastYear(): bool return $this->year === static::now($this->tz)->subYears(1)->year; } - /** - * Determines if the instance is within the first half of year - * - * @return bool - */ - public function isFirstHalfOfYear(): bool - { - return $this->halfOfYear === 1; - } - - /** - * Determines if the instance is within the second half of year - * - * @return bool - */ - public function isSecondHalfOfYear(): bool - { - return $this->halfOfYear === 2; - } - /** * Determines if the instance is in the future, ie. greater (after) than now * @@ -2613,9 +2592,6 @@ public function __get(string $name): string|float|int|bool|DateTimeZone case $name === 'quarter': return (int)ceil($this->month / 3); - case $name === 'halfOfYear': - return $this->month <= 6 ? 1 : 2; - case $name === 'offset': return $this->getOffset(); diff --git a/src/ChronosDate.php b/src/ChronosDate.php index b9a194dd..941b5158 100644 --- a/src/ChronosDate.php +++ b/src/ChronosDate.php @@ -30,16 +30,15 @@ * * @property-read int $year * @property-read int $yearIso - * @property-read int<1, 12> $month - * @property-read int<1, 31> $day - * @property-read int<1, 7> $dayOfWeek 1 (for Monday) through 7 (for Sunday) - * @property-read int<0, 365> $dayOfYear 0 through 365 - * @property-read int<1, 5> $weekOfMonth 1 through 5 - * @property-read int<1, 53> $weekOfYear ISO-8601 week number of year, weeks starting on Monday - * @property-read int<1, 31> $daysInMonth number of days in the given month + * @property-read int $month + * @property-read int $day + * @property-read int $dayOfWeek 1 (for Monday) through 7 (for Sunday) + * @property-read int $dayOfYear 0 through 365 + * @property-read int $weekOfMonth 1 through 5 + * @property-read int $weekOfYear ISO-8601 week number of year, weeks starting on Monday + * @property-read int $daysInMonth number of days in the given month * @property-read int $age does a diffInYears() with default parameters - * @property-read int<1, 4> $quarter the quarter of this instance, 1 - 4 - * @property-read int<1, 2> $halfOfYear the half of year of this instance, 1 - 2 + * @property-read int $quarter the quarter of this instance, 1 - 4 * @psalm-immutable * @psalm-consistent-constructor */ @@ -1226,26 +1225,6 @@ public function isLastYear(DateTimeZone|string|null $timezone = null): bool return $this->year === static::now($timezone)->subYears(1)->year; } - /** - * Determines if the instance is within the first half of year - * - * @return bool - */ - public function isFirstHalfOfYear(): bool - { - return $this->halfOfYear === 1; - } - - /** - * Determines if the instance is within the second half of year - * - * @return bool - */ - public function isSecondHalfOfYear(): bool - { - return $this->halfOfYear === 2; - } - /** * Determines if the instance is in the future, ie. greater (after) than now * @@ -1587,9 +1566,6 @@ public function __get(string $name): string|float|int|bool case $name === 'quarter': return (int)ceil($this->month / 3); - case $name === 'halfOfYear': - return $this->month <= 6 ? 1 : 2; - default: throw new InvalidArgumentException(sprintf("Unknown getter '%s'", $name)); } diff --git a/tests/TestCase/Date/GettersTest.php b/tests/TestCase/Date/GettersTest.php deleted file mode 100644 index 62521d3d..00000000 --- a/tests/TestCase/Date/GettersTest.php +++ /dev/null @@ -1,41 +0,0 @@ - - * @link https://cakephp.org CakePHP(tm) Project - * @license https://www.opensource.org/licenses/mit-license.php MIT License - */ - -namespace Cake\Chronos\Test\TestCase\Date; - -use Cake\Chronos\ChronosDate; -use Cake\Chronos\Test\TestCase\TestCase; -use PHPUnit\Framework\Attributes\TestWith; - -class GettersTest extends TestCase -{ - #[TestWith([1, 1])] - #[TestWith([2, 1])] - #[TestWith([3, 1])] - #[TestWith([4, 1])] - #[TestWith([5, 1])] - #[TestWith([6, 1])] - #[TestWith([7, 2])] - #[TestWith([8, 2])] - #[TestWith([9, 2])] - #[TestWith([10, 2])] - #[TestWith([11, 2])] - #[TestWith([12, 2])] - public function testHalfOfYear(int $month, int $expectedHalfOfYear): void - { - $d = ChronosDate::create(year: 2012, month: $month, day: 1); - $this->assertSame($expectedHalfOfYear, $d->halfOfYear); - } -} diff --git a/tests/TestCase/Date/IsTest.php b/tests/TestCase/Date/IsTest.php index 07a4d35a..abe00902 100644 --- a/tests/TestCase/Date/IsTest.php +++ b/tests/TestCase/Date/IsTest.php @@ -17,7 +17,6 @@ use Cake\Chronos\Chronos; use Cake\Chronos\ChronosDate; use Cake\Chronos\Test\TestCase\TestCase; -use PHPUnit\Framework\Attributes\TestWith; class IsTest extends TestCase { @@ -365,23 +364,4 @@ public function testIsWithinNext() $this->assertTrue((new Chronos('+1 week'))->isWithinNext('7 day')); $this->assertTrue((new Chronos('+1 month'))->isWithinNext('1 month')); } - - #[TestWith([1, true, false])] - #[TestWith([2, true, false])] - #[TestWith([3, true, false])] - #[TestWith([4, true, false])] - #[TestWith([5, true, false])] - #[TestWith([6, true, false])] - #[TestWith([7, false, true])] - #[TestWith([8, false, true])] - #[TestWith([9, false, true])] - #[TestWith([10, false, true])] - #[TestWith([11, false, true])] - #[TestWith([12, false, true])] - public function testIsFirstOrSecondHalfOfYear(int $month, bool $isFirstHalfOfYear, bool $isSecondHalfOfYear): void - { - $d = Chronos::createFromDate(2023, $month, 1); - $this->assertSame($isFirstHalfOfYear, $d->isFirstHalfOfYear()); - $this->assertSame($isSecondHalfOfYear, $d->isSecondHalfOfYear()); - } } diff --git a/tests/TestCase/DateTime/GettersTest.php b/tests/TestCase/DateTime/GettersTest.php index cf07a6a2..419bcd0c 100644 --- a/tests/TestCase/DateTime/GettersTest.php +++ b/tests/TestCase/DateTime/GettersTest.php @@ -18,7 +18,6 @@ use Cake\Chronos\Chronos; use Cake\Chronos\Test\TestCase\TestCase; use InvalidArgumentException; -use PHPUnit\Framework\Attributes\TestWith; class GettersTest extends TestCase { @@ -163,24 +162,6 @@ public function testGetQuarterFirstLast() $this->assertSame(4, $d->quarter); } - #[TestWith([1, 1])] - #[TestWith([2, 1])] - #[TestWith([3, 1])] - #[TestWith([4, 1])] - #[TestWith([5, 1])] - #[TestWith([6, 1])] - #[TestWith([7, 2])] - #[TestWith([8, 2])] - #[TestWith([9, 2])] - #[TestWith([10, 2])] - #[TestWith([11, 2])] - #[TestWith([12, 2])] - public function testHalfOfYear(int $month, int $expectedHalfOfYear): void - { - $d = Chronos::createFromDate(2012, $month, 1); - $this->assertSame($expectedHalfOfYear, $d->halfOfYear); - } - public function testGetLocalTrue() { // Default timezone has been set to America/Toronto in TestCase.php diff --git a/tests/TestCase/DateTime/IsTest.php b/tests/TestCase/DateTime/IsTest.php index 9b430ec7..4ce8ef0d 100644 --- a/tests/TestCase/DateTime/IsTest.php +++ b/tests/TestCase/DateTime/IsTest.php @@ -16,9 +16,7 @@ namespace Cake\Chronos\Test\TestCase\DateTime; use Cake\Chronos\Chronos; -use Cake\Chronos\ChronosDate; use Cake\Chronos\Test\TestCase\TestCase; -use PHPUnit\Framework\Attributes\TestWith; class IsTest extends TestCase { @@ -442,23 +440,4 @@ public function testIsWithinNext() $this->assertTrue((new Chronos('+1 second'))->isWithinNext('1 minute')); $this->assertTrue((new Chronos('+1 month'))->isWithinNext('1 month')); } - - #[TestWith([1, true, false])] - #[TestWith([2, true, false])] - #[TestWith([3, true, false])] - #[TestWith([4, true, false])] - #[TestWith([5, true, false])] - #[TestWith([6, true, false])] - #[TestWith([7, false, true])] - #[TestWith([8, false, true])] - #[TestWith([9, false, true])] - #[TestWith([10, false, true])] - #[TestWith([11, false, true])] - #[TestWith([12, false, true])] - public function testIsFirstOrSecondHalfOfYear(int $month, bool $isFirstHalfOfYear, bool $isSecondHalfOfYear): void - { - $d = ChronosDate::create(2023, $month, 1); - $this->assertSame($isFirstHalfOfYear, $d->isFirstHalfOfYear()); - $this->assertSame($isSecondHalfOfYear, $d->isSecondHalfOfYear()); - } } diff --git a/tests/TestCase/DateTime/IssetTest.php b/tests/TestCase/DateTime/IssetTest.php index 3d16c533..3d8c6d54 100644 --- a/tests/TestCase/DateTime/IssetTest.php +++ b/tests/TestCase/DateTime/IssetTest.php @@ -47,7 +47,6 @@ public function testIssetReturnTrueForProperties() 'timezoneName', 'tz', 'tzName', - 'halfOfYear', ]; foreach ($properties as $property) { From 3a877f8ad03ff6c2f6de99524cfe2f6ffcbe92d6 Mon Sep 17 00:00:00 2001 From: breno Date: Mon, 18 Sep 2023 16:42:17 -0400 Subject: [PATCH 3/5] fix typo --- src/Chronos.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Chronos.php b/src/Chronos.php index 969802b7..b77db146 100644 --- a/src/Chronos.php +++ b/src/Chronos.php @@ -1898,12 +1898,12 @@ public function closest(Chronos $first, Chronos $second): Chronos * Get the farthest date from the instance. * * @param \Cake\Chronos\Chronos $first The instance to compare with. - * @param \Cake\Chronos\Chronos $seocnd The instance to compare with. + * @param \Cake\Chronos\Chronos $second The instance to compare with. * @return self */ - public function farthest(Chronos $first, Chronos $seocnd): Chronos + public function farthest(Chronos $first, Chronos $second): Chronos { - return $this->diffInSeconds($first) > $this->diffInSeconds($seocnd) ? $first : $seocnd; + return $this->diffInSeconds($first) > $this->diffInSeconds($second) ? $first : $second; } /** From 1621ea90a7bbea84a0d5268a78a6f1b429e56b59 Mon Sep 17 00:00:00 2001 From: breno Date: Mon, 18 Sep 2023 16:59:31 -0400 Subject: [PATCH 4/5] add third param $others to farthest and closest methods --- src/Chronos.php | 30 +++++++++++++++++++--- src/ChronosDate.php | 30 +++++++++++++++++++--- tests/TestCase/Date/ComparisonTest.php | 23 +++++++++++++++++ tests/TestCase/DateTime/ComparisonTest.php | 22 ++++++++++++++++ 4 files changed, 97 insertions(+), 8 deletions(-) diff --git a/src/Chronos.php b/src/Chronos.php index b77db146..36f74e02 100644 --- a/src/Chronos.php +++ b/src/Chronos.php @@ -1887,11 +1887,22 @@ public function between(Chronos $start, Chronos $end, bool $equals = true): bool * * @param \Cake\Chronos\Chronos $first The instance to compare with. * @param \Cake\Chronos\Chronos $second The instance to compare with. + * @param \Cake\Chronos\Chronos ...$others Others instances to compare with. * @return self */ - public function closest(Chronos $first, Chronos $second): Chronos + public function closest(Chronos $first, Chronos $second, Chronos ...$others): Chronos { - return $this->diffInSeconds($first) < $this->diffInSeconds($second) ? $first : $second; + $closest = $first; + $closestDiffInSeconds = $this->diffInSeconds($first); + foreach ([$second, ...$others] as $other) { + $otherDiffInSeconds = $this->diffInSeconds($other); + if ($otherDiffInSeconds < $closestDiffInSeconds) { + $closest = $other; + $closestDiffInSeconds = $otherDiffInSeconds; + } + } + + return $closest; } /** @@ -1899,11 +1910,22 @@ public function closest(Chronos $first, Chronos $second): Chronos * * @param \Cake\Chronos\Chronos $first The instance to compare with. * @param \Cake\Chronos\Chronos $second The instance to compare with. + * @param \Cake\Chronos\Chronos ...$others Others instances to compare with. * @return self */ - public function farthest(Chronos $first, Chronos $second): Chronos + public function farthest(Chronos $first, Chronos $second, Chronos ...$others): Chronos { - return $this->diffInSeconds($first) > $this->diffInSeconds($second) ? $first : $second; + $farthest = $first; + $farthestDiffInSeconds = $this->diffInSeconds($first); + foreach ([$second, ...$others] as $other) { + $otherDiffInSeconds = $this->diffInSeconds($other); + if ($otherDiffInSeconds > $farthestDiffInSeconds) { + $farthest = $other; + $farthestDiffInSeconds = $otherDiffInSeconds; + } + } + + return $farthest; } /** diff --git a/src/ChronosDate.php b/src/ChronosDate.php index 941b5158..a49681f9 100644 --- a/src/ChronosDate.php +++ b/src/ChronosDate.php @@ -1087,11 +1087,22 @@ public function between(ChronosDate $start, ChronosDate $end, bool $equals = tru * * @param \Cake\Chronos\ChronosDate $first The instance to compare with. * @param \Cake\Chronos\ChronosDate $second The instance to compare with. + * @param \Cake\Chronos\ChronosDate ...$others Others instance to compare with. * @return self */ - public function closest(ChronosDate $first, ChronosDate $second): ChronosDate + public function closest(ChronosDate $first, ChronosDate $second, ChronosDate ...$others): ChronosDate { - return $this->diffInDays($first) < $this->diffInDays($second) ? $first : $second; + $closest = $first; + $closestDiffInDays = $this->diffInDays($first); + foreach ([$second, ...$others] as $other) { + $otherDiffInDays = $this->diffInDays($other); + if ($otherDiffInDays < $closestDiffInDays) { + $closest = $other; + $closestDiffInDays = $otherDiffInDays; + } + } + + return $closest; } /** @@ -1099,11 +1110,22 @@ public function closest(ChronosDate $first, ChronosDate $second): ChronosDate * * @param \Cake\Chronos\ChronosDate $first The instance to compare with. * @param \Cake\Chronos\ChronosDate $second The instance to compare with. + * @param \Cake\Chronos\ChronosDate ...$others Others instance to compare with. * @return self */ - public function farthest(ChronosDate $first, ChronosDate $second): ChronosDate + public function farthest(ChronosDate $first, ChronosDate $second, ChronosDate ...$others): ChronosDate { - return $this->diffInDays($first) > $this->diffInDays($second) ? $first : $second; + $farthest = $first; + $farthestDiffInDays = $this->diffInDays($first); + foreach ([$second, ...$others] as $other) { + $otherDiffInDays = $this->diffInDays($other); + if ($otherDiffInDays > $farthestDiffInDays) { + $farthest = $other; + $farthestDiffInDays = $otherDiffInDays; + } + } + + return $farthest; } /** diff --git a/tests/TestCase/Date/ComparisonTest.php b/tests/TestCase/Date/ComparisonTest.php index a0956153..2d6ca583 100644 --- a/tests/TestCase/Date/ComparisonTest.php +++ b/tests/TestCase/Date/ComparisonTest.php @@ -180,6 +180,17 @@ public function testClosestWithEquals() $this->assertSame($dt1, $closest); } + public function testClosestWithOthers(): void + { + $instance = ChronosDate::create(2015, 5, 10); + $dt1 = ChronosDate::create(2015, 5, 4); + $dt2 = ChronosDate::create(2015, 5, 20); + $dt3 = ChronosDate::create(2015, 5, 21); + $dt4 = ChronosDate::create(2015, 5, 22); + $closest = $instance->closest($dt1, $dt2, $dt3, $dt4); + $this->assertSame($dt1, $closest); + } + public function testFarthest() { $instance = ChronosDate::create(2015, 5, 10); @@ -197,4 +208,16 @@ public function testFarthestWithEquals() $Farthest = $instance->farthest($dt1, $dt2); $this->assertSame($dt2, $Farthest); } + + public function testFarthestWithOthers(): void + { + $instance = ChronosDate::create(2015, 5, 10); + $dt1 = ChronosDate::create(2015, 5, 4); + $dt2 = ChronosDate::create(2015, 5, 20); + $dt3 = ChronosDate::create(2015, 5, 21); + $dt4 = ChronosDate::create(2015, 5, 22); + $dt5 = ChronosDate::create(2015, 5, 23); + $Farthest = $instance->farthest($dt1, $dt2, $dt3, $dt4, $dt5); + $this->assertSame($dt5, $Farthest); + } } diff --git a/tests/TestCase/DateTime/ComparisonTest.php b/tests/TestCase/DateTime/ComparisonTest.php index f7ff51be..cd5d508b 100644 --- a/tests/TestCase/DateTime/ComparisonTest.php +++ b/tests/TestCase/DateTime/ComparisonTest.php @@ -296,6 +296,17 @@ public function testClosestWithEquals() $this->assertSame($dt1, $closest); } + public function testClosestWithOthers(): void + { + $instance = Chronos::create(2015, 5, 28, 12, 0, 0); + $dt1 = Chronos::create(2015, 5, 28, 11, 0, 0); + $dt2 = Chronos::create(2015, 5, 28, 14, 0, 0); + $dt3 = Chronos::create(2015, 5, 28, 15, 0, 0); + $dt4 = Chronos::create(2015, 5, 28, 16, 0, 0); + $closest = $instance->closest($dt1, $dt2, $dt3, $dt4); + $this->assertSame($dt1, $closest); + } + public function testFarthest() { $instance = Chronos::create(2015, 5, 28, 12, 0, 0); @@ -313,4 +324,15 @@ public function testFarthestWithEquals() $Farthest = $instance->farthest($dt1, $dt2); $this->assertSame($dt2, $Farthest); } + + public function testFarthestWithOthers(): void + { + $instance = Chronos::create(2015, 5, 28, 12, 0, 0); + $dt1 = Chronos::create(2015, 5, 28, 11, 0, 0); + $dt2 = Chronos::create(2015, 5, 28, 14, 0, 0); + $dt3 = Chronos::create(2015, 5, 28, 15, 0, 0); + $dt4 = Chronos::create(2015, 5, 28, 16, 0, 0); + $Farthest = $instance->farthest($dt1, $dt2, $dt3, $dt4); + $this->assertSame($dt4, $Farthest); + } } From a55d22ae01aafc6fff9c0c53c011833e079f6b59 Mon Sep 17 00:00:00 2001 From: breno Date: Mon, 18 Sep 2023 17:19:06 -0400 Subject: [PATCH 5/5] test closest methods --- tests/TestCase/Date/ComparisonTest.php | 2 +- tests/TestCase/DateTime/ComparisonTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/TestCase/Date/ComparisonTest.php b/tests/TestCase/Date/ComparisonTest.php index 2d6ca583..1613d456 100644 --- a/tests/TestCase/Date/ComparisonTest.php +++ b/tests/TestCase/Date/ComparisonTest.php @@ -187,7 +187,7 @@ public function testClosestWithOthers(): void $dt2 = ChronosDate::create(2015, 5, 20); $dt3 = ChronosDate::create(2015, 5, 21); $dt4 = ChronosDate::create(2015, 5, 22); - $closest = $instance->closest($dt1, $dt2, $dt3, $dt4); + $closest = $instance->closest($dt4, $dt3, $dt1, $dt2); $this->assertSame($dt1, $closest); } diff --git a/tests/TestCase/DateTime/ComparisonTest.php b/tests/TestCase/DateTime/ComparisonTest.php index cd5d508b..eeef1c53 100644 --- a/tests/TestCase/DateTime/ComparisonTest.php +++ b/tests/TestCase/DateTime/ComparisonTest.php @@ -303,7 +303,7 @@ public function testClosestWithOthers(): void $dt2 = Chronos::create(2015, 5, 28, 14, 0, 0); $dt3 = Chronos::create(2015, 5, 28, 15, 0, 0); $dt4 = Chronos::create(2015, 5, 28, 16, 0, 0); - $closest = $instance->closest($dt1, $dt2, $dt3, $dt4); + $closest = $instance->closest($dt4, $dt3, $dt1, $dt2); $this->assertSame($dt1, $closest); }