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.
Drupal_Sniffs_Commenting_InlineCommentSniff
has a test for inline doc block comments, which it asserts are not allowed. It turns out, however, that Drupal core now uses them all over the place (see attached inline-doc-blocks-in-core.txt), and @webchick, for one, has explicitly asserted the validity of the pattern in at least one place. It's causing confusion in #1518116: [meta] Make Core pass Coder Review, and it seems like it should be removed. Patch to follow.
Comment | File | Size | Author |
---|---|---|---|
#1 | inline-doc-blocks-in-core.txt | 23.09 KB | TravisCarden |
#1 | coder-inline-doc-blocks-2236393-1.patch | 2.56 KB | TravisCarden |
Comments
Comment #1
TravisCarden CreditAttribution: TravisCarden commentedComment #2
klausiSo it looks like we just want to allow /** @var ... */ inline comments that are used for type inference for IDEs, all other inline comments should still use the "//" notation.
This should be fixed in 8.x-2.x first and can then be backported. And this should come with a test case in good.php.
Comment #3
thursday_bw CreditAttribution: thursday_bw commentedI can't see anywhere where in line doc comments are disallowed by the Drupal coding standards.
But rather see descriptions of the standards that implies their necessity. Not just for declaring /** @var ... */ comments.
Referring to https://www.drupal.org/coding-standards/docs#drupal
'(code being document, such as function declaration, class, etc..)'
Etc being the word that implies other code may be documented.
Referring to https://www.drupal.org/coding-standards/docs#inline
Given that "using the @todo tag in docblocks." links to https://www.drupal.org/coding-standards/docs#todo This suggests that if we are going to place a @todo in inline comments then, admittedly contrary to the given example, the @todo must be in an inline doc comment or it will be ignored.
Nowhere in this documentation are in line doc comments explicitly disallowed.
It makes much more sense that if something is not explicitly disallowed then it is in fact allowed.
Comment #4
klausiThe point is that doc blocks are used for blocks before functions, classes etc.
from https://www.drupal.org/node/1354#inline
So in function bodies using // comments is preferred in Drupal. If you find any other usage besides /** @var ... let me know, otherwise I think we can close this since /** @var ... is an exception in the rules already.
And @todo comments work just fine after // ?
Comment #5
cburschkaIf @var is an exception, then why does the code-sniffer not allow it?
I'm currently having trouble seeing how my code can fulfil both this and normal PHP inspections. Children of EntityAccessControlHandler::checkAccess must accept an EntityInterface instead of my entity class (because PHP doesn't have generics). The @var comment is essential to be able to access my entity's methods at this point. The only way to do that and pass the code inspection is to turn off the code-sniffer, which rather defeats the point.
Comment #6
klausiCoder allows /** @var just fine. Could you test with the most recent Coder release and post a code snippet that throws the error for you?
Comment #7
klausiPlease reopen if this is still an issue.
Comment #8
jonathanshawUsing phpcs 2.8.1 and coder 8.12 I'm still getting this.
Triggered by contrib/drupal/core/lib/Drupal/Core/Datetime/Element/Datetime.php
Comment #9
gappleWith PHPCS 2.9.1 and Coder 8.2.12
This is allowed:
--
But this throws a warning:
PHPStom autocompleted the multi-line format when type hinting the key and value variables for a foreach loop.
Comment #10
joelpittet/** @var string $var */
isn't allowed in Coder 8.3 and PHPCS 3.4.0Comment #12
klausiI relaxed the Coder check now, so any inline comment that has @var in any form is allowed. That gives us flexibility until we decide @var formatting standards in inline comments.
Thank you very much for all your examples, I think they are all covered.