Part of meta-issue #2571965: [meta] Fix PHP coding standards in core
Step 1: Preparation
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 & configure PHPCS
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 -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 |
---|---|---|---|
#21 | reroll_diff_19-21.txt | 1.46 KB | Spokje |
#21 | 2902540-21.patch | 2.14 KB | Spokje |
Comments
Comment #2
mfernea CreditAttribution: mfernea at AmeXio commentedWe should wait for #2902536: $config in $coreGlobals from Drupal.NamingConventions.ValidGlobal to be fixed.
I'm not really sure what to do with global $efq_test_metadata;
Comment #9
quietone CreditAttribution: quietone as a volunteer commentedThis issue this was postponed on is fixed.
Comment #10
guilhermevp CreditAttribution: guilhermevp at CI&T commentedPhpcs Summary:
Sending patch, please review.
Comment #11
guilhermevp CreditAttribution: guilhermevp at CI&T commentedBetter organized patch.
Comment #13
quietone CreditAttribution: quietone as a volunteer commented@guilhermevp, thanks for working on this.
It isn't clean to me what 'better organized patch' means in the context of a coding standard issue. What change was made and why does it result in a 'better organized patch'? Thx.
Comment #14
guilhermevp CreditAttribution: guilhermevp at CI&T commentedHi @quietone!
The better patch would about the placement of the rule in phpcs.xml.dist, but then I found about the issue #3135933: Sort sniffs/rules in phpcs.xml.dist and write test to keep them sorted.
Can the patch #10 be the reviewed one?
Comment #15
quietone CreditAttribution: quietone as a volunteer commentedHi! Yes, the patch in #10 can be the one to review. I would add that to the Remaining Task in the Issue Summary and a big obvious comment. Since these coding standards have a unique Issue Summary we will just have to rely on the comment. I've done that and changed the status. Cheers.
To all reviewers, Review the patch in #10! Ignore the patch in #11. Thanks.
Comment #16
guilhermevp CreditAttribution: guilhermevp at CI&T commentedThanks @quietone!
Comment #17
longwaveThe new line is in the wrong place, we have
<rule>
nested inside another<rule>
.I also think we should be able to get rid of that global entirely, somehow.
Comment #18
longwaveFixed both points in #18.
Comment #20
quietone CreditAttribution: quietone as a volunteer commentedI came to review.
This no longer applies to 9.2.x or 9.3.x. Since this is small and I am not familiar with field_test_memorize, I rerolled it locally and read the docs and played with the test. I get what this is doing and it looks fine to me, it is ready for RTBC, just needs a patch that applies to 9.3.x.
Needs a reroll.
Comment #21
SpokjeRerolled patch #19 for
9.3.x
Comment #22
quietone CreditAttribution: quietone as a volunteer commentedSpokje, thanks.
This is ready now.
Comment #24
catchCommitted 91b2327 and pushed to 9.3.x. Thanks!
Comment #26
pameeela CreditAttribution: pameeela commentedWould someone be able to add a release note snippet summarising any disruptions this change may cause?
Comment #27
Wim Leers9.3.0 shipped about a year ago. Release notes are irrelevant now 😅