Problem/Motivation
In #3161129: [PP-1] Fix phpdoc return type for RouteProvider::getAllRoutes() we're trying to document the @return value of a function that returns an \Iterator. For good reason, we like to be specific. Apparently, phpdoc now supports a syntax for defining the type of the values (and optionally, keys) in an an array or Iterator. It's something like this:
@return \Iterator<string, \Symfony\Component\Routing\Route>
However, we don't currently do this anywhere, and Coder doesn't understand this syntax yet (see #3347758: [PP-1] Support the recommended phpdoc syntax for documenting Iterator return types).
So we need to discuss if this is the right thing, decide on the appropriate style for it (e.g. do we put the space after the comma?), etc.
Steps to reproduce
Proposed resolution
When documenting collections (eg Iterator and friends), allow using the <keyType, valueType> syntax to define the types of the keys and values.
If the keys don't matter, you can just use the valueType like so:
<valueType>
Remaining tasks
User interface changes
API changes
Data model changes
Issue fork coding_standards-3348310
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
andypostIt works for any collection, see related links
- https://docs.phpdoc.org/classes/phpDocumentor-Descriptor-Collection.html
- https://phpstan.org/blog/generics-in-php-using-phpdocs
- https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iter...
- https://github.com/JetBrains/phpstorm-stubs/commit/89f6911ddcc89dd6497ab...
Comment #3
dwwThanks, good point! Minor updates to title and summary to capture that.
Comment #4
drunken monkeyYes, please!
I’d definitely put a space after the comma. That’s also the style used in all of @andypost’s links.
Comment #5
mglamanIs this related to #3253472: Support advanced PHPStan data types (general, arrays)?
Comment #6
mondrakeand to #3309010: Support PHPDoc Types in @param @var @return annotations...
Hopefully one day we can start using them and stop opening issues to use them :)
In any case we can use
@phpstan-returnannotations already now without sniff failures happening. See #2664570: Move Attribute classes under Drupal\Component and MR!141.Comment #7
andypostComment #8
dwwYeah, this is duplicate. 😅 I hadn't found the related issues when searching. Moving to #3309010: Support PHPDoc Types in @param @var @return annotations now. Thanks, everyone!