Skip to content

Commit

Permalink
reflection: Fix ReflectionFunction::getShortName() for Closures (#14001)
Browse files Browse the repository at this point in the history
see #13550
  • Loading branch information
TimWolla authored Apr 19, 2024
1 parent 2447cb2 commit d03d436
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
20 changes: 20 additions & 0 deletions Zend/tests/closure_067.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
ReflectionFunction::getShortName() returns the full name for closures defined in namespaces.
--FILE--
<?php
namespace Foo;

class Bar {
public function baz() {
return function () {

};
}
}

$c = (new Bar())->baz();
$r = new \ReflectionFunction($c);
var_dump($r->getShortName());
?>
--EXPECT--
string(26) "{closure:Foo\Bar::baz():6}"
9 changes: 6 additions & 3 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -3609,10 +3609,13 @@ ZEND_METHOD(ReflectionFunctionAbstract, getShortName)
GET_REFLECTION_OBJECT_PTR(fptr);

zend_string *name = fptr->common.function_name;
const char *backslash = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
if (backslash) {
RETURN_STRINGL(backslash + 1, ZSTR_LEN(name) - (backslash - ZSTR_VAL(name) + 1));
if (!(fptr->common.fn_flags & ZEND_ACC_CLOSURE)) {
const char *backslash = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
if (backslash) {
RETURN_STRINGL(backslash + 1, ZSTR_LEN(name) - (backslash - ZSTR_VAL(name) + 1));
}
}

RETURN_STR_COPY(name);
}
/* }}} */
Expand Down

0 comments on commit d03d436

Please sign in to comment.