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

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 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.NamingConventions.ValidVariableName.LowerCamelName 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

$ 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 -ps

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. The -s flag shows the sniffs when displaying results.

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.

CommentFileSizeAuthor
#26 3207734-26.patch166.8 KBquietone
#26 diff-21-26.txt30.36 KBquietone

Issue fork drupal-3207734

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

Spokje created an issue. See original summary.

spokje’s picture

Assigned: spokje » Unassigned
Status: Active » Needs review

A lot of changes, but I think this is an easy review:

- All phpcbf changes, so there's a nice predictable pattern in there.
- No human-readable text involved, so no thinking about the wording.
- All changes are one-line changes.

quietone’s picture

Status: Needs review » Needs work

Looks really good! Just two suggestions and one comment.

spokje’s picture

Status: Needs work » Needs review
spokje’s picture

Issue tags: +Coding standards
quietone’s picture

Status: Needs review » Needs work

Changes looks good! I made one more pass and this time I seemed to find a groove and found some more things. Including some lines that look like they can be removed.

spokje’s picture

Status: Needs work » Needs review

Nice groove there @quietone.

All threads resolved, back to NR.

quietone’s picture

Status: Needs review » Reviewed & tested by the community

Wow, that was a quick turnaround! I reviewed the latest commit and this is RTBC for me, assuming tests pass.

quietone’s picture

Status: Reviewed & tested by the community » Postponed

I just found out that the related policy issue for this fix still has work to do #2305593: [policy] Set a standard for @var inline variable type declarations. Postponing until that is sorted out.

spokje’s picture

Status: Postponed » Reviewed & tested by the community
spokje’s picture

The above is a simple re-roll/rebase/merging-of-HEAD into the MR, no code affecting this issue was harmed changed during this process.

alexpott’s picture

I think we need to schedule this one for sometime in the 9.2 beta as we're changing 170 files.

spokje’s picture

@alexpott: Do you mean this would be too disruptive for any other patches that are RTBC?

longwave’s picture

Opened #3210939: [meta] Disruptive patches for 9.2 beta as there are a number of disruptive patches waiting for beta at the moment.

spokje’s picture

Ecellent @longwave!

Can I just pick a free date or is there more to the order in that issue?

spokje’s picture

Status: Reviewed & tested by the community » Postponed

To prevent re-rolling this huge patch every time a commit is made on 9.2.x-dev (since it touches so many files), let's wait until this one is up for actually being committed in #3210939: [meta] Disruptive patches for 9.2 beta.

Postponing until then.

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.

spokje’s picture

Category: Plan » Task

Rebased MR on 9.3.x

spokje’s picture

Status: Postponed » Reviewed & tested by the community

Unpostponing since it's around "sometime in the 9.2 beta"-o'clock (@alexpott in #13), back to RTBC per #9.

alexpott’s picture

Version: 9.3.x-dev » 9.1.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Committed and pushed 4a72cc3169 to 9.3.x and d6b00c13b9 to 9.2.x. Thanks!

It'll be good to backport this one to 9.1.x to make it easier to apply patches across all 3 branches - but without the change from core/phpcs.xml.dist

  • alexpott committed 4a72cc3 on 9.3.x
    Issue #3207734 by Spokje, quietone: Fix Drupal.Commenting....

  • alexpott committed d6b00c1 on 9.2.x
    Issue #3207734 by Spokje, quietone: Fix Drupal.Commenting....
quietone’s picture

StatusFileSize
new30.36 KB
new166.8 KB

Let's see if I can reroll this.

spokje’s picture

Status: Patch (to be ported) » Needs review

I think this should be NR

andypost’s picture

Issue summary needs update, why phpcs.xml is not updated in commited patches

longwave’s picture

Status: Needs review » Reviewed & tested by the community

Backport looks correct to me.

@andypost we only enable new sniffs in x.y.0 minor releases so we can't add this to 9.1.x phpcs.xml.dist, but we can still backport the fixes to make other patches easier to port.

andypost’s picture

@longwave thanks for explanation, then it needs follow-up for 9.3 I bet

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

@andypost the phpcs.xml.dist changes are in the version that was committed to 9.2.x and 9.3.x as far as I can see.

Committed dd3d8d6 and pushed to 9.1.x. Thanks!

  • alexpott committed dd3d8d6 on 9.1.x
    Issue #3207734 by Spokje, quietone: Fix Drupal.Commenting....

Status: Fixed » Closed (fixed)

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