Skip to content

Commit

Permalink
Merge pull request #435 from cakephp/date-create
Browse files Browse the repository at this point in the history
Restore Date::create() compatibility with no params
  • Loading branch information
LordSimal authored Oct 10, 2023
2 parents 1d3536e + 370caa8 commit 79b58b7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
17 changes: 1 addition & 16 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 9 additions & 4 deletions src/ChronosDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions tests/TestCase/Date/ConstructTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 79b58b7

Please sign in to comment.