Consistent use of (psalm) phpdoc array annotation syntax #6832

Open
opened 2026-01-22 15:39:46 +01:00 by admin · 6 comments
Owner

Originally created by @simonberger on GitHub (Sep 20, 2021).

My last state of information was that only a notation like string[] should be used for annotations like param and return.
I was about to request a change in #9024 but then noticed that those few lines of changed lines revealed three different types of array annotations in the old code:

  • array<string>
  • array<string, string>
  • string[]

It would be great to have a rule which array syntax is allowed for standard phpdoc annotation and which needs to be put into the psalm variants for more detailed description.
Personally I would prefer to just allow array<string, string>- kind syntax everywhere but then we should move them all out of the psalm variants. Otherwise some sort of CI check would be helpful to get some consistency.

As the first and third syntax of the example are the same it would be great to allow just one but that's maybe too pedantic.

Originally created by @simonberger on GitHub (Sep 20, 2021). My last state of information was that only a notation like `string[]` should be used for annotations like `param` and `return`. I was about to request a change in #9024 but then noticed that those few lines of changed lines revealed three different types of array annotations in the old code: - `array<string>` - `array<string, string>` - `string[]` It would be great to have a rule which array syntax is allowed for standard phpdoc annotation and which needs to be put into the psalm variants for more detailed description. Personally I would prefer to just allow `array<string, string>`- kind syntax everywhere but then we should move them all out of the psalm variants. Otherwise some sort of CI check would be helpful to get some consistency. As the first and third syntax of the example are the same it would be great to allow just one but that's maybe too pedantic.
Author
Owner

@phansys commented on GitHub (Sep 20, 2021):

IMO, the syntax array<type, type> is useful when we must tell the type used in the keys. For the rest of the cases, I think the type[] syntax is simpler.

@phansys commented on GitHub (Sep 20, 2021): IMO, the syntax `array<type, type>` is useful when we must tell the type used in the keys. For the rest of the cases, I think the `type[]` syntax is simpler.
Author
Owner

@simonberger commented on GitHub (Sep 21, 2021):

@phansys array<type, type> has more detail this is not really questionable. The issue is about what is allowed to be used with standard PHPdoc annotations and when psalm-annotations have to be used.

@simonberger commented on GitHub (Sep 21, 2021): @phansys `array<type, type>` has more detail this is not really questionable. The issue is about what is allowed to be used with standard PHPdoc annotations and when psalm-annotations have to be used.
Author
Owner

@greg0ire commented on GitHub (Sep 21, 2021):

IMO it should be array<type, type> or list<type> if the type inside the list doesn't matter. It's kinda unfriendly to consumers that pass the result of array_filter, but using list shows that we already looked into the type of the keys, and that it shouldn't matter. IMO, any time we would be using a list in another language that supports them (such as Python), we should use list here. So I'd say, let's avoid type[] entirely.

@greg0ire commented on GitHub (Sep 21, 2021): IMO it should be `array<type, type>` or `list<type>` if the type inside the list doesn't matter. It's kinda unfriendly to consumers that pass the result of `array_filter`, but using `list` shows that we already looked into the type of the keys, and that it shouldn't matter. IMO, any time we would be using a list in another language that supports them (such as Python), we should use `list` here. So I'd say, let's avoid `type[]` entirely.
Author
Owner

@stof commented on GitHub (Oct 21, 2021):

list<type> does not mean that you don't care about the type of keys. It means you actually want a list, i.e. sequencial integer keys from 0 to $count - 1 (see array_is_list in PHP 8.1). Psalm actually enforces that meaning for list (phpstan just treats it as an alias of array<int, type> for now)

@stof commented on GitHub (Oct 21, 2021): `list<type>` does not mean that you don't care about the type of keys. It means you actually want a list, i.e. sequencial integer keys from `0` to `$count - 1` (see `array_is_list` in PHP 8.1). Psalm actually enforces that meaning for `list` (phpstan just treats it as an alias of `array<int, type>` for now)
Author
Owner

@greg0ire commented on GitHub (Oct 21, 2021):

@stof yeah but conceptually, it's called a list because that's exactly the data type that is meant (not required, meant) to be passed here. If you use the PHP syntax to create a list, you will end up with an array that happens to keys in a sequence.

So while on the one hand, using lists add an unwanted requirement, on the other hand, it lets the user know that they should not expect anything special to happen if they pass a map instead, and might even let them know they have a bug in their code because of that expectation.

@greg0ire commented on GitHub (Oct 21, 2021): @stof yeah but conceptually, it's called a list because that's exactly the data type that is meant (not required, meant) to be passed here. If you use the PHP syntax to create a list, you will end up with an array that happens to keys in a sequence. So while on the one hand, using lists add an unwanted requirement, on the other hand, it lets the user know that they should not expect anything special to happen if they pass a map instead, and might even let them know they have a bug in their code because of that expectation.
Author
Owner

@stof commented on GitHub (Oct 22, 2021):

@greg0ire if we really don't care about the keys (some places do care), I'd rather use type[] than list<type>

@stof commented on GitHub (Oct 22, 2021): @greg0ire if we really don't care about the keys (some places do care), I'd rather use `type[]` than `list<type>`
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6832