Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Currently we use the following kind of code to document the type of a parameter/return type:
@param array $name
Some information about the content.
For certain code though it would be really helpful to directly document the type of variable in the array.
The suggested syntax would be
@param \Drupal\node\NodeInterface[] $nodes
An array of nodes
This syntax is suggested by http://www.phpdoc.org/docs/latest/for-users/types.html#arrays and at least supported by PhpStorm.
Comments
Comment #1
xjmComment #2
jhodgdonUm. If something is an array, it's an array. If it's a Node, it's an object and not an array, right?
Comment #3
superspring CreditAttribution: superspring commented@jhodgdon, I think they're talking about what is inside the array.
I do like this idea, in some cases it would give far more detail in terms of what type of array is expected/produced.
I think it would be less useful when stdClasses are used but suplimenting the documentation with expected keys would be helpful.
I like
@param array[Drupal\node\Node] $nodes
the most although suplimenting this with any non-standard keys (such as a string index to the map) would give even more information, when available.Comment #4
jhodgdonOh, you mean it's an array of Node objects?
I would like to see evidence that IDEs recognize syntax like @param array[\Drupal\node\Node] before we even think about adopting it. And note that all namespaces should start with \ in documentation too... And is this standard in other documentation (PHPDoc, JavaDoc, etc.?)
As far as I am concerned, this syntax was far from obvious to human readers, so I would be against adopting it unless it is industry-standard and/or recognized by several major IDEs. If it's just for human readers, maybe we can think of some better way to write it that would be more intuitive for human readers?
Comment #5
dawehnerAccording to http://stackoverflow.com/questions/9132062/is-there-a-way-for-phpdoc-to-... we should better use TypeOfObject[]
as it is part of phpdoc.
Comment #6
jhodgdonOK. Maybe update the issue summary and set to needs review so we can get input?
Comment #6.0
jhodgdonUpdated issue summary.
Comment #7
dawehnerUpdated the issue summary.
Comment #8
jhodgdonThanks! So now we have a concrete proposal. Let's see what people think.
Comment #9
andypost+1 here, already using that in
#2092265: Fix type-hinting for FieldInfo methods
and would be awesome for #2083895: Clean-up comment module doc blocks
Comment #10
tim.plunkettWhy is this a question? If it is an array of objects of a known interface, you typehint it as such. Core already does this, this is a non-issue.
Comment #11
tim.plunkettHere are examples from our codebase.
Comment #12
tim.plunkettComment #13
jhodgdonIt seems that this proposal is (a) in use in Core already and (b) industry standard. Should be non-controversial. Let's leave this at RTBC for now and in about a week I'll update node/1354 (docs standards) to include it.
I don't think we need to do a wholesale core patch at this time once the standard is officially adopted.
Comment #14
webchickIt's been a week. :)
Comment #15
dawehnerThis is also a the standard added in PSR-5: https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpd... (Search for 'The value represented by "Type" can be an array.')
Comment #16
jhodgdonOkey dokey! I edited node/1354:
https://drupal.org/coding-standards/docs#types
Let me know if you think it's clear enough.
Comment #17
dawehnerThank you very much!
Comment #18.0
(not verified) CreditAttribution: commentedupdated