Skip to content

Commit 973c3f6

Browse files
committed
Fixed #80047: DatePeriod doesn't warn with custom DateTimeImmutable
1 parent 13f55d5 commit 973c3f6

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? 2022, PHP 8.0.22
44

5+
- Date:
6+
. Fixed bug #80047 (DatePeriod doesn't warn with custom DateTimeImmutable).
7+
(Derick)
8+
59
- GD:
610
. Fixed bug GH-8848 (imagecopyresized() error refers to the wrong argument).
711
(cmb)

ext/date/php_date.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4227,6 +4227,20 @@ PHP_METHOD(DatePeriod, __construct)
42274227
}
42284228
dpobj->start_ce = date_ce_date;
42294229
} else {
4230+
/* Sanity checks */
4231+
if (start && Z_OBJCE_P(start) != date_ce_date && Z_OBJCE_P(start) != date_ce_immutable) {
4232+
zend_string *func = get_active_function_or_method_name();
4233+
zend_throw_error(zend_ce_exception, "%s(): Class of start date must be exactly DateTime or DateTimeImmutable, object of class %s provided", ZSTR_VAL(func), ZSTR_VAL(Z_OBJCE_P(start)->name));
4234+
zend_string_release(func);
4235+
RETURN_THROWS();
4236+
}
4237+
if (end && Z_OBJCE_P(end) != date_ce_date && Z_OBJCE_P(end) != date_ce_immutable) {
4238+
zend_string *func = get_active_function_or_method_name();
4239+
zend_throw_error(zend_ce_exception, "%s(): Class of end date must be exactly DateTime or DateTimeImmutable, object of class %s provided", ZSTR_VAL(func), ZSTR_VAL(Z_OBJCE_P(end)->name));
4240+
zend_string_release(func);
4241+
RETURN_THROWS();
4242+
}
4243+
42304244
/* init */
42314245
php_interval_obj *intobj = Z_PHPINTERVAL_P(interval);
42324246

ext/date/tests/bug80042.phpt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
Bug #80047: DatePeriod doesn't support custom DateTimeImmutable
3+
--INI--
4+
date.timezone=UTC
5+
--FILE--
6+
<?php
7+
class CustomDateTime extends DateTime {}
8+
class CustomDateTimeImmutable extends DateTimeImmutable {}
9+
10+
$dt = new DateTime('2022-06-24');
11+
$dti = new DateTimeImmutable('2022-06-24');
12+
$cdt = new CustomDateTime('2022-06-24');
13+
$cdti = new CustomDateTimeImmutable('2022-06-24');
14+
$i = new DateInterval('P1D');
15+
16+
$tests = [
17+
[ $dt, $i, $cdt ],
18+
[ $cdt, $i, $dt ],
19+
[ $cdt, $i, $cdt ],
20+
[ $dti, $i, $cdti ],
21+
[ $cdti, $i, $dti ],
22+
[ $cdti, $i, $cdti ],
23+
[ $cdt, $i, $cdti ],
24+
];
25+
26+
foreach ($tests as $test) {
27+
try {
28+
$dp = new DatePeriod(...$test);
29+
} catch ( Exception $e ) {
30+
echo $e->getMessage(), "\n";
31+
}
32+
}
33+
?>
34+
--EXPECT--
35+
DatePeriod::__construct(): Class of end date must be exactly DateTime or DateTimeImmutable, object of class CustomDateTime provided
36+
DatePeriod::__construct(): Class of start date must be exactly DateTime or DateTimeImmutable, object of class CustomDateTime provided
37+
DatePeriod::__construct(): Class of start date must be exactly DateTime or DateTimeImmutable, object of class CustomDateTime provided
38+
DatePeriod::__construct(): Class of end date must be exactly DateTime or DateTimeImmutable, object of class CustomDateTimeImmutable provided
39+
DatePeriod::__construct(): Class of start date must be exactly DateTime or DateTimeImmutable, object of class CustomDateTimeImmutable provided
40+
DatePeriod::__construct(): Class of start date must be exactly DateTime or DateTimeImmutable, object of class CustomDateTimeImmutable provided
41+
DatePeriod::__construct(): Class of start date must be exactly DateTime or DateTimeImmutable, object of class CustomDateTime provided

0 commit comments

Comments
 (0)