From b5c3c2d1d5f16792e716e8ab1dbbc1069bc08417 Mon Sep 17 00:00:00 2001 From: DanielEScherzer Date: Fri, 6 Dec 2024 10:23:32 -0800 Subject: [PATCH] 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. --- ext/date/php_date.c | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/ext/date/php_date.c b/ext/date/php_date.c index f1bfbfbf3f1..1ca4d26cf5b 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -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; php_date_obj *dateobj; @@ -3035,29 +3036,23 @@ PHP_METHOD(DateTime, __wakeup) myht = Z_OBJPROP_P(object); 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(); } } /* }}} */ +/* {{{ */ +PHP_METHOD(DateTime, __wakeup) +{ + php_do_date_time_wakeup(INTERNAL_FUNCTION_PARAM_PASSTHRU, "DateTime"); +} +/* }}} */ + /* {{{ */ PHP_METHOD(DateTimeImmutable, __wakeup) { - zval *object = ZEND_THIS; - 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(); - } + php_do_date_time_wakeup(INTERNAL_FUNCTION_PARAM_PASSTHRU, "DateTimeImmutable"); } /* }}} */