Skip to content

Commit 063fdd9

Browse files
committed
Use ValueError instead of exceptions in SPL extension
1 parent b620733 commit 063fdd9

14 files changed

+86
-101
lines changed

ext/spl/spl_directory.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -720,19 +720,19 @@ void spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAMETERS, zend_long cto
720720
flags |= SPL_FILE_DIR_UNIXPATHS;
721721
}
722722
if (parsed == FAILURE) {
723-
return;
723+
RETURN_THROWS();
724724
}
725725

726-
if (!len) {
727-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Directory name must not be empty.");
728-
return;
726+
if (len == 0) {
727+
zend_argument_value_error(1, "cannot be empty");
728+
RETURN_THROWS();
729729
}
730730

731731
intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
732732
if (intern->_path) {
733733
/* object is already initialized */
734734
zend_throw_error(NULL, "Directory object is already initialized");
735-
return;
735+
RETURN_THROWS();
736736
}
737737
intern->flags = flags;
738738

@@ -2277,7 +2277,7 @@ PHP_METHOD(SplFileObject, setMaxLineLen)
22772277
}
22782278

22792279
if (max_len < 0) {
2280-
zend_throw_exception_ex(spl_ce_DomainException, 0, "Maximum line length must be greater than or equal zero");
2280+
zend_argument_value_error(1, "must be greater than or equal to 0");
22812281
RETURN_THROWS();
22822282
}
22832283

@@ -2724,7 +2724,7 @@ PHP_METHOD(SplFileObject, seek)
27242724
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
27252725

27262726
if (line_pos < 0) {
2727-
zend_throw_exception_ex(spl_ce_LogicException, 0, "Can't seek file %s to negative line " ZEND_LONG_FMT, intern->file_name, line_pos);
2727+
zend_argument_value_error(1, "must be greater than or equal to 0");
27282728
RETURN_THROWS();
27292729
}
27302730

ext/spl/spl_fixedarray.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ PHP_METHOD(SplFixedArray, __construct)
529529
}
530530

531531
if (size < 0) {
532-
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "array size cannot be less than zero");
532+
zend_argument_value_error(1, "must be greater than or equal to 0");
533533
RETURN_THROWS();
534534
}
535535

@@ -704,7 +704,7 @@ PHP_METHOD(SplFixedArray, setSize)
704704
}
705705

706706
if (size < 0) {
707-
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "array size cannot be less than zero");
707+
zend_argument_value_error(1, "must be greater than or equal to 0");
708708
RETURN_THROWS();
709709
}
710710

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/DirectoryIterator_empty_constructor.phpt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ Havard Eide <nucleuz@gmail.com>
55
#PHPTestFest2009 Norway 2009-06-09 \o/
66
--FILE--
77
<?php
8-
$it = new DirectoryIterator("");
8+
try {
9+
$it = new DirectoryIterator("");
10+
} catch (\ValueError $e) {
11+
echo $e->getMessage() . \PHP_EOL;
12+
}
913
?>
10-
--EXPECTF--
11-
Fatal error: Uncaught RuntimeException: Directory name must not be empty. in %s:%d
12-
Stack trace:
13-
#0 %s(%d): DirectoryIterator->__construct('')
14-
#1 {main}
15-
thrown in %s on line %d
14+
--EXPECT--
15+
DirectoryIterator::__construct(): Argument #1 ($path) cannot be empty

ext/spl/tests/SplFileObject_seek_error_001.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ SplFileObject::seek function - test parameters
55
$obj = new SplFileObject(__FILE__);
66
try {
77
$obj->seek(-1);
8-
} catch (LogicException $e) {
8+
} catch (\ValueError $e) {
99
echo($e->getMessage());
1010
}
1111
?>
12-
--EXPECTF--
13-
Can't seek file %s to negative line -1
12+
--EXPECT--
13+
SplFileObject::seek(): Argument #1 ($line_pos) must be greater than or equal to 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/fileobject_setmaxlinelen_error001.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ $s = new SplFileObject( __FILE__ );
88
try {
99
$s->setMaxLineLen(-1);
1010
}
11-
catch (DomainException $e) {
12-
echo 'DomainException thrown';
11+
catch (\ValueError $e) {
12+
echo $e->getMessage() . \PHP_EOL;
1313
}
1414

1515
?>
1616
--EXPECT--
17-
DomainException thrown
17+
SplFileObject::setMaxLineLen(): Argument #1 ($max_len) must be greater than or equal to 0

ext/spl/tests/fixedarray_021.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ var_dump($a->count());
1212
/* negative init value */
1313
try {
1414
$b = new SplFixedArray(-10);
15-
} catch (Exception $e) {
16-
var_dump($e->getMessage());
15+
} catch (\ValueError $e) {
16+
echo $e->getMessage() . \PHP_EOL;
1717
}
1818

1919
/* resize and negative value */
2020
$b = new SplFixedArray();
2121
try {
2222
$b->setSize(-5);
23-
} catch (Exception $e) {
24-
var_dump($e->getMessage());
23+
} catch (\ValueError $e) {
24+
echo $e->getMessage() . \PHP_EOL;
2525
}
2626

2727
/* calling __construct() twice */
@@ -63,8 +63,8 @@ var_dump(empty($a["3"]));
6363
--EXPECTF--
6464
int(0)
6565
int(0)
66-
string(35) "array size cannot be less than zero"
67-
string(35) "array size cannot be less than zero"
66+
SplFixedArray::__construct(): Argument #1 ($size) must be greater than or equal to 0
67+
SplFixedArray::setSize(): Argument #1 ($size) must be greater than or equal to 0
6868
NULL
6969
int(0)
7070
int(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)

0 commit comments

Comments
 (0)