I have this:
/**
* @var string[]
*/
#[ORMColumn(type: 'json', nullable: true)]
private array $roles = [];
How can I change string[]
to php8 attribute ?
Something like #[Array(string)]
I am working with Symfony and Doctrine, and I am trying to fix phpstan errors at level 9
2
Answers
Concerning symfony constraints. You could use the
All
constraintWhich mean you could remove the phpdoc mentioning
@var string[]
that would be redundant.Attributes are backed by classes, and core has provided very few attributes so far, and none specific to typing of arrays (or typing of anything for that matter). If core ever decides to support a typed-array syntax, it would probably come with generics, and I would assume they would use the existing type declaration system.
To the best of my knowledge, neither Symfony nor Doctrine have any attributes for this either. The closest might be something from Symfony’s assert.
But no matter what, at this time you will need to have code backing the attribute. That code will either need to be custom-written by you, brought in by a third-party library, or an IDE. That code will also need to be specifically supported by whichever static analyzer tool you are using.
As to this last bit, PHPStan at least appears to be completely fine with the docblock syntax which is still the recommended path, and on Level 9 I’m not seeing any errors: https://phpstan.org/r/ed9f4718-315d-4034-aee8-186ce70f9282