Fix Drupal.Commenting.FunctionComment.MissingReturnComment in extension tests

Created on 6 February 2025, 3 months ago

Problem/Motivation

Drupal.Commenting.FunctionComment.MissingReturnComment is not enable for all of core yet.

Approach

We are testing coding standards with PHP CodeSniffer, using the Drupal coding standards from the Coder module. We need to do a couple of steps in order to download and configure them so we can run a coding standards check.

Step 1: Add the coding standard

Every coding standard is identified by a "sniff". For example, an imaginary coding standard that would require all llamas to be placed inside a square bracket fence would be called the "Drupal.AnimalControlStructure.BracketedFence sniff".

Open the file core/phpcs.xml.dist and add a line for the sniff of this ticket. The sniff name is in the issue title. Make sure your patch will include the addition of this line.

Step 2: Install PHP CodeSniffer and the ruleset from the Coder module

$ composer install

Once you have installed the phpcs package, you can list all the sniffs available to you like this:

$ ./vendor/bin/phpcs --standard=Drupal -e

This will give you a list of sniffs, and the Drupal-based ones should be present.

Step 3: Prepare the phpcs.xml file

Add <include-pattern>core/*/tests/*</include-pattern> to the Drupal.Commenting.FunctionComment.MissingReturnComment rule.

Step 4: Run the test

Now you are ready to run the test! From within the core/ folder, run the following command to launch the test:

$ composer phpcs

Step 5: Fix the failures

When the test is complete it will present you a list of all the files that contain violations of your sniff, and the line numbers where the violations occur.

Steps to reproduce

Proposed resolution

Enable sniff for the directory core/tests,

Remaining tasks

Follow the 'Approach' above to fix the failures
Review
Commit

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

📌 Task
Status

Active

Version

11.0 🔥

Component

other

Created by

🇳🇿New Zealand quietone

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

Merge Requests

Comments & Activities

  • Issue created by @quietone
  • Pipeline finished with Failed
    3 months ago
    Total: 315s
    #421994
  • Pipeline finished with Success
    3 months ago
    Total: 345s
    #422007
  • 🇺🇸United States smustgrave

    Only looked at like 3

    * @return array
       *   The render array.
    

    Render array of what?

  • 🇳🇿New Zealand quietone

    I did some research on @return void. I searched the Coder issue queue and found #1519442: Add sniff for @return void which led me to #1510838: Define and document @return void policy . Comment #1 of that issue says the when there is no return value then the @return is not needed. I then checked the standards for @return and it states,

    Drupal standards: The documentation is indented two spaces (see example). Data types are required to be included as of Drupal 8.x. Functions without return values must not have @return documentation.

    and there at Drupal API documentation standards for functions

    If there is no return value for a function, there must not be a @return tag.

    I then looked at the instances of @return void in tests and used git blame (using PHPStorm) to see when they were committed. All were committed in 2024 or 2025 and in issues not related to standards. Perhaps people are working to be thorough?

    So, to enable the sniff, we need to remove @return void.

  • First commit to issue fork.
  • 🇬🇧United Kingdom oily Greater London

    I understand from #6 that any non-test files with a @return void should have the @return removed. i have committed that one edit. The other @return void blocks are in test files so i have left those alone.

  • Pipeline finished with Failed
    3 months ago
    Total: 1097s
    #422727
  • First commit to issue fork.
  • Pipeline finished with Failed
    3 months ago
    Total: 206s
    #423825
  • Pipeline finished with Success
    3 months ago
    Total: 931s
    #423829
  • 🇮🇳India annmarysruthy

    Updated comments for return values as per review comment #5.

  • Pipeline finished with Success
    3 months ago
    Total: 3643s
    #424143
  • 🇳🇿New Zealand quietone

    I went to rebase this due to recent commits and there are a huge number of conflicts. On a closer look this has changes to core/tests, which is done in another issue. All of those are out of scope and unfortunately, duplicating work in another issue. I didn't look at the scope when I visited this issue a few days ago. That seems like a good reminder to all of us to check the scope of an issue.

    I have rebased, removing the out of scope changes.

    This does need a followup work for '@return void'. I guess one in coding standards to sort that out.

  • 🇳🇿New Zealand quietone

    Also see #6 about the @return void.

  • 🇳🇿New Zealand quietone

    Did cleanup here and removed the out of scope changes and the '@return void'.

    I think what is left is to improve the comments in core/modules/system/tests/modules/render_placeholder_message_test/src/RenderPlaceholderMessageTestController.php, some of which are incomplete sentences.

  • 🇳🇿New Zealand quietone

    I read the test \Drupal\Tests\system\Functional\Render\PlaceholderMessageTest::testMessagePlaceholder to improve the descriptions for the @return descriptions in RenderPlaceholderMessageTestController.php.

    Linting has passed, so back to NR.

  • Pipeline finished with Success
    2 months ago
    Total: 387s
    #435394
  • 🇧🇪Belgium borisson_ Mechelen, 🇧🇪

    To me, this looks like it is good to go. While not all the documentation is perfect, it's a lot better than nothing.

  • First commit to issue fork.
  • Pipeline finished with Success
    2 months ago
    Total: 322s
    #438866
    • nod_ committed ce8a3d5d on 11.x
      Issue #3504776 by quietone, annmarysruthy, vighneshh, oily, smustgrave,...
  • 🇫🇷France nod_ Lille

    Committed ce8a3d5 and pushed to 11.x. Thanks!

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

Production build 0.71.5 2024