diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bb6830..bb60195 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to `pollora/hook` are documented in this file. ### Fixed +- An asynchronous handler whose descriptor names a private or protected method was retried as many times as `tries` allowed, each attempt failing the same way with `The handler of … is not callable`. Resolving the descriptor now fails at once with an `UnresolvableHandler` that says the method is not public. - 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 diff --git a/src/Async/CallableDescriptor.php b/src/Async/CallableDescriptor.php index a048fb1..3e9972a 100644 --- a/src/Async/CallableDescriptor.php +++ b/src/Async/CallableDescriptor.php @@ -127,7 +127,7 @@ public function resolve(string $descriptor): callable } if ((new \ReflectionMethod($className, $method))->isStatic()) { - return [$className, $method]; + return $this->callable([$className, $method], $className, $method); } try { @@ -138,7 +138,24 @@ public function resolve(string $descriptor): callable throw UnresolvableHandler::notInstantiable($className, $throwable); } - return [$instance, $method]; + return $this->callable([$instance, $method], $className, $method); + } + + /** + * A method that exists but that nothing outside the class can call is no handler: failing here is final, + * where letting the call fail would send the execution through every retry for nothing. + * + * @param array{0: class-string|object, 1: string} $handler + * + * @throws UnresolvableHandler When the method cannot be called from here + */ + private function callable(array $handler, string $className, string $method): callable + { + if (! is_callable($handler)) { + throw UnresolvableHandler::notPublicMethod($className, $method); + } + + return $handler; } private function describeClosure(\Closure $closure): string diff --git a/src/Async/Exceptions/UnresolvableHandler.php b/src/Async/Exceptions/UnresolvableHandler.php index 8ea7ef4..09b666a 100644 --- a/src/Async/Exceptions/UnresolvableHandler.php +++ b/src/Async/Exceptions/UnresolvableHandler.php @@ -68,6 +68,11 @@ public static function missingMethod(string $className, string $method): self return new self(sprintf("The asynchronous handler method '%s::%s()' no longer exists.", $className, $method)); } + public static function notPublicMethod(string $className, string $method): self + { + return new self(sprintf("The asynchronous handler method '%s::%s()' is not public: nothing outside the class can call it.", $className, $method)); + } + public static function notInstantiable(string $className, \Throwable $previous): self { return new self(sprintf("The asynchronous handler class '%s' could not be instantiated: %s", $className, $previous->getMessage()), 0, $previous); diff --git a/tests/Unit/Async/CallableDescriptorTest.php b/tests/Unit/Async/CallableDescriptorTest.php index ea0835a..7eb756a 100644 --- a/tests/Unit/Async/CallableDescriptorTest.php +++ b/tests/Unit/Async/CallableDescriptorTest.php @@ -12,6 +12,14 @@ function pollora_test_async_handler(int $postId): void {} } +/** + * A class whose method exists but cannot be called from outside. + */ +final class HiddenMethodHandler +{ + protected function hidden(): void {} +} + beforeEach(function (): void { $this->descriptor = new CallableDescriptor; }); @@ -165,6 +173,11 @@ public function resolve(string $className): object 'method' => [InvoiceHandler::class.'@removed', 'InvoiceHandler::removed()'], ]); + it('fails once, with a clear message, when the method is not public, instead of leaving a retry to find out', function (): void { + expect(fn () => $this->descriptor->resolve(HiddenMethodHandler::class.'@hidden')) + ->toThrow(UnresolvableHandler::class, 'HiddenMethodHandler::hidden()\' is not public'); + }); + it('rejects a malformed descriptor', function (string $descriptor): void { expect(fn () => $this->descriptor->resolve($descriptor))->toThrow(UnresolvableHandler::class, 'not a valid handler descriptor'); })->with(['system("id")', 'Foo@bar@baz', '@handle', 'Foo@']);