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. 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 or remove the line if it's currently excluded. The sniff name is in the issue title. Make sure your patch will include the addition / removal 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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

pfrenssen created an issue. See original summary.

pfrenssen’s picture

pfrenssen’s picture

Title: Fix Squiz.ControlStructures.SwitchDeclaration.SpacingAfterBreak coding standard » Fix Squiz.ControlStructures.SwitchDeclaration coding standard
Status: Active » Needs review
FileSize
97.83 KB

I had a first pass at this. It was actually possible to autofix the entire Squiz.ControlStructures.SwitchDeclaration sniff in one go.

There was one single instance where I needed to make a manual intervention, and this is not automatable. One of the code examples in Migrate was missing a case: statement:

--- a/core/modules/field/src/Plugin/migrate/process/d7/FieldInstanceDefaults.php
+++ b/core/modules/field/src/Plugin/migrate/process/d7/FieldInstanceDefaults.php
@@ -25,6 +25,7 @@ public function transform($value, MigrateExecutableInterface $migrate_executable
     foreach ($default_value as $item) {
       switch ($widget_type) {
         // Add special processing here if needed.
+        case 'my_widget':
         default:
           $default[] = $item;
       }

In here I also saw some anomalous code style which is not yet detected. I'll make an issue for this in Coder so we can fix this in a followup:

--- a/core/modules/views/src/Tests/Plugin/StyleGridTest.php
+++ b/core/modules/views/src/Tests/Plugin/StyleGridTest.php
@@ -80,9 +80,13 @@ protected function assertGrid(ViewExecutable $view, $alignment, $columns) {
     $width = '0';
     switch ($columns) {
       case 5: $width = '20'; break;
+
       case 4: $width = '25'; break;
+
       case 3: $width = '33.3333'; break;
+
       case 2: $width = '50'; break;
+
       case 1: $width = '100'; break;
     }
     // Ensure last column exists.
pfrenssen’s picture

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

hgunicamp’s picture

Status: Needs review » Needs work

I tested the '2824935-3.patch' patch and it fail in some files

cit008490cpsubuntu:drupal progestag* 8.4.x$ find . -type f -name '*rej'
./core/modules/views/views.tokens.inc.rej
./core/modules/views/src/Plugin/views/display/Page.php.rej
./core/modules/migrate/src/MigrateExecutable.php.rej
./core/modules/search/src/Tests/SearchRankingTest.php.rej
./core/modules/block/src/Plugin/migrate/source/Block.php.rej
./core/modules/taxonomy/taxonomy.module.rej
./core/modules/rest/src/Tests/RESTTestBase.php.rej

hgunicamp’s picture

Status: Needs work » Needs review
FileSize
106.4 KB

I inserted a new patch to fix the rejected changes.

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

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.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 » Needs work

No sniffs should be excluded from Squiz.ControlStructures.SwitchDeclaration. As I understand from the issue title and #3 we should fix all Squiz.ControlStructures.SwitchDeclaration sniffs.

mfernea’s picture

This is the report for the 8.5.x branch:

-----------------------------------------------------------------------------
    SOURCE                                                              COUNT
-----------------------------------------------------------------------------
[x] Squiz.ControlStructures.SwitchDeclaration.CaseIndent                1556
[x] Squiz.ControlStructures.SwitchDeclaration.SpacingAfterBreak         400
[ ] Squiz.ControlStructures.SwitchDeclaration.MissingDefault            292
[x] Squiz.ControlStructures.SwitchDeclaration.DefaultIndent             118
[ ] Squiz.ControlStructures.SwitchDeclaration.EmptyCase                 20
[ ] Squiz.ControlStructures.SwitchDeclaration.SpacingBeforeBreak        18
[ ] Squiz.ControlStructures.SwitchDeclaration.DefaultNoBreak            17
[x] Squiz.ControlStructures.SwitchDeclaration.BreakIndent               7
[ ] Squiz.ControlStructures.SwitchDeclaration.SpacingAfterCase          6
[ ] Squiz.ControlStructures.SwitchDeclaration.SpacingAfterDefaultBreak  3
[ ] Squiz.ControlStructures.SwitchDeclaration.EmptyDefault              2
[x] Squiz.ControlStructures.SwitchDeclaration.SpaceBeforeColonCase      1
[ ] Squiz.ControlStructures.SwitchDeclaration.MissingCase               1

There are many sniffs with a lot of issues, so I think we should start creating issues for individual sniffs.

mfernea’s picture

Title: Fix Squiz.ControlStructures.SwitchDeclaration coding standard » [meta] Fix Squiz.ControlStructures.SwitchDeclaration coding standard
Category: Task » Plan
Issue summary: View changes
Status: Needs work » Active
Issue tags: +Coding standards

I transformed the issue into a "plan". I will add child issues for each sniff.
I also updated the instructions.

mfernea’s picture

Title: [meta] Fix Squiz.ControlStructures.SwitchDeclaration coding standard » Fix Squiz.ControlStructures.SwitchDeclaration coding standard
Category: Plan » Task
Issue summary: View changes
Status: Active » Needs work

Actually, I was wrong.

Drupal CS' ruleset.xml has the following code:

 <rule ref="Squiz.ControlStructures.SwitchDeclaration" />
 <!-- Disable some error messages that we do not want. -->
 <rule ref="Squiz.ControlStructures.SwitchDeclaration.BreakIndent">
  <severity>0</severity>
 </rule>
 <rule ref="Squiz.ControlStructures.SwitchDeclaration.CaseIndent">
  <severity>0</severity>
 </rule>
 <rule ref="Squiz.ControlStructures.SwitchDeclaration.CloseBraceAlign">
  <severity>0</severity>
 </rule>
 <rule ref="Squiz.ControlStructures.SwitchDeclaration.DefaultIndent">
  <severity>0</severity>
 </rule>
 <rule ref="Squiz.ControlStructures.SwitchDeclaration.DefaultNoBreak">
  <severity>0</severity>
 </rule>
 <rule ref="Squiz.ControlStructures.SwitchDeclaration.EmptyCase">
  <severity>0</severity>
 </rule>
 <rule ref="Squiz.ControlStructures.SwitchDeclaration.EmptyDefault">
  <severity>0</severity>
 </rule>
 <rule ref="Squiz.ControlStructures.SwitchDeclaration.MissingDefault">
  <severity>0</severity>
 </rule>
 <rule ref="Squiz.ControlStructures.SwitchDeclaration.SpacingAfterCase">
  <severity>0</severity>
 </rule>
 <rule ref="Squiz.ControlStructures.SwitchDeclaration.SpacingAfterDefaultBreak">
  <severity>0</severity>
 </rule>
 <rule ref="Squiz.ControlStructures.SwitchDeclaration.SpacingBeforeBreak">
  <severity>0</severity>
 </rule>

Currently we have:

PHP CODE SNIFFER VIOLATION SOURCE SUMMARY
----------------------------------------------------------------------
    SOURCE                                                       COUNT
----------------------------------------------------------------------
[x] Squiz.ControlStructures.SwitchDeclaration.SpacingAfterBreak  400
[ ] Squiz.ControlStructures.SwitchDeclaration.MissingCase        1
----------------------------------------------------------------------
A TOTAL OF 401 SNIFF VIOLATIONS WERE FOUND IN 2 SOURCES
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SOURCES AUTOMATICALLY (400 VIOLATIONS IN TOTAL)
----------------------------------------------------------------------

So, indeed, it can be fixed in one go.

phpcs.xml.dist should contain the exact same code from Drupal CS' ruleset.xml related to this sniff.

mfernea’s picture

Status: Needs work » Needs review
FileSize
116.21 KB

Here is the patch.

andypost’s picture

Status: Needs review » Reviewed & tested by the community

This is a good step & sane patch size. Maybe it needs follow-up to get rid of warnings as well

+++ b/core/phpcs.xml.dist
@@ -207,6 +207,43 @@
+  <rule ref="Squiz.ControlStructures.SwitchDeclaration" />
+  <!-- Disable some error messages that we do not want. -->
+  <rule ref="Squiz.ControlStructures.SwitchDeclaration.BreakIndent">
+    <severity>0</severity>
+  </rule>
+  <rule ref="Squiz.ControlStructures.SwitchDeclaration.CaseIndent">
+    <severity>0</severity>
+  </rule>
+  <rule ref="Squiz.ControlStructures.SwitchDeclaration.CloseBraceAlign">
+    <severity>0</severity>
+  </rule>
+  <rule ref="Squiz.ControlStructures.SwitchDeclaration.DefaultIndent">
+    <severity>0</severity>
+  </rule>
+  <rule ref="Squiz.ControlStructures.SwitchDeclaration.DefaultNoBreak">
+    <severity>0</severity>
+  </rule>
+  <rule ref="Squiz.ControlStructures.SwitchDeclaration.EmptyCase">
+    <severity>0</severity>
+  </rule>
+  <rule ref="Squiz.ControlStructures.SwitchDeclaration.EmptyDefault">
+    <severity>0</severity>
+  </rule>
+  <rule ref="Squiz.ControlStructures.SwitchDeclaration.MissingDefault">
+    <severity>0</severity>
+  </rule>
+  <rule ref="Squiz.ControlStructures.SwitchDeclaration.SpacingAfterCase">
+    <severity>0</severity>
+  </rule>
+  <rule ref="Squiz.ControlStructures.SwitchDeclaration.SpacingAfterDefaultBreak">
+    <severity>0</severity>
+  </rule>
+  <rule ref="Squiz.ControlStructures.SwitchDeclaration.SpacingBeforeBreak">
+    <severity>0</severity>
+  </rule>

this is a good compromise!

mfernea’s picture

@andypost Why do you think we need a follow-up? We shouldn't go beyond Drupal CS. If it's excluding some sniffs, we should exclude them too in core's phpcs.xml.dist. The patch doesn't exclude more or less than Drupal CS' ruleset.xml does. So we should be fine with this patch only.

andypost’s picture

@mfernea I think final polishing suppose no warnings & removal of severity)

mfernea’s picture

Actually, no. The goal of the parent meta is to make Drupal core follow Drupal CS. So we should not worry about sniffs / warnings that Drupal CS excludes (using severity) in its ruleset.xml.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 13: drupal-coding-standards-2824935-14.patch, failed testing. View results

mfernea’s picture

Assigned: Unassigned » mfernea
mfernea’s picture

Assigned: mfernea » Unassigned
Status: Needs work » Needs review
FileSize
116.81 KB

Re-roll.

Status: Needs review » Needs work

The last submitted patch, 20: drupal-coding-standards-2824935-21.patch, failed testing. View results

mfernea’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 20: drupal-coding-standards-2824935-21.patch, failed testing. View results

mfernea’s picture

Status: Needs work » Needs review
andypost’s picture

Status: Needs review » Reviewed & tested by the community

CI still shows 1 coding standard issue but it's not related to the issue

mfernea’s picture

Indeed, it's not related and it's fixed now. I launched a new test now just to make it clearer.

alexpott’s picture

Status: Reviewed & tested by the community » Needs review
+++ b/core/modules/field/src/Plugin/migrate/process/d7/FieldInstanceDefaults.php
@@ -33,6 +33,7 @@ public function transform($value, MigrateExecutableInterface $migrate_executable
       switch ($widget_type) {
         // Add special processing here if needed.
+        case 'my_widget':
         default:
           $default[] = $item;
       }

I'm not sure about this change. Surely the right thing to do here is to remove the switch? Perhaps we should file an issue to just discuss how to change this because otherwise we're going to have context issue. This change needs to be reviewed wrt to migrate not coding standards.

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.

idebr’s picture

Title: Fix Squiz.ControlStructures.SwitchDeclaration coding standard » [PP-1] Fix Squiz.ControlStructures.SwitchDeclaration coding standard
Status: Needs review » Postponed
Related issues: +#2972045: Drupal\field\Plugin\migrate\process\d7\FieldInstanceDefaults contains a switch with only a default statement

Filed an issue in the migration system: #2972045: Drupal\field\Plugin\migrate\process\d7\FieldInstanceDefaults contains a switch with only a default statement

I suggest we postpone this issue until it is resolved.

idebr’s picture

Status: Postponed » Needs work
Issue tags: +Needs reroll
idebr’s picture

Title: [PP-1] Fix Squiz.ControlStructures.SwitchDeclaration coding standard » Fix Squiz.ControlStructures.SwitchDeclaration coding standard
jofitz’s picture

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

Re-rolled.

borisson_’s picture

Status: Needs review » Needs work

The testbot gives us 23 new failures. So those need to be fixed as well.

idebr’s picture

Status: Needs work » Needs review
FileSize
7.82 KB
121.78 KB

Fixed the remaining failures with phpcbf

Status: Needs review » Needs work

The last submitted patch, 34: 2824935-34.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.

idebr’s picture

FileSize
121.5 KB

Reroll against 8.7.x

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.

longwave’s picture

FileSize
125.71 KB

Recreated for 9.1.x.

longwave’s picture

Status: Needs review » Needs work

Self-review:

+++ b/core/modules/workspaces/tests/src/Functional/EntityResource/WorkspaceResourceTestBase.php
@@ -186,15 +189,19 @@ protected function getExpectedUnauthorizedAccessMessage($method) {
     switch ($method) {
       case 'GET':
         return "The 'view any workspace' permission is required.";
+
       break;
       case 'POST':
         return "The 'create workspace' permission is required.";
+
       break;
       case 'PATCH':
         return "The 'edit any workspace' permission is required.";
+
       break;
       case 'DELETE':
         return "The 'delete any workspace' permission is required.";
+
       break;

The breaks here can be removed.

Deepak Goyal’s picture

Assigned: Unassigned » Deepak Goyal
Deepak Goyal’s picture

Status: Needs work » Needs review
FileSize
125.71 KB
903 bytes

Hi @longwave
As you suggested removed breaks and created patch for same please review.

daffie’s picture

Status: Needs review » Needs work

Which PHPCS rule are we changing here. The patch does not contain any changes to core/phpcs.xml.dist.

Deepak Goyal’s picture

Hi @daffie,
No fixable errors were found in core/phpcs.xml.dist file.

longwave’s picture

Status: Needs work » Needs review
+++ b/core/phpcs.xml.dist
@@ -292,6 +292,41 @@
+  <rule ref="Squiz.ControlStructures.SwitchDeclaration" />
+  <!-- Disable some error messages that we do not want. -->
+  <rule ref="Squiz.ControlStructures.SwitchDeclaration.BreakIndent">
+    <severity>0</severity>
+  </rule>
+  <rule ref="Squiz.ControlStructures.SwitchDeclaration.CaseIndent">
+    <severity>0</severity>
+  </rule>
+  <rule ref="Squiz.ControlStructures.SwitchDeclaration.CloseBraceAlign">
+    <severity>0</severity>
+  </rule>
+  <rule ref="Squiz.ControlStructures.SwitchDeclaration.DefaultIndent">
+    <severity>0</severity>
+  </rule>
+  <rule ref="Squiz.ControlStructures.SwitchDeclaration.DefaultNoBreak">
+    <severity>0</severity>
+  </rule>
+  <rule ref="Squiz.ControlStructures.SwitchDeclaration.EmptyCase">
+    <severity>0</severity>
+  </rule>
+  <rule ref="Squiz.ControlStructures.SwitchDeclaration.EmptyDefault">
+    <severity>0</severity>
+  </rule>
+  <rule ref="Squiz.ControlStructures.SwitchDeclaration.MissingDefault">
+    <severity>0</severity>
+  </rule>
+  <rule ref="Squiz.ControlStructures.SwitchDeclaration.SpacingAfterCase">
+    <severity>0</severity>
+  </rule>
+  <rule ref="Squiz.ControlStructures.SwitchDeclaration.SpacingAfterDefaultBreak">
+    <severity>0</severity>
+  </rule>
+  <rule ref="Squiz.ControlStructures.SwitchDeclaration.SpacingBeforeBreak">
+    <severity>0</severity>
+  </rule>

@daffie The latest patch does contain phpcs.xml.dist?

daffie’s picture

Status: Needs review » Reviewed & tested by the community

Deepak Goyal & @longwave: Sorry, I missed the phpcs part in the 125 KB patch.

Tested on my local machine with only the core/phpcs.xml.dist part of the patch and I got a whole lot of errors with the message: "Case breaking statements must be followed by a single blank line".
Tested on my local machine with the whole patch and no erros where returned.
Reviewed the patch and all changes look good to me.
Almost all changes are the adding of empty lines and in a couple of places unnecessary break; are removed.
For me it is RTBC.

alexpott’s picture

Version: 9.1.x-dev » 8.9.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed 9d1faa52f4 to 9.1.x and a4cc432a2e to 9.0.x. Thanks!

I backported the changes to 8.9.x without the phpcs.xml.dist change and without the changes to core/lib/Drupal/Core/Database/Connection.php because there were conflicts. This helps us backport bugfixes to 8.9.x which pointless conflicts.

  • alexpott committed a4cc432 on 9.0.x
    Issue #2824935 by idebr, mfernea, Deepak Goyal, pfrenssen, longwave,...

  • alexpott committed 115a338 on 8.9.x
    Issue #2824935 by idebr, mfernea, Deepak Goyal, pfrenssen, longwave,...

  • alexpott committed 9d1faa5 on 9.1.x
    Issue #2824935 by idebr, mfernea, Deepak Goyal, pfrenssen, longwave,...

Status: Fixed » Closed (fixed)

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

xjm’s picture

Issue tags: +9.1.0 release notes