Reputation: 10240
I usually document my PHP function definitions with a phpDocumentor-style docblock:
/**
* This is a summary of this function.
*
* @since 1.0.0
*
* @return int
*/
function my_return_something() {
return 20;
}
My question is, how should I document an anonymous function? For example:
$length = function() {
return 20;
};
Upvotes: 1
Views: 1435
Reputation: 37365
You should not document the anonymous function, instead you should document a variable. It will look like:
/** @var \Closure $length */
$length = function() {
return 20;
};
as tag @var
is applicable for regular variables as well. That, however, normally isn't needed: as PHPDoc is intended to be used by IDE and most of IDE-s will be able to get that your variable is a closure right because you have your assignment.
If you will want to pass that variable somewhere - the you may hint in in the accepting method/function as a callable
or \Closure
explicitly, even without PHPDoc (but you also can use PHPDoc as well)
For details about @var
tag, see the documentation. Also note, that the closure type or callback will have nothing to do with the value type which is returned by that callback - it is obvious that you're declaring your callback, not calling it (that being said: I assume "length" isn't a good name for a callback as it cause confusion. Use some action-related name instead, like "lengthGetter" or so)
Upvotes: 3