Skip to content

Proposal to improve consistency of is_iterable() and isIterable Reflection class method with what foreach actually expects #10554

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

Open
wants to merge 3 commits into
base: PHP-8.1
Choose a base branch
from
Open
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
10 changes: 2 additions & 8 deletions Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -4807,14 +4807,8 @@ ZEND_API ZEND_COLD const char *zend_get_object_type(const zend_class_entry *ce)

ZEND_API bool zend_is_iterable(zval *iterable) /* {{{ */
{
switch (Z_TYPE_P(iterable)) {
case IS_ARRAY:
return 1;
case IS_OBJECT:
return zend_class_implements_interface(Z_OBJCE_P(iterable), zend_ce_traversable);
default:
return 0;
}
return EXPECTED(Z_TYPE_P(iterable) == IS_ARRAY)
|| EXPECTED(Z_TYPE_P(iterable) == IS_OBJECT);
}
/* }}} */

Expand Down
19 changes: 0 additions & 19 deletions ext/opcache/tests/iterable_type_optimization.phpt

This file was deleted.

2 changes: 1 addition & 1 deletion ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -5287,7 +5287,7 @@ ZEND_METHOD(ReflectionClass, isIterable)
RETURN_FALSE;
}

RETURN_BOOL(ce->get_iterator || instanceof_function(ce, zend_ce_traversable));
RETURN_TRUE;
}
/* }}} */

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Is IteratorImpl iterable? bool(true)
Is IteratorAggregateImpl iterable? bool(true)
Is ExtendsIteratorImpl iterable? bool(true)
Is ExtendsIteratorAggregateImpl iterable? bool(true)
Is A iterable? bool(false)
Is A iterable? bool(true)

Test static invocation:

Expand Down
6 changes: 3 additions & 3 deletions ext/reflection/tests/ReflectionClass_isIterateable_basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ class IteratorClass implements Iterator {
function rewind(): void {}
}
class DerivedClass extends IteratorClass {}
class NonIterator {}
class RegularClass {}

function dump_iterateable($class) {
$reflection = new ReflectionClass($class);
var_dump($reflection->isIterateable());
}

$classes = array("ArrayObject", "IteratorClass", "DerivedClass", "NonIterator");
$classes = array("ArrayObject", "IteratorClass", "DerivedClass", "RegularClass");
foreach ($classes as $class) {
echo "Is $class iterateable? ";
dump_iterateable($class);
Expand All @@ -31,4 +31,4 @@ foreach ($classes as $class) {
Is ArrayObject iterateable? bool(true)
Is IteratorClass iterateable? bool(true)
Is DerivedClass iterateable? bool(true)
Is NonIterator iterateable? bool(false)
Is RegularClass iterateable? bool(true)
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ dump_iterateable($stdClass);

?>
--EXPECT--
bool(false)
bool(false)
bool(true)
bool(true)
14 changes: 13 additions & 1 deletion ext/standard/tests/general_functions/is_iterable.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ Test is_iterable() function
--FILE--
<?php

function stdClassObjectFromJsonDecode() {
$exampleKeyValueStore = [
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3'
];

return json_decode(json_encode($exampleKeyValueStore));
}

function gen() {
yield;
}
Expand All @@ -13,6 +23,7 @@ var_dump(is_iterable(gen()));
var_dump(is_iterable(1));
var_dump(is_iterable(3.14));
var_dump(is_iterable(new stdClass()));
var_dump(is_iterable(stdClassObjectFromJsonDecode()));

?>
--EXPECT--
Expand All @@ -21,4 +32,5 @@ bool(true)
bool(true)
bool(false)
bool(false)
bool(false)
bool(true)
bool(true)