ext/date: reduce duplication in __wakeup() methods (#15791)

The only difference between `DateTime::__wakeup()` and
`DateTimeImmutable::__wakeup()` is which class name is included in the error
message if the initialization from the data fails. Factor out a helper method
that is given the class name, and use it for both methods.
This commit is contained in:
DanielEScherzer 2024-12-06 10:23:32 -08:00 committed by GitHub
parent b63db81086
commit b5c3c2d1d5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -3021,8 +3021,9 @@ PHP_METHOD(DateTimeImmutable, __unserialize)
} }
/* }}} */ /* }}} */
/* {{{ */ /* {{{
PHP_METHOD(DateTime, __wakeup) * Common implementation for DateTime::__wakeup() and DateTimeImmutable::__wakeup() */
static void php_do_date_time_wakeup(INTERNAL_FUNCTION_PARAMETERS, const char *class_name)
{ {
zval *object = ZEND_THIS; zval *object = ZEND_THIS;
php_date_obj *dateobj; php_date_obj *dateobj;
@ -3035,29 +3036,23 @@ PHP_METHOD(DateTime, __wakeup)
myht = Z_OBJPROP_P(object); myht = Z_OBJPROP_P(object);
if (!php_date_initialize_from_hash(&dateobj, myht)) { if (!php_date_initialize_from_hash(&dateobj, myht)) {
zend_throw_error(NULL, "Invalid serialization data for DateTime object"); zend_throw_error(NULL, "Invalid serialization data for %s object", class_name);
RETURN_THROWS(); RETURN_THROWS();
} }
} }
/* }}} */ /* }}} */
/* {{{ */
PHP_METHOD(DateTime, __wakeup)
{
php_do_date_time_wakeup(INTERNAL_FUNCTION_PARAM_PASSTHRU, "DateTime");
}
/* }}} */
/* {{{ */ /* {{{ */
PHP_METHOD(DateTimeImmutable, __wakeup) PHP_METHOD(DateTimeImmutable, __wakeup)
{ {
zval *object = ZEND_THIS; php_do_date_time_wakeup(INTERNAL_FUNCTION_PARAM_PASSTHRU, "DateTimeImmutable");
php_date_obj *dateobj;
HashTable *myht;
ZEND_PARSE_PARAMETERS_NONE();
dateobj = Z_PHPDATE_P(object);
myht = Z_OBJPROP_P(object);
if (!php_date_initialize_from_hash(&dateobj, myht)) {
zend_throw_error(NULL, "Invalid serialization data for DateTimeImmutable object");
RETURN_THROWS();
}
} }
/* }}} */ /* }}} */