Skip to content

Commit

Permalink
Fix GH-9421 Incorrect argument number for ValueError in NumberFormatter
Browse files Browse the repository at this point in the history
Closes GH-9489
  • Loading branch information
Girgias committed Sep 13, 2022
1 parent 293e691 commit 47500f3
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 3 deletions.
4 changes: 4 additions & 0 deletions NEWS
Expand Up @@ -24,6 +24,10 @@ PHP NEWS
. Fixed bug GH-9308 (GMP throws the wrong error when a GMP object is passed
to gmp_init()). (Girgias)

- Intl
. Fixed bug GH-9421 (Incorrect argument number for ValueError in NumberFormatter).
(Girgias)

- PDO_PGSQL:
. Fixed bug GH-9411 (PgSQL large object resource is incorrectly closed).
(Yurunsoft)
Expand Down
13 changes: 11 additions & 2 deletions ext/intl/formatter/formatter_format.c
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
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
@@ -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

0 comments on commit 47500f3

Please sign in to comment.