Skip to content

Commit 79b58b7

Browse files
authored
Merge pull request #435 from cakephp/date-create
Restore Date::create() compatibility with no params
2 parents 1d3536e + 370caa8 commit 79b58b7

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

phpstan-baseline.neon

+1-16
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,7 @@ parameters:
2121
path: src/Chronos.php
2222

2323
-
24-
message: "#^Parameter \\#1 \\$year of static method Cake\\\\Chronos\\\\ChronosDate\\:\\:create\\(\\) expects int, null given\\.$#"
25-
count: 1
26-
path: src/ChronosDate.php
27-
28-
-
29-
message: "#^Parameter \\#2 \\$month of static method Cake\\\\Chronos\\\\ChronosDate\\:\\:create\\(\\) expects int, null given\\.$#"
30-
count: 1
31-
path: src/ChronosDate.php
32-
33-
-
34-
message: "#^Parameter \\#3 \\$day of static method Cake\\\\Chronos\\\\ChronosDate\\:\\:create\\(\\) expects int, null given\\.$#"
35-
count: 1
36-
path: src/ChronosDate.php
37-
38-
-
39-
message: "#^Static method Cake\\\\Chronos\\\\ChronosDate\\:\\:create\\(\\) invoked with 8 parameters, 3 required\\.$#"
24+
message: "#^Static method Cake\\\\Chronos\\\\ChronosDate\\:\\:create\\(\\) invoked with 8 parameters, 0-3 required\\.$#"
4025
count: 2
4126
path: src/ChronosDate.php
4227

src/ChronosDate.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,18 @@ public function __debugInfo(): array
146146
/**
147147
* Create an instance from a specific date.
148148
*
149-
* @param int $year The year to create an instance with.
150-
* @param int $month The month to create an instance with.
151-
* @param int $day The day to create an instance with.
149+
* @param ?int $year The year to create an instance with.
150+
* @param ?int $month The month to create an instance with.
151+
* @param ?int $day The day to create an instance with.
152152
* @return static
153153
*/
154-
public static function create(int $year, int $month, int $day)
154+
public static function create(?int $year = null, ?int $month = null, ?int $day = null)
155155
{
156+
$now = static::today();
157+
$year = $year ?? (int)$now->format('Y');
158+
$month = $month ?? $now->format('m');
159+
$day = $day ?? $now->format('d');
160+
156161
$instance = static::createFromFormat(
157162
'Y-m-d',
158163
sprintf('%s-%s-%s', 0, $month, $day)

tests/TestCase/Date/ConstructTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,26 @@ public function testConstructWithTestNowNoMutation($class)
294294
$class::setTestNow(null);
295295
}
296296

297+
/**
298+
* @dataProvider dateClassProvider
299+
*/
300+
public function testCreateWithTestNowNoParams($class)
301+
{
302+
$class::setTestNow($class::create(2001, 1, 1));
303+
$date = $class::create();
304+
$this->assertDate($date, 2001, 1, 1);
305+
}
306+
307+
/**
308+
* @dataProvider dateClassProvider
309+
*/
310+
public function testCreateWithSomeParams($class)
311+
{
312+
$now = $class::today();
313+
$date = $class::create(2022, 1);
314+
$this->assertDate($date, 2022, 1, (int)$now->format('d'));
315+
}
316+
297317
/**
298318
* @dataProvider dateClassProvider
299319
*/

0 commit comments

Comments
 (0)