Problem/Motivation
As per the policy on minimum requirements for in-code documentation:
Documentation blocks for all files, interfaces, classes, methods (except constructor methods), class members, functions, hooks, and constants, which must contain...
...constructor docblocks are not required.
When we make changes to constructor params (e.g. adding, removing), we should be able to remove the docblock at the same time.
Steps to reproduce
Proposed resolution
Include a policy in the Issue scope guidelines for Drupal core issues that mentions it is possible to remove constructor docblocks when changing constructor params. It is proposed to add the following table row to the "Examples of good and bad issue scope".
| Good scope | Bad scope | Why |
|---|---|---|
| Delete a class constructor docblock when that constructor's function parameters are changed | Delete a class constructor docblock when editing the class |
If the constructor's parameters are edited, then the docblock removal prevents contributors from needing to add boilerplate documentation that is otherwise unnecessary. The changes are adjacent to the other code being edited and can be easily understood. But deleting docblocks when performing unrelated work on the class is confusing to reviewers and distracts from the issue's intent. |
Comments
Comment #2
dww+1 Yes please! If we're changing the params, we would normally have to be updating the docs to match. In this case, removing the docs is better than "fixing" them.
Comment #3
catch+1 from me too, if it's in scope to add to the docblock, it's also in scope to remove it altogether given the new coding standard. I co-incidentally ran into this today on #3379885: Use MessagesCommand in BigPipe to remove special casing of the messages placeholder and took the approach here.
Comment #4
dcam commented+1 from me. It seems sensible. I tried to review #3344389: Inject entity.repository service instead of using \Drupal::service('entity.repository') in non-tests which touches a number of constructor docblocks and wondered "Why?"
Comment #5
catchI think we've been doing this in practice quite a lot already.
Comment #6
nicxvan commentedI think we have the necessary support, but we probably need proposed text changes though.
Comment #7
nicxvan commentedMeant needs work
Comment #8
catchI don't know how to word something, it would have to be specifically about constructor documentation, because any other situation where it's like 'oh here's some extra code to remove/update to a new coding standard' can easily turn into a bad example of scope creep.
The main reason to do constructor promotion and documentation removals at once when changing one parameter is it's exactly the same line of code, or immediately adjacent, and just deletions. And because it avoids the deadening feeling of adding boilerplate you know shouldn't be there just to match existing boilerplate that should already not be there.
Comment #9
dcam commentedI studied the issue scope document last night and this morning. I think the most reasonable thing is to add another row to the "Examples of good and bad issue scope" table.
Let me know what you think of this.
If the constructor's parameters are edited, then the docblock removal prevents contributors from needing to add boilerplate documentation that is otherwise unnecessary. The changes are adjacent to the other code being edited and can be easily understood.
But deleting docblocks when performing unrelated work on the class is confusing to reviewers and distracts from the issue's intent.
Comment #10
catch#9 seems good, maybe with a link to the coding standards entry for constructors for context?
Comment #11
dcam commentedEdited #9 to add the docs link.
Comment #12
dwwThis fell way off my radar. The docs in #9 seem great. Thanks for putting those together! I think the Why column text is excellent, and helps people understand when this is a good idea, and when it isn't.
All 5 of us who have participated so far support this policy change, and no one has yet registered any opposition. That said, 5 people isn't exactly a huge sample size. 😅 But generally, the sense I get from being on the coding standards committee and following Drupal Slack + issues is that folks are revolting against boilerplate (rightfully), and this is a step in the right direction to being able to freely remove things that aren't adding value to our codebase. So I'm going to be bold and move this to RTBC, to see whatever other governance steps have to be taken to make this official.
Comment #13
kim.pepperI've actually been doing this in issues for months and have never had pushback from the committers.
Comment #14
borisson_This was linked to the old documentation, which is no longer maintained.
This is the new location: https://project.pages.drupalcode.org/coding_standards/php/documentation/..., it is still not required, so this is seems right to me. RTBC++
Comment #15
mondrakePersonally, I'd rather go in the direction of allowing partial documentation in constructors' docblocks, i.e. only add a
@paramfor those arguments of the constructor that would benefit from it.Actually in a sense I think we will hit this when raising PHPStan to higher levels, it will require docblock description for iterable arguments for instance.
In classes that implement just DIC services in the constructor the 'no docblock' policy makes sense, as with autowiring the self-explainability of the arguments is evident. But not in other classes.
Comment #16
catch@mondrake that's for #3376518: Allow omitting @var, @param and @return tags or their descriptions if the variable name and type is enough which we still need to get agreed.
fwiw I've been actively encouraging this approach rather than updating boilerplate we'll then have to remove later, it's much easier to review deletions of the entire docblock than additions, so another +1.
Comment #17
xjmI've allowed this in practice, although there's a bit of a judgment call. If it's one docblock that's being touched within the scope of a single API, that's one thing. But if it's dozens, that's a lot of extra code to review to ensure that there isn't any mismatch between what's being removed versus what's being updated by the MR. Because you have to read the whole class and check the member docs/hinting of all the params...
Do we need to amend the scope policy for this, versus just leaving it at the committer's discretion?
Edit: #9 is actually pretty good. Can we add it to the IS and maybe iterate on that?
Comment #18
dcam commentedUpdated the proposed resolution with the change from #9.
Comment #19
ghost of drupal pastWould you like a rector rule to delete all constructor documentation in a codebase? Just say the word.
Comment #20
nicxvan commentedword
Comment #21
ghost of drupal pastwell, in that case https://gist.github.com/chx/67e1e655da2b8313bf1c938a233dcfdd
Comment #22
nicxvan commentedI applied the rule here: #3565152: [rector] Remove constructor comments it's pretty clean!
Comment #23
xjmHang on there... I think we can only delete constructor documentation when the class has members documented and they're 1:1 with constructor params/services/etc. That won't be all constructors in the codebase, not by far.
Comment #24
nicxvan commentedI did it to start the discussion. Catch already pointed out we need to avoid removing value objects and timplunkett pointed out that we should keep instances of mixed.
not according to the coding standards:
All classes and all of their methods (including private methods but excluding constructors) must be documented.That reads as we can remove all constructors. I think you might be mixing up with the other issue to allow removing parameter docs in general when they are one to one and typed with an object.
Comment #25
quietone commentedAs xjm said in #17 this has been happening already so this is just clarifying the practice in documentation.
I have updated the Scope Guidelines, Examples of good and bad issue scope section with the text from the issue summary.
Thanks!
Comment #27
xjmRe: #24:
You have a logic error actually. 😀
This does not mean that no constructors require documentation. It only means that not all constructors require documentation.
Comment #28
geek-merlin