Hinting keys a certain array has / must have #4763
Csardelacal
started this conversation in
General
Replies: 1 comment
-
Hi, thank you for your kind words! You definitely want the Array shapes feature: https://phpstan.org/writing-php-code/phpdoc-types#array-shapes |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey there, first of all, thanks for the amazing work with phpstan. I really appreciate the tool. I just have been unable to figure out how to tell it that an array has, must have, or should have, certain keys.
I have seen that, for example, when I access a bad key in the
parse_url
function, it will tell me that the key I was trying to read a key that the array will not set. How would I go about something like this:I would like to be able to tell the application that I return an array that has keys
a
andb
defined. And if I try to execute something likereturns_array()['c']
I would expect phpstan to report that.The same thing goes for a structured array that I pass into a function. I'll take a constructed example for a db query, let's say our application expects to have a database it can invoke like this:
If I use the
string[][][]
hint, phpstan will rightfully warn me that a use of$query['table']
would yield an array. I'm unsure how to explain this to phpstan.Beta Was this translation helpful? Give feedback.
All reactions