phptype-hintingphpstan

PHPStan keeps saying "No value type specified in iterable type" but it is specified


When using php type declarations, PHPStan seems to think that I should specify the array members in both the PHPDoc as well as the actual declaration, but correct me if I'm wrong PHP (at least PHP 7) doesn't support array member declarations. Here's an example:

1: <?php
2: 
3: class Foo
4: {
5:     /*
6:      * @var array<string>
7:      */
8:     private array $foo;
9: }

This results in:

8   Property Foo::$foo type has no value type specified in iterable type array.

Notice, the line referred to is 8, and the PHPDoc does specify the array members. But saying

...
8:     private array<string> $foo;
...

is not valid PHP.

If I omit the array declaration and juts keep the PHPDoc, then PHPStan emits:

8   Property Foo::$foo has no type specified.

How can I get around this warning? I think I've followed the instructions in https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type


Solution

  • Based on your output, I think you're missing a *, so you have a comment rather than a docstring declaration

    <?php   
    class Foo {
        /**
         * @var array<string>
         */
        private array $foo; }