Doc comments for closures in an array?

Hi,

let’s imagine I have an array of Closures, such as this:

$closure = array(
   function() { /* do something */ },
   function() { /* do something else */ },
   function() { /* do yet another thing */ },
);



Now, I want to loop over the array:

foreach ($closure as $c) {
   $c();
}



PhpStorm will mark the $c() as a warning (“Function name must be a string, Closure or …”). How can I make PhpStorm recognize that the array DOES contain closures? I tried something like …

/**
* @var Closure[]
*/
$closure = array(


…, but without success.

Carsten
2 comments

Hi Carsten,

You have incorrect (to be precise, incomplete) syntax for @var PHPDoc for local variable. This syntax is correct for class variable, but for local variable you have to include variable name as well, e.g:

/**
* @var Closure[] $closure
*/

or

/**
* @var $closure
Closure[]
*/


Another possible approach is to provide type hint (same way as above) but for $c variable, e.g.

/** @var Closure $c */
foreach ($closure as $c) {
    $c();
}

0

Thanks, that solved the problem.

Carsten

0

Please sign in to leave a comment.