Prevent @returns from creeping back into the codebase

Created on 12 October 2023, over 1 year ago

Problem/Motivation

In πŸ› Use "@return" instead of "@returns" Postponed we fixed invalid use of @returns in comments, but we did not prevent it from happening again.

Steps to reproduce

Proposed resolution

Similar to the ban on @inheritDoc (with capital D) we can add a rule to phpcs.xml.dist to ban @returns.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ“Œ Task
Status

Active

Version

11.0 πŸ”₯

Component
DocumentationΒ  β†’

Last updated 4 days ago

No maintainer
Created by

πŸ‡¬πŸ‡§United Kingdom longwave UK

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @longwave
  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

    But isn't this covered by another sniff already? If a method has a return value, there should be a sniff already that flags the method if @return isn't specified. And if you misspell @return, then the sniff should catch this.

    Unless of course core has turned off that warning ... in that case the solution is to turn it back on and fix any other problems in core that are revealed by this.

    In contrib, however, PHPCS does tell us if the @return tag is missing, if the @return type is missing, and if the @return parameter is not documented.

    FunctionCommentSniff: "Missing @return tag in function comment"

  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

    BTW this is the same point I brought up 6 years ago when I opened πŸ› Use "@return" instead of "@returns" Postponed , and at the time @dawehner https://www.drupal.org/project/drupal/issues/2916306#comment-12300987 πŸ› Use "@return" instead of "@returns" Postponed and @alexpott https://www.drupal.org/project/drupal/issues/2916306#comment-12302537 πŸ› Use "@return" instead of "@returns" Postponed said that they though turning the sniff back on for core was the right thing to do in principle.

    Checking for one specific misspelling (@returns) may help a little, but it doesn't fix missing @return documentation and it doesn't fix any other misspellings or casing problems with this tag, so it's only a baby step in the right direction.

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    The issue to add the sniff is πŸ“Œ [Meta] Fix Drupal.Commenting.FunctionComment.InvalidNoReturn Active . It was recently RTBC but was pushed back to split into child issues. There are 5 child issues, 2 at RTBC, 1 needs work, 1 is active and the last one is to enable the sniff. I'd much rather put effort into completing those issues instead of making this change. A change that, if made, should be removed when the sniff is enabled.

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    Of the child issues mentioned above it is now, 3 at RTBC, 1 is active and the last one is to enable the sniff. It was just random failures in one of the issues.

  • Status changed to Needs review 11 days ago
  • πŸ‡³πŸ‡ΏNew Zealand quietone

    Tested on 11.x today

    I changed the '@return' to '@returns' in \Drupal\views\Plugin\views\field\MultiItemsFieldHandlerInterface::getItems().

    That resulted in this PHPStan error

     ------ ------------------------------------------------------------------------------------------------------------------ 
      34     Method Drupal\views\Plugin\views\field\MultiItemsFieldHandlerInterface::getItems() has no return type specified.  
             πŸͺͺ  missingType.return                                                                                            
     ------ ------------------------------------------------------------------------------------------------------------------ 
    

    So, although not a phpcs rule the @returns should not be creeping back in the code base.

    Shall we close this as outdated or make a change to phpcs.xml.dist to add '@returns' to the the list of 'ForbiddenAnnotations'?

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    Tested again with @returns in

    • includes/common.inc
    • modules/dblog/dblog.module
    • modules/migrate/src/Row.php
    • modules/views/src/Plugin/views/field/MultiItemsFieldHandlerInterface.php

    And PHPStan detected all of them with the same error message as in the previous comment.

  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    I don't think #6 covers this, because you could have a native return type in combination with @returns. Presumably phpstan would happily accept the native return type and nothing would complain about the annotation.

  • πŸ‡§πŸ‡ͺBelgium borisson_ Mechelen, πŸ‡§πŸ‡ͺ

    Based on #8, I think we should make a change to phpcs.xml.dist to add '@returns' to the the list of 'ForbiddenAnnotations'. Moving this issue to active so that can be picked up.

  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    +1 for #9, we already use forbiddenAnnotations to prevent a mis-application of @inheritdoc so we can do the same for this.

  • Merge request !11308Forbid @returns β†’ (Closed) created by quietone
  • πŸ‡³πŸ‡ΏNew Zealand quietone

    OK. It is now forbidden.

  • πŸ‡§πŸ‡ͺBelgium borisson_ Mechelen, πŸ‡§πŸ‡ͺ

    Looks good!

    • nod_ β†’ committed 7abcc2fb on 11.x
      Issue #3393661 by quietone, longwave, borisson_, tr, mstrelan: Prevent @...
  • πŸ‡«πŸ‡·France nod_ Lille

    Committed 7abcc2f and pushed to 11.x. Thanks!

Production build 0.71.5 2024