Search code examples
phpgithub-actionsstatic-code-analysisphp-7.4phpstan

"return type has no value type specified in iterable type array." error does not reflect reality?


I am on php 7.4 and using phpstan action in github (latest) actions-x/phpstan@v1
I am getting the following error

return type has no value type specified in iterable type array

on the following method:

class A{
    /**
     * @return array
     */
    protected function get_next_bl():array{
        return [
            ["some string",[]]
        ];
    }
}

It is not clear to me what is wrong here.


Solution

  • Finally I decided to install PHPStan locally to dig deeper.
    When installed locally and run, You will get better error messages with fix suggestions.
    PHPStan expects in the phpdoc that you tell it the possible types of the members of the array. Since the array in question is an array of arrays, the phpdoce syntax is:

    @return array<array>