Skip to content

SPL use standard Error instead of custom exception (again) #6115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ext/spl/spl_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,7 @@ static void spl_array_it_rewind(zend_object_iterator *iter) /* {{{ */
/* {{{ spl_array_set_array */
static void spl_array_set_array(zval *object, spl_array_object *intern, zval *array, zend_long ar_flags, int just_array) {
if (Z_TYPE_P(array) != IS_OBJECT && Z_TYPE_P(array) != IS_ARRAY) {
// TODO Type Error?
zend_throw_exception(spl_ce_InvalidArgumentException, "Passed variable is not an array or object", 0);
return;
}
Expand Down Expand Up @@ -1144,7 +1145,7 @@ zend_object_iterator *spl_array_get_iterator(zend_class_entry *ce, zval *object,
spl_array_object *array_object = Z_SPLARRAY_P(object);

if (by_ref && (array_object->ar_flags & SPL_ARRAY_OVERLOADED_CURRENT)) {
zend_throw_exception(spl_ce_RuntimeException, "An iterator cannot be used with foreach by reference", 0);
zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
return NULL;
}

Expand Down
29 changes: 13 additions & 16 deletions ext/spl/spl_directory.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ PHPAPI zend_class_entry *spl_ce_GlobIterator;
PHPAPI zend_class_entry *spl_ce_SplFileObject;
PHPAPI zend_class_entry *spl_ce_SplTempFileObject;

// TODO Use standard Error
#define CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(spl_filesystem_object_pointer) \
if (!(spl_filesystem_object_pointer)->u.file.stream) { \
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized"); \
zend_throw_error(NULL, "Object not initialized"); \
RETURN_THROWS(); \
}

Expand Down Expand Up @@ -209,7 +208,7 @@ static inline int spl_filesystem_object_get_file_name(spl_filesystem_object *int
case SPL_FS_INFO:
case SPL_FS_FILE:
if (!intern->file_name) {
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
zend_throw_error(NULL, "Object not initialized");
return FAILURE;
}
break;
Expand Down Expand Up @@ -721,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;

Expand Down Expand Up @@ -1393,9 +1392,7 @@ PHP_METHOD(SplFileInfo, __debugInfo)
/* {{{ */
PHP_METHOD(SplFileInfo, _bad_state_ex)
{
zend_throw_exception_ex(spl_ce_LogicException, 0,
"The parent constructor was not called: the object is in an "
"invalid state ");
zend_throw_error(NULL, "The parent constructor was not called: the object is in an invalid state");
}
/* }}} */

Expand Down Expand Up @@ -1612,7 +1609,7 @@ zend_object_iterator *spl_filesystem_dir_get_iterator(zend_class_entry *ce, zval
spl_filesystem_object *dir_object;

if (by_ref) {
zend_throw_exception(spl_ce_RuntimeException, "An iterator cannot be used with foreach by reference", 0);
zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
return NULL;
}
dir_object = Z_SPLFILESYSTEM_P(object);
Expand Down Expand Up @@ -1819,7 +1816,7 @@ zend_object_iterator *spl_filesystem_tree_get_iterator(zend_class_entry *ce, zva
spl_filesystem_object *dir_object;

if (by_ref) {
zend_throw_exception(spl_ce_RuntimeException, "An iterator cannot be used with foreach by reference", 0);
zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
return NULL;
}
dir_object = Z_SPLFILESYSTEM_P(object);
Expand Down Expand Up @@ -2021,7 +2018,7 @@ static int spl_filesystem_file_read_line(zval * this_ptr, spl_filesystem_object
static void spl_filesystem_file_rewind(zval * this_ptr, spl_filesystem_object *intern) /* {{{ */
{
if (!intern->u.file.stream) {
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
zend_throw_error(NULL, "Object not initialized");
return;
}
if (-1 == php_stream_rewind(intern->u.file.stream)) {
Expand Down Expand Up @@ -2280,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();
}

Expand Down Expand Up @@ -2727,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();
}

Expand Down
2 changes: 1 addition & 1 deletion ext/spl/spl_dllist.c
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,7 @@ zend_object_iterator *spl_dllist_get_iterator(zend_class_entry *ce, zval *object
spl_dllist_object *dllist_object = Z_SPLDLLIST_P(object);

if (by_ref) {
zend_throw_exception(spl_ce_RuntimeException, "An iterator cannot be used with foreach by reference", 0);
zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
return NULL;
}

Expand Down
6 changes: 3 additions & 3 deletions ext/spl/spl_fixedarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -955,7 +955,7 @@ zend_object_iterator *spl_fixedarray_get_iterator(zend_class_entry *ce, zval *ob
spl_fixedarray_it *iterator;

if (by_ref) {
zend_throw_exception(spl_ce_RuntimeException, "An iterator cannot be used with foreach by reference", 0);
zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
return NULL;
}

Expand Down
4 changes: 2 additions & 2 deletions ext/spl/spl_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ zend_object_iterator *spl_heap_get_iterator(zend_class_entry *ce, zval *object,
spl_heap_object *heap_object = Z_SPLHEAP_P(object);

if (by_ref) {
zend_throw_exception(spl_ce_RuntimeException, "An iterator cannot be used with foreach by reference", 0);
zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
return NULL;
}

Expand All @@ -1091,7 +1091,7 @@ zend_object_iterator *spl_pqueue_get_iterator(zend_class_entry *ce, zval *object
spl_heap_object *heap_object = Z_SPLHEAP_P(object);

if (by_ref) {
zend_throw_exception(spl_ce_RuntimeException, "An iterator cannot be used with foreach by reference", 0);
zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
return NULL;
}

Expand Down
51 changes: 25 additions & 26 deletions ext/spl/spl_iterators.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ static inline spl_recursive_it_object *spl_recursive_it_from_obj(zend_object *ob
do { \
spl_dual_it_object *it = Z_SPLDUAL_IT_P(objzval); \
if (it->dit_type == DIT_Unknown) { \
zend_throw_exception_ex(spl_ce_LogicException, 0, \
"The object is in an invalid state as the parent constructor was not called"); \
zend_throw_error(NULL, "The object is in an invalid state as the parent constructor was not called"); \
RETURN_THROWS(); \
} \
(var) = it; \
Expand All @@ -135,8 +134,7 @@ static inline spl_recursive_it_object *spl_recursive_it_from_obj(zend_object *ob
#define SPL_FETCH_SUB_ELEMENT(var, object, element) \
do { \
if(!(object)->iterators) { \
zend_throw_exception_ex(spl_ce_LogicException, 0, \
"The object is in an invalid state as the parent constructor was not called"); \
zend_throw_error(NULL, "The object is in an invalid state as the parent constructor was not called"); \
return; \
} \
(var) = (object)->iterators[(object)->level].element; \
Expand All @@ -145,8 +143,7 @@ static inline spl_recursive_it_object *spl_recursive_it_from_obj(zend_object *ob
#define SPL_FETCH_SUB_ELEMENT_ADDR(var, object, element) \
do { \
if(!(object)->iterators) { \
zend_throw_exception_ex(spl_ce_LogicException, 0, \
"The object is in an invalid state as the parent constructor was not called"); \
zend_throw_error(NULL, "The object is in an invalid state as the parent constructor was not called"); \
RETURN_THROWS(); \
} \
(var) = &(object)->iterators[(object)->level].element; \
Expand Down Expand Up @@ -448,7 +445,7 @@ static zend_object_iterator *spl_recursive_it_get_iterator(zend_class_entry *ce,
spl_recursive_it_object *object;

if (by_ref) {
zend_throw_exception(spl_ce_RuntimeException, "An iterator cannot be used with foreach by reference", 0);
zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
return NULL;
}
iterator = emalloc(sizeof(spl_recursive_it_iterator));
Expand Down Expand Up @@ -698,8 +695,7 @@ PHP_METHOD(RecursiveIteratorIterator, getSubIterator)
}

if(!object->iterators) {
zend_throw_exception_ex(spl_ce_LogicException, 0,
"The object is in an invalid state as the parent constructor was not called");
zend_throw_error(NULL, "The object is in an invalid state as the parent constructor was not called");
RETURN_THROWS();
}

Expand Down Expand Up @@ -829,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;
Expand Down Expand Up @@ -1041,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();
}

Expand All @@ -1059,8 +1055,7 @@ PHP_METHOD(RecursiveTreeIterator, getPrefix)
}

if(!object->iterators) {
zend_throw_exception_ex(spl_ce_LogicException, 0,
"The object is in an invalid state as the parent constructor was not called");
zend_throw_error(NULL, "The object is in an invalid state as the parent constructor was not called");
RETURN_THROWS();
}

Expand Down Expand Up @@ -1092,8 +1087,7 @@ PHP_METHOD(RecursiveTreeIterator, getEntry)
}

if(!object->iterators) {
zend_throw_exception_ex(spl_ce_LogicException, 0,
"The object is in an invalid state as the parent constructor was not called");
zend_throw_error(NULL, "The object is in an invalid state as the parent constructor was not called");
RETURN_THROWS();
}

Expand All @@ -1110,8 +1104,7 @@ PHP_METHOD(RecursiveTreeIterator, getPostfix)
}

if(!object->iterators) {
zend_throw_exception_ex(spl_ce_LogicException, 0,
"The object is in an invalid state as the parent constructor was not called");
zend_throw_error(NULL, "The object is in an invalid state as the parent constructor was not called");
RETURN_THROWS();
}

Expand All @@ -1131,8 +1124,7 @@ PHP_METHOD(RecursiveTreeIterator, current)
}

if(!object->iterators) {
zend_throw_exception_ex(spl_ce_LogicException, 0,
"The object is in an invalid state as the parent constructor was not called");
zend_throw_error(NULL, "The object is in an invalid state as the parent constructor was not called");
RETURN_THROWS();
}

Expand Down Expand Up @@ -1254,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(); \
Expand Down Expand Up @@ -1298,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;
Expand All @@ -1314,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;
Expand Down Expand Up @@ -1382,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;
}

Expand Down Expand Up @@ -1929,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();
}

Expand Down Expand Up @@ -2584,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) {
Expand Down
2 changes: 1 addition & 1 deletion ext/spl/tests/DirectoryIterator_by_reference.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ foreach( $it as &$file ) {
}
?>
--EXPECTF--
Fatal error: Uncaught RuntimeException: An iterator cannot be used with foreach by reference in %s:%d
Fatal error: Uncaught Error: An iterator cannot be used with foreach by reference in %s:%d
Stack trace:
#0 {main}
thrown in %s on line %d
14 changes: 7 additions & 7 deletions ext/spl/tests/DirectoryIterator_empty_constructor.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ Havard Eide <[email protected]>
#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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ try {
$value *= $value;
echo $value, PHP_EOL;
}
} catch (Exception $e) {
} catch (\Error $e) {
echo $e->getMessage(), PHP_EOL;
}

Expand Down
6 changes: 3 additions & 3 deletions ext/spl/tests/SplFileObject_seek_error_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading