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 |
---|---|---|---|
#19 | drupal-coding-standards-2901789-19.patch | 9.78 KB | mfernea |
#12 | interdiff-2901789-7-11.txt | 2.12 KB | mfernea |
#12 | drupal-coding-standards-2901789-11.patch | 9.75 KB | mfernea |
#7 | drupal-coding-standards-2901789-7.patch | 10.17 KB | mfernea |
#5 | drupal-coding-standards-2901789-5.patch | 10.24 KB | mfernea |
Comments
Comment #2
mfernea CreditAttribution: mfernea at AmeXio commentedHere is the patch.
Comment #3
mfernea CreditAttribution: mfernea at AmeXio commentedStatus change.
Comment #5
mfernea CreditAttribution: mfernea at AmeXio commentedIn fact, here is the patch :).
Comment #7
mfernea CreditAttribution: mfernea at AmeXio commentedComment #8
borisson_Not sure about this comment, I'd like the comment to explain why we don't want these comments.
Something like
Comment #9
mfernea CreditAttribution: mfernea at AmeXio commentedIt's an 1-on-1 copy / paste from Drupal CS' ruleset.xml. From my point of view, in the end phpcs.xml.dist and ruleset.xml will have to look the same.
Comment #10
andriyun CreditAttribution: andriyun as a volunteer and at Bellcom, Drupal Ukraine Community for Drupal Ukraine Community commentedAgree with @borisson_ about excluded sniffs comments.
Step by step we have new sniffs in excluded list.
Some of them we add to postponed list
But there are valuable group of sniffs which is not related to Drupal coding standards.
Would be good to have it in one place with single comment about why we exclude them.
Comment #11
mfernea CreditAttribution: mfernea at AmeXio commentedHere are the updated patch and the interdiff. Is the comment ok from your pov?
I also excluded modifications to Diff engine which is now excluded.
Comment #12
mfernea CreditAttribution: mfernea at AmeXio commented(disregard the previous comment and its files)
Here are the updated patch and the interdiff. Is the comment ok from your pov?
I also excluded modifications to Diff engine which is now excluded.
Comment #13
andriyun CreditAttribution: andriyun as a volunteer and at Bellcom, Drupal Ukraine Community for Drupal Ukraine Community commentedSeems like everything is ok with comments.
But I'm worrying about the approach that we using to exclude rules which is not relate to Drupal CS
In one case we using
exclude
directivehttp://cgit.drupalcode.org/drupal/tree/core/phpcs.xml.dist#n54
For another sniffs we added
OTOH we have two groups of sniffs that we need to exclude:
In my opinion
Would be better exclude the first group with
<severity>0</severity>
directive. This directive is more related to goal that we need: "Hide errors of postponed sniffs"And use
exclude
to second group of sniffs.What do you think?
I left RTBC to have more opinions for ^^^
Comment #14
mfernea CreditAttribution: mfernea at AmeXio commentedThe way I see it:
Our goal is to make phpcs.xml.dist look as close as possible as Drupal CS' ruleset.xml (http://cgit.drupalcode.org/coder/tree/coder_sniffer/Drupal/ruleset.xml). Doing so, it will be easier to see which are the differences, which things we still have to work on.
Drupal CS' ruleset.xml uses "severity" to exclude non-relevant sniffs.
Up until now "exclude" was used in phpcs.xml.dist to exclude postponed sniffs.
So, from my pov, it's ok to add comments to explain things in phpcs.xml.dist (but not too many to be able to compare it easily with ruleset.xml) and we shouldn't change the approach we had up untill now regarding exclude vs severity usage.
Comment #15
eiriksmReviewing...
Comment #16
eiriksmTested this by verifying there are coding standard fails with only the ruleset changed, and none with the patch applied.
So LGTM!
Comment #17
eiriksmComment #18
Mile23How did we end up with this style of fluent calling in the first place?
Anyway, yes the additions to phpcs.xml.dist match the Drupal standard ruleset.xml for PEAR.Functions.FunctionCallSignature. Also the testbot run from #12 had zero CS errors, and we can mostly trust that.
However the patch no longer applies, so it needs a re-roll.
Comment #19
mfernea CreditAttribution: mfernea at AmeXio commentedRe-roll.
Comment #20
andriyun CreditAttribution: andriyun as a volunteer and at Bellcom, Drupal Ukraine Community for Drupal Ukraine Community commentedno phpcs fails after patch applying
patch contains only sniff related changes
Comment #22
catchCommitted/pushed to 8.5.x and cherry-picked to 8.4.x. Thanks!