Skip to content

Fix GH-11310: __debugInfo() does nothing on classes extending DateTime #12534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions ext/date/php_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -1959,6 +1959,13 @@ static HashTable *date_object_get_properties_for(zend_object *object, zend_prop_

switch (purpose) {
case ZEND_PROP_PURPOSE_DEBUG:
ZEND_ASSERT(object->ce->__debugInfo != NULL);
int is_temp = 0;
HashTable *ht = zend_std_get_debug_info(object, &is_temp);
if (ht && !is_temp) {
GC_TRY_ADDREF(ht);
}
return ht;
case ZEND_PROP_PURPOSE_SERIALIZE:
case ZEND_PROP_PURPOSE_VAR_EXPORT:
case ZEND_PROP_PURPOSE_JSON:
Expand Down Expand Up @@ -2845,6 +2852,50 @@ PHP_METHOD(DateTimeImmutable, __serialize)
}
/* }}} */

/* {{{ */
PHP_METHOD(DateTime, __debugInfo)
{
zval *object = ZEND_THIS;
php_date_obj *dateobj;
HashTable *myht;

ZEND_PARSE_PARAMETERS_NONE();

dateobj = Z_PHPDATE_P(object);

array_init(return_value);
myht = Z_ARRVAL_P(return_value);

add_common_properties(myht, &dateobj->std);

if (dateobj->time) {
date_object_to_hash(dateobj, myht);
}
}
/* }}} */

/* {{{ */
PHP_METHOD(DateTimeImmutable, __debugInfo)
{
zval *object = ZEND_THIS;
php_date_obj *dateobj;
HashTable *myht;

ZEND_PARSE_PARAMETERS_NONE();

dateobj = Z_PHPDATE_P(object);

array_init(return_value);
myht = Z_ARRVAL_P(return_value);

add_common_properties(myht, &dateobj->std);

if (dateobj->time) {
date_object_to_hash(dateobj, myht);
}
}
/* }}} */

static bool date_time_is_internal_property(zend_string *name)
{
if (
Expand Down
6 changes: 6 additions & 0 deletions ext/date/php_date.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,9 @@ public function __serialize(): array {}

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

/** @tentative-return-type */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this have @tentative-return-type?

public function __debugInfo(): array {}

/** @tentative-return-type */
public function __wakeup(): void {}

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

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

/** @tentative-return-type */
public function __debugInfo(): array {}

/** @tentative-return-type */
public function __wakeup(): void {}

Expand Down
14 changes: 11 additions & 3 deletions ext/date/php_date_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions ext/date/tests/date_debugInfo.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
--TEST--
Bug GH-11310: __debugInfo() does nothing on classes extending DateTime
--INI--
date.timezone=UTC
--FILE--
<?php

class MyDateTime extends DateTime {
public function __debugInfo(): array {
return ['child' => '42', 'parent' => count(parent::__debugInfo())];
}
}

class MyDateTimeImmutable extends DateTimeImmutable {
public function __debugInfo(): array {
return ['child' => '42', 'parent' => count(parent::__debugInfo())];
}
}

class MyDateTimeZone extends DateTimeZone {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest adding a test case for MyDateTimeZone, otherwise why define the class at all?

public function __debugInfo(): array {
return ['child' => '42', 'parent' => count(parent::__debugInfo())];
}
}

var_dump(new DateTime('2023-10-26 21:23:05'));
var_dump(new DateTimeImmutable('2023-10-26 21:23:05'));
var_dump(new MyDateTime('2023-10-26 21:23:05'));
var_dump(new MyDateTimeImmutable('2023-10-26 21:23:05'));
?>
--EXPECT--
object(DateTime)#1 (3) {
["date"]=>
string(26) "2023-10-26 21:23:05.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
object(DateTimeImmutable)#1 (3) {
["date"]=>
string(26) "2023-10-26 21:23:05.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
object(MyDateTime)#1 (2) {
["child"]=>
string(2) "42"
["parent"]=>
int(3)
}
object(MyDateTimeImmutable)#1 (2) {
["child"]=>
string(2) "42"
["parent"]=>
int(3)
}