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: 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: 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 run 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 3: 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:

$ composer run phpcbf

This will fix the errors in place or composer will tell you that Script phpcbf --standard=core/phpcs.xml.dist --runtime-set installed_paths $($COMPOSER_BINARY config vendor-dir)/drupal/coder/coder_sniffer -- handling the phpcbf event returned with error code 1 meaning there was no files fixed and you need to fix them manually. 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

SherFengChong created an issue. See original summary.

SherFengChong’s picture

Assigned: SherFengChong » Unassigned
Status: Active » Needs review
FileSize
29.78 KB

Initial patch version.

RoSk0’s picture

Status: Needs review » Reviewed & tested by the community

Good to go.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

We have 2 new ones to fix because these files were not being scanned before:

FILE: /Volumes/devdisk/dev/drupal/core/scripts/drupal.sh
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
 62 | ERROR | [x] Equals sign not aligned with surrounding
    |       |     assignments; expected 24 spaces but found 1 space
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------


FILE: /Volumes/devdisk/dev/drupal/core/scripts/password-hash.sh
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
 20 | ERROR | [x] Equals sign not aligned correctly; expected 1 space
    |       |     but found 2 spaces
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

Time: 2 mins, 49.31 secs; Memory: 90Mb

Also:

alexpott’s picture

For the drupal.sh one i'd just move the assignment of $cmd above the comment.

idebr’s picture

Status: Needs work » Needs review
FileSize
788 bytes
30.59 KB

#4 Fixed the assignment in core/scripts/password-hash.sh

#5 Moved the assignment of $cmd

borisson_’s picture

This patch passes coding standards and after I ran composer phpcs there are no remaining errors, just like the testbot tells us.

I'm unsure about adding this fix though.

  1. +++ b/core/includes/theme.inc
    @@ -1355,9 +1355,9 @@ function template_preprocess_page(&$variables) {
    +  $variables['base_path']  = base_path();
    +  $variables['front_page'] = \Drupal::url('<front>');
    +  $variables['language']   = $language_interface;
    

    I'm not sure if this fix is correct. There isn't explicit documentation about this in https://www.drupal.org/docs/develop/standards/coding-standards#operators

    That was #2816445: Forbid using more than one space around operators, but it looks like that issue hasn't passed yet.

    So it looks like this is ok.

  2. +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php
    @@ -755,10 +755,10 @@ public function testPost() {
    -    $unparseable_request_body = '!{>}<';
    -    $parseable_valid_request_body   = $this->serializer->encode($this->getNormalizedPostEntity(), static::$format);
    -    $parseable_valid_request_body_2 = $this->serializer->encode($this->getSecondNormalizedPostEntity(), static::$format);
    -    $parseable_invalid_request_body = $this->serializer->encode($this->makeNormalizationInvalid($this->getNormalizedPostEntity(), 'label'), static::$format);
    +    $unparseable_request_body         = '!{>}<';
    +    $parseable_valid_request_body     = $this->serializer->encode($this->getNormalizedPostEntity(), static::$format);
    +    $parseable_valid_request_body_2   = $this->serializer->encode($this->getSecondNormalizedPostEntity(), static::$format);
    +    $parseable_invalid_request_body   = $this->serializer->encode($this->makeNormalizationInvalid($this->getNormalizedPostEntity(), 'label'), static::$format);
    
    @@ -985,9 +985,9 @@ public function testPatch() {
    -    $unparseable_request_body = '!{>}<';
    -    $parseable_valid_request_body   = $this->serializer->encode($this->getNormalizedPatchEntity(), static::$format);
    -    $parseable_valid_request_body_2 = $this->serializer->encode($this->getNormalizedPatchEntity(), static::$format);
    +    $unparseable_request_body         = '!{>}<';
    +    $parseable_valid_request_body     = $this->serializer->encode($this->getNormalizedPatchEntity(), static::$format);
    +    $parseable_valid_request_body_2   = $this->serializer->encode($this->getNormalizedPatchEntity(), static::$format);
    

    This moves us further away from #2816445: Forbid using more than one space around operators

idebr’s picture

#7.1 / #7.2 As far as I am concerned we are just bringing the current code base in line with our coding standards using automated testing tools. Once there is a change in the coding standard (like #2816445: Forbid using more than one space around operators), we will update the rule and then we have the tool available to fix violations automatically.

mfernea’s picture

As I see it, having more spaces before the assignment is less common, although allowed. So, if we have to modify those lines, I would also go for only one space before the assignment.

borisson_’s picture

I agree with #9, that sounds like the best way to fix this.

idebr’s picture

FileSize
2.27 KB
30.01 KB

Ohh I see what you are getting at, we just remove the 'extra' spaces. Something like this?

Status: Needs review » Needs work

The last submitted patch, 11: 2937853-11.patch, failed testing. View results

idebr’s picture

Status: Needs work » Needs review

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.

mfernea’s picture

Status: Needs review » Reviewed & tested by the community

Looks good now.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 11: 2937853-11.patch, failed testing. View results

mfernea’s picture

Issue tags: +Needs reroll
mfernea’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
30.29 KB

Here is the updated patch for 8.7.x.

mfernea’s picture

Status: Needs review » Reviewed & tested by the community

Since it's just a reroll and passes all the tests, I'm setting this to RTBC.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed 6c40e077ef to 8.7.x and da3e9773c1 to 8.6.x. Thanks!

As a coding-standards fix this is eligible for commit during 8.6.x beta.

  • alexpott committed 6c40e07 on 8.7.x
    Issue #2937853 by idebr, mfernea, SherFengChong, alexpott, borisson_:...
xjm’s picture

Issue tags: +8.6.0 release notes

We include a brief list of coding standards changes in the release notes, since they affect core, contrib, and custom projects that use our standards.

Status: Fixed » Closed (fixed)

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