Skip to content

Commit 167c32c

Browse files
committed
Use ValueError instead of exceptions in spl_iterators.c
1 parent 1d94286 commit 167c32c

8 files changed

+58
-73
lines changed

ext/spl/spl_iterators.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ PHP_METHOD(RecursiveIteratorIterator, setMaxDepth)
825825
RETURN_THROWS();
826826
}
827827
if (max_depth < -1) {
828-
zend_throw_exception(spl_ce_OutOfRangeException, "Parameter max_depth must be >= -1", 0);
828+
zend_argument_value_error(1, "must be greater than or equal to -1");
829829
RETURN_THROWS();
830830
} else if (max_depth > INT_MAX) {
831831
max_depth = INT_MAX;
@@ -1037,7 +1037,7 @@ PHP_METHOD(RecursiveTreeIterator, setPrefixPart)
10371037
}
10381038

10391039
if (0 > part || part > 5) {
1040-
zend_throw_exception_ex(spl_ce_OutOfRangeException, 0, "Use RecursiveTreeIterator::PREFIX_* constant");
1040+
zend_argument_value_error(1, "must be a RecursiveTreeIterator::PREFIX_* constant");
10411041
RETURN_THROWS();
10421042
}
10431043

@@ -1246,6 +1246,7 @@ static zend_function *spl_dual_it_get_method(zend_object **object, zend_string *
12461246

12471247
#define SPL_CHECK_CTOR(intern, classname) \
12481248
if (intern->dit_type == DIT_Unknown) { \
1249+
/* TODO Normal Error? */ \
12491250
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Classes derived from %s must call %s::__construct()", \
12501251
ZSTR_VAL((spl_ce_##classname)->name), ZSTR_VAL((spl_ce_##classname)->name)); \
12511252
RETURN_THROWS(); \
@@ -1290,11 +1291,11 @@ static spl_dual_it_object* spl_dual_it_construct(INTERNAL_FUNCTION_PARAMETERS, z
12901291
return NULL;
12911292
}
12921293
if (intern->u.limit.offset < 0) {
1293-
zend_throw_exception(spl_ce_OutOfRangeException, "Parameter offset must be >= 0", 0);
1294+
zend_argument_value_error(2, "must be greater than or equal to 0");
12941295
return NULL;
12951296
}
1296-
if (intern->u.limit.count < 0 && intern->u.limit.count != -1) {
1297-
zend_throw_exception(spl_ce_OutOfRangeException, "Parameter count must either be -1 or a value greater than or equal 0", 0);
1297+
if (intern->u.limit.count < -1) {
1298+
zend_argument_value_error(3, "must be greater than or equal to -1");
12981299
return NULL;
12991300
}
13001301
break;
@@ -1306,7 +1307,9 @@ static spl_dual_it_object* spl_dual_it_construct(INTERNAL_FUNCTION_PARAMETERS, z
13061307
return NULL;
13071308
}
13081309
if (spl_cit_check_flags(flags) != SUCCESS) {
1309-
zend_throw_exception(spl_ce_InvalidArgumentException, "Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER", 0);
1310+
zend_argument_value_error(2, "must contain only one of CachingIterator::CALL_TOSTRING, "
1311+
"CachingIterator::TOSTRING_USE_KEY, CachingIterator::TOSTRING_USE_CURRENT, "
1312+
"or CachingIterator::TOSTRING_USE_INNER");
13101313
return NULL;
13111314
}
13121315
intern->u.caching.flags |= flags & CIT_PUBLIC;
@@ -1374,7 +1377,8 @@ static spl_dual_it_object* spl_dual_it_construct(INTERNAL_FUNCTION_PARAMETERS, z
13741377
return NULL;
13751378
}
13761379
if (mode < 0 || mode >= REGIT_MODE_MAX) {
1377-
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "Illegal mode " ZEND_LONG_FMT, mode);
1380+
zend_argument_value_error(3, "must be RegexIterator::MATCH, RegexIterator::GET_MATCH, "
1381+
"RegexIterator::ALL_MATCHES, RegexIterator::SPLIT, or RegexIterator::REPLACE");
13781382
return NULL;
13791383
}
13801384

@@ -1921,7 +1925,8 @@ PHP_METHOD(RegexIterator, setMode)
19211925
}
19221926

19231927
if (mode < 0 || mode >= REGIT_MODE_MAX) {
1924-
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "Illegal mode " ZEND_LONG_FMT, mode);
1928+
zend_argument_value_error(1, "must be RegexIterator::MATCH, RegexIterator::GET_MATCH, "
1929+
"RegexIterator::ALL_MATCHES, RegexIterator::SPLIT, or RegexIterator::REPLACE");
19251930
RETURN_THROWS();
19261931
}
19271932

@@ -2576,7 +2581,9 @@ PHP_METHOD(CachingIterator, setFlags)
25762581
SPL_FETCH_AND_CHECK_DUAL_IT(intern, ZEND_THIS);
25772582

25782583
if (spl_cit_check_flags(flags) != SUCCESS) {
2579-
zend_throw_exception(spl_ce_InvalidArgumentException , "Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER", 0);
2584+
zend_argument_value_error(1, "must contain only one of CachingIterator::CALL_TOSTRING, "
2585+
"CachingIterator::TOSTRING_USE_KEY, CachingIterator::TOSTRING_USE_CURRENT, "
2586+
"or CachingIterator::TOSTRING_USE_INNER");
25802587
RETURN_THROWS();
25812588
}
25822589
if ((intern->u.caching.flags & CIT_CALL_TOSTRING) != 0 && (flags & CIT_CALL_TOSTRING) == 0) {

ext/spl/tests/bug51119.phpt

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,20 @@ SPL: LimitIterator zero is valid offset
66
$array = array('a', 'b', 'c');
77
$arrayIterator = new ArrayIterator($array);
88

9-
try {
10-
$limitIterator = new LimitIterator($arrayIterator, 0);
11-
foreach ($limitIterator as $item) {
9+
$limitIterator = new LimitIterator($arrayIterator, 0);
10+
foreach ($limitIterator as $item) {
1211
echo $item . "\n";
13-
}
14-
} catch (OutOfRangeException $e){
15-
print $e->getMessage() . "\n";
1612
}
1713

1814
try {
19-
$limitIterator = new LimitIterator($arrayIterator, -1);
20-
foreach ($limitIterator as $item) {
21-
echo $item . "\n";
22-
}
23-
} catch (OutOfRangeException $e){
24-
print $e->getMessage() . "\n";
15+
$limitIterator = new LimitIterator($arrayIterator, -1);
16+
} catch (\ValueError $e){
17+
print $e->getMessage() . "\n";
2518
}
2619

2720
?>
2821
--EXPECT--
2922
a
3023
b
3124
c
32-
Parameter offset must be >= 0
25+
LimitIterator::__construct(): Argument #2 ($offset) must be greater than or equal to 0

ext/spl/tests/iterator_028.phpt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,11 @@ foreach($it as $v) echo $it->getDepth() . ": $v\n";
3939
echo "===-1===\n";
4040
$it->setMaxDepth(-1);
4141
var_dump($it->getMaxDepth());
42-
try
43-
{
44-
$it->setMaxDepth(4);
42+
$it->setMaxDepth(4);
43+
try {
4544
$it->setMaxDepth(-2);
46-
}
47-
catch(Exception $e)
48-
{
49-
var_dump($e->getMessage());
45+
} catch(\ValueError $e) {
46+
echo $e->getMessage() . \PHP_EOL;
5047
}
5148
var_dump($it->getMaxDepth());
5249
?>
@@ -105,5 +102,5 @@ int(0)
105102
0: 4
106103
===-1===
107104
bool(false)
108-
string(33) "Parameter max_depth must be >= -1"
105+
RecursiveIteratorIterator::setMaxDepth(): Argument #1 ($max_depth) must be greater than or equal to -1
109106
int(4)

ext/spl/tests/iterator_037.phpt

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,20 @@ function test($ar, $flags)
77
{
88
echo "===$flags===\n";
99
$it = new CachingIterator($ar, 0);
10-
try
11-
{
10+
try {
1211
$it->setFlags($flags);
13-
}
14-
catch (Exception $e)
15-
{
12+
} catch (\ValueError $e) {
1613
echo 'Exception: ' . $e->getMessage() . "\n";
1714
var_dump($it->getFlags());
1815
return;
1916
}
2017
var_dump($it->getFlags());
21-
try
22-
{
18+
try {
2319
foreach($it as $v)
2420
{
2521
var_dump((string)$it);
2622
}
27-
}
28-
catch (Exception $e)
29-
{
23+
} catch (Exception $e) {
3024
echo 'Exception: ' . $e->getMessage() . "\n";
3125
}
3226
}
@@ -110,19 +104,19 @@ string(3) "0:1"
110104
string(3) "1:2"
111105
string(3) "2:3"
112106
===3===
113-
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
107+
Exception: CachingIterator::setFlags(): Argument #1 ($flags) must contain only one of CachingIterator::CALL_TOSTRING, CachingIterator::TOSTRING_USE_KEY, CachingIterator::TOSTRING_USE_CURRENT, or CachingIterator::TOSTRING_USE_INNER
114108
int(0)
115109
===5===
116-
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
110+
Exception: CachingIterator::setFlags(): Argument #1 ($flags) must contain only one of CachingIterator::CALL_TOSTRING, CachingIterator::TOSTRING_USE_KEY, CachingIterator::TOSTRING_USE_CURRENT, or CachingIterator::TOSTRING_USE_INNER
117111
int(0)
118112
===9===
119-
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
113+
Exception: CachingIterator::setFlags(): Argument #1 ($flags) must contain only one of CachingIterator::CALL_TOSTRING, CachingIterator::TOSTRING_USE_KEY, CachingIterator::TOSTRING_USE_CURRENT, or CachingIterator::TOSTRING_USE_INNER
120114
int(0)
121115
===6===
122-
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
116+
Exception: CachingIterator::setFlags(): Argument #1 ($flags) must contain only one of CachingIterator::CALL_TOSTRING, CachingIterator::TOSTRING_USE_KEY, CachingIterator::TOSTRING_USE_CURRENT, or CachingIterator::TOSTRING_USE_INNER
123117
int(0)
124118
===10===
125-
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
119+
Exception: CachingIterator::setFlags(): Argument #1 ($flags) must contain only one of CachingIterator::CALL_TOSTRING, CachingIterator::TOSTRING_USE_KEY, CachingIterator::TOSTRING_USE_CURRENT, or CachingIterator::TOSTRING_USE_INNER
126120
int(0)
127121
===X===
128122
Exception: Unsetting flag CALL_TO_STRING is not possible

ext/spl/tests/recursive_tree_iterator_008.phpt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,19 @@ foreach($it as $k => $v) {
2020
}
2121
try {
2222
$it->setPrefixPart(-1, "");
23-
$it->setPrefixPart(6, "");
24-
} catch (OutOfRangeException $e) {
25-
echo "OutOfRangeException thrown\n";
23+
} catch (\ValueError $e) {
24+
echo $e->getMessage() . \PHP_EOL;
2625
}
2726
try {
2827
$it->setPrefixPart(6, "");
29-
} catch (OutOfRangeException $e) {
30-
echo "OutOfRangeException thrown\n";
28+
} catch (\ValueError $e) {
29+
echo $e->getMessage() . \PHP_EOL;
3130
}
3231
?>
3332
--EXPECT--
3433
[a] => 035Array
3534
[0] => 0145b
3635
[c] => 045Array
3736
[0] => 0245d
38-
OutOfRangeException thrown
39-
OutOfRangeException thrown
37+
RecursiveTreeIterator::setPrefixPart(): Argument #1 ($part) must be a RecursiveTreeIterator::PREFIX_* constant
38+
RecursiveTreeIterator::setPrefixPart(): Argument #1 ($part) must be a RecursiveTreeIterator::PREFIX_* constant

ext/spl/tests/regexIterator_setMode_error.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ var_dump($regexIterator->getMode());
1212

1313
try {
1414
$regexIterator->setMode(7);
15-
} catch (InvalidArgumentException $e) {
16-
var_dump($e->getMessage());
15+
} catch (\ValueError $e) {
16+
echo $e->getMessage() . \PHP_EOL;
1717
var_dump($e->getCode());
1818
}
1919

2020
?>
2121
--EXPECT--
2222
int(0)
23-
string(14) "Illegal mode 7"
23+
RegexIterator::setMode(): Argument #1 ($mode) must be RegexIterator::MATCH, RegexIterator::GET_MATCH, RegexIterator::ALL_MATCHES, RegexIterator::SPLIT, or RegexIterator::REPLACE
2424
int(0)

ext/spl/tests/spl_caching_iterator_constructor_flags.phpt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@ TestFest London May 2009
88
//line 681 ...
99
$array = array(array(7,8,9),1,2,3,array(4,5,6));
1010
$arrayIterator = new ArrayIterator($array);
11+
new CachingIterator($arrayIterator, 0); /* TODO Should this throw? */
12+
new CachingIterator($arrayIterator, CachingIterator::CALL_TOSTRING);
13+
new CachingIterator($arrayIterator, CachingIterator::TOSTRING_USE_KEY);
14+
new CachingIterator($arrayIterator, CachingIterator::TOSTRING_USE_CURRENT);
15+
new CachingIterator($arrayIterator, CachingIterator::TOSTRING_USE_INNER);
1116
try {
12-
$test = new CachingIterator($arrayIterator, 0);
13-
$test = new CachingIterator($arrayIterator, 1);
14-
$test = new CachingIterator($arrayIterator, 2);
15-
$test = new CachingIterator($arrayIterator, 3); // this throws an exception
16-
} catch (InvalidArgumentException $e){
17+
$test = new CachingIterator($arrayIterator, 3); // this throws an exception
18+
} catch (\ValueError $e){
1719
print $e->getMessage() . "\n";
1820
}
1921

2022

2123
?>
2224
--EXPECT--
23-
Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
25+
CachingIterator::__construct(): Argument #2 ($flags) must contain only one of CachingIterator::CALL_TOSTRING, CachingIterator::TOSTRING_USE_KEY, CachingIterator::TOSTRING_USE_CURRENT, or CachingIterator::TOSTRING_USE_INNER

ext/spl/tests/spl_limit_iterator_check_limits.phpt

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,19 @@ $arrayIterator = new ArrayIterator($array);
1010

1111
try {
1212
$limitIterator = new LimitIterator($arrayIterator, -1);
13-
} catch (OutOfRangeException $e){
13+
} catch (\ValueError $e){
1414
print $e->getMessage(). "\n";
1515
}
1616

17-
1817
try {
1918
$limitIterator = new LimitIterator($arrayIterator, 0, -2);
20-
} catch (OutOfRangeException $e){
21-
print $e->getMessage() . "\n";
22-
}
23-
24-
try {
25-
$limitIterator = new LimitIterator($arrayIterator, 0, -1);
26-
} catch (OutOfRangeException $e){
19+
} catch (\ValueError $e){
2720
print $e->getMessage() . "\n";
2821
}
2922

30-
23+
$limitIterator = new LimitIterator($arrayIterator, 0, -1);
3124

3225
?>
3326
--EXPECT--
34-
Parameter offset must be >= 0
35-
Parameter count must either be -1 or a value greater than or equal 0
27+
LimitIterator::__construct(): Argument #2 ($offset) must be greater than or equal to 0
28+
LimitIterator::__construct(): Argument #3 ($count) must be greater than or equal to -1

0 commit comments

Comments
 (0)