diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 509adb3..71d6787 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 f22474d..41d168b 100644 --- a/src/ChronosDate.php +++ b/src/ChronosDate.php @@ -146,13 +146,18 @@ 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, 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 042b5b2..fb817eb 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 */