Part of #2571965: [meta] Fix PHP coding standards in core, stage 1.
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 |
|---|---|---|---|
| #20 | 2937513-8.9.x-20.patch | 55.11 KB | longwave |
| #14 | 2937513-14.patch | 51.58 KB | longwave |
Comments
Comment #2
eltori commentedInitial patch version.
Comment #3
rosk0Applied patch, ran
$ composer run phpcs- no issues.Comment #4
catchThis doesn't look right to me - i.e. it looks like a bug in the phpcs rule.
Comment #5
borisson_Postponing this on #2947589: TagGroupSpacing coding space fix doesn't work as it should
Comment #8
klausiI just released a new Coder version where the wrong fixes from above should not happen again. This is now postponed on #3063323: Update drupal/coder to 8.3.6.
Comment #9
idebr commented#3063323: Update drupal/coder to 8.3.6 has been committed, but the sniff still generates a few false positives. I have documented these on #2947589: TagGroupSpacing coding space fix doesn't work as it should
Comment #10
klausiPushed a fix to Coder, please test with the Coder dev version here if we have resolved everything now.
Comment #12
nginex commentedTagging for Drupal Global Contribution Weekend
Comment #14
longwavephpcbf seemed to do a good job on this one.
Comment #15
klausiNice, thanks!
* Checked that the patch only contains the comment empty lines changes
* Rule exclude in PHPCS config is removed
* PHPCS output on testbot is clean
Comment #18
xjmInteresting that it's detecting the missing newlines between
@paramand@return, or@returnand@throws, but not between@returnand@see.Also interesting that it doesn't detect the missing newline between the one-line summary and
@param.Is there a different rule that will catch those things? If so, we can wait for that rule; if not, we need a followup coder issue to expand the scope of the sniff a bit.
Meanwhile, this is successfully fixing the scope of "missing newline between @param/@return/@throws". I reviewed to ensure all changes were fixing that, and nothing else. I also ran
composer run phpcs -- -pwith the patch applied on 9.1.x to verify the rule was entirely fixed. Did likewise on 9.0.x after cherry-picking.Tagging for the release notes as a newly enabled standard in 9.0. This is also eligible for backport to 8.9 still, but the patch did not cherry-pick cleanly. The backport should also be checked to ensure it fixes all cases on 8.9.x.
Thanks!
Comment #19
klausiHaha, I read the issue comment notification email but didn't catch the author and by the end of it I knew this could only be an xjm comment. I love your rigor!
1. @see tag line spacing: I saw some usage of @see tags within @return or @param tags, so decided to not enforce any spacing on @see tags as they are a bit special.
2. Summary spacing: I think Coder might not complain here because this is an {@inheritdoc} comment, which is also treated as special case in Coder.
We can open issues for those in Coder to improve that, I personally think they are not that important. Coder pull requests welcome anyway!
Comment #20
longwaveRerolled for 8.9.x by running phpcbf again.
Comment #21
klausiThanks! Verified the changes in the patch and that the phpcs.xml.dist change is also there.
PHPCS output is clean on the testbot.
Comment #22
alexpottCommitted 4f61b6f and pushed to 8.9.x. Thanks!
Comment #25
jonathan1055 commentedI have raised #3183656: Fix 'Drupal.Commenting.DocComment.TagGroupSpacing' coding standard [part 2] as a follow-up
Comment #26
jonathan1055 commentedFollowing up xjm's comments in #18 and Klausi's answers:
#18.1 The adjustment I have made to this sniff now fixes this, and a blank line is required either side of the
@eturnregardless of what is before or after it. This was always the intention but was not achieving it in practice.#18.2 The missing blank line in this case is detected by another sniff
Drupal.Commenting.DocComment.SpacingBeforeTagswhich reports "There must be exactly one blank line before the tags in a doc comment" and is dealt with in #2842949: Fix Drupal.Commenting.DocComment.SpacingBeforeTags coding standardSo #3183656: Fix 'Drupal.Commenting.DocComment.TagGroupSpacing' coding standard [part 2] is now ready for final review.