Skip to content

Fix GH-8932: Wrong closure scope class reported for static methods #8935

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
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions Zend/zend_closures.c
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,14 @@ ZEND_API const zend_function *zend_get_closure_method_def(zend_object *obj) /* {
}
/* }}} */

ZEND_API const zend_class_entry *zend_get_closure_called_scope(zend_object *obj) /* {{{ */
{
zend_closure *closure = (zend_closure *) obj;
return closure->called_scope;

}
/* }}} */

ZEND_API zval* zend_get_closure_this_ptr(zval *obj) /* {{{ */
{
zend_closure *closure = (zend_closure *)Z_OBJ_P(obj);
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_closures.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ ZEND_API void zend_create_closure(zval *res, zend_function *op_array, zend_class
ZEND_API void zend_create_fake_closure(zval *res, zend_function *op_array, zend_class_entry *scope, zend_class_entry *called_scope, zval *this_ptr);
ZEND_API zend_function *zend_get_closure_invoke_method(zend_object *obj);
ZEND_API const zend_function *zend_get_closure_method_def(zend_object *obj);
ZEND_API const zend_class_entry *zend_get_closure_called_scope(zend_object *obj);
ZEND_API zval* zend_get_closure_this_ptr(zval *obj);

END_EXTERN_C()
Expand Down
6 changes: 4 additions & 2 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -1687,15 +1687,17 @@ ZEND_METHOD(ReflectionFunctionAbstract, getClosureScopeClass)
{
reflection_object *intern;
const zend_function *closure_func;
const zend_class_entry *called_scope;

if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}
GET_REFLECTION_OBJECT();
if (!Z_ISUNDEF(intern->obj)) {
closure_func = zend_get_closure_method_def(Z_OBJ(intern->obj));
if (closure_func && closure_func->common.scope) {
zend_reflection_class_factory(closure_func->common.scope, return_value);
called_scope = zend_get_closure_called_scope(Z_OBJ(intern->obj));
if (closure_func && (called_scope || closure_func->common.scope)) {
zend_reflection_class_factory(called_scope ? (zend_class_entry *) called_scope : closure_func->common.scope, return_value);
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions ext/reflection/tests/gh8932.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
GH-8932 (Wrong closure scope class reported for static methods)
--FILE--
<?php
class A {
public static function b() {
echo static::class;
}
}

class B extends A {}

$c = ['B', 'b'];
$d = \Closure::fromCallable($c);
$r = new \ReflectionFunction($d);
var_dump($r->getClosureScopeClass());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReflectionMethod returns null

https://3v4l.org/hXBYu

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is to be expected; since there is no closure, there is no closure scope class. But in any way, ReflectionFunctionAbstract::getClosureScopeClass() needs proper documentation.

$d();
?>
--EXPECTF--
object(ReflectionClass)#%d (1) {
["name"]=>
string(1) "B"
}
B