Skip to content

Commit f296aad

Browse files
committed
Promote warnings to exceptions in ext/intl
1 parent f4b2497 commit f296aad

12 files changed

+92
-88
lines changed

ext/intl/converter/converter.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -693,8 +693,8 @@ PHP_METHOD(UConverter, reasonText) {
693693
UCNV_REASON_CASE(CLOSE)
694694
UCNV_REASON_CASE(CLONE)
695695
default:
696-
php_error_docref(NULL, E_WARNING, "Unknown UConverterCallbackReason: " ZEND_LONG_FMT, reason);
697-
RETURN_FALSE;
696+
zend_argument_value_error(1, "must be a UConverter::REASON_* constant");
697+
RETURN_THROWS();
698698
}
699699
}
700700
/* }}} */

ext/intl/formatter/formatter_format.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,8 @@ PHP_FUNCTION( numfmt_format )
105105
break;
106106

107107
default:
108-
php_error_docref(NULL, E_WARNING, "Unsupported format type " ZEND_LONG_FMT, type);
109-
RETURN_FALSE;
110-
break;
108+
zend_argument_value_error(3, "must be a NumberFormatter::TYPE_* constant");
109+
RETURN_THROWS();
111110
}
112111

113112
INTL_METHOD_RETVAL_UTF8( nfo, formatted, formatted_len, ( formatted != format_buf ) );

ext/intl/formatter/formatter_parse.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ PHP_FUNCTION( numfmt_parse )
4444
FORMATTER_METHOD_INIT_VARS;
4545

4646
/* Parse parameters. */
47-
if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Os|lz!",
47+
if (zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Os|lz!",
4848
&object, NumberFormatter_ce_ptr, &str, &str_len, &type, &zposition ) == FAILURE )
4949
{
5050
RETURN_THROWS();
5151
}
5252

53-
if(zposition) {
53+
if (zposition) {
5454
position = (int32_t) zval_get_long(zposition);
5555
position_p = &position;
5656
}
@@ -86,17 +86,20 @@ PHP_FUNCTION( numfmt_parse )
8686
RETVAL_DOUBLE(val_double);
8787
break;
8888
default:
89-
php_error_docref(NULL, E_WARNING, "Unsupported format type " ZEND_LONG_FMT, type);
90-
RETVAL_FALSE;
91-
break;
89+
zend_argument_value_error(3, "must be a NumberFormatter::TYPE_* constant");
90+
goto cleanup;
91+
}
92+
93+
if (zposition) {
94+
ZEND_TRY_ASSIGN_REF_LONG(zposition, position);
9295
}
96+
97+
cleanup:
98+
9399
#if ICU_LOCALE_BUG && defined(LC_NUMERIC)
94100
setlocale(LC_NUMERIC, oldlocale);
95101
efree(oldlocale);
96102
#endif
97-
if(zposition) {
98-
ZEND_TRY_ASSIGN_REF_LONG(zposition, position);
99-
}
100103

101104
if (sstr) {
102105
efree(sstr);

ext/intl/grapheme/grapheme_string.c

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ PHP_FUNCTION(grapheme_strpos)
125125
/* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */
126126

127127
if (needle_len == 0) {
128-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Empty delimiter", 1 );
129-
RETURN_FALSE;
128+
zend_argument_value_error(2, "cannot be empty");
129+
RETURN_THROWS();
130130
}
131131

132132
if (offset >= 0) {
@@ -184,8 +184,8 @@ PHP_FUNCTION(grapheme_stripos)
184184
/* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */
185185

186186
if (needle_len == 0) {
187-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_stripos: Empty delimiter", 1 );
188-
RETURN_FALSE;
187+
zend_argument_value_error(2, "cannot be empty");
188+
RETURN_THROWS();
189189
}
190190

191191
is_ascii = ( grapheme_ascii_check((unsigned char*)haystack, haystack_len) >= 0 );
@@ -250,7 +250,7 @@ PHP_FUNCTION(grapheme_strrpos)
250250
/* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */
251251

252252
if (needle_len == 0) {
253-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Empty delimiter", 1 );
253+
zend_argument_value_error(2, "cannot be empty");
254254
RETURN_FALSE;
255255
}
256256

@@ -310,8 +310,8 @@ PHP_FUNCTION(grapheme_strripos)
310310
/* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */
311311

312312
if (needle_len == 0) {
313-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Empty delimiter", 1 );
314-
RETURN_FALSE;
313+
zend_argument_value_error(2, "cannot be empty");
314+
RETURN_THROWS();
315315
}
316316

317317
is_ascii = grapheme_ascii_check((unsigned char *)haystack, haystack_len) >= 0;
@@ -582,10 +582,8 @@ static void strstr_common_handler(INTERNAL_FUNCTION_PARAMETERS, int f_ignore_cas
582582
}
583583

584584
if (needle_len == 0) {
585-
586-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Empty delimiter", 1 );
587-
588-
RETURN_FALSE;
585+
zend_argument_value_error(2, "cannot be empty");
586+
RETURN_THROWS();
589587
}
590588

591589

@@ -783,20 +781,25 @@ PHP_FUNCTION(grapheme_extract)
783781
}
784782

785783
if ( extract_type < GRAPHEME_EXTRACT_TYPE_MIN || extract_type > GRAPHEME_EXTRACT_TYPE_MAX ) {
786-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
787-
"grapheme_extract: unknown extract type param", 0 );
788-
RETURN_FALSE;
784+
zend_argument_value_error(3, "must be either GRAPHEME_EXTR_COUNT, GRAPHEME_EXTR_MAXBYTES, or GRAPHEME_EXTR_MAXCHARS");
785+
RETURN_THROWS();
789786
}
790787

791788
if ( lstart > INT32_MAX || lstart < 0 || (size_t)lstart >= str_len ) {
792789
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_extract: start not contained in string", 0 );
793790
RETURN_FALSE;
794791
}
795792

796-
if ( size > INT32_MAX || size < 0) {
797-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_extract: size is invalid", 0 );
798-
RETURN_FALSE;
793+
if (size < 0) {
794+
zend_argument_value_error(2, "must be greater than or equal to 0");
795+
RETURN_THROWS();
799796
}
797+
798+
if ( size > INT32_MAX) {
799+
zend_argument_value_error(2, "is too large");
800+
RETURN_THROWS();
801+
}
802+
800803
if (size == 0) {
801804
RETURN_EMPTY_STRING();
802805
}

ext/intl/locale/locale_methods.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -882,10 +882,9 @@ PHP_FUNCTION(locale_compose)
882882
/* Not grandfathered */
883883
result = append_key_value(loc_name, hash_arr , LOC_LANG_TAG);
884884
if( result == LOC_NOT_FOUND ){
885-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
886-
"locale_compose: parameter array does not contain 'language' tag.", 0 );
885+
zend_argument_value_error(1, "must contain a \"%s\" key", LOC_LANG_TAG);
887886
smart_str_free(loc_name);
888-
RETURN_FALSE;
887+
RETURN_THROWS();
889888
}
890889
if( !handleAppendResult( result, loc_name)){
891890
RETURN_FALSE;
@@ -1348,7 +1347,7 @@ static zend_string* lookup_loc_range(const char* loc_range, HashTable* hash_arr,
13481347
/* convert the array to lowercase , also replace hyphens with the underscore and store it in cur_arr */
13491348
if(Z_TYPE_P(ele_value)!= IS_STRING) {
13501349
/* element value is not a string */
1351-
intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "lookup_loc_range: locale array element is not a string", 0);
1350+
zend_argument_type_error(2, "must only contain string values");
13521351
LOOKUP_CLEAN_RETURN(NULL);
13531352
}
13541353
cur_arr[cur_arr_len*2] = estrndup(Z_STRVAL_P(ele_value), Z_STRLEN_P(ele_value));

ext/intl/normalizer/normalizer_normalize.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,8 @@ PHP_FUNCTION( normalizer_normalize )
121121
#endif
122122
break;
123123
default:
124-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
125-
"normalizer_normalize: illegal normalization form", 0 );
126-
RETURN_FALSE;
124+
zend_argument_value_error(2, "must be a a valid normalization form");
125+
RETURN_THROWS();
127126
}
128127

129128
/*
@@ -248,9 +247,8 @@ PHP_FUNCTION( normalizer_is_normalized )
248247
#endif
249248
break;
250249
default:
251-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
252-
"normalizer_normalize: illegal normalization form", 0 );
253-
RETURN_FALSE;
250+
zend_argument_value_error(2, "must be a a valid normalization form");
251+
RETURN_THROWS();
254252
}
255253

256254

ext/intl/resourcebundle/resourcebundle_class.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,7 @@ static int resourcebundle_ctor(INTERNAL_FUNCTION_PARAMETERS)
105105
}
106106

107107
if (bundlename_len >= MAXPATHLEN) {
108-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "Bundle name too long", 0 );
109-
zval_ptr_dtor(return_value);
110-
ZVAL_NULL(return_value);
108+
zend_argument_value_error(2, "is too long");
111109
return FAILURE;
112110
}
113111

@@ -296,8 +294,8 @@ PHP_FUNCTION( resourcebundle_locales )
296294
}
297295

298296
if (bundlename_len >= MAXPATHLEN) {
299-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "resourcebundle_locales: bundle name too long", 0 );
300-
RETURN_FALSE;
297+
zend_argument_value_error(1, "is too long");
298+
RETURN_THROWS();
301299
}
302300

303301
if(bundlename_len == 0) {

ext/intl/tests/bug62083.phpt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ if (!extension_loaded('intl'))
77
--FILE--
88
<?php
99
$arr1 = array();
10-
var_dump(grapheme_extract(-1, -1, -1,-1, $arr1));
10+
try {
11+
grapheme_extract(-1, -1, -1,-1, $arr1);
12+
} catch (ValueError $exception) {
13+
echo $exception->getMessage() . "\n";
14+
}
1115
?>
1216
--EXPECT--
13-
bool(false)
17+
grapheme_extract(): Argument #3 ($extract_type) must be either GRAPHEME_EXTR_COUNT, GRAPHEME_EXTR_MAXBYTES, or GRAPHEME_EXTR_MAXCHARS

ext/intl/tests/locale_compose_locale.phpt

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,17 @@ function ut_main()
113113
$res_str .= $valKey ."->".$valValue." " ;
114114
}
115115
*/
116-
117-
$locale = ut_loc_locale_compose( $value);
118-
$res_str .= "\n\nComposed Locale: ";
119-
if( $locale){
120-
$res_str .= "$locale";
121-
}else{
122-
$res_str .= "No values found from Locale compose due to the following error:\n";
123-
$res_str .= intl_get_error_message() ;
116+
try {
117+
$locale = ut_loc_locale_compose( $value);
118+
$res_str .= "\n\nComposed Locale: ";
119+
if( $locale){
120+
$res_str .= "$locale";
121+
}else{
122+
$res_str .= "No values found from Locale compose due to the following error:\n";
123+
$res_str .= intl_get_error_message() ;
124+
}
125+
} catch (ValueError $exception) {
126+
echo $exception->getMessage() . "\n";
124127
}
125128
}
126129

@@ -135,6 +138,9 @@ ut_run();
135138

136139
?>
137140
--EXPECT--
141+
Locale::composeLocale(): Argument #1 ($subtags) must contain a "language" key
142+
locale_compose(): Argument #1 ($subtags) must contain a "language" key
143+
138144
------------
139145
Input Array name is : loc1
140146

@@ -169,9 +175,6 @@ Input Array name is : loc8
169175
Composed Locale: en_lng_ing_Hans_CN_nedis_rozaj_x_prv1_prv2
170176
------------
171177
Input Array name is : loc9
172-
173-
Composed Locale: No values found from Locale compose due to the following error:
174-
locale_compose: parameter array does not contain 'language' tag.: U_ILLEGAL_ARGUMENT_ERROR
175178
------------
176179
Input Array name is : loc10
177180

ext/intl/tests/transliterator_transliterate_error.phpt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ $tr = Transliterator::create("latin");
1111

1212
//Arguments
1313
var_dump(transliterator_transliterate($tr,"str",7));
14-
var_dump(transliterator_transliterate($tr,"str",7,6));
14+
15+
try {
16+
transliterator_transliterate($tr,"str",7,6);
17+
} catch (ValueError $exception) {
18+
echo $exception->getMessage() . "\n";
19+
}
1520

1621
//bad UTF-8
1722
transliterator_transliterate($tr, "\x80\x03");
@@ -21,9 +26,7 @@ echo "Done.\n";
2126
--EXPECTF--
2227
Warning: transliterator_transliterate(): transliterator_transliterate: Neither "start" nor the "end" arguments can exceed the number of UTF-16 code units (in this case, 3) in %s on line %d
2328
bool(false)
24-
25-
Warning: transliterator_transliterate(): transliterator_transliterate: "start" argument should be non-negative and not bigger than "end" (if defined) in %s on line %d
26-
bool(false)
29+
transliterator_transliterate(): Argument #2 ($subject) must be less than or equal to argument #3 ($end)
2730

2831
Warning: transliterator_transliterate(): String conversion of string to UTF-16 failed in %s on line %d
2932
Done.

ext/intl/transliterator/transliterator_class.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,7 @@ static zend_object *Transliterator_clone_obj( zend_object *object )
179179
else
180180
{
181181
/* We shouldn't have unconstructed objects in the first place */
182-
php_error_docref( NULL, E_WARNING,
183-
"Cloning unconstructed transliterator." );
182+
zend_throw_error(NULL, "Unconstructed Transliterator object cannot be cloned");
184183
}
185184

186185
return ret_val;
@@ -215,7 +214,7 @@ static zval *Transliterator_read_property( zend_object *object, zend_string *nam
215214
( zend_binary_strcmp( "id", sizeof( "id" ) - 1,
216215
ZSTR_VAL( name ), ZSTR_LEN( name ) ) == 0 ) )
217216
{
218-
php_error_docref(NULL, E_WARNING, "The property \"id\" is read-only" );
217+
zend_throw_error(NULL, "Transliterator::$id is read-only");
219218
retval = &EG( uninitialized_zval );
220219
}
221220
else
@@ -243,7 +242,7 @@ static zval *Transliterator_write_property( zend_object *object, zend_string *na
243242
( zend_binary_strcmp( "id", sizeof( "id" ) - 1,
244243
ZSTR_VAL( name ), ZSTR_LEN( name ) ) == 0 ) )
245244
{
246-
php_error_docref(NULL, E_WARNING, "The property \"id\" is read-only" );
245+
zend_throw_error(NULL, "Transliterator::$id is read-only");
247246
}
248247
else
249248
{

0 commit comments

Comments
 (0)