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 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
6 changes: 3 additions & 3 deletions Zend/tests/closure_042.phpt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--TEST--
Closure 042: Binding an instance to a non-scoped non-static closures gives it a dummy scope
Closure 042: Binding an instance to non-scoped non-static closures
--FILE--
<?php

Expand All @@ -20,8 +20,8 @@ echo "Done.\n";
--EXPECTF--
object(stdClass)#%d (0) {
}
string(7) "Closure"
string(8) "stdClass"
object(stdClass)#%d (0) {
}
string(7) "Closure"
string(8) "stdClass"
Done.
11 changes: 7 additions & 4 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -1686,16 +1686,19 @@ ZEND_METHOD(ReflectionFunctionAbstract, getClosureThis)
ZEND_METHOD(ReflectionFunctionAbstract, getClosureScopeClass)
{
reflection_object *intern;
const zend_function *closure_func;

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);
zend_class_entry *called_scope;
zend_function *closure_func;
zend_object *object;
if (Z_OBJ_HANDLER(intern->obj, get_closure)
&& Z_OBJ_HANDLER(intern->obj, get_closure)(Z_OBJ(intern->obj), &called_scope, &closure_func, &object, 1) == SUCCESS
&& 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.

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