summaryrefslogtreecommitdiff
path: root/ext/spl
diff options
context:
space:
mode:
authorMáté Kocsis <kocsismate@woohoolabs.com>2020-09-04 14:23:43 +0200
committerMáté Kocsis <kocsismate@woohoolabs.com>2020-09-04 14:32:34 +0200
commit8107a1da5af480839b226882e5c27fd76b191ee1 (patch)
tree07513d5929667e627b0fd23adffd005940ebf87c /ext/spl
parente50449bcb4c72f13577aecc195baf691a8341a29 (diff)
downloadphp-git-8107a1da5af480839b226882e5c27fd76b191ee1.tar.gz
Use ZPP instead of custom type checks
We can add these types as a native type declaration to stubs as a side-effect. Closes GH-6068
Diffstat (limited to 'ext/spl')
-rw-r--r--ext/spl/spl_observer.c35
-rw-r--r--ext/spl/spl_observer.stub.php7
-rw-r--r--ext/spl/spl_observer_arginfo.h4
-rw-r--r--ext/spl/tests/multiple_iterator_001.phpt6
4 files changed, 29 insertions, 23 deletions
diff --git a/ext/spl/spl_observer.c b/ext/spl/spl_observer.c
index eaad4db815..3f134b77db 100644
--- a/ext/spl/spl_observer.c
+++ b/ext/spl/spl_observer.c
@@ -935,34 +935,43 @@ PHP_METHOD(MultipleIterator, setFlags)
/* {{{ Attach a new iterator */
PHP_METHOD(MultipleIterator, attachIterator)
{
- spl_SplObjectStorage *intern;
- zval *iterator = NULL, *info = NULL;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!", &iterator, zend_ce_iterator, &info) == FAILURE) {
- RETURN_THROWS();
- }
+ spl_SplObjectStorage *intern;
+ zval *iterator = NULL;
+ zval zinfo;
+ zend_string *info_str;
+ zend_long info_long;
+ zend_bool info_is_null = 1;
+
+ ZEND_PARSE_PARAMETERS_START(1, 2)
+ Z_PARAM_OBJECT_OF_CLASS(iterator, zend_ce_iterator)
+ Z_PARAM_OPTIONAL
+ Z_PARAM_STR_OR_LONG_OR_NULL(info_str, info_long, info_is_null)
+ ZEND_PARSE_PARAMETERS_END();
intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
- if (info != NULL) {
+ if (!info_is_null) {
spl_SplObjectStorageElement *element;
- if (Z_TYPE_P(info) != IS_LONG && Z_TYPE_P(info) != IS_STRING) {
- zend_throw_exception(spl_ce_InvalidArgumentException, "Info must be NULL, integer or string", 0);
- RETURN_THROWS();
+ if (info_str) {
+ ZVAL_STR(&zinfo, info_str);
+ } else {
+ ZVAL_LONG(&zinfo, info_long);
}
zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL) {
- if (fast_is_identical_function(info, &element->inf)) {
+ if (fast_is_identical_function(&zinfo, &element->inf)) {
zend_throw_exception(spl_ce_InvalidArgumentException, "Key duplication error", 0);
RETURN_THROWS();
}
zend_hash_move_forward_ex(&intern->storage, &intern->pos);
}
- }
- spl_object_storage_attach(intern, iterator, info);
+ spl_object_storage_attach(intern, iterator, &zinfo);
+ } else {
+ spl_object_storage_attach(intern, iterator, NULL);
+ }
}
/* }}} */
diff --git a/ext/spl/spl_observer.stub.php b/ext/spl/spl_observer.stub.php
index e399b13c98..475f3b8dc7 100644
--- a/ext/spl/spl_observer.stub.php
+++ b/ext/spl/spl_observer.stub.php
@@ -120,11 +120,8 @@ class MultipleIterator implements Iterator
/** @return void */
public function setFlags(int $flags) {}
- /**
- * @param int|string|null $info
- * @return void
- */
- public function attachIterator(Iterator $iterator, $info = null) {}
+ /** @return void */
+ public function attachIterator(Iterator $iterator, string|int|null $info = null) {}
/** @return void */
public function detachIterator(Iterator $iterator) {}
diff --git a/ext/spl/spl_observer_arginfo.h b/ext/spl/spl_observer_arginfo.h
index 3c08a44268..eed972b283 100644
--- a/ext/spl/spl_observer_arginfo.h
+++ b/ext/spl/spl_observer_arginfo.h
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
- * Stub hash: f795244462fc7b6aed3f38f6b2e1985b3a2ab7c1 */
+ * Stub hash: aab6134fb2223ffe4d686f3a601e66da17c99511 */
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_SplObserver_update, 0, 0, 1)
ZEND_ARG_OBJ_INFO(0, subject, SplSubject, 0)
@@ -94,7 +94,7 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_MultipleIterator_attachIterator, 0, 0, 1)
ZEND_ARG_OBJ_INFO(0, iterator, Iterator, 0)
- ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, info, "null")
+ ZEND_ARG_TYPE_MASK(0, info, MAY_BE_STRING|MAY_BE_LONG|MAY_BE_NULL, "null")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_MultipleIterator_detachIterator, 0, 0, 1)
diff --git a/ext/spl/tests/multiple_iterator_001.phpt b/ext/spl/tests/multiple_iterator_001.phpt
index 56a08da272..7f2b971b55 100644
--- a/ext/spl/tests/multiple_iterator_001.phpt
+++ b/ext/spl/tests/multiple_iterator_001.phpt
@@ -79,8 +79,8 @@ echo "-- Associate with invalid value --\n";
try {
$m->attachIterator($iter3, new stdClass());
-} catch(InvalidArgumentException $e) {
- echo "InvalidArgumentException thrown: " . $e->getMessage() . "\n";
+} catch(TypeError $e) {
+ echo "TypeError thrown: " . $e->getMessage() . "\n";
}
echo "-- Associate with duplicate value --\n";
@@ -297,7 +297,7 @@ array(3) {
int(3)
}
-- Associate with invalid value --
-InvalidArgumentException thrown: Info must be NULL, integer or string
+TypeError thrown: MultipleIterator::attachIterator(): Argument #2 ($info) must be of type string|int|null, stdClass given
-- Associate with duplicate value --
InvalidArgumentException thrown: Key duplication error
-- Count, contains, detach, count, contains, iterate --