From 3f168ffe4ba07a028c7ca734d359bd25ef5b8c0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CLamentXU123=E2=80=9D?= <108666168+LamentXU123@users.noreply.github.com> Date: Sun, 10 May 2026 21:24:12 +0800 Subject: [PATCH] ext/intl: Throw ValueError for invalid break iterator locale type --- NEWS | 2 ++ UPGRADING | 3 +++ .../breakiterator/breakiterator_methods.cpp | 6 ++--- ext/intl/tests/breakiter_getLocale_error.phpt | 25 +++++++++++++++++++ 4 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 ext/intl/tests/breakiter_getLocale_error.phpt diff --git a/NEWS b/NEWS index 4f8fe29d53e7..3f1738f763b3 100644 --- a/NEWS +++ b/NEWS @@ -64,6 +64,8 @@ PHP NEWS . Added grapheme_strrev (Yuya Hamada) . Passing a non-stringable object as a time zone to Intl time zone argument handling now raises TypeError instead of Error. (Weilin Du) + . IntlBreakIterator::getLocale() now raises ValueError for invalid locale + types. (Weilin Du) - JSON: . Enriched JSON last error / exception message with error location. diff --git a/UPGRADING b/UPGRADING index f4853af485fe..6d2cb40cbe6f 100644 --- a/UPGRADING +++ b/UPGRADING @@ -37,6 +37,9 @@ PHP 8.6 UPGRADE NOTES - Intl: . Passing a non-stringable object as a time zone to Intl APIs that accept time zone objects or strings now raises a TypeError instead of an Error. + . IntlBreakIterator::getLocale() now raises a ValueError when the type is + neither Locale::ACTUAL_LOCALE nor Locale::VALID_LOCALE instead of + returning false. - PCNTL: . pcntl_alarm() now raises a ValueError if the seconds argument is diff --git a/ext/intl/breakiterator/breakiterator_methods.cpp b/ext/intl/breakiterator/breakiterator_methods.cpp index 972cd28cf38e..66a35e65ac6a 100644 --- a/ext/intl/breakiterator/breakiterator_methods.cpp +++ b/ext/intl/breakiterator/breakiterator_methods.cpp @@ -297,11 +297,9 @@ U_CFUNC PHP_METHOD(IntlBreakIterator, getLocale) Z_PARAM_LONG(locale_type) ZEND_PARSE_PARAMETERS_END(); - /* TODO: Change to ValueError? */ if (locale_type != ULOC_ACTUAL_LOCALE && locale_type != ULOC_VALID_LOCALE) { - intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, - "invalid locale type"); - RETURN_FALSE; + zend_argument_value_error(1, "must be either Locale::ACTUAL_LOCALE or Locale::VALID_LOCALE"); + RETURN_THROWS(); } BREAKITER_METHOD_FETCH_OBJECT; diff --git a/ext/intl/tests/breakiter_getLocale_error.phpt b/ext/intl/tests/breakiter_getLocale_error.phpt new file mode 100644 index 000000000000..07d57dbc4265 --- /dev/null +++ b/ext/intl/tests/breakiter_getLocale_error.phpt @@ -0,0 +1,25 @@ +--TEST-- +IntlBreakIterator::getLocale(): bad arguments +--EXTENSIONS-- +intl +--FILE-- +getLocale(2); +} catch (Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; +} + +try { + $bi->getLocale(-1); +} catch (Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; +} + +?> +--EXPECT-- +ValueError: IntlBreakIterator::getLocale(): Argument #1 ($type) must be either Locale::ACTUAL_LOCALE or Locale::VALID_LOCALE +ValueError: IntlBreakIterator::getLocale(): Argument #1 ($type) must be either Locale::ACTUAL_LOCALE or Locale::VALID_LOCALE