From 5f9c61b010d5d7696230fc7e9a59c01cf74fbe7f Mon Sep 17 00:00:00 2001 From: DanielEScherzer Date: Sat, 7 Sep 2024 09:12:56 -0700 Subject: [PATCH] ext/date: reduce duplication in __wakeup() methods 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 aa7899e5e315a..1215dda878b8a 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -3051,8 +3051,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; @@ -3065,29 +3066,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(DateTimeImmutable, __wakeup) +PHP_METHOD(DateTime, __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); + php_do_date_time_wakeup(INTERNAL_FUNCTION_PARAM_PASSTHRU, "DateTime"); +} +/* }}} */ - if (!php_date_initialize_from_hash(&dateobj, myht)) { - zend_throw_error(NULL, "Invalid serialization data for DateTimeImmutable object"); - RETURN_THROWS(); - } +/* {{{ */ +PHP_METHOD(DateTimeImmutable, __wakeup) +{ + php_do_date_time_wakeup(INTERNAL_FUNCTION_PARAM_PASSTHRU, "DateTimeImmutable"); } /* }}} */