Part of meta-issue #2571965: [meta] Fix PHP coding standards in core

Step 1: Preparation

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 & configure PHPCS

Install PHP CodeSniffer and the ruleset from the Coder module:

$ composer install
$ ./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.Classes.UnusedUseStatement"/>

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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mfernea created an issue. See original summary.

mfernea’s picture

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

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now 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.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now 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.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.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.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). 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.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now 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.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

quietone’s picture

Status: Postponed » Active

No longer postponed

guilhermevp’s picture

FileSize
12.36 KB

In my tests <rule ref="Drupal.Semantics.RemoteAddress"/> is returning zero errors.

guilhermevp’s picture

Status: Active » Needs review
FileSize
504 bytes

Adding phpcs.xml.dist patch.

Spokje’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Bug Smash Initiative

@guilhermevp is correct in #10, the sniff Drupal.Semantics.RemoteAddress does not currently affect any files in 9.2.x, making this a _very_ easy patch to review.

- Tests are green
- Patch just adds the sniff to core/phpcs.xml.dist

One could argue that the sniff is not added to core/phpcs.xml.dist in an alphabetical order, in which case it should come right after <rule ref="Drupal.Semantics.PregSecurity"/>.

Then again: this bit of that file isn't in alphabetical order:

    <exclude name="Drupal.Commenting.DocComment.ParamNotFirst"/>
    <exclude name="Drupal.Commenting.DocComment.SpacingBeforeTags"/>
    <exclude name="Drupal.Commenting.DocComment.LongFullStop"/>
    <exclude name="Drupal.Commenting.DocComment.ShortNotCapital"/>
    <exclude name="Drupal.Commenting.DocComment.ShortSingleLine"/>
    <exclude name="Drupal.Commenting.DocComment.MissingShort"/>

So either that's something for a follow-up at the end (if there ever will be one...) of all Coding Standard issues, or we don't do alphabetical ordering here.

RTBC for me.

longwave’s picture

Regarding sort order there is #3135933: Sort sniffs/rules in phpcs.xml.dist and write test to keep them sorted to solve it permanently.

Spokje’s picture

Excellent, thanks @longwave for seemingly knowing just about any open issue in the queue 🙏

RTBC it is and stays even more firm now.

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

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

guilhermevp’s picture

Re-rolled for 9.3.x.

alexpott’s picture

Version: 9.3.x-dev » 9.2.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed 0e5db56ef2 to 9.3.x and ba7e2fec67 to 9.2.x. Thanks!

  • alexpott committed 0e5db56 on 9.3.x
    Issue #2902548 by guilhermevp, mfernea, Spokje, quietone, longwave: Fix...

  • alexpott committed ba7e2fe on 9.2.x
    Issue #2902548 by guilhermevp, mfernea, Spokje, quietone, longwave: Fix...

Status: Fixed » Closed (fixed)

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