Part of meta-issue #2571965: [meta] Fix PHP coding standards in core, stage 1
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="PSR2.Namespaces.UseDeclaration.UseAfterNamespace"/>
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 |
|---|---|---|---|
| #34 | 2901745-34.patch | 550 bytes | longwave |
| #33 | interdiff_26-33.txt | 1022 bytes | ridhimaabrol24 |
| #33 | 2901745-33.patch | 2.36 KB | ridhimaabrol24 |
| #26 | 2901745-26.patch | 3.11 KB | vacho |
| #21 | interdiff-17-21.txt | 690 bytes | MerryHamster |
Comments
Comment #2
pazhyn commentedComment #3
pazhyn commentedComment #4
pazhyn commentedThere are some rows wrapped with
Because there are several classes in one file and use is declared after next classes in the middle of the files.
So it should be ok.
Comment #5
pazhyn commentedAdded rule
<rule ref="PSR2.Namespaces.UseDeclaration.UseAfterNamespace"/>intophpcs.xml.distComment #6
mfernea commentedThe patch remove blank lines and although this it's correct to do so, it's not related to this sniff.
I'm not sure that ignoring those lines is a good solution.
Comment #7
mfernea commentedAs per issue summary, let's wait until #2901744: Fix 'PSR2.Namespaces' coding standard is merged.
Comment #8
andypostThis needs follow-up to get rid of this cos otherwise this "use" with not be analysed at all
Comment #10
idebr commented#2901744: Fix 'PSR2.Namespaces' coding standard was committed, so this is no longer postponed.
Comment #11
borisson_@idebr is right, this can go in. But it needs a reroll first.
Comment #12
kostyashupenkoComment #13
andypostWrong re-roll so here's a rule clean-up only patch to see the number of current regressions
this rule needs to be added
Comment #14
andypostI got following warnings locally with prev patch
Comment #15
kostyashupenkoagain reroll
Comment #16
andypostplease add fix for
CryptRandomFallbackTest.phphttp://cgit.drupalcode.org/drupal/tree/core/tests/Drupal/Tests/Component...Comment #17
kostyashupenkoComment #18
andypostGreat! Good to go
So only a question about how to add comments why this lines ignored left
Comment #19
mfernea commentedIn this case we can remove the namespace declaration and merge the use statements.
In this case we can remove the namespace declaration and merge the use statements.
We can also use
// @codingStandardsIgnoreLineto keep the modifications footprint low.We can also use
// @codingStandardsIgnoreLineto keep the modifications footprint low.We can also use
// @codingStandardsIgnoreLineto keep the modifications footprint low.Comment #21
MerryHamster commentedReroll for 8.7.x
Comment #22
MerryHamster commentedComment #23
longwaveRe: #19 I agree with changes 1 and 2 but for 3-5 should we switch to the braces style for the multiple namespaces declared in each file? That would mean we don't have to ignore the coding standards, I believe.
Comment #25
vacho commentedComment #26
vacho commentedPatch rerolled to 8.8.x
Comment #27
vacho commentedComment #28
jmikii commentedComment #29
mfernea commentedWe can also use // @codingStandardsIgnoreLine to keep the modifications footprint low.
We can also use // @codingStandardsIgnoreLine to keep the modifications footprint low.
@longwave I'm not sure I understand what you mean can you please give some examples?
Comment #30
mfernea commentedRemove "postponed" message.
Comment #33
ridhimaabrol24 commentedPatch for Drupal 9.1
Comment #34
longwaveLocally there were no failures after enabling this sniff, although DrupalSqlBaseTest looks the same as it did before to me. Let's see what the bot thinks.
Comment #35
daffie commentedRemoving the rule exclusion will result in no errors.
The patch only makes the rule active.
Tested this on my local machine.
All code changes look good to me.
For me it is RTBC.
Comment #36
alexpottCommitted and pushed b5fa69586c to 9.1.x and 28b5e7cea5 to 9.0.x and b31c7541dc to 8.9.x. Thanks!
Tested on 8.9.x and it passed without issue.
Comment #40
xjmJust a heads-up that while the cleanups of this issue are patch-safe (and therefore allowed during RC), enabling new PHPCS rules is not. We've actually just restored the rulesets from 9.0.0-rc1 and 8.9.0-rc1 because of this:
https://git.drupalcode.org/project/drupal/-/commit/6144e3b1f69b5ff9c786c...
https://git.drupalcode.org/project/drupal/-/commit/2a5cd8c8adcdae777324d...
The rule is still enabled in 9.1.x which makes it unlikely that a regression will be committed to 9.0 or 8.9 either.
Thanks!
Comment #42
xjm