Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
683 views
in Technique[技术] by (71.8m points)

php - "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.

question from:https://stackoverflow.com/questions/65832692/return-type-has-no-value-type-specified-in-iterable-type-array-error-does-not

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...