ext/intl: Use ZPP specifier for IntlTimeZone::createEnumeration() (#19393)

The existing code is extremely convoluted, incorrectly documented, and does not follow the usual semantics
This commit is contained in:
Gina Peter Banyard 2025-08-07 22:37:16 +01:00 committed by GitHub
parent 140fc69b4a
commit fe88711b14
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 34 additions and 54 deletions

View file

@ -571,8 +571,7 @@ function intltz_count_equivalent_ids(string $timezoneId): int|false {}
function intltz_create_default(): IntlTimeZone {}
/** @param IntlTimeZone|string|int|float|null $countryOrRawOffset */
function intltz_create_enumeration($countryOrRawOffset = null): IntlIterator|false {}
function intltz_create_enumeration(string|int|null $countryOrRawOffset = null): IntlIterator|false {}
function intltz_create_time_zone(string $timezoneId): ?IntlTimeZone {}

View file

@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 0d5b028a1ab8f35e8ee1b51ce3141b6ef782af28 */
* Stub hash: bd78e0b6aec5c52b13f83e3c3cc7770c942d7c20 */
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_intlcal_create_instance, 0, 0, IntlCalendar, 1)
ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, timezone, "null")
@ -679,7 +679,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_intltz_create_default, 0, 0, Intl
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_intltz_create_enumeration, 0, 0, IntlIterator, MAY_BE_FALSE)
ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, countryOrRawOffset, "null")
ZEND_ARG_TYPE_MASK(0, countryOrRawOffset, MAY_BE_STRING|MAY_BE_LONG|MAY_BE_NULL, "null")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_intltz_create_time_zone, 0, 1, IntlTimeZone, 1)

View file

@ -4,10 +4,19 @@ IntlTimeZone::createEnumeration(): errors
intl
--FILE--
<?php
ini_set("intl.error_level", E_WARNING);
var_dump(IntlTimeZone::createEnumeration(array()));
try {
var_dump(IntlTimeZone::createEnumeration([]));
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
try {
var_dump(IntlTimeZone::createEnumeration(new stdClass()));
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
?>
--EXPECTF--
Warning: IntlTimeZone::createEnumeration(): invalid argument type in %s on line %d
bool(false)
--EXPECT--
TypeError: IntlTimeZone::createEnumeration(): Argument #1 ($countryOrRawOffset) must be of type string|int|null, array given
TypeError: IntlTimeZone::createEnumeration(): Argument #1 ($countryOrRawOffset) must be of type string|int|null, stdClass given

View file

@ -45,11 +45,10 @@ class IntlTimeZone
public static function createDefault(): IntlTimeZone {}
/**
* @param IntlTimeZone|string|int|float|null $countryOrRawOffset
* @tentative-return-type
* @alias intltz_create_enumeration
*/
public static function createEnumeration($countryOrRawOffset = null): IntlIterator|false {}
public static function createEnumeration(string|int|null $countryOrRawOffset = null): IntlIterator|false {}
/**
* @tentative-return-type

View file

@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 6ffeea8491aa48c49879fa77bdb644d10d5c71bd */
* Stub hash: 22e652c6a05ade0a6fd3119e4742cd260ba27146 */
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_IntlTimeZone___construct, 0, 0, 0)
ZEND_END_ARG_INFO()
@ -12,7 +12,7 @@ ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_OBJ_INFO_EX(arginfo_class_IntlTimeZone_crea
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_IntlTimeZone_createEnumeration, 0, 0, IntlIterator, MAY_BE_FALSE)
ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, countryOrRawOffset, "null")
ZEND_ARG_TYPE_MASK(0, countryOrRawOffset, MAY_BE_STRING|MAY_BE_LONG|MAY_BE_NULL, "null")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_OBJ_INFO_EX(arginfo_class_IntlTimeZone_createTimeZone, 0, 1, IntlTimeZone, 1)

View file

@ -125,62 +125,35 @@ U_CFUNC PHP_FUNCTION(intltz_get_unknown)
U_CFUNC PHP_FUNCTION(intltz_create_enumeration)
{
zval *arg = NULL;
StringEnumeration *se = NULL;
intl_error_reset(NULL);
zend_string *timezone = nullptr;
zend_long timezone_shift = 0;
bool is_null = true;
StringEnumeration *se = nullptr;
intl_error_reset(nullptr);
/* double indirection to have the zend engine destroy the new zval that
* results from separation */
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_ZVAL(arg)
Z_PARAM_STR_OR_LONG_OR_NULL(timezone, timezone_shift, is_null)
ZEND_PARSE_PARAMETERS_END();
if (arg == NULL || Z_TYPE_P(arg) == IS_NULL) {
if (is_null) {
se = TimeZone::createEnumeration();
} else if (Z_TYPE_P(arg) == IS_LONG) {
int_offset:
if (UNEXPECTED(Z_LVAL_P(arg) < (zend_long)INT32_MIN ||
Z_LVAL_P(arg) > (zend_long)INT32_MAX)) {
intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
"value is out of range");
RETURN_FALSE;
} else {
se = TimeZone::createEnumeration((int32_t) Z_LVAL_P(arg));
}
} else if (Z_TYPE_P(arg) == IS_DOUBLE) {
double_offset:
convert_to_long(arg);
goto int_offset;
} else if (Z_TYPE_P(arg) == IS_OBJECT || Z_TYPE_P(arg) == IS_STRING) {
zend_long lval;
double dval;
if (!try_convert_to_string(arg)) {
} else if (timezone != nullptr) {
se = TimeZone::createEnumeration(ZSTR_VAL(timezone));
} else {
if (UNEXPECTED(ZEND_LONG_EXCEEDS_INT(timezone_shift))) {
zend_argument_value_error(1, "must be between %d and %d", INT32_MIN, INT32_MAX);
RETURN_THROWS();
}
switch (is_numeric_string(Z_STRVAL_P(arg), Z_STRLEN_P(arg), &lval, &dval, 0)) {
case IS_DOUBLE:
zval_ptr_dtor(arg);
ZVAL_DOUBLE(arg, dval);
goto double_offset;
case IS_LONG:
zval_ptr_dtor(arg);
ZVAL_LONG(arg, lval);
goto int_offset;
}
/* else call string version */
se = TimeZone::createEnumeration(Z_STRVAL_P(arg));
} else {
// TODO Should be a TypeError
intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
"invalid argument type");
RETURN_FALSE;
se = TimeZone::createEnumeration(static_cast<int32_t>(timezone_shift));
}
if (se) {
IntlIterator_from_StringEnumeration(se, return_value);
} else {
intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
intl_error_set(nullptr, U_ILLEGAL_ARGUMENT_ERROR,
"error obtaining enumeration");
RETVAL_FALSE;
}