diff --git a/CHANGELOG.md b/CHANGELOG.md index ca2d826..7bb6830 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to `pollora/hook` are documented in this file. ## [Unreleased] +### Fixed + +- Registering an invokable object (`Action::add('init', new Handler)`) without an argument count threw a `TypeError`, and a static method written as a string (`'Handler::run'`) threw `CallbackAnalysisFailedException`: the parameters were counted with `ReflectionFunction`, which reads neither. They are now read from the object's `__invoke()` and from the named static method. + ## [2.0.2] - 2026-11-25 ### Fixed diff --git a/src/Domain/Service/AbstractHook.php b/src/Domain/Service/AbstractHook.php index 521cf2c..b6dadc7 100644 --- a/src/Domain/Service/AbstractHook.php +++ b/src/Domain/Service/AbstractHook.php @@ -464,6 +464,13 @@ private function countParameters(callable|array $callback): int if (is_array($callback)) { [$object, $method] = $callback; $reflection = new \ReflectionMethod($object, $method); + } elseif (is_string($callback) && str_contains($callback, '::')) { + // A static method written 'Class::method' is not a function name + [$class, $staticMethod] = explode('::', $callback, 2); + $reflection = new \ReflectionMethod($class, $staticMethod); + } elseif (is_object($callback) && ! $callback instanceof \Closure) { + // An invokable object: its __invoke() method is what WordPress calls + $reflection = new \ReflectionMethod($callback, '__invoke'); } else { $reflection = new \ReflectionFunction($callback); } @@ -621,6 +628,10 @@ private function getCacheKeyForCallback(callable|array $callback): string } // Closures never reach this method: detectArguments() keys them by the closure itself. + if (is_object($callback)) { + return $callback::class.'::__invoke@'.spl_object_id($callback); + } + // Fallback for any other type of callable return serialize($callback); } diff --git a/tests/Unit/Domain/Service/AbstractHookTest.php b/tests/Unit/Domain/Service/AbstractHookTest.php index 00fc70e..4fa048e 100644 --- a/tests/Unit/Domain/Service/AbstractHookTest.php +++ b/tests/Unit/Domain/Service/AbstractHookTest.php @@ -30,6 +30,27 @@ public function testResolveCallback(string $hook, callable|string|array $callbac AbstractHook::clearReflectionCache(); }); +final class InvokableProbe +{ + public function __invoke(string $a, int $b): void {} + + public static function staticProbe(string $a, int $b, int $c): void {} +} + +describe('Argument detection', function (): void { + it('counts the parameters of an invokable object through its __invoke()', function (): void { + $this->hook->add('init', new InvokableProbe); + + expect($this->hook->callbacks('init')[0]['args'])->toBe(2); + }); + + it('counts the parameters of a static method written Class::method', function (): void { + $this->hook->add('init', InvokableProbe::class.'::staticProbe'); + + expect($this->hook->callbacks('init')[0]['args'])->toBe(3); + }); +}); + describe('P0 — Permissive callback resolution', function (): void { it('stores a string callback that is not yet callable without throwing', function (): void { $this->hook->add('init', 'my_future_function');