#41756 closed enhancement (fixed)
Document arrays of a known type as such
Reported by: | johnbillion | Owned by: | janak007 |
---|---|---|---|
Milestone: | 5.4 | Priority: | low |
Severity: | normal | Version: | |
Component: | General | Keywords: | |
Focuses: | docs | Cc: |
Description
A parameter that accepts an array (or avariable that contains an array) usually has its type documented with the array
keyword:
@param array $args Arguments.
phpDocumentor and IDEs such as PhpStorm support a secondary notation for arrays which specifies the type of the elements that the array contains. Example:
@param string[] $args Arguments.
This form of notation makes it clearer which type the application expects in an array's elements. WordPress core should switch to documenting arrays of known types to this format.
Ref: https://docs.phpdoc.org/references/phpdoc/types.html#arrays
Addendum: phpDocumentor also supports documenting arrays containing mixed types with the following syntax, but this could get messy:
@param (int|string)[] $args Arguments.
Attachments (6)
Change History (28)
#2
@
7 years ago
I think this change should definitely happen!
Does phpdoc-parser support "typed arrays", or will it need to be modified in conjunction with this, in order for the CodeRef to work correctly?
#3
@
7 years ago
The draft PSR-5 (which phpDocumentor generally follows) also allows for an inline PHPDoc for arrays contained mixed type items, or there's mixed[]
.
So long as the parser can (be made to) understand it, and that popular IDEs understand the formats (or it at least has no detrimental effects), then +1.
#4
@
7 years ago
- Keywords dev-feedback added; 2nd-opinion removed
@GaryJ Are you in a position to test the docs parser with this format for docs? Or do you know someone who is? I've unfortunately not been able to get the docs parser to work at all on my local setup (the generated JSON file is always empty).
#5
@
7 years ago
Documenting my process, so others can verify.
- Checkout https://github.com/WordPress/phpdoc-parser.git into the plugins directory of a fresh WP install.
- Active plugin - need PHP 5.4 or later. Open PRs suggest may be issues with running PHP 7.0.
- Pulled down theme:
svn checkout https://meta.svn.wordpress.org/sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/ wp-content/themes/wporg-developer
- Activated theme, and replaced references to
WPORGPATH
files with header and footer markup from Twenty Seventeen theme. - Ran
wp parser create wp-admin/includes/bookmark.php --user=admin
- file chosen as it was one of the files in the patch. - Front end rendered as:
Note that $link_categories
is an (array)
.
- Manually changed the docs for
wp_set_link_cats()
as per patch. - Front end now rendered as:
Note that $link_categories
is an (int[])
.
- Realised you wanted the JSON file. Ran
wp parser export wp-admin/includes/bookmark.php --user=admin
- Ran
cat phpdoc.json
and relevant JSON section is:{ "name": "param", "content": "Array of link category IDs to add the link to.", "types": [ "int[]" ], "variable": "$link_categories" }
- Repeated for an
array
inbulk_post_updated_messages
filter hook, and got an equivalant result:
- Repeated with
@param string|array $body_classes
fromsetup_config_display_header()
, but changed it tostring|string[]
. Resulted in:
{ "name": "param", "content": "", "types": [ "string", "string[]" ], "variable": "$body_classes" }
- Just for fun, repeated 12. but with
string|foobar[]
. Resulted in:{ "name": "param", "content": "", "types": [ "string", "\\foobar[]" ], "variable": "$body_classes" }
- Tried the exotic format in the OP, on
wp_generate_attachment_metadata
filter. Result:{ "name": "param", "content": "An array of attachment meta data.", "types": [ "\\(int", "\\string)[]" ], "variable": "$metadata" },
Conclusion
- The basic alternative format seems to be accepted without throwing errors.
- The exotic format (array-expression) seems to be thrown off by the non-recognised / non-valid keywords ("foobar", or exactly "(int" or exactly "string)[]").
#6
@
7 years ago
- Keywords needs-patch added; dev-feedback removed
Thanks for that, Gary. I think we can go ahead and begin switching over some known parameters.
#8
@
7 years ago
- Owner set to janak007
- Status changed from new to assigned
Assigning to mark the good-first-bug
as "claimed".
#11
@
7 years ago
- Keywords needs-patch added; good-first-bug has-patch removed
Thanks for the patch, @janak007! A few of the instances that you changed were incorrect, so I corrected them or left them out. The changes will appear on the developer.wordpress.org site once WordPress 5.0 is released.
41756.diff is a patch of a handful of occurrences.