1

how can I describe the exceptions thrown from callable in phpDoc? For example I have:

/**
 * @param callable(int): bool
 */
function (callable $foo, int $number): ?bool
{
    try {
        return $foo($number);
    } catch (InvalidNumberException) {
        return null;
    }
}

And I want something like this:

/**
 * @param callable(int): bool throws InvalidNumberException
 */
function (callable $foo, int $number): ?bool
{
    try {
        return $foo($number);
    } catch (InvalidNumberException) {
        return null;
    }
}
3
  • This is not the correct place to document the callable. You might declare an interface that the callable needs to adhere to, and then document that. Commented Oct 26, 2023 at 14:06
  • To document exceptions for a callable in PHPDoc, you can't specify the exception in the @param line for the callable. Instead, you should describe the potential exception in the general description of the function or the parameter. Commented Oct 26, 2023 at 19:56
  • I'm trying to limit the signature of callbacks. I need phpstan to show an error when I try to pass functions without the exception specified in the signature. Commented Nov 12, 2023 at 11:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.