Follow-up to #2572649: Fix 'Drupal.Commenting.HookComment' coding standard
This was not really fixed... there is well over 100+ in core... :( and they have not been introduced since. Maybe the sniff has changed. We need a followup issue to fix the rest of these and remove the exclusion from phpcs.xml.dist
Part of #2571965: [meta] Fix PHP coding standards in core.
Approach
We are testing coding standards with PHP CodeSniffer, using the Drupal coding standards from the Coder module. Both of these packages are not installed in Drupal core. 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.AnimalControlStructure.BracketedFence
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
Both of these packages are not installed by default in Drupal core, so we need to download them. This can be done with Composer, from the root folder of your Drupal installation:
$ composer require drupal/coder squizlabs/php_codesniffer
$ ./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 |
---|---|---|---|
#46 | interdiff_2623718_43-46.txt | 759 bytes | ankithashetty |
#46 | 2623718-46.patch | 11.83 KB | ankithashetty |
#43 | interdiff_40-43.txt | 1.41 KB | vsujeetkumar |
#43 | 2623718_43.patch | 10.85 KB | vsujeetkumar |
#40 | 2623718-40.patch | 11.6 KB | vsujeetkumar |
Comments
Comment #2
attiks CreditAttribution: attiks at Attiks commentedComment #3
attiks CreditAttribution: attiks at Attiks commentedSad thing is that there's no phpcf for this
Comment #4
attiks CreditAttribution: attiks at Attiks commentedAttached the output
Comment #5
attiks CreditAttribution: attiks at Attiks commentedPatch created using
grep -r -l Implements * | xargs sed -i 's/^\( \* Implements [^(].*\)().*/\1()./g'
Comment #6
attiks CreditAttribution: attiks at Attiks commentedFix remaining once manually
Comment #8
attiks CreditAttribution: attiks at Attiks commentedPatch rebased
Comment #9
pfrenssenComment #11
pfrenssenComment #12
pfrenssenComment #13
pfrenssenComment #14
alexpottWe've changed the way we do phpcs.xml.dist - its inclusive not exclusive.
Comment #15
alexpottI'm not sure about these changes. I think we should update the rule to permit this.
Comment #16
dawehnerAT least #15 is a good novice task.
Comment #18
ZeiP CreditAttribution: ZeiP as a volunteer and at Citrus Solutions Oy commentedThe change in #15 has been submitted as issue #2821834: Loosen the Drupal.Commenting.HookComment sniff per #2623718, is that ok?
Comment #21
andypostAbout #15 from
core/vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Commenting/HookCommentSniff.php:92
Format should be "* Implements hook_foo().", "* Implements hook_foo_BAR_ID_bar() for xyz_bar().",, "* Implements hook_foo_BAR_ID_bar() for xyz-bar.html.twig.", "* Implements hook_foo_BAR_ID_bar() for xyz-bar.tpl.php.", or "* Implements hook_foo_BAR_ID_bar() for block templates."
Comment #22
andypostThis patch needs rework, here's a patch to check state
Locally
Comment #23
andypostTotally new patch, looks everything fixed
Comment #25
borisson_This patch no longer applies, but all the changes in it look really solid. Setting to needs work so someone can reroll this.
Comment #26
MerryHamster CreditAttribution: MerryHamster at Skilld commentedHere reroll for 8.6.x
Comment #27
MerryHamster CreditAttribution: MerryHamster at Skilld commentedComment #28
borisson_Removing the needs reroll tag, patch applies and after running
composer phpcs
there are no remaining errors.Comment #29
alexpottThis comment needs re-flowing - we can use the full 80 chars here.
We've got some new errors here and they are interesting...
This is a class and not a direct hook implementation. I think the rule should be updated to only test procedural code and not classes. Once we have class based hooks they'll probably use an interface and therefore we'll have @inheritdoc.
So we need an issue filed against coder to fix the rule.
Comment #30
idebr CreditAttribution: idebr at iO commentedFiled an issue in the Coder module: #2971177: Drupal.Commenting.HookComment should only test procedural code and not classes
Comment #33
klausiPushed a fix for Coder. It will take a couple of weeks until I will make the next release, you can help updating Coder at #3063323: Update drupal/coder to 8.3.6 in the meantime.
Comment #37
longwaveThe fix for coder has been implemented, currently the failures for this sniff are in the following files, so the issue in #29 is resolved and this can be picked up again:
Comment #38
anmolgoyal74 CreditAttribution: anmolgoyal74 at OpenSense Labs commentedRe-rolled for 9.2.x.
Comment #39
idebr CreditAttribution: idebr at iO commentedThe patch in #38 is empty
Comment #40
vsujeetkumar CreditAttribution: vsujeetkumar at Srijan | A Material+ Company for Drupal India Association commentedRe-roll patch for 9.2.x.
Comment #41
vsujeetkumar CreditAttribution: vsujeetkumar at Srijan | A Material+ Company for Drupal India Association commentedComment #42
longwaveThis is looking close now.
This isn't strictly true as we allow multiple image libraries - I think we can just remove this line and just say
This should be
Is this change really in scope here?
Comment #43
vsujeetkumar CreditAttribution: vsujeetkumar at Srijan | A Material+ Company for Drupal India Association commented@longwave Changes has been done according to #42, Please have a look and advise.
Comment #44
vsujeetkumar CreditAttribution: vsujeetkumar at Srijan | A Material+ Company for Drupal India Association commentedComment #45
longwaveThanks, those changes all look good. I ran phpcs locally over the full codebase and there is one remaining issue here:
In other words the @param lines need to be removed from:
Comment #46
ankithashettyUpdated the patch in #43 as suggested in #45. Thanks!
Comment #47
longwaveThanks, this looks ready to commit now.
Comment #50
catchCommitted/pushed to 9.2.x and 9.1.x.
Removed this hunk for the 9.1.x backport.