Part of meta-issue #2571965: [meta] Fix coding standards in core
Step 1: Preparation
Open the file core/phpcs.xml.dist
and add yml as file extention. Make sure your patch will include this addition.
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: Remove phpcs.xml file
When adding a new file extenstion all tests should run so no need for phpcs.xml file. PHPCS will use phpcs.xml.dist.
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 |
---|---|---|---|
#14 | 2901653-14.patch | 17.37 KB | idebr |
#14 | interdiff-11-14.txt | 698 bytes | idebr |
Comments
Comment #2
mfernea CreditAttribution: mfernea at AmeXio commentedHere is the patch.
Comment #4
ankitjain28may CreditAttribution: ankitjain28may as a volunteer and at Google Summer of Code commentedThe above patch failed to apply on drupal 8.6.x. Here is the patch for it.
Comment #5
ankitjain28may CreditAttribution: ankitjain28may as a volunteer and at Google Summer of Code commentedComment #6
borisson_Patch is not correct, after I applied this and ran
composer phpcs
I got this:FILE: ...web/core/node_modules/babel-plugin-add-header-comment/circle.yml
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
3 | ERROR | [x] Expected 1 newline at end of file; 0 found
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------
I think this means that we have to update the phpcs script in composer to ignore the node_modules directory?
Comment #7
ankitjain28may CreditAttribution: ankitjain28may as a volunteer and at Google Summer of Code commented@borisson_ Yes, we should ignore node_modules dir in phpcs script.
Comment #8
borisson_NW based on #6 and #7.
Comment #9
rakesh.gectcrComment #10
idebr CreditAttribution: idebr at ezCompany commentedAttached patch includes the following changes:
Comment #11
idebr CreditAttribution: idebr at ezCompany commentedWe can expand on the existing exclude pattern for *.info.yml files to bypass "No PHP" errors in yml files.
Comment #12
borisson_That looks great!
Comment #13
alexpottYeah this looks like a sensible widening. I'm not a huge fan of coding standards checking other languages with PHPCS because generally there are better tools for each language. But in the case of yml this makes sense.
Let's copy the exclude in settings.php ie.
So that this is consistent. And add a comment saying the this excludes the directory based on what's in settings.php.
Comment #14
idebr CreditAttribution: idebr at ezCompany commented#13.1 Added an exclude-pattern for the "bower_components" folder
#13.2 Added a comment referencing the file_scan_ignore_directories option in settings.php
Comment #15
borisson_Thanks @idebr, the changes in #14 seems to resolve the issue @alexpott mentioned in #13. I just ran
composer phpcs
again and there are no new failures.Comment #16
alexpottCommitted 1b07d55 and pushed to 8.6.x. Thanks!