Follow-up to #2572619: Fix 'Drupal.Classes.ClassDeclaration' coding standard

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: 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.Classes.ClassDeclaration.CloseBraceAfterBody"/>

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

andypost created an issue. See original summary.

andypost’s picture

Status: Needs work » Active
Mile23’s picture

Title: Fix 'Drupal.Classes.ClassDeclaration.CloseBraceAfterBody' coding standard » Fix 'Drupal.Classes.ClassDeclaration.CloseBraceAfterBody' coding standard in core/lib
Status: Active » Needs review
FileSize
84.23 KB
$ git diff --name-only | wc -l
     868

There are a huge number of these errors, but the changes are easy to review, and auto-fix seems to be doing a good job since this is an easy rule.

Here's a patch addressing core/lib, and we can move on to other directories here or in another issue.

$ git diff --name-only core/lib/ | wc -l
     198
dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Let's go with the core/lib patch and one for all the rest of the code, given how easy they are to review.
Went through this patch and there are no problems.

alexpott’s picture

Title: Fix 'Drupal.Classes.ClassDeclaration.CloseBraceAfterBody' coding standard in core/lib » Fix 'Drupal.Classes.ClassDeclaration.CloseBraceAfterBody' coding standard
Status: Reviewed & tested by the community » Needs review

Given auto-fix let's do everything.

alexpott’s picture

Here's a patch that does this...

git diff 8.2.x HEAD --ignore-blank-lines | pbcopy

diff --git a/core/phpcs.xml.dist b/core/phpcs.xml.dist
index 66aa485..f4a9fe6 100644
--- a/core/phpcs.xml.dist
+++ b/core/phpcs.xml.dist
@@ -13,6 +13,13 @@
   <!-- Only include specific sniffs that pass. This ensures that, if new sniffs are added, HEAD does not fail.-->
   <!-- Drupal sniffs -->
   <rule ref="Drupal.Classes.ClassCreateInstance"/>
+  <rule ref="Drupal.Classes.ClassDeclaration">
+    <exclude name="Drupal.Classes.ClassDeclaration.BraceOnNewLine"/>
+    <exclude name="Drupal.Classes.ClassDeclaration.SpaceAfterName"/>
+    <exclude name="Drupal.Classes.ClassDeclaration.SpaceBeforeBrace"/>
+    <exclude name="Drupal.Classes.ClassDeclaration.SpaceBeforeExtends"/>
+    <exclude name="Drupal.Classes.ClassDeclaration.SpaceBeforeImplements"/>
+  </rule>
   <rule ref="Drupal.Classes.FullyQualifiedNamespace"/>
   <rule ref="Drupal.Classes.UnusedUseStatement"/>
   <rule ref="Drupal.CSS.ClassDefinitionNameSpacing"/>
dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Thank you alex for thinking!

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

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

  • alexpott committed 133c212 on 8.2.x
    Issue #2719695 by alexpott, Mile23: Fix 'Drupal.Classes.ClassDeclaration...

Status: Fixed » Closed (fixed)

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