Skip to content

Commit 46a5da4

Browse files
committed
Fix GH-11310: __debugInfo() does nothing on classes extending DateTime
Makes the `__debugInfo` method work in classes that extend `DateTime` and `DateTimeImmutable`. * Implemented `__debugInfo` for `DateTime` and `DateTimeImmutable`. * Modified the `get_properties_for` handlers of `DateTime` and `DateTimeImmutable` to invoke `__debugInfo`.
1 parent bd185c3 commit 46a5da4

File tree

4 files changed

+127
-3
lines changed

4 files changed

+127
-3
lines changed

ext/date/php_date.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,6 +1959,13 @@ static HashTable *date_object_get_properties_for(zend_object *object, zend_prop_
19591959

19601960
switch (purpose) {
19611961
case ZEND_PROP_PURPOSE_DEBUG:
1962+
assert(object->ce->__debugInfo != NULL);
1963+
int is_temp = 0;
1964+
HashTable *ht = zend_std_get_debug_info(object, &is_temp);
1965+
if (ht && !is_temp) {
1966+
GC_TRY_ADDREF(ht);
1967+
}
1968+
return ht;
19621969
case ZEND_PROP_PURPOSE_SERIALIZE:
19631970
case ZEND_PROP_PURPOSE_VAR_EXPORT:
19641971
case ZEND_PROP_PURPOSE_JSON:
@@ -2845,6 +2852,50 @@ PHP_METHOD(DateTimeImmutable, __serialize)
28452852
}
28462853
/* }}} */
28472854

2855+
/* {{{ */
2856+
PHP_METHOD(DateTime, __debugInfo)
2857+
{
2858+
zval *object = ZEND_THIS;
2859+
php_date_obj *dateobj;
2860+
HashTable *myht;
2861+
2862+
ZEND_PARSE_PARAMETERS_NONE();
2863+
2864+
dateobj = Z_PHPDATE_P(object);
2865+
2866+
array_init(return_value);
2867+
myht = Z_ARRVAL_P(return_value);
2868+
2869+
add_common_properties(myht, &dateobj->std);
2870+
2871+
if (dateobj->time) {
2872+
date_object_to_hash(dateobj, myht);
2873+
}
2874+
}
2875+
/* }}} */
2876+
2877+
/* {{{ */
2878+
PHP_METHOD(DateTimeImmutable, __debugInfo)
2879+
{
2880+
zval *object = ZEND_THIS;
2881+
php_date_obj *dateobj;
2882+
HashTable *myht;
2883+
2884+
ZEND_PARSE_PARAMETERS_NONE();
2885+
2886+
dateobj = Z_PHPDATE_P(object);
2887+
2888+
array_init(return_value);
2889+
myht = Z_ARRVAL_P(return_value);
2890+
2891+
add_common_properties(myht, &dateobj->std);
2892+
2893+
if (dateobj->time) {
2894+
date_object_to_hash(dateobj, myht);
2895+
}
2896+
}
2897+
/* }}} */
2898+
28482899
static bool date_time_is_internal_property(zend_string *name)
28492900
{
28502901
if (

ext/date/php_date.stub.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ public function __serialize(): array {}
344344

345345
public function __unserialize(array $data): void {}
346346

347+
/** @tentative-return-type */
348+
public function __debugInfo(): array {}
349+
347350
/** @tentative-return-type */
348351
public function __wakeup(): void {}
349352

@@ -454,6 +457,9 @@ public function __serialize(): array {}
454457

455458
public function __unserialize(array $data): void {}
456459

460+
/** @tentative-return-type */
461+
public function __debugInfo(): array {}
462+
457463
/** @tentative-return-type */
458464
public function __wakeup(): void {}
459465

ext/date/php_date_arginfo.h

Lines changed: 11 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/date/tests/date_debugInfo.phpt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
--TEST--
2+
Bug GH-11310: __debugInfo() does nothing on classes extending DateTime
3+
--INI--
4+
date.timezone=UTC
5+
--FILE--
6+
<?php
7+
8+
class MyDateTime extends DateTime {
9+
public function __debugInfo(): array {
10+
return ['child' => '42', 'parent' => count(parent::__debugInfo())];
11+
}
12+
}
13+
14+
class MyDateTimeImmutable extends DateTimeImmutable {
15+
public function __debugInfo(): array {
16+
return ['child' => '42', 'parent' => count(parent::__debugInfo())];
17+
}
18+
}
19+
20+
class MyDateTimeZone extends DateTimeZone {
21+
public function __debugInfo(): array {
22+
return ['child' => '42', 'parent' => count(parent::__debugInfo())];
23+
}
24+
}
25+
26+
var_dump(new DateTime('2023-10-26 21:23:05'));
27+
var_dump(new DateTimeImmutable('2023-10-26 21:23:05'));
28+
var_dump(new MyDateTime('2023-10-26 21:23:05'));
29+
var_dump(new MyDateTimeImmutable('2023-10-26 21:23:05'));
30+
?>
31+
--EXPECT--
32+
object(DateTime)#1 (3) {
33+
["date"]=>
34+
string(26) "2023-10-26 21:23:05.000000"
35+
["timezone_type"]=>
36+
int(3)
37+
["timezone"]=>
38+
string(3) "UTC"
39+
}
40+
object(DateTimeImmutable)#1 (3) {
41+
["date"]=>
42+
string(26) "2023-10-26 21:23:05.000000"
43+
["timezone_type"]=>
44+
int(3)
45+
["timezone"]=>
46+
string(3) "UTC"
47+
}
48+
object(MyDateTime)#1 (2) {
49+
["child"]=>
50+
string(2) "42"
51+
["parent"]=>
52+
int(3)
53+
}
54+
object(MyDateTimeImmutable)#1 (2) {
55+
["child"]=>
56+
string(2) "42"
57+
["parent"]=>
58+
int(3)
59+
}

0 commit comments

Comments
 (0)