From e66f24b1527d35b6705c6fefdce01b0959d0e891 Mon Sep 17 00:00:00 2001 From: Pollora maintainer Date: Wed, 25 Nov 2026 12:19:25 +0100 Subject: [PATCH] fix(async): fail at once when a handler's method is not callable from outside --- CHANGELOG.md | 4 ++++ src/Async/CallableDescriptor.php | 21 ++++++++++++++++++-- src/Async/Exceptions/UnresolvableHandler.php | 5 +++++ tests/Unit/Async/CallableDescriptorTest.php | 13 ++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca2d826..0c1d47c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to `pollora/hook` are documented in this file. ## [Unreleased] +### 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. + ## [2.0.2] - 2026-11-25 ### Fixed 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@']);