Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to PHPDoc an anonymous function when passed as an argument?

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.

like image 575
henrywright Avatar asked Dec 30 '15 19:12

henrywright


1 Answers

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; };
like image 183
bishop Avatar answered Nov 11 '22 17:11

bishop