Skip to content

Commit

Permalink
Callback::unwrap() returns correct class name for private methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jul 30, 2023
1 parent 8d80a2d commit 28547a3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Utils/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,15 @@ public static function isStatic(callable $callable): bool
public static function unwrap(\Closure $closure): callable|array
{
$r = new \ReflectionFunction($closure);
$class = $r->getClosureScopeClass()?->name;
if (str_ends_with($r->name, '}')) {
return $closure;

} elseif ($obj = $r->getClosureThis()) {
} elseif (($obj = $r->getClosureThis()) && $obj::class === $class) {
return [$obj, $r->name];

} elseif ($class = $r->getClosureScopeClass()) {
return [$class->name, $r->name];
} elseif ($class) {
return [$class, $r->name];

} else {
return $r->name;
Expand Down
2 changes: 2 additions & 0 deletions tests/Utils/Callback.closure.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ test('object methods', function () {

Assert::same('Test::privateFun', getName(Callback::toReflection([$test, 'privateFun'])));
Assert::same('Test::privateFun', getName(Callback::toReflection($test->createPrivateClosure())));

Assert::same(['Test', 'privateFun'], Callback::unwrap((new TestChild)->createPrivateClosure()));
Assert::same('Test::privateFun', getName(Callback::toReflection((new TestChild)->createPrivateClosure())));

Assert::same('Test::privateFun*', $test->createPrivateClosure()->__invoke('*'));
Expand Down

0 comments on commit 28547a3

Please sign in to comment.