From 496447a58bb6f2cf1b5027951413c8e1a7024a7b Mon Sep 17 00:00:00 2001 From: Mark Story Date: Fri, 6 Oct 2023 23:04:30 -0400 Subject: [PATCH 1/2] Restore Date::create() compatibility with no params Restore the historical behavior of create() that uses the current day by default. Fixes #434 --- src/ChronosDate.php | 7 ++++++- tests/TestCase/Date/ConstructTest.php | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/ChronosDate.php b/src/ChronosDate.php index f22474dc..897b89cb 100644 --- a/src/ChronosDate.php +++ b/src/ChronosDate.php @@ -151,8 +151,13 @@ public function __debugInfo(): array * @param int $day The day to create an instance with. * @return static */ - public static function create(int $year, int $month, int $day) + public static function create(?int $year = null, ?int $month = null, ?int $day = null) { + $now = static::today(); + $year = $year ?? (int)$now->format('Y'); + $month = $month ?? $now->format('m'); + $day = $day ?? $now->format('d'); + $instance = static::createFromFormat( 'Y-m-d', sprintf('%s-%s-%s', 0, $month, $day) diff --git a/tests/TestCase/Date/ConstructTest.php b/tests/TestCase/Date/ConstructTest.php index 042b5b2a..fb817eb7 100644 --- a/tests/TestCase/Date/ConstructTest.php +++ b/tests/TestCase/Date/ConstructTest.php @@ -294,6 +294,26 @@ public function testConstructWithTestNowNoMutation($class) $class::setTestNow(null); } + /** + * @dataProvider dateClassProvider + */ + public function testCreateWithTestNowNoParams($class) + { + $class::setTestNow($class::create(2001, 1, 1)); + $date = $class::create(); + $this->assertDate($date, 2001, 1, 1); + } + + /** + * @dataProvider dateClassProvider + */ + public function testCreateWithSomeParams($class) + { + $now = $class::today(); + $date = $class::create(2022, 1); + $this->assertDate($date, 2022, 1, (int)$now->format('d')); + } + /** * @dataProvider dateClassProvider */ From 370caa8c7deb694a123e37c7398b4fc44db9479a Mon Sep 17 00:00:00 2001 From: Mark Story Date: Fri, 6 Oct 2023 23:28:48 -0400 Subject: [PATCH 2/2] Fix phpstan issues --- phpstan-baseline.neon | 17 +---------------- src/ChronosDate.php | 6 +++--- 2 files changed, 4 insertions(+), 19 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 509adb3c..71d6787d 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -21,22 +21,7 @@ parameters: path: src/Chronos.php - - message: "#^Parameter \\#1 \\$year of static method Cake\\\\Chronos\\\\ChronosDate\\:\\:create\\(\\) expects int, null given\\.$#" - count: 1 - path: src/ChronosDate.php - - - - message: "#^Parameter \\#2 \\$month of static method Cake\\\\Chronos\\\\ChronosDate\\:\\:create\\(\\) expects int, null given\\.$#" - count: 1 - path: src/ChronosDate.php - - - - message: "#^Parameter \\#3 \\$day of static method Cake\\\\Chronos\\\\ChronosDate\\:\\:create\\(\\) expects int, null given\\.$#" - count: 1 - path: src/ChronosDate.php - - - - message: "#^Static method Cake\\\\Chronos\\\\ChronosDate\\:\\:create\\(\\) invoked with 8 parameters, 3 required\\.$#" + message: "#^Static method Cake\\\\Chronos\\\\ChronosDate\\:\\:create\\(\\) invoked with 8 parameters, 0-3 required\\.$#" count: 2 path: src/ChronosDate.php diff --git a/src/ChronosDate.php b/src/ChronosDate.php index 897b89cb..41d168bf 100644 --- a/src/ChronosDate.php +++ b/src/ChronosDate.php @@ -146,9 +146,9 @@ public function __debugInfo(): array /** * Create an instance from a specific date. * - * @param int $year The year to create an instance with. - * @param int $month The month to create an instance with. - * @param int $day The day to create an instance with. + * @param ?int $year The year to create an instance with. + * @param ?int $month The month to create an instance with. + * @param ?int $day The day to create an instance with. * @return static */ public static function create(?int $year = null, ?int $month = null, ?int $day = null)