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. 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="PSR2.Classes.PropertyDeclaration.Underscore"/>
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 |
---|
Issue fork drupal-3123070
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
msutharsComment #3
msuthars@jungle I followed all the steps that you mentioned. When I run
../vendor/bin/phpcs -p
command it shows me warnings related to yml files.Like: No PHP code was found in this file and short open tags are not allowed by this install of PHP. This file may be using short open tags but PHP does not allow them.
Then I run the
../vendor/bin/phpcbf
command and found there is no fixable errors.Please check my attached phpcs.xml (uploaded as text file) file. Is there something that I missed.
Comment #5
SpokjeTurns out this sniff (
PSR2.Classes.PropertyDeclaration.Underscore
) flags exactly the same as the sniff `Drupal.Classes.PropertyDeclaration` (#2937882: Fix 'Drupal.Classes.PropertyDeclaration' coding standard).So basically we only have to add a comment to `core/phpcs.xml.dist` stating the above.
Comment #7
SpokjeComment #8
quietone CreditAttribution: quietone as a volunteer commentedComment #9
Spokje@quietone Can you have another look?
Comment #10
SpokjeComment #11
quietone CreditAttribution: quietone as a volunteer commented@Spokje, Sorry trouble on my end. Gitlab is showing only one change to the file, the removal of the out of scope line. What happened to the other lines? Not trusting Gitlab I went to apply the diff but it doesn't apply. This will have to wait for another day. I am off to bed and I may not be on line tomorrow.
Comment #12
Spokje@quietone:
So basically there are 2 changes:
- The out-of-scope line deletion
- A comment addition explaining why sniff
PSR2.Classes.PropertyDeclaration.Underscore
is ignored. (this to prevent future attempts to maybe "fix" this by enabling it.)There is a known issue about the creation of patch files from an MR (See: #3204538: GitLab Merge Requests Unable to Generate Incremental Patch Files) which might have bitten you.
I've attached a patch file that applies, also this URL: https://git.drupalcode.org/project/drupal/-/merge_requests/541/diffs works for me, 2 changes in 1 file visible.
Your original comment on the comment addition seems to have left the building, no clue why...
Comment #13
quietone CreditAttribution: quietone as a volunteer commented@Spokje, thanks for making the patch.
I applied the patch and then browsed the file. On reading,
I searched for
Drupal.Methods.MethodDeclaration.Underscore
and found that it is excluded on line 113 of the file.So, if that sniff is excluded it can't be covering anything. I then searched for
Drupal.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
and didn't find it, nor did I findDrupal.NamingConventions.ValidFunctionName.
I am a bit baffled by these comments. And to add to that the title here is PropertyDeclaration not Method Declaration.
Comment #14
Spokje@quietone:
Hmmm, seem to have made a mess by doing things to quickly.
So I _am_ sure this sniff can be excluded, but seem to have forgotten the sniff that covers this one in a frenzy of copy-pasting and doing 23 things at the some time. :/
Luckily, you are here to prevent me from doing some actually damage...
Not sure if I have time today (22 things left to do... ;) but I'll get back to this and find the correct sniff and this time actually _think and check_ the comment...
Thanks for staying sharp and keeping me in check!
Comment #15
SpokjeComment #18
SpokjeRunning
../vendor/bin/phpcs -s
with only these sniffs enabled:returns:
This is exactly the same output as was produced #2937882-16: Fix 'Drupal.Classes.PropertyDeclaration' coding standard.
We decided to
phpcs:ignore
those, because the refactoring would be almost impossible without breaking BC.This was committed to
9.2.x
.So by adding this sniff (
PSR2.Classes.PropertyDeclaration.Underscore
), we would have to ignore those again for this sniff.Since I see no added value and that, and being convinced that basically
PSR2.Classes.PropertyDeclaration.Underscore
does the same as the already used sniffDrupal.Classes.PropertyDeclaration
I propose the following:We don't enable this sniff, but keep excluding it as we already do.
But to prevent people from wondering why it's excluded or trying to include it again, we add a remark with a explanation why it's excluded.
The MR tries to do just that.
Comment #19
SpokjeComment #20
SpokjeComment #21
longwaveI'm not sure the comment is actually correct.
Also hiding the patch as this issue is using the merge request workflow now.
Comment #22
SpokjeThanks @longwave, you're absolutely right (as usual).
This was supposed to be the simplest patch ever, but it seems to have some kind of vengeance against me...
Let's see if I got the comment right this time :sigh:...
Comment #23
SpokjeComment #24
longwaveWe'll get there in the end!
Comment #27
catchCommitted/pushed to 9.3.x and cherry-picked to 9.2.x, thanks!