Part of #2571965: [meta] Fix PHP coding standards in core, stage 1 and a follow-up to #2937513: Fix 'Drupal.Commenting.DocComment.TagGroupSpacing' coding standard
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.Commenting.DocComment.TagGroupSpacing"/>
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.
| Comment | File | Size | Author |
|---|---|---|---|
| #10 | 3183656-10.interdiff-8-10.txt | 927 bytes | jonathan1055 |
| #10 | 3183656-10.tag-group-spacing.patch | 6.82 KB | jonathan1055 |
Comments
Comment #2
jonathan1055 commentedIn #2937513: Fix 'Drupal.Commenting.DocComment.TagGroupSpacing' coding standard there was quite a bit of discussion about Coder sniffs, and some changes were made. There were also some questions about why missing blank lines were not detected.
In #3181485: Regression in @ tag sniff TagGroupSpacing I discovered that the sniff was indeed not detecting all of the correct places where blank lines should inserted. Now that this is fixed, there are 12 additional places in core code which can be corrected.
The Coder fix is committed in dev, and will be available when Core 9.2 uses the new version 8.3.12 (not released yet). However, as this sniff is already enabled in Core, we should fix the 12 faults now, so that when 8.3.12 is used the problems do not suddenly appear.
locally gives
I will make a patch to demonstrate the errors using the dev version of Coder
Comment #3
jonathan1055 commentedAttempt to run the sniff using the Coder dev package.
Comment #4
jonathan1055 commentedPatch #4 only changes drupalci.yml, no change to phpcs.xml.dist
Comment #5
jonathan1055 commentedSo if the patch alters phpcs.xml(.dist) then we get:
But if the patch only changes drupalci.yml then we get
Now trying with
Comment #6
jonathan1055 commentedSo it looks like Coder 8.x-3.x-dev is being loaded by Composer. All files are being sniffed. But we still do not get new failures. Trying with a patch to the Coder folder.
Comment #7
jonathan1055 commentedThat worked. We now get the expected 12 coding standards warning which I see locally.Here's a patch to fix these 12.
Comment #8
jonathan1055 commentedNow the real patch to be committed, with no change to drupalci.yml
Comment #9
daffie commentedI am missing the change to the file
core/phpcs.xml.dist.Comment #10
jonathan1055 commentedHi @daffie,
Thanks for the comment. However, the sniff
Drupal.Commenting.DocComment.TagGroupSpacingis already enabled and active in core as there is no exclude for it - see core/phpcs.xml.dist#L47 and my comments in #2 above - the sniff was incorrectly not detecting these errors, so we need to fix them in core before the new Coder version gets used.I notice that the sniff name is not mentioned in the phpcs.xml.dist comment listing the sniffs which are checked. I looked back and this sniff was fixed and removed from the exclusion list in https://git.drupalcode.org/project/drupal/commit/4f61b6f but the comment was not changed. That can be ammeded here, and the same goes for the sniffs
DocComment.EmptyandDocComment.TagsNotGroupedwhich are already active.Comment #11
daffie commentedThe patch adds the Drupal.Commenting.DocComment.TagGroupSpacing, Drupal.Commenting.DocComment.Empty and Drupal.Commenting.DocComment.TagsNotGrouped rules to PHPCS.
All violations are fixed.
All code changes look good to me.
For me it is RTBC.
Comment #12
jonathan1055 commentedThanks.
Just to be clear, for anyone else reading this issue, those three sniffs are already active in phpcs.xml.dist, this patch ammends the comment to indicate that they are active, and fixes 12 regressions in TagGroupSpacing which would fail under Coder 8.3.12 (core 9.2 is currently using Coder 8.3.10)
Comment #13
daffie commentedIf those sniffs are already active, then why are there no coding standard violations without this patch?
Comment #14
jonathan1055 commentedIt is all explained in the linked issue #3181485: Regression in @ tag sniff TagGroupSpacing. The fault in Coder (starting with 8.3.7) allowed incorrect lines which should have failed DocComment.TagGroupSpacing to be committed. This patch fixes those 12 regressions. The faults do not show yet because Core 9.2 is still running Coder 8.3.10 and the corrected sniff will be available from Coder 8.3.12. You can see in the results of patch #6 above where I patched Coder to update the sniff, that the test does show the 12 failures.
This patch does not do anything with Drupal.Commenting.DocComment.Empty and Drupal.Commenting.DocComment.TagsNotGrouped apart from list them in the phpcs.xml.dist comment where they should have been listed already.
Comment #15
longwaveOnce this is committed we can bump to Coder 8.3.12 in #3187025: Update dependencies for Drupal 9.2
Comment #16
jonathan1055 commentedHi @longwave
The latest version of Coder is 8.3.11 which was released on 4 November 2020. The regression fix is still only available in Coder -dev (hence my use of the coder patch in #6 above). No harm in updating to 8.3.11 but would be nice to use 8.3.12 in core 9.2. If you have a planned timetable on #3187025: Update dependencies for Drupal 9.2 then Klausi may well release 8.3.12 to conincide with that (if you ask him nicely ;-)
Comment #18
catchCommitted 1fcf53c and pushed to 9.2.x. Thanks!
Comment #19
xjmComment #20
jonathan1055 commented@xjm This issue does not actually change the list of active coding standards sniffs - see my comments in #2 and #12. No core developer/reviewer would have to do anything differently as a result of this commit. Does that change the need for release notes?