Search code examples
phpstorm

How to type function's return to be an array of instances of a dynamic class via .phpstorm.meta.php file?


With .phpstorm.meta.php you can overwrite the return type of your class methods. For example:

// .phpstorm.meta.php 

namespace PHPSTORM_META
{
    $STATIC_METHOD_TYPES = [
      \Acme\Node::closest('') => [
         "" == "@",
      ],
    ];
}

The example above lets PhpStorm understand the return of $node->closest(SomeNodeType::class) is an instance of SomeNodeType.

However, my question is how to make PhpStorm understand that a function returns an array of instances of a given type.

For example: $node->findAllOfType(SomeNodeType::class) would return an array of SomeNodeType instances (normally one would use SomeNodeType[] if it would not be dynamic).

Attempts to use "@[]" or "@Iterator" instead of "@", in the above code example, failed.


Solution

  • That's simply not supported ... unless you can try and list all possible types manually instead of using single generic rule (which may work in theory -- sorry, have not tried it myself).

    https://youtrack.jetbrains.com/issue/WI-27832 -- watch that and related tickets (star/vote/comment) to get notified on any progress.