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: Run the test
Now you are ready to run the test! From within the core/ folder, run the following command to launch the test:
$ composer run 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 3: 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:
$ composer run phpcbf
This will fix the errors in place or composer will tell you that Script phpcbf --standard=core/phpcs.xml.dist --runtime-set installed_paths $($COMPOSER_BINARY config vendor-dir)/drupal/coder/coder_sniffer -- handling the phpcbf event returned with error code 1 meaning there was no files fixed and you need to fix them manually. 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 |
|---|---|---|---|
| #18 | 2937853-18.patch | 30.29 KB | mfernea |
| #11 | 2937853-11.patch | 30.01 KB | idebr |
| #11 | 2937853-6-11.txt | 2.27 KB | idebr |
Comments
Comment #2
SherFengChong commentedInitial patch version.
Comment #3
rosk0Good to go.
Comment #4
alexpottWe have 2 new ones to fix because these files were not being scanned before:
Also:
Comment #5
alexpottFor the
drupal.shone i'd just move the assignment of$cmdabove the comment.Comment #6
idebr commented#4 Fixed the assignment in core/scripts/password-hash.sh
#5 Moved the assignment of
$cmdComment #7
borisson_This patch passes coding standards and after I ran composer phpcs there are no remaining errors, just like the testbot tells us.
I'm unsure about adding this fix though.
I'm not sure if this fix is correct. There isn't explicit documentation about this in https://www.drupal.org/docs/develop/standards/coding-standards#operators
That was #2816445: Forbid using more than one space around operators, but it looks like that issue hasn't passed yet.
So it looks like this is ok.
This moves us further away from #2816445: Forbid using more than one space around operators
Comment #8
idebr commented#7.1 / #7.2 As far as I am concerned we are just bringing the current code base in line with our coding standards using automated testing tools. Once there is a change in the coding standard (like #2816445: Forbid using more than one space around operators), we will update the rule and then we have the tool available to fix violations automatically.
Comment #9
mfernea commentedAs I see it, having more spaces before the assignment is less common, although allowed. So, if we have to modify those lines, I would also go for only one space before the assignment.
Comment #10
borisson_I agree with #9, that sounds like the best way to fix this.
Comment #11
idebr commentedOhh I see what you are getting at, we just remove the 'extra' spaces. Something like this?
Comment #13
idebr commentedSetting status back to 'Needs review' after #2973992: Permission issue in Nightwatch step marks all full testruns as unstable
Comment #15
mfernea commentedLooks good now.
Comment #17
mfernea commentedComment #18
mfernea commentedHere is the updated patch for 8.7.x.
Comment #19
mfernea commentedSince it's just a reroll and passes all the tests, I'm setting this to RTBC.
Comment #20
alexpottCommitted and pushed 6c40e077ef to 8.7.x and da3e9773c1 to 8.6.x. Thanks!
As a coding-standards fix this is eligible for commit during 8.6.x beta.
Comment #22
xjmWe include a brief list of coding standards changes in the release notes, since they affect core, contrib, and custom projects that use our standards.