From 91badc209fb1aa9b0745e305aab3b3db59dbd76d Mon Sep 17 00:00:00 2001 From: George Peter Banyard Date: Tue, 30 Aug 2022 17:16:27 +0100 Subject: [PATCH] Fix GH-9421 Incorrect argument number for ValueError in NumberFormatter --- ext/intl/formatter/formatter_format.c | 13 +++- ext/intl/formatter/formatter_parse.c | 12 +++- .../formatter_format_and_parse_errors.phpt | 65 +++++++++++++++++++ 3 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 ext/intl/tests/formatter_format_and_parse_errors.phpt diff --git a/ext/intl/formatter/formatter_format.c b/ext/intl/formatter/formatter_format.c index aa9c5915adeb8..7c10e2b668f97 100644 --- a/ext/intl/formatter/formatter_format.c +++ b/ext/intl/formatter/formatter_format.c @@ -103,9 +103,18 @@ PHP_FUNCTION( numfmt_format ) } INTL_METHOD_CHECK_STATUS( nfo, "Number formatting failed" ); break; - + case FORMAT_TYPE_CURRENCY: + if (getThis()) { + const char *space; + const char *class_name = get_active_class_name(&space); + zend_argument_value_error(2, "cannot be NumberFormatter::TYPE_CURRENCY constant, " + "use %s%sformatCurrency() method instead", class_name, space); + } else { + zend_argument_value_error(3, "cannot be NumberFormatter::TYPE_CURRENCY constant, use numfmt_format_currency() function instead"); + } + RETURN_THROWS(); default: - zend_argument_value_error(3, "must be a NumberFormatter::TYPE_* constant"); + zend_argument_value_error(getThis() ? 2 : 3, "must be a NumberFormatter::TYPE_* constant"); RETURN_THROWS(); } diff --git a/ext/intl/formatter/formatter_parse.c b/ext/intl/formatter/formatter_parse.c index 9940cc5fe7bd0..1a0db887328b8 100644 --- a/ext/intl/formatter/formatter_parse.c +++ b/ext/intl/formatter/formatter_parse.c @@ -85,8 +85,18 @@ PHP_FUNCTION( numfmt_parse ) val_double = unum_parseDouble(FORMATTER_OBJECT(nfo), sstr, sstr_len, position_p, &INTL_DATA_ERROR_CODE(nfo)); RETVAL_DOUBLE(val_double); break; + case FORMAT_TYPE_CURRENCY: + if (getThis()) { + const char *space; + const char *class_name = get_active_class_name(&space); + zend_argument_value_error(2, "cannot be NumberFormatter::TYPE_CURRENCY constant, " + "use %s%sparseCurrency() method instead", class_name, space); + } else { + zend_argument_value_error(3, "cannot be NumberFormatter::TYPE_CURRENCY constant, use numfmt_parse_currency() function instead"); + } + goto cleanup; default: - zend_argument_value_error(3, "must be a NumberFormatter::TYPE_* constant"); + zend_argument_value_error(getThis() ? 2 : 3, "must be a NumberFormatter::TYPE_* constant"); goto cleanup; } diff --git a/ext/intl/tests/formatter_format_and_parse_errors.phpt b/ext/intl/tests/formatter_format_and_parse_errors.phpt new file mode 100644 index 0000000000000..061b0eac250af --- /dev/null +++ b/ext/intl/tests/formatter_format_and_parse_errors.phpt @@ -0,0 +1,65 @@ +--TEST-- +ValueErrors for format/parse methods and procedural functions +--SKIPIF-- + +--FILE-- +getMessage(), \PHP_EOL; +} +try { + $o->format($num, -20); +} catch (\ValueError $e) { + echo $e->getMessage(), \PHP_EOL; +} +try { + numfmt_parse($o, $str, -20); +} catch (\ValueError $e) { + echo $e->getMessage(), \PHP_EOL; +} +try { + $o->parse($str, -20); +} catch (\ValueError $e) { + echo $e->getMessage(), \PHP_EOL; +} + +/* With NumberFormatter::TYPE_CURRENCY */ +try { + numfmt_format($o, $num, NumberFormatter::TYPE_CURRENCY); +} catch (\ValueError $e) { + echo $e->getMessage(), \PHP_EOL; +} +try { + $o->format($num, NumberFormatter::TYPE_CURRENCY); +} catch (\ValueError $e) { + echo $e->getMessage(), \PHP_EOL; +} +try { + numfmt_parse($o, $str, NumberFormatter::TYPE_CURRENCY); +} catch (\ValueError $e) { + echo $e->getMessage(), \PHP_EOL; +} +try { + $o->parse($str, NumberFormatter::TYPE_CURRENCY); +} catch (\ValueError $e) { + echo $e->getMessage(), \PHP_EOL; +} + +?> +--EXPECT-- +numfmt_format(): Argument #3 ($type) must be a NumberFormatter::TYPE_* constant +NumberFormatter::format(): Argument #2 ($type) must be a NumberFormatter::TYPE_* constant +numfmt_parse(): Argument #3 ($type) must be a NumberFormatter::TYPE_* constant +NumberFormatter::parse(): Argument #2 ($type) must be a NumberFormatter::TYPE_* constant +numfmt_format(): Argument #3 ($type) cannot be NumberFormatter::TYPE_CURRENCY constant, use numfmt_format_currency() function instead +NumberFormatter::format(): Argument #2 ($type) cannot be NumberFormatter::TYPE_CURRENCY constant, use NumberFormatter::formatCurrency() method instead +numfmt_parse(): Argument #3 ($type) cannot be NumberFormatter::TYPE_CURRENCY constant, use numfmt_parse_currency() function instead +NumberFormatter::parse(): Argument #2 ($type) cannot be NumberFormatter::TYPE_CURRENCY constant, use NumberFormatter::parseCurrency() method instead