Skip to content

Commit bd40510

Browse files
committed
- Two new exceptions
- Make use of new exception classes
1 parent 623f564 commit bd40510

File tree

5 files changed

+30
-8
lines changed

5 files changed

+30
-8
lines changed

ext/spl/php_spl.c

+1
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ PHP_FUNCTION(class_implements)
166166
SPL_ADD_CLASS(AppendIterator, z_list, sub, allow, ce_flags); \
167167
SPL_ADD_CLASS(ArrayObject, z_list, sub, allow, ce_flags); \
168168
SPL_ADD_CLASS(ArrayIterator, z_list, sub, allow, ce_flags); \
169+
SPL_ADD_CLASS(BadFunctionCallException, z_list, sub, allow, ce_flags); \
169170
SPL_ADD_CLASS(CachingIterator, z_list, sub, allow, ce_flags); \
170171
SPL_ADD_CLASS(CachingRecursiveIterator, z_list, sub, allow, ce_flags); \
171172
SPL_ADD_CLASS(Countable, z_list, sub, allow, ce_flags); \

ext/spl/spl.php

+14
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,20 @@ class LogicException extends Exception
145145
{
146146
}
147147

148+
/** @ingroup SPL
149+
* @brief Exception thrown when a function call was illegal.
150+
*/
151+
class BadFunctionCallException extends LogicException
152+
{
153+
}
154+
155+
/** @ingroup SPL
156+
* @brief Exception thrown when a method call was illegal.
157+
*/
158+
class BadMethodCallException extends BadFunctionCallException
159+
{
160+
}
161+
148162
/** @ingroup SPL
149163
* @brief Exception that denotes a value not in the valid domain was used.
150164
*

ext/spl/spl_exceptions.c

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include "spl_exceptions.h"
3535

3636
zend_class_entry *spl_ce_LogicException;
37+
zend_class_entry *spl_ce_BadFunctionCallException;
38+
zend_class_entry *spl_ce_BadMethodCallException;
3739
zend_class_entry *spl_ce_DomainException;
3840
zend_class_entry *spl_ce_InvalidArgumentException;
3941
zend_class_entry *spl_ce_LengthException;
@@ -50,6 +52,8 @@ zend_class_entry *spl_ce_UnderflowException;
5052
PHP_MINIT_FUNCTION(spl_exceptions)
5153
{
5254
REGISTER_SPL_SUB_CLASS_EX(LogicException, Exception, NULL, NULL);
55+
REGISTER_SPL_SUB_CLASS_EX(BadFunctionCallException, LogicException, NULL, NULL);
56+
REGISTER_SPL_SUB_CLASS_EX(BadMethodCallException, BadFunctionCallException, NULL, NULL);
5357
REGISTER_SPL_SUB_CLASS_EX(DomainException, LogicException, NULL, NULL);
5458
REGISTER_SPL_SUB_CLASS_EX(InvalidArgumentException, LogicException, NULL, NULL);
5559
REGISTER_SPL_SUB_CLASS_EX(LengthException, LogicException, NULL, NULL);

ext/spl/spl_exceptions.h

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include "php_spl.h"
2626

2727
extern zend_class_entry *spl_ce_LogicException;
28+
extern zend_class_entry *spl_ce_BadFunctionCallException;
29+
extern zend_class_entry *spl_ce_BadMethodCallException;
2830
extern zend_class_entry *spl_ce_DomainException;
2931
extern zend_class_entry *spl_ce_InvalidArgumentException;
3032
extern zend_class_entry *spl_ce_LengthException;

ext/spl/spl_iterators.c

+9-8
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "spl_iterators.h"
3535
#include "spl_directory.h"
3636
#include "spl_array.h"
37+
#include "spl_exceptions.h"
3738

3839
#define INLINE inline
3940

@@ -244,7 +245,7 @@ static void spl_recursive_it_move_forward_ex(spl_recursive_it_object *object TSR
244245
if (child) {
245246
zval_ptr_dtor(&child);
246247
}
247-
zend_throw_exception(zend_exception_get_default(), "Objects returned by RecursiveIterator::getChildren() must implement RecursiveIterator", 0 TSRMLS_CC);
248+
zend_throw_exception(spl_ce_InvalidArgumentException, "Objects returned by RecursiveIterator::getChildren() must implement RecursiveIterator", 0 TSRMLS_CC);
248249
return;
249250
}
250251
if (object->mode == RIT_CHILD_FIRST) {
@@ -612,12 +613,12 @@ static INLINE spl_dual_it_object* spl_dual_it_construct(INTERNAL_FUNCTION_PARAME
612613
}
613614
if (intern->u.limit.offset < 0) {
614615
php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);
615-
zend_throw_exception(zend_exception_get_default(), "Parameter offset must be > 0", 0 TSRMLS_CC);
616+
zend_throw_exception(spl_ce_OutOfRangeException, "Parameter offset must be > 0", 0 TSRMLS_CC);
616617
return NULL;
617618
}
618619
if (intern->u.limit.count < 0 && intern->u.limit.count != -1) {
619620
php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);
620-
zend_throw_exception(zend_exception_get_default(), "Parameter count must either be -1 or a value greater than or equal 0", 0 TSRMLS_CC);
621+
zend_throw_exception(spl_ce_OutOfRangeException, "Parameter count must either be -1 or a value greater than or equal 0", 0 TSRMLS_CC);
621622
return NULL;
622623
}
623624
break;
@@ -1041,11 +1042,11 @@ static INLINE void spl_limit_it_seek(spl_dual_it_object *intern, long pos TSRMLS
10411042

10421043
spl_dual_it_free(intern TSRMLS_CC);
10431044
if (pos < intern->u.limit.offset) {
1044-
zend_throw_exception_ex(zend_exception_get_default(), 0 TSRMLS_CC, "Cannot seek to %ld which is below the offset %ld", pos, intern->u.limit.offset);
1045+
zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0 TSRMLS_CC, "Cannot seek to %ld which is below the offset %ld", pos, intern->u.limit.offset);
10451046
return;
10461047
}
10471048
if (pos > intern->u.limit.offset + intern->u.limit.count && intern->u.limit.count != -1) {
1048-
zend_throw_exception_ex(zend_exception_get_default(), 0 TSRMLS_CC, "Cannot seek to %ld which is behind offest %ld plus count %ld", pos, intern->u.limit.offset, intern->u.limit.count);
1049+
zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0 TSRMLS_CC, "Cannot seek to %ld which is behind offest %ld plus count %ld", pos, intern->u.limit.offset, intern->u.limit.count);
10491050
return;
10501051
}
10511052
if (instanceof_function(intern->inner.ce, spl_ce_SeekableIterator TSRMLS_CC)) {
@@ -1325,7 +1326,7 @@ SPL_METHOD(CachingIterator, __toString)
13251326
intern = (spl_dual_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
13261327

13271328
if (!(intern->u.caching.flags & CIT_CALL_TOSTRING)) {
1328-
zend_throw_exception_ex(zend_exception_get_default(), 0 TSRMLS_CC, "%s does not fetch string value (see CachingIterator::__construct)", Z_OBJCE_P(getThis())->name);
1329+
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "%s does not fetch string value (see CachingIterator::__construct)", Z_OBJCE_P(getThis())->name);
13291330
}
13301331
if (intern->u.caching.zstr) {
13311332
RETURN_STRINGL(Z_STRVAL_P(intern->u.caching.zstr), Z_STRLEN_P(intern->u.caching.zstr), 1);
@@ -1554,14 +1555,14 @@ SPL_METHOD(EmptyIterator, valid)
15541555
Throws exception */
15551556
SPL_METHOD(EmptyIterator, key)
15561557
{
1557-
zend_throw_exception(NULL, "Accessing the key of an EmptyIterator", 0 TSRMLS_CC);
1558+
zend_throw_exception(spl_ce_BadMethodCallException, "Accessing the key of an EmptyIterator", 0 TSRMLS_CC);
15581559
} /* }}} */
15591560

15601561
/* {{{ proto EmptyIterator::current()
15611562
Throws exception */
15621563
SPL_METHOD(EmptyIterator, current)
15631564
{
1564-
zend_throw_exception(NULL, "Accessing the value of an EmptyIterator", 0 TSRMLS_CC);
1565+
zend_throw_exception(spl_ce_BadMethodCallException, "Accessing the value of an EmptyIterator", 0 TSRMLS_CC);
15651566
} /* }}} */
15661567

15671568
/* {{{ proto EmptyIterator::next()

0 commit comments

Comments
 (0)