1
0
mirror of https://github.com/php/php-src.git synced 2026-04-12 02:23:18 +02:00

Fixed bug #80047 (DatePeriod doesn't warn with custom DateTimeImmutable)

This commit is contained in:
Derick Rethans
2022-07-28 10:28:10 +01:00
committed by Gabriel Caruso
parent 9230e02e8f
commit f3738d6900
2 changed files with 51 additions and 1 deletions

View File

@@ -1475,6 +1475,17 @@ static int date_period_it_has_more(zend_object_iterator *iter)
}
/* }}} */
static zend_class_entry *get_base_date_class(zend_class_entry *start_ce)
{
zend_class_entry *tmp = start_ce;
while (tmp != date_ce_date && tmp != date_ce_immutable && tmp->parent) {
tmp = tmp->parent;
}
return tmp;
}
/* {{{ date_period_it_current_data */
static zval *date_period_it_current_data(zend_object_iterator *iter)
{
@@ -1484,7 +1495,7 @@ static zval *date_period_it_current_data(zend_object_iterator *iter)
php_date_obj *newdateobj;
/* Create new object */
php_date_instantiate(object->start_ce, &iterator->current);
php_date_instantiate(get_base_date_class(object->start_ce), &iterator->current);
newdateobj = Z_PHPDATE_P(&iterator->current);
newdateobj->time = timelib_time_ctor();
*newdateobj->time = *it_time;

View File

@@ -0,0 +1,39 @@
--TEST--
Bug #80047: DatePeriod doesn't support custom DateTimeImmutable
--INI--
date.timezone=UTC
--FILE--
<?php
class CustomDateTime extends DateTime {}
class CustomDateTimeImmutable extends DateTimeImmutable {}
$dt = new DateTime('2022-06-24');
$dti = new DateTimeImmutable('2022-06-24');
$cdt = new CustomDateTime('2022-06-25');
$cdti = new CustomDateTimeImmutable('2022-06-25');
$i = new DateInterval('P1D');
$tests = [
[ $dt, $i, $cdt ],
[ $cdt, $i, $dt ],
[ $cdt, $i, $cdt ],
[ $dti, $i, $cdti ],
[ $cdti, $i, $dti ],
[ $cdti, $i, $cdti ],
[ $cdt, $i, $cdti ],
];
foreach ($tests as $test) {
$dp = new DatePeriod(...$test);
foreach ($dp as $date) {}
echo get_class($date), "\n";
}
?>
--EXPECT--
DateTime
DateTime
DateTime
DateTimeImmutable
DateTimeImmutable
DateTimeImmutable
DateTimeImmutable