0

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

1
  • 1
    That isn't a docblock but a regular comment. Commented Jun 19, 2024 at 6:48

1 Answer 1

1

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; }
Sign up to request clarification or add additional context in comments.

1 Comment

Doh! Thank you, I had overlooked this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.