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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 19 additions & 2 deletions src/Async/CallableDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/Async/Exceptions/UnresolvableHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions tests/Unit/Async/CallableDescriptorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down Expand Up @@ -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@']);
Expand Down
Loading