From d534cb2c40c1ee68393947e9bb317468608d18e2 Mon Sep 17 00:00:00 2001 From: Owen Voke Date: Fri, 23 Oct 2026 17:35:56 +0100 Subject: [PATCH] fix: avoid passing an object when invoking static methods via reflection (#1988) * fix: avoid passing an object when invoking static methods via reflection PHP 8.6 deprecates passing an object to `ReflectionMethod::invoke()` for static methods, which breaks higher-order assertions such as `it('...')->assertTrue(true)` since `Assert::assertTrue()` is static. * test: update visual snapshots for new reflection tests --- src/Support/Reflection.php | 2 +- tests/.snapshots/success.txt | 4 +++- tests/Unit/Support/Reflection.php | 26 ++++++++++++++++++++++++++ tests/Visual/Parallel.php | 2 +- 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/Support/Reflection.php b/src/Support/Reflection.php index 6c8f6a92..42e4cbb6 100644 --- a/src/Support/Reflection.php +++ b/src/Support/Reflection.php @@ -33,7 +33,7 @@ final class Reflection try { $reflectionMethod = $reflectionClass->getMethod($method); - return $reflectionMethod->invoke($object, ...$args); + return $reflectionMethod->invoke($reflectionMethod->isStatic() ? null : $object, ...$args); } catch (ReflectionException $exception) { if (method_exists($object, '__call')) { return $object->__call($method, $args); diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 0ea8b3f6..f216215d 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -2146,6 +2146,8 @@ ✓ it gets property values ✓ it gets properties from classes ✓ it gets methods from classes + ✓ it calls instance methods + ✓ it calls static methods without passing an object PASS Tests\Unit\Support\Str ✓ it evaluates the code with ('version()', '__pest_evaluable_version__') @@ -2271,4 +2273,4 @@ ✓ pass with dataset with ('my-datas-set-value') ✓ within describe → pass with dataset with ('my-datas-set-value') - Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 35 skipped, 1610 passed (3516 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 35 skipped, 1612 passed (3518 assertions) \ No newline at end of file diff --git a/tests/Unit/Support/Reflection.php b/tests/Unit/Support/Reflection.php index fa73e037..3fbcf1ee 100644 --- a/tests/Unit/Support/Reflection.php +++ b/tests/Unit/Support/Reflection.php @@ -74,3 +74,29 @@ it('gets methods from classes', function (): void { 'getBar', ]); }); + +it('calls instance methods', function (): void { + $class = new class + { + private string $foo = 'bar'; + + private function getFoo(): string + { + return $this->foo; + } + }; + + expect(Reflection::call($class, 'getFoo'))->toBe('bar'); +}); + +it('calls static methods without passing an object', function (): void { + $class = new class + { + private static function concat(string $a, string $b): string + { + return $a.$b; + } + }; + + expect(Reflection::call($class, 'concat', ['foo', 'bar']))->toBe('foobar'); +}); diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index a775c992..45cb7fe7 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -20,7 +20,7 @@ test('parallel', function () use ($run): void { $output = $run('--exclude-group=integration'); $output = implode("\n", array_slice(explode("\n", (string) $output), -10)); - $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 27 skipped, 1591 passed (3458 assertions)'; + $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 27 skipped, 1593 passed (3460 assertions)'; expect($output) ->toContain("Tests: {$expected}")