Problem/Motivation

In #3129184: Remove description from @return $this pointed that core should not provide description for @return $this but that's not pointed in coding standards https://www.drupal.org/docs/develop/standards/api-documentation-and-comm...

Update coding standards about that case and introduce sniff #3326850: Warn if @return $this has description

The current sniff allows for no description of "@return $this" and "@return static".

Benefits

If we adopted this change, the Drupal Project would benefit by being more consistent.

Three supporters required

  1. https://www.drupal.org/u/borisson_ (2025-02-11)
  2. https://www.drupal.org/u/drunken-monkey (2025-02-15)
  3. https://www.drupal.org/u/joachim (2025-02-18)

Proposed changes

Change documentation to explicitly allow no description for "@return $this", "@return self", and "@return static".
Change the relevant sniff to allow not no description for "@return self".

1. https://www.drupal.org/docs/develop/standards/php/api-documentation-and-comment-standards#types

Current text

Syntax notes:

  • ...
  • When you are returning the main class object ($this), use @return $this.
  • When creating a new instance of the same class, use @return static.

Drupal standards notes:

  • ...
  • You may omit the description if using @return $this or @return static.
Proposed text

Syntax notes:

  • ...
  • When you are returning the main class object ($this), use @return $this.
  • When creating a new instance of the same class, use @return static.
  • When creating a new instance of the same class, and that class is final, use @return self.

Drupal standards notes:

  • ...
  • You may omit the description if using @return self, @return static, or @return $this.

2. Repeat the above for each page or sub-page that needs to be changed.

Remaining tasks

  1. Create this issue in the Coding Standards queue, using the defined template
  2. Add supporters
  3. Create a Change Record
  4. Review by the Coding Standards Committee
  5. Coding Standards Committee takes action as required
  6. Discussed by the Core Committer Committee, if it impacts Drupal Core
  7. Final review by Coding Standards Committee
  8. Documentation updates
    1. Edit all pages
    2. Publish change record
    3. Remove 'Needs documentation edits' tag
  9. If applicable, create follow-up issues for PHPCS rules/sniffs changes

For a full explanation of these steps see the Coding Standards project page

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:

Comments

andypost created an issue. See original summary.

quietone’s picture

Issue summary: View changes

We should check the history to see if "return $this" was discussed earlier.

Also, from longwave, we should do discuss, "@return self" and "@return static". #3129184-6: Remove description from @return $this

drunken monkey’s picture

It’s documented a bit further down, under Indicating data types in documentation. But yes, I also often struggle to find things in our documentation.

quietone’s picture

Title: Update about @return $this » Allow no description for "@return $this", "@return self", and "@return static"
Issue summary: View changes
Parent issue: #3129184: Remove description from @return $this »
Related issues: +#3129184: Remove description from @return $this

Update to new issue template

borisson_’s picture

You may omit the description if using @return $this or @return static.

This is already in the docs. So that should be fine already? What do we need to do for this issue?

quietone’s picture

Issue summary: View changes

@borisson_, thanks for finding that sentence! I surely missed it.

So, the sentence is in the section "Indicating data types in documentation" which is the wrong place. It should be with the '@return'. I have updated the issue summary with the proposal. The difference is also to add @return self.

quietone’s picture

Testing with Drupal.Commenting.FunctionComment.MissingReturnComment enabled and "@return $this", and "@return static" pass without a comment but "@return self" fails with "Description for the @return value is missing".

quietone’s picture

Issue summary: View changes

Improving the issue summary

borisson_’s picture

Issue summary: View changes
quietone’s picture

Title: Allow no description for "@return $this", "@return self", and "@return static" » Allow no description for "@return self"
Issue summary: View changes

Updated title. I also changed the current and proposed text since the information is in a different section.

There is a TBA in the proposed text which needs to completed.

borisson_’s picture

Issue summary: View changes

Filled in the when TBA,
return static is only useful because it can do late binding, which is really helpful when that class is extended by another one. However final classes cannot be extended, so they can use self instead.

drunken monkey’s picture

Issue summary: View changes

Added a comma in the proposed text, but looks good otherwise.

joachim’s picture

Issue summary: View changes

+1

quietone’s picture

Issue summary: View changes
Status: Active » Needs work
Issue tags: +Needs change record

Setting to NW and tagging for a change record.

borisson_’s picture

Issue tags: -Needs change record

Added a change record.

We need to update the text change to a merge request still

quietone’s picture

Issue tags: +Needs merge request
quietone’s picture

Status: Needs work » Needs review

Created an MR. The syntax notes in the issue summary were removed in #3527545: Formatting fixes, whitespace, links, rename files