Skip to content

Fix GH-9421 Incorrect argument number for ValueError in NumberFormatter #9489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions ext/intl/formatter/formatter_format.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
12 changes: 11 additions & 1 deletion ext/intl/formatter/formatter_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
65 changes: 65 additions & 0 deletions ext/intl/tests/formatter_format_and_parse_errors.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
--TEST--
ValueErrors for format/parse methods and procedural functions
--SKIPIF--
<?php if( !extension_loaded( 'intl' ) ) print 'skip'; ?>
--FILE--
<?php

$o = new NumberFormatter('en_US', NumberFormatter::PATTERN_DECIMAL);
$num = 5;
$str = "string";

/* Unknown type constant */
try {
numfmt_format($o, $num, -20);
} catch (\ValueError $e) {
echo $e->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