Problem/Motivation
If a class has a method that is overriding a method from a parent class/interface, and the documentation is identical, use this short form for the documentation:
/** * {@inheritdoc} */ public function …
(https://www.drupal.org/docs/develop/standards/php/api-documentation-and-..., 2nd bullet-point)
(At least) PHPStorm out-of-the-box comes up with {@inheritDoc} (notice the capital D) as the default autocomplete option.
Let's fix the occurrences that are currently already in the Core codebase and use a Slevomat Rule to make sure no new occurrences creep in.
Steps to reproduce
Proposed resolution
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
The SlevomatCodingStandard.Commenting.ForbiddenAnnotations
and SlevomatCodingStandard.Commenting.ForbiddenComments
rules have been enabled to standardize the format of {@inheritdoc}
for API docblocks.
Comment | File | Size | Author |
---|---|---|---|
#8 | interdiff.3-8.txt | 4.58 KB | Spokje |
#8 | 3327853-8.patch | 10.15 KB | Spokje |
|
Issue fork drupal-3327853
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:
- 3327853-9.5.x changes, plain diff MR !3213
- 3327853-10.0.x changes, plain diff MR !3162
Comments
Comment #2
SpokjeComment #3
SpokjeComment #4
SpokjeComment #5
bbralaI am very confused, my phpstorm tells me there are 85 results, this patch doesn't find that many?
Comment #6
SpokjeThanks @bbrale, was very confused as well, looked into it.
I too found 85 occurrences of
inheritDoc
in10.1.x
.However a lot of them are in files that are excluded from PHPCS-ing, mostly because they're (almost) 1-on-1 copies from other frameworks, adapted for Drupal use.
If you look here: https://git.drupalcode.org/project/drupal/-/blob/10.1.x/core/phpcs.xml.d... you'll see what's excluded.
Also there's the nifty
// phpcs:ignoreFile
, which excludes a specific file which contains the line from being checked by PHPCS.Having said that, there were a few escapees, which weren't wrapped in {curly braces}, so I improved the sniff.
I think the left occurrences after the patch are all covered by the 2 reasons above as to why they're not detected.
Thanks for being confused :)
Comment #7
SpokjeComment #8
SpokjeActually, we need both sniffs to catch both
{@inheritDoc}
and@inheritDoc
Comment #9
quietone CreditAttribution: quietone at PreviousNext commentedAdding related issue from the Coding Standards project, #3060580: Allow inheritdoc and inheritDoc?.
Comment #10
longwaveUsing a dynamic rule provided by the Slevomat package is a really nice solution to this!
Comment #13
xjm10.1.x
With the patch applied:
I'm assuming the Annotation component is what #6 is referring to as well. I confirmed the files in the component are already ignored for coding standards. And hopefully #3252386: Use PHP attributes instead of doctrine annotations will get rid of that component for us. I was also able to commit the patch locally, which means PHPCS passed.
10.0.x
Rather than cherry-picking the patch to 10.0.x, I applied and committed it there too in order to check it separately. Same grep results:
PHPCS passed there locally as well.
9.5.x
The patch does not apply. Can we get a backport version?
Thanks everyone!
Comment #15
xjmSorry, folks, I spaced out. We need a version of the changes only, without the new rule enabled, for the 10.0.x backport. Reverted from 10.0.x for now.
Comment #16
xjmOops.
Comment #21
longwaveThis broke CI in 9.5.x:
Reverted from both 10.0.x and 9.5.x, as we cannot add new coding standards there now.
Comment #22
SpokjeSo is it OK to upload a patch for 10.0.x (which would be the same as 10.1.x) and 9.5.x (which would cover the offenders that were removed from the 10.x branch) or do we want "just" the code changes and not the sniff?
Comment #23
longwaveCode changes only, no new sniff - you can still run the sniff locally to check you got everything, or upload a test-only patch with the sniff included if you want to prove it with DrupalCI. The 9.5.x patch will need to cover AjaxCssForm as well.
Comment #24
SpokjeThanks @longwave, still a little confused though: If I can prove a 9.5.x patch passes with a new sniff, would that be up for committing, or are we in the land of sniff only in current release + 1 and make back-porting easier with only code changes in 10.0.x and 9.5.x?
(Yes, I know I'm even more annoying than usual, but to prevent me from going on and on during the 9.5.x/10.0.x release cycle, my tiny mind needs some clarity on what is and isn't allowed. For me it's a bit confusing ATM what is allowed in all of the three open branches currently)
Comment #25
xjmOK:
inherirtDoc
than 10.0.x that we need to correct.I have no idea what's going on with the git integration. I reverted it from 10.0.x last night and I swear I never even committed it to 9.5.x. I also swear I did not re-commit them, so I don't understand how there was anything to revert. Anyway. The path forward is described above :)
Comment #26
Wim LeersThis brokeAh, @longwave already said this in #21 and reverted. 👍9.5.x
Comment #27
xjmBetter status.
Comment #28
fabiansierra5191 CreditAttribution: fabiansierra5191 at Agileana commentedComment #29
mondrakeFWIW, Symfony has abandoned the usage of @inheritdoc,
.
https://github.com/symfony/symfony/pull/47390
Comment #31
fabiansierra5191 CreditAttribution: fabiansierra5191 at Agileana commentedMR created with all the
{@inheritDoc}
changes.I found those files in the
Drupal\Component\Annotation
namespace were checked and confirmed they were all ignored.Comment #32
rpayanmI tested it and it looks good for me.
I check the patch like this:
I copied this sniffs to
core/phpcs.xml.dist
:I ran this command to find any issues:
./vendor/bin/phpcs --standard="core/phpcs.xml.dist" --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml --sniffs=SlevomatCodingStandard.Commenting.ForbiddenAnnotations,SlevomatCodingStandard.Commenting.ForbiddenComments core/
I fixed all errors, ran the previous command , it gave me no errors. So I removed the code in
core/phpcs.xml.dist
, generated a patch and compared it with the Fabian's. They were the same.Comment #34
xjmThanks @rpayanm.
In addition to testing the same steps as #32, I reviewed the changeset with
git diff --color-words
to include that it includes only changes to mis-capitalized docblocks. I also spot-checked the phpcs results against grep:Those remaining instances are ignored in PHPCS checks as per #13, so this is the complete fix for 10.0.x. Therefore, committed there and moving to 9.5.x for the final backport.
In the process of reviewing #31, I did notice that there is an additional pattern not currently covered by the phpcs rule, where the curlies are missing but the correct casing is used:
Most of the results for that are generated CKE5 assets and therefore out of scope for our PHP coding standards. However, there were two results in core PHPUnit tests:
E.g.:
This issue is also present in 10.1.x and not covered by the current rule. It's out of scope for this issue, but maybe we could file a followup and use the Slevomat rule to check for the no-curly format?
Thanks!
Comment #35
SpokjeOpened #3331633: Don't allow @inheritdoc (no curly braces) annotation in PHPDocBlocks for
@inheritdoc
without the curly braces.Comment #38
fabiansierra5191 CreditAttribution: fabiansierra5191 at Agileana commentedThanks, @rpayanm, and @xjm for the tests and feedback.
MR was created with all the
{@inheritDoc}
changes but now for branch 9.5.x.Those files in the
Drupal\Component\Annotation
namespace were checked and it confirmed they were all ignored.The review process can be made with comments #32 and #34
Thanks!
Comment #39
rpayanmThe patch does not apply.
Comment #40
xjmActually it applies fine if I use the "plain diff" link and 9.5.x HEAD. I also just clicked the rebase button so that tests will keep running.
Comment #41
rpayanmI reviewed the patch from #36 following the review process in comments #32 and #34 and I see the patch ok.
Comment #43
xjmThis was tagged "Needs followup" for #3331633: Don't allow @inheritdoc (no curly braces) annotation in PHPDocBlocks which exists now.
Committed and pushed the backport to 9.5.x, thanks!
I also added a release note and updated the doc at #3331949: 10.1.0 release notes with it.