Problem/Motivation
Drupal Coding standards state that @inheritdoc should be the only line in a docblock comment.
https://www.drupal.org/docs/develop/coding-standards/api-documentation-a...
When @inheritdoc is entered, coder produces:
----------------------------------------------------------
32 | ERROR | Missing short description in doc comment
| | (Drupal.Commenting.DocComment.MissingShort)
----------------------------------------------------------
This is becuse @inheritdoc is interpreted as a header block tag. It requires { } around it to treated as a proper inheritdoc.
Proposed resolution
Give a different message if we discover @inheritdoc but without the enclosing { }
For example
FOUND 1 ERROR AFFECTING 1 LINE
-----------------------------------------------------------------------
9 | ERROR | [x] @inheritdoc found. Did you mean {@inheritdoc}?
| | (Drupal.Commenting.DocComment.InheritDocWithoutBraces)
-----------------------------------------------------------------------
| Comment | File | Size | Author |
|---|
Issue fork coder-2904801
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:
- 2904801-inheritdoc---the
compare
- SerkanB-8.3.x-patch-53570
changes, plain diff MR !21
Comments
Comment #4
serkanb commentedThis patch seems to do the job.
Updating Version to 8.3.x. I'm on that version right now, the original post is from 2017... so I assume 8.3.x is what we should use nowadays. But the patch is probably compatible with anything, adding 3 lines.
Comment #5
klausiThanks, can you file a pull request against https://github.com/pfrenssen/coder and link it here? Please also add a test case.
Comment #6
jonathan1055 commentedCan I ask a dumb question - what exactly is the problem or enhancement that this patch fixes? Is it that without the early return added here, Coder continues and tries to check other sniffs against the comment block and gives false results? If so, would we not be seeing lots of those already in current code? Or is it just an efficiency measure to exit early? When we know this, the tests will become easy to write, and we can also update the issue summary and title which don't give much clue.
Comment #7
serkanb commentedIf I remember correctly, the issue just was: if I add "@inheritdoc", I do that because there is already a doc-comment somewhere, I don't want to replicate that.
Without the return there, it did his job and told you "please write a comment!", which isn't the point of "@inheritdoc" :D (maybe even @inheritDoc ... should've added that in there as well.)
Comment #8
jonathan1055 commentedIf that is what this issue is about, then it is already covered. But we are expecting
{@inheritdoc}with curly braces. If you have this, then there is no problem. But if you just have@inheritdocthen it does produceSo there may be something to fix, but it is not exactly as in the patch provided.
In Drupal/Sniffs/Commenting/DocCommentSniff.php#L129 the block is only executed if the $short code is not T_DOC_COMMENT_STRING. For
{@inheritdoc}the short code is T_DOC_COMMENT_STRING which is right. But if you have missed the curly braces, then@inheritdocis treated as a tag, and the short code is T_DOC_COMMENT_TAG so that block is executed, and we get the 'missing short description'We can help developers to get it right, maybe by testing for @inheritdoc and giving an extra separate message "Did you mean {@inheritdoc}", or some other fix. But the patch as it stands allows @inheritdoc without the { } to be accepted. Which I don't think follows the coding standards?
Comment #9
serkanb commented@jonathan1055 you sir are correct!
Removing the patch and adding those {} works just fine.
Well then, forget what I said. Reading the docs properly would've saved me from all this.
But! it seems like I'm not the only one forgetting about {}. I see it in the ckeditor5 module in core, and some contrib modules as well :D
Maybe ... add a notice to the error message, that those things are important.
Comment #10
jonathan1055 commentedThanks @SerkanB for confirming that we now agree on the problem. I have updated the issue summary with a suggested solution and we can now discuss the best way forward.
Comment #12
jonathan1055 commentedThat's good, it rmeoves it from the top of the issue. It also shows as closed on https://git.drupalcode.org/project/coder/-/merge_requests/21#note_150497 and only 5 are open. All of those should be closed too, but I think only the person who opened it or a project maintainer can do that. The project front page does have a small section on how to contribute but it is not very obvious, and users will always be openeing MRs here. It would be nice if there was a way to disable that feature. Anyway, that's for a different issue.
Comment #13
jonathan1055 commentedI have added a small bit to the DocCommentSniff to detect this situation and give a better non-misleading message. Also included a fixer to add the missing curly braces { }. You now get
https://github.com/pfrenssen/coder/pull/180
My repo https://github.com/jonathan1055/coder/tree/inheritdoc-braces
I think this is helpful, and will avoid the kind of problems seen above. If you consider that you would commit this I will write test coverage for the new bit of code.
Comment #14
klausiYep, makes a lot of sense to me, I would merge this!
Comment #15
jonathan1055 commentedWould you prefer a new pair of test files
tests/Drupal/Commenting/DocCommentUnitTest.4.incandtests/Drupal/Commenting/DocCommentUnitTest.4.inc.fixed?Or is it OK to add on to the end of the base file
DocCommentUnitTest.incor one of the existingDocCommentUnitTest.n.inc?Comment #16
klausiI think you can add to the end of DocCommentUnitTest.inc, the others are mostly testing file comments.
Comment #17
jonathan1055 commentedAdded tests. Intentionally incorrect 'fixed' file and no update to expected lines, to demonstrate that the tests are run and fail. We get
Comment #18
jonathan1055 commentedTest files corrected, to demonstrate passing tests. Ready for review.
https://github.com/pfrenssen/coder/pull/180
Comment #19
klausiThanks, left 2 minor comments there!
Comment #20
jonathan1055 commentedThanks for the review, I have answered one question and changed the code in the other. Ready for review again.
Comment #22
klausiMerged, thanks!
Comment #23
jonathan1055 commentedThanks for commiting. I have added a minor follow-up pr to add a fourth inheritdoc example https://github.com/pfrenssen/coder/pull/185
Comment #24
jonathan1055 commentedThis is in prep for a future sniff which may enforce either lowercase or camelcase inheritdoc. Currently we allow both versions. Having the four examples close in the file will make it simpler to adjust the fixed file and expected lines, rather than add the fourth case later, after other test examples may have been added at the end.
Comment #26
klausiMakes sense, thanks a lot!