Use ValueError instead of exceptions in SPL extension

This commit is contained in:
George Peter Banyard 2020-09-12 03:00:53 +02:00
parent b6207338e8
commit 063fdd9422
14 changed files with 86 additions and 101 deletions

View File

@ -720,19 +720,19 @@ void spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAMETERS, zend_long cto
flags |= SPL_FILE_DIR_UNIXPATHS;
}
if (parsed == FAILURE) {
return;
RETURN_THROWS();
}
if (!len) {
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Directory name must not be empty.");
return;
if (len == 0) {
zend_argument_value_error(1, "cannot be empty");
RETURN_THROWS();
}
intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
if (intern->_path) {
/* object is already initialized */
zend_throw_error(NULL, "Directory object is already initialized");
return;
RETURN_THROWS();
}
intern->flags = flags;
@ -2277,7 +2277,7 @@ PHP_METHOD(SplFileObject, setMaxLineLen)
}
if (max_len < 0) {
zend_throw_exception_ex(spl_ce_DomainException, 0, "Maximum line length must be greater than or equal zero");
zend_argument_value_error(1, "must be greater than or equal to 0");
RETURN_THROWS();
}
@ -2724,7 +2724,7 @@ PHP_METHOD(SplFileObject, seek)
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
if (line_pos < 0) {
zend_throw_exception_ex(spl_ce_LogicException, 0, "Can't seek file %s to negative line " ZEND_LONG_FMT, intern->file_name, line_pos);
zend_argument_value_error(1, "must be greater than or equal to 0");
RETURN_THROWS();
}

View File

@ -529,7 +529,7 @@ PHP_METHOD(SplFixedArray, __construct)
}
if (size < 0) {
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "array size cannot be less than zero");
zend_argument_value_error(1, "must be greater than or equal to 0");
RETURN_THROWS();
}
@ -704,7 +704,7 @@ PHP_METHOD(SplFixedArray, setSize)
}
if (size < 0) {
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "array size cannot be less than zero");
zend_argument_value_error(1, "must be greater than or equal to 0");
RETURN_THROWS();
}

View File

@ -825,7 +825,7 @@ PHP_METHOD(RecursiveIteratorIterator, setMaxDepth)
RETURN_THROWS();
}
if (max_depth < -1) {
zend_throw_exception(spl_ce_OutOfRangeException, "Parameter max_depth must be >= -1", 0);
zend_argument_value_error(1, "must be greater than or equal to -1");
RETURN_THROWS();
} else if (max_depth > INT_MAX) {
max_depth = INT_MAX;
@ -1037,7 +1037,7 @@ PHP_METHOD(RecursiveTreeIterator, setPrefixPart)
}
if (0 > part || part > 5) {
zend_throw_exception_ex(spl_ce_OutOfRangeException, 0, "Use RecursiveTreeIterator::PREFIX_* constant");
zend_argument_value_error(1, "must be a RecursiveTreeIterator::PREFIX_* constant");
RETURN_THROWS();
}
@ -1246,6 +1246,7 @@ static zend_function *spl_dual_it_get_method(zend_object **object, zend_string *
#define SPL_CHECK_CTOR(intern, classname) \
if (intern->dit_type == DIT_Unknown) { \
/* TODO Normal Error? */ \
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Classes derived from %s must call %s::__construct()", \
ZSTR_VAL((spl_ce_##classname)->name), ZSTR_VAL((spl_ce_##classname)->name)); \
RETURN_THROWS(); \
@ -1290,11 +1291,11 @@ static spl_dual_it_object* spl_dual_it_construct(INTERNAL_FUNCTION_PARAMETERS, z
return NULL;
}
if (intern->u.limit.offset < 0) {
zend_throw_exception(spl_ce_OutOfRangeException, "Parameter offset must be >= 0", 0);
zend_argument_value_error(2, "must be greater than or equal to 0");
return NULL;
}
if (intern->u.limit.count < 0 && intern->u.limit.count != -1) {
zend_throw_exception(spl_ce_OutOfRangeException, "Parameter count must either be -1 or a value greater than or equal 0", 0);
if (intern->u.limit.count < -1) {
zend_argument_value_error(3, "must be greater than or equal to -1");
return NULL;
}
break;
@ -1306,7 +1307,9 @@ static spl_dual_it_object* spl_dual_it_construct(INTERNAL_FUNCTION_PARAMETERS, z
return NULL;
}
if (spl_cit_check_flags(flags) != SUCCESS) {
zend_throw_exception(spl_ce_InvalidArgumentException, "Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER", 0);
zend_argument_value_error(2, "must contain only one of CachingIterator::CALL_TOSTRING, "
"CachingIterator::TOSTRING_USE_KEY, CachingIterator::TOSTRING_USE_CURRENT, "
"or CachingIterator::TOSTRING_USE_INNER");
return NULL;
}
intern->u.caching.flags |= flags & CIT_PUBLIC;
@ -1374,7 +1377,8 @@ static spl_dual_it_object* spl_dual_it_construct(INTERNAL_FUNCTION_PARAMETERS, z
return NULL;
}
if (mode < 0 || mode >= REGIT_MODE_MAX) {
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "Illegal mode " ZEND_LONG_FMT, mode);
zend_argument_value_error(3, "must be RegexIterator::MATCH, RegexIterator::GET_MATCH, "
"RegexIterator::ALL_MATCHES, RegexIterator::SPLIT, or RegexIterator::REPLACE");
return NULL;
}
@ -1921,7 +1925,8 @@ PHP_METHOD(RegexIterator, setMode)
}
if (mode < 0 || mode >= REGIT_MODE_MAX) {
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "Illegal mode " ZEND_LONG_FMT, mode);
zend_argument_value_error(1, "must be RegexIterator::MATCH, RegexIterator::GET_MATCH, "
"RegexIterator::ALL_MATCHES, RegexIterator::SPLIT, or RegexIterator::REPLACE");
RETURN_THROWS();
}
@ -2576,7 +2581,9 @@ PHP_METHOD(CachingIterator, setFlags)
SPL_FETCH_AND_CHECK_DUAL_IT(intern, ZEND_THIS);
if (spl_cit_check_flags(flags) != SUCCESS) {
zend_throw_exception(spl_ce_InvalidArgumentException , "Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER", 0);
zend_argument_value_error(1, "must contain only one of CachingIterator::CALL_TOSTRING, "
"CachingIterator::TOSTRING_USE_KEY, CachingIterator::TOSTRING_USE_CURRENT, "
"or CachingIterator::TOSTRING_USE_INNER");
RETURN_THROWS();
}
if ((intern->u.caching.flags & CIT_CALL_TOSTRING) != 0 && (flags & CIT_CALL_TOSTRING) == 0) {

View File

@ -5,11 +5,11 @@ Havard Eide <nucleuz@gmail.com>
#PHPTestFest2009 Norway 2009-06-09 \o/
--FILE--
<?php
$it = new DirectoryIterator("");
try {
$it = new DirectoryIterator("");
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
?>
--EXPECTF--
Fatal error: Uncaught RuntimeException: Directory name must not be empty. in %s:%d
Stack trace:
#0 %s(%d): DirectoryIterator->__construct('')
#1 {main}
thrown in %s on line %d
--EXPECT--
DirectoryIterator::__construct(): Argument #1 ($path) cannot be empty

View File

@ -5,9 +5,9 @@ SplFileObject::seek function - test parameters
$obj = new SplFileObject(__FILE__);
try {
$obj->seek(-1);
} catch (LogicException $e) {
} catch (\ValueError $e) {
echo($e->getMessage());
}
?>
--EXPECTF--
Can't seek file %s to negative line -1
--EXPECT--
SplFileObject::seek(): Argument #1 ($line_pos) must be greater than or equal to 0

View File

@ -6,22 +6,15 @@ SPL: LimitIterator zero is valid offset
$array = array('a', 'b', 'c');
$arrayIterator = new ArrayIterator($array);
try {
$limitIterator = new LimitIterator($arrayIterator, 0);
foreach ($limitIterator as $item) {
$limitIterator = new LimitIterator($arrayIterator, 0);
foreach ($limitIterator as $item) {
echo $item . "\n";
}
} catch (OutOfRangeException $e){
print $e->getMessage() . "\n";
}
try {
$limitIterator = new LimitIterator($arrayIterator, -1);
foreach ($limitIterator as $item) {
echo $item . "\n";
}
} catch (OutOfRangeException $e){
print $e->getMessage() . "\n";
$limitIterator = new LimitIterator($arrayIterator, -1);
} catch (\ValueError $e){
print $e->getMessage() . "\n";
}
?>
@ -29,4 +22,4 @@ try {
a
b
c
Parameter offset must be >= 0
LimitIterator::__construct(): Argument #2 ($offset) must be greater than or equal to 0

View File

@ -8,10 +8,10 @@ $s = new SplFileObject( __FILE__ );
try {
$s->setMaxLineLen(-1);
}
catch (DomainException $e) {
echo 'DomainException thrown';
catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
?>
--EXPECT--
DomainException thrown
SplFileObject::setMaxLineLen(): Argument #1 ($max_len) must be greater than or equal to 0

View File

@ -12,16 +12,16 @@ var_dump($a->count());
/* negative init value */
try {
$b = new SplFixedArray(-10);
} catch (Exception $e) {
var_dump($e->getMessage());
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
/* resize and negative value */
$b = new SplFixedArray();
try {
$b->setSize(-5);
} catch (Exception $e) {
var_dump($e->getMessage());
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
/* calling __construct() twice */
@ -63,8 +63,8 @@ var_dump(empty($a["3"]));
--EXPECTF--
int(0)
int(0)
string(35) "array size cannot be less than zero"
string(35) "array size cannot be less than zero"
SplFixedArray::__construct(): Argument #1 ($size) must be greater than or equal to 0
SplFixedArray::setSize(): Argument #1 ($size) must be greater than or equal to 0
NULL
int(0)
int(0)

View File

@ -39,14 +39,11 @@ foreach($it as $v) echo $it->getDepth() . ": $v\n";
echo "===-1===\n";
$it->setMaxDepth(-1);
var_dump($it->getMaxDepth());
try
{
$it->setMaxDepth(4);
$it->setMaxDepth(4);
try {
$it->setMaxDepth(-2);
}
catch(Exception $e)
{
var_dump($e->getMessage());
} catch(\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
var_dump($it->getMaxDepth());
?>
@ -105,5 +102,5 @@ int(0)
0: 4
===-1===
bool(false)
string(33) "Parameter max_depth must be >= -1"
RecursiveIteratorIterator::setMaxDepth(): Argument #1 ($max_depth) must be greater than or equal to -1
int(4)

View File

@ -7,26 +7,20 @@ function test($ar, $flags)
{
echo "===$flags===\n";
$it = new CachingIterator($ar, 0);
try
{
try {
$it->setFlags($flags);
}
catch (Exception $e)
{
} catch (\ValueError $e) {
echo 'Exception: ' . $e->getMessage() . "\n";
var_dump($it->getFlags());
return;
}
var_dump($it->getFlags());
try
{
try {
foreach($it as $v)
{
var_dump((string)$it);
}
}
catch (Exception $e)
{
} catch (Exception $e) {
echo 'Exception: ' . $e->getMessage() . "\n";
}
}
@ -110,19 +104,19 @@ string(3) "0:1"
string(3) "1:2"
string(3) "2:3"
===3===
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
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
int(0)
===5===
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
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
int(0)
===9===
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
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
int(0)
===6===
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
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
int(0)
===10===
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
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
int(0)
===X===
Exception: Unsetting flag CALL_TO_STRING is not possible

View File

@ -20,14 +20,13 @@ foreach($it as $k => $v) {
}
try {
$it->setPrefixPart(-1, "");
$it->setPrefixPart(6, "");
} catch (OutOfRangeException $e) {
echo "OutOfRangeException thrown\n";
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
try {
$it->setPrefixPart(6, "");
} catch (OutOfRangeException $e) {
echo "OutOfRangeException thrown\n";
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
?>
--EXPECT--
@ -35,5 +34,5 @@ try {
[0] => 0145b
[c] => 045Array
[0] => 0245d
OutOfRangeException thrown
OutOfRangeException thrown
RecursiveTreeIterator::setPrefixPart(): Argument #1 ($part) must be a RecursiveTreeIterator::PREFIX_* constant
RecursiveTreeIterator::setPrefixPart(): Argument #1 ($part) must be a RecursiveTreeIterator::PREFIX_* constant

View File

@ -12,13 +12,13 @@ var_dump($regexIterator->getMode());
try {
$regexIterator->setMode(7);
} catch (InvalidArgumentException $e) {
var_dump($e->getMessage());
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
var_dump($e->getCode());
}
?>
--EXPECT--
int(0)
string(14) "Illegal mode 7"
RegexIterator::setMode(): Argument #1 ($mode) must be RegexIterator::MATCH, RegexIterator::GET_MATCH, RegexIterator::ALL_MATCHES, RegexIterator::SPLIT, or RegexIterator::REPLACE
int(0)

View File

@ -8,16 +8,18 @@ TestFest London May 2009
//line 681 ...
$array = array(array(7,8,9),1,2,3,array(4,5,6));
$arrayIterator = new ArrayIterator($array);
new CachingIterator($arrayIterator, 0); /* TODO Should this throw? */
new CachingIterator($arrayIterator, CachingIterator::CALL_TOSTRING);
new CachingIterator($arrayIterator, CachingIterator::TOSTRING_USE_KEY);
new CachingIterator($arrayIterator, CachingIterator::TOSTRING_USE_CURRENT);
new CachingIterator($arrayIterator, CachingIterator::TOSTRING_USE_INNER);
try {
$test = new CachingIterator($arrayIterator, 0);
$test = new CachingIterator($arrayIterator, 1);
$test = new CachingIterator($arrayIterator, 2);
$test = new CachingIterator($arrayIterator, 3); // this throws an exception
} catch (InvalidArgumentException $e){
$test = new CachingIterator($arrayIterator, 3); // this throws an exception
} catch (\ValueError $e){
print $e->getMessage() . "\n";
}
?>
--EXPECT--
Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
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

View File

@ -10,26 +10,19 @@ $arrayIterator = new ArrayIterator($array);
try {
$limitIterator = new LimitIterator($arrayIterator, -1);
} catch (OutOfRangeException $e){
} catch (\ValueError $e){
print $e->getMessage(). "\n";
}
try {
$limitIterator = new LimitIterator($arrayIterator, 0, -2);
} catch (OutOfRangeException $e){
} catch (\ValueError $e){
print $e->getMessage() . "\n";
}
try {
$limitIterator = new LimitIterator($arrayIterator, 0, -1);
} catch (OutOfRangeException $e){
print $e->getMessage() . "\n";
}
$limitIterator = new LimitIterator($arrayIterator, 0, -1);
?>
--EXPECT--
Parameter offset must be >= 0
Parameter count must either be -1 or a value greater than or equal 0
LimitIterator::__construct(): Argument #2 ($offset) must be greater than or equal to 0
LimitIterator::__construct(): Argument #3 ($count) must be greater than or equal to -1