Skip to content

Commit acd688c

Browse files
committed
Convert some SPL Exceptions to normal Errors in spl_iterators.c
1 parent 478e78a commit acd688c

8 files changed

+57
-67
lines changed

ext/spl/spl_iterators.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ SPL_METHOD(RecursiveIteratorIterator, setMaxDepth)
855855
RETURN_THROWS();
856856
}
857857
if (max_depth < -1) {
858-
zend_throw_exception(spl_ce_OutOfRangeException, "Parameter max_depth must be >= -1", 0);
858+
zend_value_error("Parameter max_depth must be greater or equal than -1");
859859
RETURN_THROWS();
860860
} else if (max_depth > INT_MAX) {
861861
max_depth = INT_MAX;
@@ -1105,7 +1105,7 @@ SPL_METHOD(RecursiveTreeIterator, setPrefixPart)
11051105
}
11061106

11071107
if (0 > part || part > 5) {
1108-
zend_throw_exception_ex(spl_ce_OutOfRangeException, 0, "Use RecursiveTreeIterator::PREFIX_* constant");
1108+
zend_value_error("Use RecursiveTreeIterator::PREFIX_* constant");
11091109
RETURN_THROWS();
11101110
}
11111111

@@ -1408,12 +1408,12 @@ static spl_dual_it_object* spl_dual_it_construct(INTERNAL_FUNCTION_PARAMETERS, z
14081408
return NULL;
14091409
}
14101410
if (intern->u.limit.offset < 0) {
1411-
zend_throw_exception(spl_ce_OutOfRangeException, "Parameter offset must be >= 0", 0);
1412-
return NULL;
1411+
zend_value_error("Parameter offset must be >= 0");
1412+
RETURN_THROWS();
14131413
}
14141414
if (intern->u.limit.count < 0 && intern->u.limit.count != -1) {
1415-
zend_throw_exception(spl_ce_OutOfRangeException, "Parameter count must either be -1 or a value greater than or equal 0", 0);
1416-
return NULL;
1415+
zend_value_error("Parameter count must either be -1 or a value greater than or equal 0");
1416+
RETURN_THROWS();
14171417
}
14181418
break;
14191419
}
@@ -1424,8 +1424,8 @@ static spl_dual_it_object* spl_dual_it_construct(INTERNAL_FUNCTION_PARAMETERS, z
14241424
return NULL;
14251425
}
14261426
if (spl_cit_check_flags(flags) != SUCCESS) {
1427-
zend_throw_exception(spl_ce_InvalidArgumentException, "Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER", 0);
1428-
return NULL;
1427+
zend_value_error("Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER");
1428+
RETURN_THROWS();
14291429
}
14301430
intern->u.caching.flags |= flags & CIT_PUBLIC;
14311431
array_init(&intern->u.caching.zcache);
@@ -1445,8 +1445,8 @@ static spl_dual_it_object* spl_dual_it_construct(INTERNAL_FUNCTION_PARAMETERS, z
14451445
|| !instanceof_function(ce, ce_cast)
14461446
|| !ce_cast->get_iterator
14471447
) {
1448-
zend_throw_exception(spl_ce_LogicException, "Class to downcast to not found or not base class or does not implement Traversable", 0);
1449-
return NULL;
1448+
zend_type_error("Class to downcast to not found or not base class or does not implement Traversable");
1449+
RETURN_THROWS();
14501450
}
14511451
ce = ce_cast;
14521452
}
@@ -1456,6 +1456,7 @@ static spl_dual_it_object* spl_dual_it_construct(INTERNAL_FUNCTION_PARAMETERS, z
14561456
zval_ptr_dtor(&retval);
14571457
return NULL;
14581458
}
1459+
/* TODO Change to normal behaviour */
14591460
if (Z_TYPE(retval) != IS_OBJECT || !instanceof_function(Z_OBJCE(retval), zend_ce_traversable)) {
14601461
zend_throw_exception_ex(spl_ce_LogicException, 0, "%s::getIterator() must return an object that implements Traversable", ZSTR_VAL(ce->name));
14611462
return NULL;
@@ -1486,8 +1487,8 @@ static spl_dual_it_object* spl_dual_it_construct(INTERNAL_FUNCTION_PARAMETERS, z
14861487
return NULL;
14871488
}
14881489
if (mode < 0 || mode >= REGIT_MODE_MAX) {
1489-
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "Illegal mode " ZEND_LONG_FMT, mode);
1490-
return NULL;
1490+
zend_value_error("Illegal mode " ZEND_LONG_FMT, mode);
1491+
RETURN_THROWS();
14911492
}
14921493
intern->u.regex.mode = mode;
14931494
intern->u.regex.regex = zend_string_copy(regex);
@@ -2086,7 +2087,7 @@ SPL_METHOD(RegexIterator, setMode)
20862087
}
20872088

20882089
if (mode < 0 || mode >= REGIT_MODE_MAX) {
2089-
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "Illegal mode " ZEND_LONG_FMT, mode);
2090+
zend_value_error("Illegal mode " ZEND_LONG_FMT, mode);
20902091
RETURN_THROWS();
20912092
}
20922093

@@ -2883,7 +2884,7 @@ SPL_METHOD(CachingIterator, setFlags)
28832884
}
28842885

28852886
if (spl_cit_check_flags(flags) != SUCCESS) {
2886-
zend_throw_exception(spl_ce_InvalidArgumentException , "Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER", 0);
2887+
zend_value_error("Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER");
28872888
RETURN_THROWS();
28882889
}
28892890
if ((intern->u.caching.flags & CIT_CALL_TOSTRING) != 0 && (flags & CIT_CALL_TOSTRING) == 0) {

ext/spl/tests/bug51119.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ try {
2020
foreach ($limitIterator as $item) {
2121
echo $item . "\n";
2222
}
23-
} catch (OutOfRangeException $e){
24-
print $e->getMessage() . "\n";
23+
} catch (\ValueError $e){
24+
echo $e->getMessage() . PHP_EOL;
2525
}
2626

2727
?>

ext/spl/tests/iterator_028.phpt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,12 @@ 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-
{
42+
43+
try {
4444
$it->setMaxDepth(4);
4545
$it->setMaxDepth(-2);
46-
}
47-
catch(Exception $e)
48-
{
49-
var_dump($e->getMessage());
46+
} catch (\ValueError $e) {
47+
echo $e->getMessage() . PHP_EOL;
5048
}
5149
var_dump($it->getMaxDepth());
5250
?>
@@ -105,5 +103,5 @@ int(0)
105103
0: 4
106104
===-1===
107105
bool(false)
108-
string(33) "Parameter max_depth must be >= -1"
106+
Parameter max_depth must be greater or equal than -1
109107
int(4)

ext/spl/tests/iterator_037.phpt

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@ 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-
{
16-
echo 'Exception: ' . $e->getMessage() . "\n";
12+
} catch (\ValueError $e) {
13+
echo $e->getMessage() . PHP_EOL;
1714
var_dump($it->getFlags());
1815
return;
19-
}
16+
}
2017
var_dump($it->getFlags());
2118
try
2219
{
@@ -110,19 +107,19 @@ string(3) "0:1"
110107
string(3) "1:2"
111108
string(3) "2:3"
112109
===3===
113-
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
110+
Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
114111
int(0)
115112
===5===
116-
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
113+
Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
117114
int(0)
118115
===9===
119-
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
116+
Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
120117
int(0)
121118
===6===
122-
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
119+
Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
123120
int(0)
124121
===10===
125-
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
122+
Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
126123
int(0)
127124
===X===
128125
Exception: Unsetting flag CALL_TO_STRING is not possible

ext/spl/tests/recursive_tree_iterator_008.phpt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,23 @@ for($i = 0; $i < 6; ++$i) {
1818
foreach($it as $k => $v) {
1919
echo "[$k] => $v\n";
2020
}
21+
2122
try {
2223
$it->setPrefixPart(-1, "");
2324
$it->setPrefixPart(6, "");
24-
} catch (OutOfRangeException $e) {
25-
echo "OutOfRangeException thrown\n";
25+
} catch (\ValueError $e) {
26+
echo $e->getMessage() . PHP_EOL;
2627
}
2728
try {
2829
$it->setPrefixPart(6, "");
29-
} catch (OutOfRangeException $e) {
30-
echo "OutOfRangeException thrown\n";
30+
} catch (\ValueError $e) {
31+
echo $e->getMessage() . PHP_EOL;
3132
}
3233
?>
3334
--EXPECT--
3435
[a] => 035Array
3536
[0] => 0145b
3637
[c] => 045Array
3738
[0] => 0245d
38-
OutOfRangeException thrown
39-
OutOfRangeException thrown
39+
Use RecursiveTreeIterator::PREFIX_* constant
40+
Use RecursiveTreeIterator::PREFIX_* constant

ext/spl/tests/regexIterator_setMode_error.phpt

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

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

2019
?>
21-
--EXPECTF--
22-
int(0)
23-
string(14) "Illegal mode 7"
20+
--EXPECT--
2421
int(0)
22+
Illegal mode 7

ext/spl/tests/spl_caching_iterator_constructor_flags.phpt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ 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-
try {
1211
$test = new CachingIterator($arrayIterator, 0);
1312
$test = new CachingIterator($arrayIterator, 1);
1413
$test = new CachingIterator($arrayIterator, 2);
15-
$test = new CachingIterator($arrayIterator, 3); // this throws an exception
16-
} catch (InvalidArgumentException $e){
17-
print $e->getMessage() . "\n";
18-
}
1914

15+
try {
16+
$test = new CachingIterator($arrayIterator, 3); // this throws an exception
17+
} catch (\ValueError $e) {
18+
echo $e->getMessage() . PHP_EOL;
19+
}
2020

2121
?>
2222
--EXPECT--

ext/spl/tests/spl_limit_iterator_check_limits.phpt

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,22 @@ TestFest London May 2009
99
$arrayIterator = new ArrayIterator($array);
1010

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

17-
1817
try {
19-
$limitIterator = new LimitIterator($arrayIterator, 0, -2);
20-
} catch (OutOfRangeException $e){
21-
print $e->getMessage() . "\n";
18+
var_dump(new LimitIterator($arrayIterator, 0, -2));
19+
} catch (\ValueError $e) {
20+
echo $e->getMessage() . PHP_EOL;
2221
}
2322

24-
try {
25-
$limitIterator = new LimitIterator($arrayIterator, 0, -1);
26-
} catch (OutOfRangeException $e){
27-
print $e->getMessage() . "\n";
28-
}
29-
30-
23+
var_dump(new LimitIterator($arrayIterator, 0, -1));
3124

3225
?>
33-
--EXPECT--
26+
--EXPECTF--
3427
Parameter offset must be >= 0
3528
Parameter count must either be -1 or a value greater than or equal 0
29+
object(LimitIterator)#%d (%d) {
30+
}

0 commit comments

Comments
 (0)