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. 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.NamingConventions.ValidVariableName.LowerCamelName 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
$ 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 -ps
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. The -s flag shows the sniffs when displaying results.
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 |
|---|---|---|---|
| #26 | 3207734-26.patch | 166.8 KB | quietone |
| #26 | diff-21-26.txt | 30.36 KB | quietone |
Issue fork drupal-3207734
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:
- 3207734-fix-drupal.commenting.inlinevariablecomment
changes, plain diff MR !509
- 9.3.x
changes, plain diff MR !691
Comments
Comment #3
spokjeA lot of changes, but I think this is an easy review:
- All
phpcbfchanges, so there's a nice predictable pattern in there.- No human-readable text involved, so no thinking about the wording.
- All changes are one-line changes.
Comment #4
quietone commentedLooks really good! Just two suggestions and one comment.
Comment #5
spokjeComment #6
spokjeComment #7
quietone commentedChanges looks good! I made one more pass and this time I seemed to find a groove and found some more things. Including some lines that look like they can be removed.
Comment #8
spokjeNice groove there @quietone.
All threads resolved, back to NR.
Comment #9
quietone commentedWow, that was a quick turnaround! I reviewed the latest commit and this is RTBC for me, assuming tests pass.
Comment #10
quietone commentedI just found out that the related policy issue for this fix still has work to do #2305593: [policy] Set a standard for @var inline variable type declarations. Postponing until that is sorted out.
Comment #11
spokje#2305593: [policy] Set a standard for @var inline variable type declarations is Fixed, returning this one to RTBC as per #9.
Comment #12
spokjeThe above is a simple re-roll/rebase/merging-of-HEAD into the MR, no code affecting this issue was
harmedchanged during this process.Comment #13
alexpottI think we need to schedule this one for sometime in the 9.2 beta as we're changing 170 files.
Comment #14
spokje@alexpott: Do you mean this would be too disruptive for any other patches that are RTBC?
Comment #15
longwaveOpened #3210939: [meta] Disruptive patches for 9.2 beta as there are a number of disruptive patches waiting for beta at the moment.
Comment #16
spokjeEcellent @longwave!
Can I just pick a free date or is there more to the order in that issue?
Comment #17
spokjeTo prevent re-rolling this huge patch every time a commit is made on 9.2.x-dev (since it touches so many files), let's wait until this one is up for actually being committed in #3210939: [meta] Disruptive patches for 9.2 beta.
Postponing until then.
Comment #19
spokjeRebased MR on
9.3.xComment #22
spokjeUnpostponing since it's around "sometime in the 9.2 beta"-o'clock (@alexpott in #13), back to RTBC per #9.
Comment #23
alexpottCommitted and pushed 4a72cc3169 to 9.3.x and d6b00c13b9 to 9.2.x. Thanks!
It'll be good to backport this one to 9.1.x to make it easier to apply patches across all 3 branches - but without the change from core/phpcs.xml.dist
Comment #26
quietone commentedLet's see if I can reroll this.
Comment #28
spokjeI think this should be
NRComment #29
andypostIssue summary needs update, why
phpcs.xmlis not updated in commited patchesComment #30
longwaveBackport looks correct to me.
@andypost we only enable new sniffs in x.y.0 minor releases so we can't add this to 9.1.x phpcs.xml.dist, but we can still backport the fixes to make other patches easier to port.
Comment #31
andypost@longwave thanks for explanation, then it needs follow-up for 9.3 I bet
Comment #32
alexpott@andypost the phpcs.xml.dist changes are in the version that was committed to 9.2.x and 9.3.x as far as I can see.
Committed dd3d8d6 and pushed to 9.1.x. Thanks!