diff --git a/docs/changelogs/v4.8.0.md b/docs/changelogs/v4.8.0.md index 92a08a293887..a45cb9afaeda 100644 --- a/docs/changelogs/v4.8.0.md +++ b/docs/changelogs/v4.8.0.md @@ -394,5 +394,6 @@ Release Date: Unreleased ## Bugs Fixed - **Config:** Fixed a bug where instantiating an anonymous `BaseConfig` subclass threw a `ValueError` from `getenv()` on PHP 8.6. +- **Testing:** Fixed a bug where `ReflectionHelper::getPrivateMethodInvoker()` passed an object when invoking a static method, which is deprecated in PHP 8.6. See the repo's [CHANGELOG.md](https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md) for a complete list of bugs fixed. diff --git a/system/Test/ReflectionHelper.php b/system/Test/ReflectionHelper.php index 47e85f4a8903..c112e26ed42b 100644 --- a/system/Test/ReflectionHelper.php +++ b/system/Test/ReflectionHelper.php @@ -38,9 +38,9 @@ trait ReflectionHelper public static function getPrivateMethodInvoker($obj, $method) { $refMethod = new ReflectionMethod($obj, $method); - $obj = (gettype($obj) === 'object') ? $obj : null; + $object = gettype($obj) === 'object' && ! $refMethod->isStatic() ? $obj : null; - return static fn (...$args): mixed => $refMethod->invokeArgs($obj, $args); + return static fn (...$args): mixed => $refMethod->invokeArgs($object, $args); } /** diff --git a/tests/system/Test/ReflectionHelperTest.php b/tests/system/Test/ReflectionHelperTest.php index f1c3ee1f368d..e8e35e32b189 100644 --- a/tests/system/Test/ReflectionHelperTest.php +++ b/tests/system/Test/ReflectionHelperTest.php @@ -91,6 +91,18 @@ public function testGetPrivateMethodInvokerWithObject(): void ); } + public function testGetPrivateMethodInvokerWithStaticMethodOnObject(): void + { + $method = self::getPrivateMethodInvoker( + new TestForReflectionHelper(), + 'privateStaticMethod', + ); + $this->assertSame( + 'private_static param1param2', + $method('param1', 'param2'), + ); + } + public function testGetPrivateMethodInvokerWithStatic(): void { $method = self::getPrivateMethodInvoker(