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.

Issue fork drupal-3327853

Command icon 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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Spokje created an issue. See original summary.

Spokje’s picture

Spokje’s picture

FileSize
5.89 KB
611 bytes
Spokje’s picture

Status: Active » Needs review
bbrala’s picture

Status: Needs review » Needs work

I am very confused, my phpstorm tells me there are 85 results, this patch doesn't find that many?

Spokje’s picture

Status: Needs work » Needs review
FileSize
9.91 KB
4.27 KB

Thanks @bbrale, was very confused as well, looked into it.

I too found 85 occurrences of inheritDoc in 10.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 :)

Spokje’s picture

Title: Don't allow {@inheritDoc} in PHPDocBlocks » Don't allow @inheritDoc-annotation in PHPDocBlocks
Spokje’s picture

Title: Don't allow @inheritDoc-annotation in PHPDocBlocks » Don't allow {@inheritDoc} annotation in PHPDocBlocks
FileSize
10.15 KB
4.58 KB

Actually, we need both sniffs to catch both {@inheritDoc} and @inheritDoc

quietone’s picture

Adding related issue from the Coding Standards project, #3060580: Allow inheritdoc and inheritDoc?.

longwave’s picture

Status: Needs review » Reviewed & tested by the community

Using a dynamic rule provided by the Slevomat package is a really nice solution to this!

  • xjm committed 36a8edd9 on 10.1.x
    Issue #3327853 by Spokje, bbrala, longwave: Don't allow {@inheritDoc}...

  • xjm committed ef4e0bb3 on 10.0.x
    Issue #3327853 by Spokje, bbrala, longwave: Don't allow {@inheritDoc}...
xjm’s picture

Title: Don't allow {@inheritDoc} annotation in PHPDocBlocks » [9.5.x backport] Don't allow {@inheritDoc} annotation in PHPDocBlocks
Status: Reviewed & tested by the community » Patch (to be ported)

10.1.x

With the patch applied:

[ayrton:maintainer | Mon 17:12:34] $ grep -rl "inheritDoc" * | grep -v "vendor" |grep -v "node_modules"
core/lib/Drupal/Component/Annotation/Doctrine/StaticReflectionClass.php
core/lib/Drupal/Component/Annotation/Doctrine/SimpleAnnotationReader.php
core/phpcs.xml.dist

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:

[ayrton:maintainer | Mon 17:21:14] $ grep -rl "inheritDoc" * | grep -v "vendor" |grep -v "node_modules"
core/lib/Drupal/Component/Annotation/Doctrine/StaticReflectionClass.php
core/lib/Drupal/Component/Annotation/Doctrine/SimpleAnnotationReader.php
core/phpcs.xml.dist

PHPCS passed there locally as well.

9.5.x

The patch does not apply. Can we get a backport version?

Thanks everyone!

  • xjm committed 354f9520 on 10.0.x
    Revert "Issue #3327853 by Spokje, bbrala, longwave: Don't allow {@...
xjm’s picture

Title: [9.5.x backport] Don't allow {@inheritDoc} annotation in PHPDocBlocks » [10.0.x and 9.5.x backports] Don't allow {@inheritDoc} annotation in PHPDocBlocks
Version: 10.1.x-dev » 9.5.x-dev
Status: Patch (to be ported) » Reviewed & tested by the community
Issue tags: +10.1.0 release notes, +Needs release note

Sorry, 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.

xjm’s picture

Status: Reviewed & tested by the community » Patch (to be ported)

Oops.

  • xjm committed a997dea2 on 10.0.x
    Issue #3327853 by Spokje, bbrala, longwave: Don't allow {@inheritDoc}...

  • xjm committed 8a4328c8 on 9.5.x
    Issue #3327853 by Spokje, bbrala, longwave: Don't allow {@inheritDoc}...

  • longwave committed 5011ccc4 on 10.0.x
    Revert "Issue #3327853 by Spokje, bbrala, longwave: Don't allow {@...

  • longwave committed 6b77b5a5 on 9.5.x
    Revert "Issue #3327853 by Spokje, bbrala, longwave: Don't allow {@...
longwave’s picture

This broke CI in 9.5.x:

FILE: ...tml/core/modules/ckeditor/tests/modules/src/Form/AjaxCssForm.php
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
 37 | ERROR | [x] Use of annotation @inheritDoc is forbidden.
    |       |     (SlevomatCodingStandard.Commenting.ForbiddenAnnotations.AnnotationForbidden)
----------------------------------------------------------------------

Reverted from both 10.0.x and 9.5.x, as we cannot add new coding standards there now.

Spokje’s picture

So 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?

longwave’s picture

Code 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.

Spokje’s picture

Thanks @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)

xjm’s picture

OK:

  1. The 10.0.x patch just needs to be identical to the 10.1.x patch, with no rule. Because that passed tests.
  2. The 9.5.x patch needs to be created separately, because 9.5.x includes more inherirtDoc than 10.0.x that we need to correct.
  3. We keep the same coding standard across the three branches for ease of backports and so that the community gets use to the standards. We just only enforce it with a rule on 10.1.x because that's a disruption.

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 :)

Wim Leers’s picture

This broke 9.5.x Ah, @longwave already said this in #21 and reverted. 👍

xjm’s picture

Status: Patch (to be ported) » Needs work

Better status.

fabiansierra5191’s picture

Version: 9.5.x-dev » 10.0.x-dev
Assigned: Unassigned » fabiansierra5191
mondrake’s picture

FWIW, Symfony has abandoned the usage of @inheritdoc,

It looks like this PHP Doc is useless

.

https://github.com/symfony/symfony/pull/47390

fabiansierra5191’s picture

Status: Needs work » Needs review

MR created with all the {@inheritDoc} changes.
I found those files in the Drupal\Component\Annotation namespace were checked and confirmed they were all ignored.

rpayanm’s picture

Status: Needs review » Reviewed & tested by the community

I tested it and it looks good for me.

I check the patch like this:
I copied this sniffs to core/phpcs.xml.dist:

<!-- SlevomatCodingStandard sniffs -->
<rule ref="SlevomatCodingStandard.Commenting.ForbiddenAnnotations">
<properties>
  <property name="forbiddenAnnotations" type="array">
    <element value="@inheritDoc"/>
  </property>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.Commenting.ForbiddenComments">
<properties>
   <property name="forbiddenCommentPatterns" type="array">
     <element value="/@inheritDoc/"/>
   </property>
</properties>
</rule>

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.

  • xjm committed 9e818ad8 on 10.0.x
    Issue #3327853 by Spokje, fabiansierra5191, xjm, longwave, bbrala,...
xjm’s picture

Version: 10.0.x-dev » 9.5.x-dev
Issue tags: +Needs followup

Thanks @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:

[ayrton:maintainer | Fri 14:07:51] $ grep -rl "inheritDoc" * | grep -v "vendor" | grep -v "node_modules"
core/lib/Drupal/Component/Annotation/Doctrine/StaticReflectionClass.php
core/lib/Drupal/Component/Annotation/Doctrine/SimpleAnnotationReader.php

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:

[ayrton:maintainer | Fri 14:09:10] $ grep -ril " @inheritdoc" * | grep -v 

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:

core/tests/Drupal/KernelTests/Core/Database/DriverSpecificKernelTestBase.php
core/tests/Drupal/KernelTests/Core/Database/DriverSpecificDatabaseTestBase.php

E.g.:

  /**                                                                           
   * @inheritdoc                                                                
   */
  protected function setUp(): void {

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!

Spokje’s picture

fabiansierra5191’s picture

Assigned: fabiansierra5191 » Unassigned
Status: Reviewed & tested by the community » Needs review

Thanks, @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!

rpayanm’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

The patch does not apply.

xjm’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll

Actually 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.

rpayanm’s picture

Status: Needs review » Reviewed & tested by the community

I reviewed the patch from #36 following the review process in comments #32 and #34 and I see the patch ok.

  • xjm committed 8813ad5c on 9.5.x
    Issue #3327853 by Spokje, fabiansierra5191, xjm, longwave, rpayanm,...
xjm’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Fixed
Issue tags: -Needs release note, -Needs followup
Related issues: +#3331949: 10.1.0 release notes

This 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.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.