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="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 |
|---|---|---|---|
| #33 | 2902636-33.patch | 166.22 KB | izus |
| #28 | 2902636-28.patch | 166.18 KB | izus |
| #23 | 2902636-23.patch | 99.06 KB | neclimdul |
| #21 | 2902636-21.patch | 98.63 KB | jofitz |
| #18 | interdiff-2902636-15-18.txt | 26.23 KB | mariiadeny |
Comments
Comment #2
eiriksmHere is a rule-update only patch (that should fail) and a complete update.
Comment #4
mfernea commentedHi @eiriksm, cs violations don't fail the tests. So you can add only one patch. Note that the second patch is empty.
Comment #5
mfernea commentedComment #6
eiriksmTrue. But you can see them listed in the results. Where i see i forgot to remove a couple of other array rules.
Strange that the patch was 0b I'll try to upload it again maybe later today.
Thanks!
Comment #7
mfernea commentedNow I see what you mean. In fact it's a good idea.
Comment #8
eiriksmOk, trying again :)
Comment #9
eiriksmSo, so manual fixes were required I guess (https://www.drupal.org/pift-ci-job/744001).
Trying again.
Comment #11
eiriksmOops. syntax errors, not good.
Comment #13
eiriksmComment #15
eiriksmNew take
Comment #16
andypostPart of patch will not be needed after fix of related #2911280: RectangleTest.php takes a very long time to scan for coding standards
Comment #17
andypostThis needs work to update
core/phpcs.xml.distwith sniffer & exclude other sub-sniffsComment #18
mariiadeny commentedComment #19
andypostComment #20
borisson_This needs a reroll but looks solid, we should probably also prepare a patch for 8.4 without the phpcs changes.
Comment #21
jofitzRe-rolled.
Comment #23
neclimdulFriendly post #2902381: Revert phpcs.xml.dist changes from "Ensure that we're using the right ruleset for coding standards checking" reroll.
No feedback addressed. No credit needed.
Comment #24
neclimdulDrive by review:
1) when testing the reroll there where quite a few array indentation failures for me. Doesn't seem related to the reroll though.
2) it seems the standard that exclusion lists like is in this patch include a comment with the tests being run but this patch doesn't include that.
Comment #25
ivan berezhnov commentedComment #28
izus commentedhi,
i gave this a new try
hope it'll be green
Comment #29
borisson_This looks great and it has the required changes to the phpcs file.
Comment #31
alexpottAlready needs a reroll... :(
This is likely to be a tricky on to get in.
Comment #32
alexpottAlso +1 to get this done it will will fix lots of common errors and help patch review of big arrays way easier. One less thing to visually check.
Comment #33
izus commentedrerolled #28
Comment #34
izus commentedComment #35
borisson_Comment #37
alexpottThis change doesn't really look correct. In fact the whole way the translatable text contains HTML is super interesting. I think we should open a separate blocking issue to address this. We don't usually translate exception messages so I think we should go back to the original issue and see discussions there.
This looks odd.
Let's bring the ); up to the ]
This looks odd.
I think this needs a different fix. Maybe by creating the array key outside of the array.
I would fix the array key here to be
Same for the MAILTO URLs test...
Comment #46
mile23This is a duplicate of #2937515: Fix Drupal.Array.Array.[ArrayClosingIndentation, ArrayIndentation] coding standard
Current config in 9.5.x:
Comment #47
quietone commented@Mile23, thanks. I don't know how I missed that this is a duplicate.
I have moved credit to other issue.