Skip to content

Commit 5887890

Browse files
committed
ext/intl: Throw TypeError for non-stringable timezone objects
1 parent 630555f commit 5887890

4 files changed

Lines changed: 14 additions & 2 deletions

File tree

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ PHP NEWS
6262
. Fixed bug GH-20426 (Spoofchecker::setRestrictionLevel() error message
6363
suggests missing constants). (DanielEScherzer)
6464
. Added grapheme_strrev (Yuya Hamada)
65+
. Passing a non-stringable object as a time zone to Intl time zone
66+
argument handling now raises TypeError instead of Error. (Weilin Du)
6567

6668
- JSON:
6769
. Enriched JSON last error / exception message with error location.

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ PHP 8.6 UPGRADE NOTES
3434
array arguments types/values and raise a TypeError/ValueError
3535
accordingly.
3636

37+
- Intl:
38+
. Passing a non-stringable object as a time zone to Intl APIs that accept
39+
time zone objects or strings now raises a TypeError instead of an Error.
40+
3741
- PCNTL:
3842
. pcntl_alarm() now raises a ValueError if the seconds argument is
3943
lower than zero or greater than platform's UINT_MAX.

ext/intl/tests/dateformat_setTimeZone_error.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ try {
1616
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
1717
}
1818

19+
try {
20+
$df->setTimeZone(new stdClass());
21+
} catch (Throwable $e) {
22+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
23+
}
24+
1925
try {
2026
$df->setTimeZone('non existing timezone');
2127
} catch (Throwable $e) {
@@ -24,4 +30,5 @@ try {
2430
?>
2531
--EXPECT--
2632
TypeError: IntlDateFormatter::setTimeZone(): Argument #1 ($timezone) must be of type object|string|null, array given
33+
TypeError: Object of class stdClass could not be converted to string
2734
IntlException: IntlDateFormatter::setTimeZone(): No such time zone: "non existing timezone"

ext/intl/timezone/timezone_class.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@ U_CFUNC TimeZone *timezone_process_timezone_argument(
160160
free_string = true;
161161
} else {
162162
if (!EG(exception)) {
163-
// TODO Proper type error
164-
zend_throw_error(nullptr, "Object of class %s could not be converted to string", ZSTR_VAL(timezone_object->ce->name));
163+
zend_type_error("Object of class %s could not be converted to string", ZSTR_VAL(timezone_object->ce->name));
165164
}
166165
return nullptr;
167166
}

0 commit comments

Comments
 (0)