Skip to content

Commit 02dfe4f

Browse files
committed
SPL Observer
1 parent 2816653 commit 02dfe4f

File tree

3 files changed

+36
-36
lines changed

3 files changed

+36
-36
lines changed

ext/spl/spl_observer.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ SPL_METHOD(SplObjectStorage, offsetGet)
439439
spl_object_storage_free_hash(intern, &key);
440440

441441
if (!element) {
442+
/* Should be converted? */
442443
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Object not found");
443444
} else {
444445
zval *value = &element->inf;
@@ -891,23 +892,22 @@ SPL_METHOD(SplObjectStorage, __unserialize)
891892

892893
storage_zv = zend_hash_index_find(data, 0);
893894
members_zv = zend_hash_index_find(data, 1);
894-
if (!storage_zv || !members_zv ||
895-
Z_TYPE_P(storage_zv) != IS_ARRAY || Z_TYPE_P(members_zv) != IS_ARRAY) {
896-
zend_throw_exception(spl_ce_UnexpectedValueException,
897-
"Incomplete or ill-typed serialization data", 0);
895+
if (!storage_zv || !members_zv || Z_TYPE_P(storage_zv) != IS_ARRAY ||
896+
Z_TYPE_P(members_zv) != IS_ARRAY) {
897+
zend_throw_error(NULL, "Incomplete or ill-typed serialization data", 0);
898898
RETURN_THROWS();
899899
}
900900

901901
if (zend_hash_num_elements(Z_ARRVAL_P(storage_zv)) % 2 != 0) {
902-
zend_throw_exception(spl_ce_UnexpectedValueException, "Odd number of elements", 0);
902+
zend_value_error("Odd number of elements");
903903
RETURN_THROWS();
904904
}
905905

906906
key = NULL;
907907
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(storage_zv), val) {
908908
if (key) {
909909
if (Z_TYPE_P(key) != IS_OBJECT) {
910-
zend_throw_exception(spl_ce_UnexpectedValueException, "Non-object key", 0);
910+
zend_type_error("Key must be an object");
911911
RETURN_THROWS();
912912
}
913913

@@ -1033,7 +1033,7 @@ SPL_METHOD(MultipleIterator, attachIterator)
10331033
spl_SplObjectStorageElement *element;
10341034

10351035
if (Z_TYPE_P(info) != IS_LONG && Z_TYPE_P(info) != IS_STRING) {
1036-
zend_type_error("Info must be NULL, integer or string");
1036+
zend_argument_type_error(2, "must be int|string|null, %s given", zend_zval_type_name(info));
10371037
RETURN_THROWS();
10381038
}
10391039

@@ -1202,14 +1202,14 @@ static void spl_multiple_iterator_get_all(spl_SplObjectStorage *intern, int get_
12021202
zend_call_method_with_0_params(Z_OBJ_P(it), Z_OBJCE_P(it), &Z_OBJCE_P(it)->iterator_funcs_ptr->zf_key, "key", &retval);
12031203
}
12041204
if (Z_ISUNDEF(retval)) {
1205-
zend_throw_exception(spl_ce_RuntimeException, "Failed to call sub iterator method", 0);
1205+
zend_throw_error(NULL, "Failed to call sub iterator method");
12061206
return;
12071207
}
12081208
} else if (intern->flags & MIT_NEED_ALL) {
12091209
if (SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT == get_type) {
1210-
zend_throw_exception(spl_ce_RuntimeException, "Called current() with non valid sub iterator", 0);
1210+
zend_throw_error(NULL, "Called current() with non valid sub iterator");
12111211
} else {
1212-
zend_throw_exception(spl_ce_RuntimeException, "Called key() with non valid sub iterator", 0);
1212+
zend_throw_error(NULL, "Called key() with non valid sub iterator");
12131213
}
12141214
return;
12151215
} else {
@@ -1226,7 +1226,7 @@ static void spl_multiple_iterator_get_all(spl_SplObjectStorage *intern, int get_
12261226
break;
12271227
default:
12281228
zval_ptr_dtor(&retval);
1229-
zend_throw_exception(spl_ce_InvalidArgumentException, "Sub-Iterator is associated with NULL", 0);
1229+
zend_throw_error(NULL, "Sub-Iterator is associated with NULL");
12301230
return;
12311231
}
12321232
} else {

ext/spl/tests/multiple_iterator_001.phpt

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ foreach($m as $key => $value) {
2828
}
2929
try {
3030
$m->current();
31-
} catch(RuntimeException $e) {
32-
echo "RuntimeException thrown: " . $e->getMessage() . "\n";
31+
} catch(\Throwable $e) {
32+
echo get_class($e) . ': ' . $e->getMessage() . \PHP_EOL;
3333
}
3434
try {
3535
$m->key();
36-
} catch(RuntimeException $e) {
37-
echo "RuntimeException thrown: " . $e->getMessage() . "\n";
36+
} catch(\Throwable $e) {
37+
echo get_class($e) . ': ' . $e->getMessage() . \PHP_EOL;
3838
}
3939

4040
echo "-- Flags = MultipleIterator::MIT_NEED_ANY | MultipleIterator::MIT_KEYS_NUMERIC --\n";
@@ -61,8 +61,8 @@ $m->setFlags(MultipleIterator::MIT_NEED_ALL | MultipleIterator::MIT_KEYS_ASSOC);
6161
$m->rewind();
6262
try {
6363
$m->current();
64-
} catch(InvalidArgumentException $e) {
65-
echo "InvalidArgumentException thrown: " . $e->getMessage() . "\n";
64+
} catch(\Throwable $e) {
65+
echo get_class($e) . ': ' . $e->getMessage() . \PHP_EOL;
6666
}
6767

6868
echo "-- Flags |= MultipleIterator::MIT_KEYS_ASSOC --\n";
@@ -87,8 +87,8 @@ echo "-- Associate with duplicate value --\n";
8787

8888
try {
8989
$m->attachIterator($iter3, "iter1");
90-
} catch(InvalidArgumentException $e) {
91-
echo "InvalidArgumentException thrown: " . $e->getMessage() . "\n";
90+
} catch(\Throwable $e) {
91+
echo get_class($e) . ': ' . $e->getMessage() . \PHP_EOL;
9292
}
9393

9494
echo "-- Count, contains, detach, count, contains, iterate --\n";
@@ -103,7 +103,7 @@ foreach($m as $key => $value) {
103103
}
104104

105105
?>
106-
--EXPECTF--
106+
--EXPECT--
107107
-- Default flags, no iterators --
108108
bool(false)
109109
-- Default flags, MultipleIterator::MIT_NEED_ALL | MultipleIterator::MIT_KEYS_NUMERIC --
@@ -122,7 +122,7 @@ array(3) {
122122
[1]=>
123123
int(1)
124124
[2]=>
125-
object(stdClass)#%d (0) {
125+
object(stdClass)#4 (0) {
126126
}
127127
}
128128
array(3) {
@@ -141,8 +141,8 @@ array(3) {
141141
[2]=>
142142
string(6) "string"
143143
}
144-
RuntimeException thrown: Called current() with non valid sub iterator
145-
RuntimeException thrown: Called key() with non valid sub iterator
144+
Error: Called current() with non valid sub iterator
145+
Error: Called key() with non valid sub iterator
146146
-- Flags = MultipleIterator::MIT_NEED_ANY | MultipleIterator::MIT_KEYS_NUMERIC --
147147
bool(true)
148148
array(3) {
@@ -159,7 +159,7 @@ array(3) {
159159
[1]=>
160160
int(1)
161161
[2]=>
162-
object(stdClass)#%d (0) {
162+
object(stdClass)#4 (0) {
163163
}
164164
}
165165
array(3) {
@@ -209,7 +209,7 @@ array(3) {
209209
[1]=>
210210
int(1)
211211
[2]=>
212-
object(stdClass)#%d (0) {
212+
object(stdClass)#4 (0) {
213213
}
214214
}
215215
array(3) {
@@ -245,7 +245,7 @@ array(3) {
245245
int(3)
246246
}
247247
-- Flags |= MultipleIterator::MIT_KEYS_ASSOC, with iterator associated with NULL --
248-
InvalidArgumentException thrown: Sub-Iterator is associated with NULL
248+
Error: Sub-Iterator is associated with NULL
249249
-- Flags |= MultipleIterator::MIT_KEYS_ASSOC --
250250
array(3) {
251251
["iter1"]=>
@@ -261,7 +261,7 @@ array(3) {
261261
["iter2"]=>
262262
int(1)
263263
[3]=>
264-
object(stdClass)#%d (0) {
264+
object(stdClass)#4 (0) {
265265
}
266266
}
267267
array(3) {
@@ -297,9 +297,9 @@ array(3) {
297297
int(3)
298298
}
299299
-- Associate with invalid value --
300-
Info must be NULL, integer or string
300+
MultipleIterator::attachIterator(): Argument #2 ($info) must be int|string|null, object given
301301
-- Associate with duplicate value --
302-
InvalidArgumentException thrown: Key duplication error
302+
InvalidArgumentException: Key duplication error
303303
-- Count, contains, detach, count, contains, iterate --
304304
int(3)
305305
bool(true)
@@ -316,7 +316,7 @@ array(2) {
316316
["iter1"]=>
317317
int(1)
318318
[3]=>
319-
object(stdClass)#%d (0) {
319+
object(stdClass)#4 (0) {
320320
}
321321
}
322322
array(2) {

ext/spl/tests/unserialize_errors.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,31 +85,31 @@ echo "SplObjectStorage:\n";
8585

8686
try {
8787
unserialize('O:16:"SplObjectStorage":0:{}');
88-
} catch (Exception $e) {
88+
} catch (Error $e) {
8989
echo $e->getMessage(), "\n";
9090
}
9191

9292
try {
9393
unserialize('O:16:"SplObjectStorage":2:{i:0;i:0;i:1;a:0:{}}');
94-
} catch (Exception $e) {
94+
} catch (Error $e) {
9595
echo $e->getMessage(), "\n";
9696
}
9797

9898
try {
9999
unserialize('O:16:"SplObjectStorage":2:{i:0;a:0:{}i:1;i:1;}');
100-
} catch (Exception $e) {
100+
} catch (Error $e) {
101101
echo $e->getMessage(), "\n";
102102
}
103103

104104
try {
105105
unserialize('O:16:"SplObjectStorage":2:{i:0;a:1:{i:0;i:0;}i:1;a:0:{}}');
106-
} catch (Exception $e) {
106+
} catch (ValueError $e) {
107107
echo $e->getMessage(), "\n";
108108
}
109109

110110
try {
111111
unserialize('O:16:"SplObjectStorage":2:{i:0;a:2:{i:0;i:0;i:1;i:0;}i:1;a:0:{}}');
112-
} catch (Exception $e) {
112+
} catch (TypeError $e) {
113113
echo $e->getMessage(), "\n";
114114
}
115115

@@ -135,4 +135,4 @@ Incomplete or ill-typed serialization data
135135
Incomplete or ill-typed serialization data
136136
Incomplete or ill-typed serialization data
137137
Odd number of elements
138-
Non-object key
138+
Key must be an object

0 commit comments

Comments
 (0)