Skip to content

Commit b5c3c2d

Browse files
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.
1 parent b63db81 commit b5c3c2d

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

ext/date/php_date.c

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3021,8 +3021,9 @@ PHP_METHOD(DateTimeImmutable, __unserialize)
30213021
}
30223022
/* }}} */
30233023

3024-
/* {{{ */
3025-
PHP_METHOD(DateTime, __wakeup)
3024+
/* {{{
3025+
* Common implementation for DateTime::__wakeup() and DateTimeImmutable::__wakeup() */
3026+
static void php_do_date_time_wakeup(INTERNAL_FUNCTION_PARAMETERS, const char *class_name)
30263027
{
30273028
zval *object = ZEND_THIS;
30283029
php_date_obj *dateobj;
@@ -3035,29 +3036,23 @@ PHP_METHOD(DateTime, __wakeup)
30353036
myht = Z_OBJPROP_P(object);
30363037

30373038
if (!php_date_initialize_from_hash(&dateobj, myht)) {
3038-
zend_throw_error(NULL, "Invalid serialization data for DateTime object");
3039+
zend_throw_error(NULL, "Invalid serialization data for %s object", class_name);
30393040
RETURN_THROWS();
30403041
}
30413042
}
30423043
/* }}} */
30433044

30443045
/* {{{ */
3045-
PHP_METHOD(DateTimeImmutable, __wakeup)
3046+
PHP_METHOD(DateTime, __wakeup)
30463047
{
3047-
zval *object = ZEND_THIS;
3048-
php_date_obj *dateobj;
3049-
HashTable *myht;
3050-
3051-
ZEND_PARSE_PARAMETERS_NONE();
3052-
3053-
dateobj = Z_PHPDATE_P(object);
3054-
3055-
myht = Z_OBJPROP_P(object);
3048+
php_do_date_time_wakeup(INTERNAL_FUNCTION_PARAM_PASSTHRU, "DateTime");
3049+
}
3050+
/* }}} */
30563051

3057-
if (!php_date_initialize_from_hash(&dateobj, myht)) {
3058-
zend_throw_error(NULL, "Invalid serialization data for DateTimeImmutable object");
3059-
RETURN_THROWS();
3060-
}
3052+
/* {{{ */
3053+
PHP_METHOD(DateTimeImmutable, __wakeup)
3054+
{
3055+
php_do_date_time_wakeup(INTERNAL_FUNCTION_PARAM_PASSTHRU, "DateTimeImmutable");
30613056
}
30623057
/* }}} */
30633058

0 commit comments

Comments
 (0)