Skip to content

Commit 4147257

Browse files
committed
Merge remote-tracking branch 'derickr/bug80047-take2' into PHP-8.0
2 parents 8ed21a8 + 001e7db commit 4147257

File tree

4 files changed

+55
-56
lines changed

4 files changed

+55
-56
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.23
44

5+
- Date:
6+
. Fixed bug #80047 (DatePeriod doesn't warn with custom DateTimeImmutable).
7+
(Derick)
8+
59
- DBA:
610
. Fixed LMDB driver memory leak on DB creation failure (Girgias)
711
. Fixed bug GH-9155 (dba_open("non-existing", "c-", "flatfile") segfaults).

ext/date/php_date.c

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,17 @@ static int date_period_it_has_more(zend_object_iterator *iter)
14751475
}
14761476
/* }}} */
14771477

1478+
static zend_class_entry *get_base_date_class(zend_class_entry *start_ce)
1479+
{
1480+
zend_class_entry *tmp = start_ce;
1481+
1482+
while (tmp != date_ce_date && tmp != date_ce_immutable && tmp->parent) {
1483+
tmp = tmp->parent;
1484+
}
1485+
1486+
return tmp;
1487+
}
1488+
14781489
/* {{{ date_period_it_current_data */
14791490
static zval *date_period_it_current_data(zend_object_iterator *iter)
14801491
{
@@ -1484,7 +1495,7 @@ static zval *date_period_it_current_data(zend_object_iterator *iter)
14841495
php_date_obj *newdateobj;
14851496

14861497
/* Create new object */
1487-
php_date_instantiate(object->start_ce, &iterator->current);
1498+
php_date_instantiate(get_base_date_class(object->start_ce), &iterator->current);
14881499
newdateobj = Z_PHPDATE_P(&iterator->current);
14891500
newdateobj->time = timelib_time_ctor();
14901501
*newdateobj->time = *it_time;
@@ -4227,20 +4238,6 @@ PHP_METHOD(DatePeriod, __construct)
42274238
}
42284239
dpobj->start_ce = date_ce_date;
42294240
} 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-
42444241
/* init */
42454242
php_interval_obj *intobj = Z_PHPINTERVAL_P(interval);
42464243

ext/date/tests/bug80042.phpt

Lines changed: 0 additions & 41 deletions
This file was deleted.

ext/date/tests/bug80047.phpt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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-25');
13+
$cdti = new CustomDateTimeImmutable('2022-06-25');
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+
$dp = new DatePeriod(...$test);
28+
foreach ($dp as $date) {}
29+
echo get_class($date), "\n";
30+
}
31+
?>
32+
--EXPECT--
33+
DateTime
34+
DateTime
35+
DateTime
36+
DateTimeImmutable
37+
DateTimeImmutable
38+
DateTimeImmutable
39+
DateTimeImmutable

0 commit comments

Comments
 (0)