From 28547a3b911caaaebc4ca6966c82a5e719e771c6 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sun, 30 Jul 2023 14:31:35 +0200 Subject: [PATCH] Callback::unwrap() returns correct class name for private methods --- src/Utils/Callback.php | 7 ++++--- tests/Utils/Callback.closure.phpt | 2 ++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Utils/Callback.php b/src/Utils/Callback.php index 496c1e7ab..b9bbb3ee1 100644 --- a/src/Utils/Callback.php +++ b/src/Utils/Callback.php @@ -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; diff --git a/tests/Utils/Callback.closure.phpt b/tests/Utils/Callback.closure.phpt index 9655dd3e1..eeb608411 100644 --- a/tests/Utils/Callback.closure.phpt +++ b/tests/Utils/Callback.closure.phpt @@ -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('*'));