Follow-up to #2572659: Fix 'Drupal.Commenting.InlineComment.SpacingAfter' coding standard

Part of #2571965: [meta] Fix PHP coding standards in core, stage 1.

This relates to Drupal API documentation standards (general) and Drupal standards for in-line code comments.

Approach

We are testing coding standards with PHP CodeSniffer, using the Drupal coding standards from the Coder module. Both of these packages are not installed in Drupal core. 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 to the whitelist

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". There are dozens of such coding standards, and to make the work easier we have started by only whitelisting the sniffs that pass. For the moment all coding standards that are not yet fixed are simply skipped during the test.

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

Both of these packages are not installed by default in Drupal core, so we need to download them. This can be done with Composer, from the root folder of your Drupal installation:

$ composer require drupal/coder squizlabs/php_codesniffer
$ ./vendor/bin/phpcs --config-set installed_paths ../../drupal/coder/coder_sniffer

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 big list of sniffs, and the Drupal-based ones should be present.

Step 3: Prepare the phpcs.xml file

To speed up the testing you should make a copy of the file phpcs.xml.dist (in the core/ folder) and save it as phpcs.xml. This is the configuration file for PHP CodeSniffer.

We only want this phpcs.xml file to specify the sniff we're interested in. So we need to remove all the rule items, and add only our own sniff's rule. Rule items look like this:

<rule ref="Drupal.Commenting.InlineComment.NotCapital"/>

Remove all of them, and add only the sniff from this issue title. This will make sure that our tests run quickly, and are not going to contain any output from unrelated sniffs.

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:

$ cd core/
$ ../vendor/bin/phpcs -p

This takes a couple of minutes. The -p flag shows the progress, so you have a bunch of nice dots to look at while it is running.

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. You could fix all of these manually, but thankfully phpcbf can fix many of them. You can call phpcbf like this:

$ ../vendor/bin/phpcbf

This will fix the errors in place. You can then make a diff of the changes using git. You can also re-run the test with phpcs and determine if that fixed all of them.

Issue fork drupal-2719657

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.

pashupathi nath gajawada’s picture

Assigned: Unassigned » pashupathi nath gajawada

I'm on it.

andypost’s picture

Status: Active » Needs review
StatusFileSize
new84.74 KB
new54.74 KB
new88.04 KB

This issue has collisions with other inline comments.

phpcbf fixed most of lines but I manually edited that places to conform standards (see interdiff)

Status: Needs review » Needs work

The last submitted patch, 4: 2719657-manual-4.patch, failed testing.

mile23’s picture

Unfortunately neither patch applies.

dawehner’s picture

Issue tags: +Needs reroll

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

ilya.no’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll +Needs Review
StatusFileSize
new86.68 KB

Check, please, re-rolled patch.

Status: Needs review » Needs work

The last submitted patch, 9: 2719657-manual-reroll-9.patch, failed testing.

alexpott’s picture

Issue tags: -Needs Review
  1. +++ b/core/lib/Drupal/Core/Database/StatementInterface.php
    @@ -36,8 +36,11 @@
    +   *
    +   * @todo Move out of interface in https://www.drupal.org/node/2168241
    +   *
    +   * protected function __construct(Connection $dbh);
        */
    -  //protected function __construct(Connection $dbh);
    

    This looks wrong. Let's add the @todo but remove the code.

  2. +++ b/core/lib/Drupal/Core/FileTransfer/FTPExtension.php
    @@ -108,7 +108,7 @@ function chmodJailed($path, $mode, $recursive) {
    -        //empty directory - returns false
    +        // Empty directory - returns FALSE.
             return;
    

    Well it doesn't return FALSE. I think we should just make the comment // Empty directory.

  3. +++ b/core/lib/Drupal/Core/StreamWrapper/LocalStream.php
    @@ -242,8 +242,8 @@ public function stream_eof() {
    -    // fseek returns 0 on success and -1 on a failure.
    -    // stream_seek   1 on success and  0 on a failure.
    +    // The fseek() returns 0 on success and -1 on a failure.
    +    // The stream_seek()   1 on success and  0 on a failure.
    

    This looks wrong. The fseek() is odd. The PHPCS rule should be clever enough to allow comments to begin with function names imo.

  4. +++ b/core/modules/color/color.module
    @@ -681,7 +681,7 @@ function _color_shift($given, $ref1, $ref2, $target) {
    -  // delta = 1 - |ref2 - ref1| / |white - ref1|
    +  // Calculates delta = 1 - |ref2 - ref1| / |white - ref1|.
    

    Hmmm this all looks odd.

  5. +++ b/core/modules/field/tests/src/Kernel/Migrate/d7/MigrateFieldFormatterSettingsTest.php
    @@ -132,7 +132,7 @@ protected function setUp() {
    -                  // settings is always expected to be an array. Making it NULL
    +                  // Settings is always expected to be an array. Making it NULL
                       // causes a fatal.
    

    Maybe The 'settings' value is... since Settings is not in the array.

  6. +++ b/core/modules/filter/filter.module
    @@ -496,8 +496,8 @@ function _filter_url($text, $filter) {
    -  //full path
    -  //and allow @ in a url, but only in the middle. Catch things like http://example.com/@user/
    +  // Allow @ in a URL but only in the middle.
    +  // Catch things like http://example.com/@user/.
    

    This comment is not correctly wrapped.

  7. +++ b/core/modules/node/src/Plugin/migrate/source/d6/Node.php
    @@ -113,7 +113,7 @@ public function fields() {
    +    // The format can be 0 when the body field is hidden. Set the format to 1
         // to avoid migration map issues (since the body field isn't used anyway).
    

    Comment wrapping. to can now go on the previous line.

  8. +++ b/core/modules/views_ui/src/Form/Ajax/RearrangeFilter.php
    @@ -119,7 +119,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
    -      $form['remove_groups'][$id] = array(); // to prevent a notice
    +      $form['remove_groups'][$id] = array(); // To prevent a notice.
    

    Bit of a useless comment.

andypost’s picture

@alexpott do you think we should manually fix all this comments or use phpcbf (suppose this wrong in that case)

Because maybe better to file separate issue to actualize pointed places?

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

pfrenssen’s picture

Version: 8.4.x-dev » 8.3.x-dev
Status: Needs work » Postponed

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

quietone’s picture

Version: 8.9.x-dev » 9.3.x-dev
Assigned: pashupathi nath gajawada » Unassigned
Issue summary: View changes
Status: Postponed » Needs review

This was postponed on an issue that is now closed as outdated, so work can begin again.

#11.3 Here alexpott thinks the sniff should be smarter so that lower case function names can start a sentence. Should this be postponed on improvements to the sniff or add ignore lines before comments like that?

quietone’s picture

StatusFileSize
new81.83 KB
new81.1 KB

Rerolled the patch in #9. Still need to address #11.

quietone’s picture

StatusFileSize
new2.79 KB
new81.09 KB

And now respond to #11.

1. No longer relevant, constructor removed.
2. Fixed.
3. Surrounded comment with phpcs:disable/enable
4. Looks odd to me to. Not sure what to do to improve it.
5. No longer relevant
6. Wrapping fixed.
7. Wrapping fixed.
8. No longer relevant.

The drupalci.yml file is changed to just run commit-code-check. I did that in the patch in #21 but made a mistake.

quietone’s picture

StatusFileSize
new15.65 KB
new81.12 KB

Add periods to end of comments.

I also noticed that the sniff finds // prefix but not // prefix.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mile23’s picture

StatusFileSize
new79.8 KB

A do-over is easier than a re-roll.

Special call-out to core/modules/views/tests/src/Functional/Plugin/ArgumentDefaultTest.php, where a test function was commended out with a todo, and now I've upgraded it to a test marked incomplete. This makes me wonder if #2905007: Allow run-tests.sh to report skipped/incomplete PHPUnit tests is still needed, but that's for later. My wager is that a reviewer will want to just delete the test function.

The rest of the changes are pretty simple, and it looks like all the concerns from #11 have been refactored away in some form or another over time.

mile23’s picture

StatusFileSize
new79.8 KB
new619 bytes

Must declare visibility....

erikaagp’s picture

Status: Needs review » Reviewed & tested by the community
alexpott’s picture

Status: Reviewed & tested by the community » Needs work

We're going to need a patch for 10.1.x / 10.0.x too.

Also I think we need to have a think about some of the comments that are being changed here.

  1. +++ b/core/modules/file/tests/src/Kernel/CopyTest.php
    @@ -165,7 +165,7 @@ public function testExistingError() {
    -      // expected exception.
    +      // Expected exception.
    
    +++ b/core/modules/file/tests/src/Kernel/FileRepositoryTest.php
    @@ -151,7 +151,7 @@ public function testExistingError() {
    -      // expected exception.
    +      // Expected exception.
    
    +++ b/core/modules/file/tests/src/Kernel/MoveTest.php
    @@ -154,7 +154,7 @@ public function testExistingReplaceSelf() {
    -      // expected exception.
    +      // Expected exception.
    
    @@ -186,7 +186,7 @@ public function testExistingError() {
    -      // expected exception.
    +      // Expected exception.
    

    This comment is not worthwhile.

  2. +++ b/core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php
    @@ -373,7 +373,7 @@ public function providerTestRenderAsLinkWithPathAndOptions() {
         // entity_type flag.
         $entity_type_id = 'node';
         $data[] = ['test-path', ['entity_type' => $entity_type_id], '<a href="/test-path">value</a>'];
    -    // prefix
    +    // Prefix
         $data[] = ['test-path', ['prefix' => 'test_prefix'], '<a href="/test-path">value</a>', 'test_prefix<a href="/test-path">value</a>'];
         // suffix.
         $data[] = ['test-path', ['suffix' => 'test_suffix'], '<a href="/test-path">value</a>', '<a href="/test-path">value</a>test_suffix'];
    

    Why not // suffix. too - also this would be better to change all this to keys for $data.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

ayush.khare’s picture

StatusFileSize
new78.43 KB
new39.03 KB

10.1.x Reroll for #27

ayush.khare’s picture

StatusFileSize
new3.29 KB
new80.45 KB

Fixed CCF in #31

spokje’s picture

Status: Needs work » Needs review
himanshu_jhaloya’s picture

Assigned: Unassigned » himanshu_jhaloya

I will review the patch

himanshu_jhaloya’s picture

Assigned: himanshu_jhaloya » Unassigned

#31 #32 patch failed to applied

spokje’s picture

Status: Needs review » Needs work

Can we not "just" get a reroll but can we also, at the same time, address the points in #29?

spokje’s picture

Assigned: Unassigned » spokje
spokje’s picture

StatusFileSize
new8.35 KB
new83.21 KB
spokje’s picture

Assigned: spokje » Unassigned
Status: Needs work » Needs review
mile23’s picture

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

Patch in #39 no longer applies. Using git apply --rej shows us that all changes apply except the one to core/phpunit.xml.dist, and then passes composer phpcs. So it should be easy to reroll.

MR in #35 seems broken.

In an ideal world, the MR would reflect the current changes, but it might be less work just to stick with patches. Dunno.

ankithashetty’s picture

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

Hello @Mile23! Could you please try against the latest core 10.1.x? The #39 patch still applies successfully.
Also, there is no change made to file core/phpunit.xml.dist in the patch.🤔

Restoring back to the previous state. Thanks!

rpayanm’s picture

Status: Needs review » Reviewed & tested by the community

I created a patch following the steps described in the IS and compared it with the patch from #39, I saw some differences, but they were discussed in the comments. So it looks good for me.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 39: 2719657-39.patch, failed testing. View results

xjm’s picture

Status: Needs work » Reviewed & tested by the community

The test failure was a known random failure, so retesting and setting back RTBC.

xjm’s picture

Closing the broken MR for clarity and removing credit for it.

xjm’s picture

Is there a separate rule we can enable for comments that don't end in a period? Because a lot of these qualify.

Edit: Similarly an inline comment line length rule.

xjm’s picture

Status: Reviewed & tested by the community » Needs work
  1. Etag must match.
    

    I think ETag is actually the canonical capitalization. The method is only named getEtag() because of our camel-casing spelling rules.
     

  2. I think we need to rethink how we're addressing the comments in
    core/modules/system/tests/src/Kernel/Migrate/d6/MigrateSystemConfigurationTest.php.

    For example

    • Uuid is not handled by the migration.
      

      This should be "UUID" when used in a sentence.

    • Autorun is not handled by the migration.
      

      If this is used as a word in a sentence, it should be "Auto-run".

    • Langcode and default_langcode are not handled by the migration.

      This is inconsistent. Either both langcode and default_langcode are machine names, or they should be treated as words in a sentence, i.e. "Language code" and "Default language code".

    For all of the above -- and other similar comments in the same file about gzip and such I think the sentence should be rephrased as:

    The 'foo' property is not handled by the migration.

    I'm not sure if it's a "property", maybe it's a "configuration" or a "settting" or something else. Use the appropriate noun in context.
     

  3. // suggestionnotimplemented() is not an implemented theme hook so

    I could not find suggestionnotimplemented() anywhere in our API. It appears to be test data used in ThemeTest and RendererTest. The CSpell issues will need to fix this eventually, but just adding parentheses is fine for now.

  4.     // Allow + for or, , for and
    

    Uh, what? Reading the code, I think this means:

    // Allow '+' for "or". Allow ',' for "and".
    

    (Though who would think + meant "or"?) Anyway. Followup for this.

  5.     // Init the table queue with our primary table.
    

    This needs to use the full word, "Initialize".
     

  6. There is an out-of-scope hunk in core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php adding string typehints to a method.
     

  7. That same class rewrites a data provider to use meaningful array keys rather than inline comments on the cases, which is a good change. That could be handled in a separate, blocking issue to make things easier.
     

  8. The hunks in core/modules/views/views.api.php are weird; we have the sentence fragments:
    // Although the table aliases will be different.

    I looked at this in context and the inline comment before the code snippet is:

      // If you've decided an automatic join is a good idea, here's how to do it;   
      // the resulting SQL query will look something like this: 
    

    So, I think we should changes these two to:

    // (The table aliases will be different.)
    
  9.     // Todo mock a request with a route.
    

    This should actually be changed to:

        // @todo Mock a request with a route.
    
Aadhar_Gupta’s picture

StatusFileSize
new83.54 KB
new3.43 KB

Changes described in comment number 49 have been applied.

Here is the patch and inter-diff for the same -

Aadhar_Gupta’s picture

Status: Needs work » Needs review
xjm’s picture

Status: Needs review » Needs work

Very little of #49 is actually addressed, at least based on the interdiff posted.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Converted to an MR, ran phpbcf and then started work on #49.

#49.
1. Fixed
2. The first two are changed as asked for. The third corrects 'Langcode' but does not rework the sentences to use quotes around the variable names. I agree that it is correct and we are touching that line but I stopped because of the comment suggests it be done in the rest of the file. That would be out of scope. Even with inconsistencies across files I prefer to keep the style of comments in a file the same for readability.
3. Fixed in #3395900: Correct spelling of words in module core/tests
4. Still needs a followup.
5. Fixed.
6, 7 No longer relvant
8. No change. The suggested change doesn't make sense to me at this late hour.
9. Fixed

quietone’s picture

Status: Needs work » Needs review

#49.
4. Fixed.
8. Fixed

smustgrave’s picture

Of the points in #49 only 49.2 was one I had a question on. Left the question in the MR but will leave in review for others.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new90 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

quietone’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

My one comment/concern has been answered. Believe this is good

  • catch committed 833aa93b on 11.0.x
    Issue #2719657 by quietone, andypost, Mile23, Spokje, ilya.no, xjm,...

  • catch committed d2fb43a8 on 10.3.x
    Issue #2719657 by quietone, andypost, Mile23, Spokje, ilya.no, xjm,...

  • catch committed 47c7289d on 10.4.x
    Issue #2719657 by quietone, andypost, Mile23, Spokje, ilya.no, xjm,...

  • catch committed c31d595f on 11.x
    Issue #2719657 by quietone, andypost, Mile23, Spokje, ilya.no, xjm,...
catch’s picture

Version: 11.x-dev » 10.3.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to 11.x. I also committed/cherry-picked the same diff minus the actual phpcs.xml.dist change, to 11.0.x, 10.4.x, and 10.3.x to keep codebase in sync.

Status: Fixed » Closed (fixed)

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