diff --git a/src/functions.php b/src/functions.php index 4dc7298..e02fe53 100644 --- a/src/functions.php +++ b/src/functions.php @@ -41,13 +41,14 @@ function closure($callable) case is_array($callable): list($object, $method) = $callable; - $reflection = new \ReflectionObject($object); - $method = $reflection->getMethod($method); + // A static method ([Foo::class, 'method']) is given by its class name, not an object. + $class = is_string($object) ? $object : get_class($object); + $method = new \ReflectionMethod($object, $method); if (!$method->isPublic()) { $backtrace = debug_backtrace(); foreach ($backtrace as $trace) { - if (isset($trace['class']) && $trace['class'] === $reflection->name) { + if (isset($trace['class']) && $trace['class'] === $class) { break; } @@ -58,7 +59,7 @@ function closure($callable) } } - return $method->getClosure($object); + return $method->getClosure(is_string($object) ? null : $object); default: return \Closure::fromCallable($callable); diff --git a/tests/Collections/TransformsTest.php b/tests/Collections/TransformsTest.php index 1e47a65..6b954c2 100644 --- a/tests/Collections/TransformsTest.php +++ b/tests/Collections/TransformsTest.php @@ -62,6 +62,11 @@ class TransformsTest extends \PHPUnit\Framework\TestCase $this->assertSeq(['x:1', 'y:2'], c\map(fn ($v, $k) => "$k:$v")(self::gen(['x' => 1, 'y' => 2]))); } + public function testMapWithStaticMethodArrayCallable() + { + $this->assertSame(['a' => 2, 'b' => 4], c\map([TransformsTestHelper::class, 'double'])(['a' => 1, 'b' => 2])); + } + public function testMapOnSetFollowsSetMap() { $result = c\map(fn ($v) => $v % 2)(new Set([1, 2, 3])); @@ -418,3 +423,11 @@ class TransformsTest extends \PHPUnit\Framework\TestCase } } } + +class TransformsTestHelper +{ + public static function double(int $value): int + { + return $value * 2; + } +} diff --git a/tests/ReferencingTest.php b/tests/ReferencingTest.php index 8027182..9a4591a 100644 --- a/tests/ReferencingTest.php +++ b/tests/ReferencingTest.php @@ -28,10 +28,20 @@ class TestObject { return ref([$this, 'priv']); } + + public static function twice($x) + { + return $x * 2; + } } class ReferencingTest extends \PHPUnit\Framework\TestCase { + public function testReferenceStaticMethodArrayCallable() + { + $this->assertSame(4, ref([TestObject::class, 'twice'])(2)); + } + public function testReferenceClosure() { $f = function () { };