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 |
|---|---|---|---|
| #17 | 2937858-17.patch | 20.63 KB | longwave |
Comments
Comment #2
SherFengChong commentedInitial patch version.
Comment #3
rosk0Good to go.
Comment #4
catchI think we need to double check these aren't false positives - the indentation is due to the inclusion of the @code examples in lists. Not sure if the coding standards themselves are explicit either way?
Comment #5
rosk0Cross-linking to related code standard issue #2060925: False-positives for @code/@endcode by Drupal.Commenting.DocCommentSniff and Drupal.Commenting.DocCommentAlignment sniff.
Comment #6
borisson_Patch no longer applies.
Comment #7
savkaviktor16@gmail.com commentedRe-rolled
Comment #8
borisson_The testbot agrees with this patch, and I ran composer phpcs locally and got no remaining errors.
Looks great!
Comment #9
alexpottLooking at https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Component%21Rend... I think the things being fixed here a not quite right. There's no need to for @code, @link etc to not be indented.
We need to fix the PHPCS rules in coder before implementing this.
Comment #10
idebr commentedPostponing on #2060925: False-positives for @code/@endcode by Drupal.Commenting.DocCommentSniff and Drupal.Commenting.DocCommentAlignment sniff
Comment #11
jhodgdonThe changes in the patch in #7 will result in things being totally broken on api.drupal.org. @code and @link need to be indented the same depth as the surrounding text, not moved to the left as they are in that patch. I guess someone figured that out, but we definitely do not want to commit that patch!!
Comment #17
longwaveThe linked issue in #10 is fixed so we can enable this sniff and fix the existing alignment issues.
Comment #18
jhodgdonI checked over the patch. Everything in there looks good to me. If the tests pass, including the new Coder sniff, this is RTBC.
Comment #19
quietone commentedSetting to RTBC based on the comment in #18.
Comment #20
alexpottCommitted e3dfd1a and pushed to 9.2.x. Thanks!