Skip to content

Commit 1cba736

Browse files
committed
Throw correct exception from ArrayObject sort methods
Let normal zpp throw ArgumentCountErrors.
1 parent 81d8d60 commit 1cba736

File tree

5 files changed

+34
-24
lines changed

5 files changed

+34
-24
lines changed

ext/spl/spl_array.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,12 +1422,15 @@ static void spl_array_method(INTERNAL_FUNCTION_PARAMETERS, char *fname, int fnam
14221422
GC_ADDREF(aht);
14231423

14241424
if (!use_arg) {
1425+
if (zend_parse_parameters_none() == FAILURE) {
1426+
goto exit;
1427+
}
1428+
14251429
intern->nApplyCount++;
14261430
call_user_function(EG(function_table), NULL, &function_name, return_value, 1, params);
14271431
intern->nApplyCount--;
14281432
} else if (use_arg == SPL_ARRAY_METHOD_MAY_USER_ARG) {
1429-
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "|z", &arg) == FAILURE) {
1430-
zend_throw_exception(spl_ce_BadMethodCallException, "Function expects one argument at most", 0);
1433+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|z", &arg) == FAILURE) {
14311434
goto exit;
14321435
}
14331436
if (arg) {
@@ -1437,8 +1440,7 @@ static void spl_array_method(INTERNAL_FUNCTION_PARAMETERS, char *fname, int fnam
14371440
call_user_function(EG(function_table), NULL, &function_name, return_value, arg ? 2 : 1, params);
14381441
intern->nApplyCount--;
14391442
} else {
1440-
if (ZEND_NUM_ARGS() != 1 || zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
1441-
zend_throw_exception(spl_ce_BadMethodCallException, "Function expects exactly one argument", 0);
1443+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
14421444
goto exit;
14431445
}
14441446
ZVAL_COPY_VALUE(&params[1], arg);

ext/spl/tests/arrayObject_natcasesort_basic1.phpt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ $ao1 = new ArrayObject(array('boo10','boo1','boo2','boo22','BOO5'));
1313
$ao2 = new ArrayObject(array('a'=>'boo10','b'=>'boo1','c'=>'boo2','d'=>'boo22','e'=>'BOO5'));
1414
var_dump($ao1->natcasesort());
1515
var_dump($ao1);
16-
var_dump($ao2->natcasesort('blah'));
16+
try {
17+
var_dump($ao2->natcasesort('blah'));
18+
} catch (ArgumentCountError $e) {
19+
echo $e->getMessage(), "\n";
20+
}
1721
var_dump($ao2);
1822
?>
1923
--EXPECT--
@@ -34,19 +38,19 @@ object(ArrayObject)#1 (1) {
3438
string(5) "boo22"
3539
}
3640
}
37-
bool(true)
41+
ArrayObject::natcasesort() expects exactly 0 parameters, 1 given
3842
object(ArrayObject)#2 (1) {
3943
["storage":"ArrayObject":private]=>
4044
array(5) {
45+
["a"]=>
46+
string(5) "boo10"
4147
["b"]=>
4248
string(4) "boo1"
4349
["c"]=>
4450
string(4) "boo2"
45-
["e"]=>
46-
string(4) "BOO5"
47-
["a"]=>
48-
string(5) "boo10"
4951
["d"]=>
5052
string(5) "boo22"
53+
["e"]=>
54+
string(4) "BOO5"
5155
}
5256
}

ext/spl/tests/arrayObject_natsort_basic1.phpt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ $ao1 = new ArrayObject(array('boo10','boo1','boo2','boo22','BOO5'));
1313
$ao2 = new ArrayObject(array('a'=>'boo10','b'=>'boo1','c'=>'boo2','d'=>'boo22','e'=>'BOO5'));
1414
var_dump($ao1->natsort());
1515
var_dump($ao1);
16-
var_dump($ao2->natsort('blah'));
16+
try {
17+
var_dump($ao2->natsort('blah'));
18+
} catch (ArgumentCountError $e) {
19+
echo $e->getMessage(), "\n";
20+
}
1721
var_dump($ao2);
1822
?>
1923
--EXPECT--
@@ -34,19 +38,19 @@ object(ArrayObject)#1 (1) {
3438
string(5) "boo22"
3539
}
3640
}
37-
bool(true)
41+
ArrayObject::natsort() expects exactly 0 parameters, 1 given
3842
object(ArrayObject)#2 (1) {
3943
["storage":"ArrayObject":private]=>
4044
array(5) {
41-
["e"]=>
42-
string(4) "BOO5"
45+
["a"]=>
46+
string(5) "boo10"
4347
["b"]=>
4448
string(4) "boo1"
4549
["c"]=>
4650
string(4) "boo2"
47-
["a"]=>
48-
string(5) "boo10"
4951
["d"]=>
5052
string(5) "boo22"
53+
["e"]=>
54+
string(4) "BOO5"
5155
}
5256
}

ext/spl/tests/arrayObject_uasort_error1.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ $ao = new ArrayObject();
1111

1212
try {
1313
$ao->uasort();
14-
} catch (BadMethodCallException $e) {
14+
} catch (ArgumentCountError $e) {
1515
echo $e->getMessage() . "\n";
1616
}
1717

1818
try {
1919
$ao->uasort(1,2);
20-
} catch (BadMethodCallException $e) {
20+
} catch (ArgumentCountError $e) {
2121
echo $e->getMessage() . "\n";
2222
}
2323
?>
2424
--EXPECT--
25-
Function expects exactly one argument
26-
Function expects exactly one argument
25+
ArrayObject::uasort() expects exactly 1 parameter, 0 given
26+
ArrayObject::uasort() expects exactly 1 parameter, 2 given

ext/spl/tests/arrayObject_uksort_error1.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ $ao = new ArrayObject();
1111

1212
try {
1313
$ao->uksort();
14-
} catch (BadMethodCallException $e) {
14+
} catch (ArgumentCountError $e) {
1515
echo $e->getMessage() . "\n";
1616
}
1717

1818
try {
1919
$ao->uksort(1,2);
20-
} catch (BadMethodCallException $e) {
20+
} catch (ArgumentCountError $e) {
2121
echo $e->getMessage() . "\n";
2222
}
2323
?>
2424
--EXPECT--
25-
Function expects exactly one argument
26-
Function expects exactly one argument
25+
ArrayObject::uksort() expects exactly 1 parameter, 0 given
26+
ArrayObject::uksort() expects exactly 1 parameter, 2 given

0 commit comments

Comments
 (0)