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

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: Remove the coding standard from the blacklist

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 blacklisting all the sniffs. 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 remove the line that matches the sniff of this ticket (it's in the issue title). Make sure your patch will include the removal 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

Step 3: Prepare the phpcs.xml file

Next we need to 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 have to configure the location of the Drupal coding standard ruleset from the Coder module that we just downloaded. To do this open the newly created file core/phpcs.xml. Find the line containing:

<rule ref="Drupal">

Change it to:

<rule ref="../vendor/drupal/coder/coder_sniffer/Drupal">

Or if you prefer the command line:

$ cd core/
$ cp phpcs.xml.dist phpcs.xml
$ perl -pi -e "s|ref=\"Drupal|ref=\"../vendor/drupal/coder/coder_sniffer/Drupal|" phpcs.xml

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:

$ ../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. Go ahead and fix them all!

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

attiks created an issue. See original summary.

Status: Needs review » Needs work

The last submitted patch, Drupal.Files_.EndFileNewline.patch, failed testing.

maris.abols’s picture

Assigned: Unassigned » maris.abols
attiks’s picture

Issue summary: View changes
DuaelFr’s picture

Issue tags: -Novice

As agreed between the mentors at Drupalcon, according to issues to avoid for novices, I am untagging this issue as "Beginner". This issue contains changes across a very wide range of files and might create too many other patches to need to be rerolled at this particular time. This patch has an automated way to be rerolled later so better to implement it after Drupalcon.

maris.abols’s picture

Assigned: maris.abols » Unassigned
pfrenssen’s picture

Issue summary: View changes

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

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

alexpott’s picture

Status: Needs work » Needs review
FileSize
37.04 KB

Let's add the rule and run fixer...

pfrenssen’s picture

Status: Needs review » Reviewed & tested by the community

This was the easiest 30 kB code review ever.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed c59b953 and pushed to 8.1.x and 8.2.x. Thanks!

  • alexpott committed 1b3ffe2 on 8.2.x
    Issue #2572707 by attiks, alexpott: Fix 'Drupal.Files.EndFileNewline'...

Status: Fixed » Closed (fixed)

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