Part of meta-issue #2571965: [meta] Fix PHP coding standards in core, stage 1
Here we are fixing:
- PSR2.Namespaces.NamespaceDeclaration
- PSR2.Namespaces.UseDeclaration
Except PSR2.Namespaces.UseDeclaration.UseAfterNamespace which is fixed in #2901745: Fix 'PSR2.Namespaces.UseDeclaration.UseAfterNamespace' coding standard.
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="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 -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 |
|---|---|---|---|
| #36 | drupal-coding-standards-2901744-35.patch | 587 bytes | mfernea |
| #30 | drupal-coding-standards-2901744-30.patch | 47.81 KB | mfernea |
| #28 | drupal-coding-standards-2901744-28.patch | 48.22 KB | mfernea |
| #25 | drupal-coding-standards-2901744-25.patch | 48.22 KB | mfernea |
| #21 | interdiff-2901744-18-21.txt | 359 bytes | mfernea |
Comments
Comment #2
mfernea commentedHere is the patch.
Comment #3
mfernea commentedComment #5
mfernea commentedRe-roll.
Comment #7
mfernea commentedRe-roll.
Comment #8
mfernea commentedComment #9
borisson_Looks solid.
Comment #10
catchThis seems to be missing the phpcs.xml.dist change, am I missing something?
Comment #11
borisson_It's in the patch, is that not the correct file @catch?
Comment #12
mfernea commentedI think it should have the Needs review status.
Comment #13
catchNo you're right, I did look twice but just could not see it this morning. Back to RTBC and apologies for the red herring.
Comment #14
xjmNeeds a reroll now though.
Comment #15
mfernea commentedRe-roll.
Comment #16
andriyun commentedLooks good to me.
Comment #17
xjmSo the only funkiness is around:
core/modules/migrate_drupal/tests/src/Unit/source/DrupalSqlBaseTest.phpcore/modules/migrate_drupal/tests/src/Unit/source/d6/Drupal6SqlBaseTest.php(Which still has a @file docblock? How did that happen? I guess because it contains two classes and so therefore isn't PSR-4. It looks like there are dozens, maybe hundreds of unit tests in core that still have @file docblocks for the same reason.)
Looking at this, the double namespace declaration for (2) dates all the way to #2121299: Migrate in Core: Drupal 6 to Drupal 8. The double namespace in (1) was added along with the file in #2549801: Improve source provider missing exception message., so probably just copied it from (2).
It also looks like there is a similar double namespace declaration in:
core/tests/Drupal/Tests/Core/Theme/RegistryTest.php...which was not touched by this patch.
It looks like maybe those two hunks are left over from including #2901745: Fix 'PSR2.Namespaces.UseDeclaration.UseAfterNamespace' coding standard in this patch as? Let's remove them; they pass with just the rules enabled here.
Comment #18
mfernea commentedIndeed, those changes are not required. Here is the updated patch and interdiff.
Comment #19
andriyun commentednew phpcs issue was added in one of recent patches
Comment #20
mfernea commentedComment #21
mfernea commentedHere is the patch and interdiff.
Comment #22
andriyun commentedLooks good to me
Comment #24
andriyun commentedComment #25
mfernea commentedRe-roll.
Comment #26
mfernea commentedComment #28
mfernea commentedRe-roll again. :D
Comment #30
mfernea commentedRe-roll.
Comment #31
andriyun commentedno phpcs fails after patch applying
patch contains only sniff related changes
Comment #32
catchCommitted/pushed to 8.5.x and cherry-picked to 8.4.x. Thanks!
Comment #34
mfernea commentedOne error got through.
Comment #35
mfernea commentedHere is the patch.
Comment #36
mfernea commentedComment #38
mfernea commentedComment #40
mfernea commentedComment #41
alexpott@mfernea in general it is best to create a new issue rather than re-open an already fixed issue. This keeps contribution and issue metrics sane.
Comment #43
alexpottDiscussed with @catch - we agreed to commit #36 and move on.
Committed 0b1741f and pushed to 8.5.x. Thanks!
Comment #44
mfernea commentedOk, I understand. I will do that in the future. Thanks!