Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/Domain/Service/AbstractHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
21 changes: 21 additions & 0 deletions tests/Unit/Domain/Service/AbstractHookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Loading