How should I document an anonymous function when it's passed as an argument? For example:
// Call my_function(), passing 2 arguments.
my_function( 'foo', function() {
// Body of the anon function I'd like to document.
} );
Thanks in advance.
How should I document an anonymous function when it's passed as an argument? For example:
// Call my_function(), passing 2 arguments.
my_function( 'foo', function() {
// Body of the anon function I'd like to document.
} );
Thanks in advance.
To document that a function accepts a Closure, I'd suggest callable:
/**
* Do something.
* @param callable $code
*/
function foo(callable $code) {
}
Regarding the commentary, PHPDoc uses DocBlocks, which the PHP engine Tokenizer only recognizes above formal definitions. Thus, PHPDoc will not see this:
/**
* My closure. PHPDoc will *not* parse this, because it's not a formal definition.
* @param string $name
*/
$closure = function ($name) { return $name; };
This has worked for me so far:
interface CustomCallback
{
/**
* @return string
*/
public function __invoke();
}
/**
* @param string $a
* @param CustomCallback $b
* @return void
*/
my_function($a, $b) {
}
This tells us that the second parameter of my_function
expects something that implements the CustomCallback
interface. By looking in the CustomCallback interface, we can see that it's callable because of the __invoke method. Then by looking at the __invoke method's documentation, we see what parameters it expects and the return type.