Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot specify array type #3604

Open
smileBeda opened this issue Oct 12, 2023 · 1 comment
Open

Cannot specify array type #3604

smileBeda opened this issue Oct 12, 2023 · 1 comment

Comments

@smileBeda
Copy link

Seems to have been described once at https://www.phpdoc.org/docs/latest/references/phpdoc/types.html#arrays, according to https://stackoverflow.com/questions/17042613/how-to-document-an-array-of-type:
to define an array type (string) do $return array string[]

That does nothing: the doc still says method_name() : array<string|int, string>

In here it is stated to say @return array [type]
Yet as well that makes no difference.

All difference it makes is that the following changes:

Return values

array<string|int, mixed> —
string

But that is not what I want to modify, I want to modify the actual method entry, which keeps saying method_name() : array<string|int, mixed>

Is that because it reads the return type off the return type declaration instead of the docblock return flag?

@phpdoc-bot phpdoc-bot added this to Needs triage in phpDoc issue triage board Oct 12, 2023
@jaapio
Copy link
Member

jaapio commented Oct 12, 2023

Thanks for reaching out to us. We changed quite some things in the past years, but the documentation is a bit behind on the types. My answer will be the basis to update the documentation.

phpDocumentor supports 3 variants of arrays.

  • simple arrays, which are the old existing string[] this can be combined to document more complex data types like this string|int[]. Which defines an array of integers and strings. you can go crazy with this, as we do support brackets and combining operators. e.g. (string|(Interface1&Interface2))[] which states that the array value is a string or an object implementing Interface1 and Interface2 . It's up to you how far you will go with this :-).
  • complex arrays, are the array types where you can specify the index type of the array and the type of all indexes. Right now the index type must be int, string or a combination of both string|int defining the index type is mixed. Like with the simple array's you can go crazy with the value type definition.
  • array shapes, this one is fairly new. People have been looking for years to define the content of an array, until the static analysers in php (phpstan, psalm) defined a format which is now also supported by phpDoc. An array shape is written like this:
array { 
   'myKey' : string, 
   'otherKey': int
}

Again you can go crazy with the type definition of each index. Which can be like any other type. The example above defines that the array has 2 indexes myKey and otherKey with their type definition.


During the rendering of the array in the docs we lose some information, because the simple and complex variant are normalized to an single array type. Which results in the rendering of the complex variant, as that fits best in all cases. e.g
string[] will be rendered as array<int|string, string> right now we do not have the information of the original format, so we can not restore this easily.

I hope this answers your question?


The stackoverflow post you linked is mosty correct except for the array shape part.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
phpDoc issue triage board
  
Needs triage
Development

No branches or pull requests

2 participants