Any way to type-hint the result of a PHP-closure?

Hi,

unfortunately I could not find any informations on type-hinting a result of a clousre call, so I would get autocomplition for the returned object. An example:

$myClosure = function($cnt) {
  $result = array();
  for ($i = 0; $i < $cnt; $i++) {
      $result[] = new MyObject($i);
    }
    return $result;
}

foreach ($myClosure(5) as $myObject) {
    otherFunction($myObject->getValue());
}


The only way I found to get autocomplition for $myObject was to insert /* @var $myObject MyObject */ after the opening bracket of the foreach.

Is there any way to tell PhpStorm that $myClosure will return MyObject[] and requires an int as parameter?

3 comments
Comment actions Permalink

Hi there,

Good question.

As far as I can see there is nothing even in PHPDoc for this -- https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc.md

You can describe parameters (using standard @param -- so inside the closure you will have proper type hint for $cnt parameter) but the actual $myClosure ... -- it treated as Closure class automatically

Related tickets:


I may only suggest to submit the ticket to the Issue Tracker + create an Issue (ask question / feature request ticket) on actual PHPDoc on "How to define it?".

0
Comment actions Permalink

Hi Andriy,

thank you for your anwser! Indeed the @param-tag is interpreted. The lag of @return-support is strange and I hope that the feature-request will be implemented in next future.

I don't think, that this is an issue for PHPDoc - this standard does not cover type-hinting and you are not going to generate a documentatiaion for closures because they are not visible outside of you methods.

0
Comment actions Permalink

I don't think, that this is an issue for PHPDoc - this standard does not cover type-hinting and you are not going to generate a documentatiaion for closures because they are not visible outside of you methods

Yes and No. Yes, it's mainly PhpStorm issue (for the reason you have mentioned) and No -- PhpStorm tries to follow PHPDoc for PHP stuff and not introduce custom tags / different meanings of standard tags. Custom tags only used for non-PHP related stuff (like custom language injection, inspection suppression etc).

In any case: asking what creators and/or users of PHPDoc think about what can be used in such case will not harm at all.

----

https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc.md#724-var

Another example is to document the variable in a foreach explicitly; many IDEs use this information to help you with auto-completion:

 
/** @var \Sqlite3 $sqlite */
foreach($connections as $sqlite) {
    // there should be no docblock here
    $sqlite->open('/my/database/path');
    <...>
}


https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc.md#valid-class-name

Points 13, 14 & 15 were introduced because of IDEs (PhpStorm in particular) -- originally it was meant to be explicit class names only.

0

Please sign in to leave a comment.