Follow-up to #2572659: Fix 'Drupal.Commenting.InlineComment.SpacingAfter' coding standard
Part of #2571965: [meta] Fix PHP coding standards in core, stage 1.
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.Commenting.InlineComment.DocBlock"/>
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 |
|---|
Issue fork drupal-2719653
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
andypostComment #3
pashupathi nath gajawada commentedI would fix it.
Comment #4
vprocessor commentedI am on it
Comment #5
vprocessor commenteddone
Comment #6
mile23Comment #7
prabhurajn654 commentedComment #9
vermauv commentedComment #10
basil_snowman commentedpatch added
Comment #14
mfernea commentedHere we should only target Drupal.Commenting.InlineComment.DocBlock sniff. Previous patches contain unrelated fixes.
Comment #15
mfernea commentedHere is the patch.
Comment #16
mfernea commentedAs in, here.
Comment #17
mfernea commentedI'm not sure about this modification, as @deprecated should be used for declarations not for usages. So, we can also remove the comment.
Comment #18
savkaviktor16@gmail.com commentedComment #19
savkaviktor16@gmail.com commentedre-rolled
Comment #21
savkaviktor16@gmail.com commentedre-rolled again
Comment #22
savkaviktor16@gmail.com commentedComment #23
mile23So is Link deprecated or its use of LinkGeneratorTrait deprecated? If it's Link that's deprecated, then this deprecation message is in the wrong place. LinkGeneratorTrait is deprecated itself, so maybe this is marking that. Basically, it's unclear.
Wrap at 80 chars.
Comment #24
mfernea commentedComment #25
mfernea commentedInlineCommentSniff is added twice in the patch from #24. I fixed that.
Comment #26
mile23Sorry I wasn't clearer earlier. If it's deprecated then remove the comment because it has its own deprecation message. If the usage of this trait is deprecated here and it's going to be removed from Link, then change it to a @todo with a follow-up issue.
Comment #27
mfernea commentedThis comment was introduced in #2529560: Expand support for link objects.
Few comments related to this:
Comment #28
ivan berezhnov commentedComment #35
longwaveI guess the sniff has been changed here at some point as many hunks of the previous patches in this thread are no longer needed for phpcs to pass. This patch only contains the changes required for the sniff to pass, no interdiff as this was rolled from scratch.
Comment #36
andypostNot clear how to find coder's results at CI...
I bet it should trigger other rule
Comment #37
quietone commented@andypost, do you mean the one about space after inline comments? That is still in discussion, particularly 2572659#comment-79. Or are you referring to something else?
Comment #38
andypostI mean wrong indent should be caught by other sniffer
Comment #39
quietone commented@andypost, I still do not understand. What is the 'other sniffer' you are referring to? And what action is to be taken here in this patch?
Comment #40
longwavePHPCS results are at https://dispatcher.drupalci.org/job/drupal_patches/77111/consoleFull
Search for "commit-code-check", this is the output from commit-code-check.sh that you can also run locally.
@andypost those lines look indented correctly to me, if you mean the empty comment line then that will have to be handled in #2572659: Fix 'Drupal.Commenting.InlineComment.SpacingAfter' coding standard where there are other similar cases already.
Comment #42
daffie commentedComment #43
ankithashettyJust re-rolled the patch in #35, thanks!
Comment #44
gauravvvv commentedRe-rolled patch #43, Attached interdiff for same.
Comment #45
longwaveThe code block in RearrangeFilter looks wrong to me, and:
Comment #46
andypostLet's see another array syntax
Comment #47
daffie commentedAll code changes look good to me.
The rule change is in phpcs.xml.dist.
To me it is RTBC.
Comment #49
andypostunrelated test failure
Comment #50
alexpotthttps://www.drupal.org/pift-ci-job/2188125 shows that this introduces quite a few fails when run the coding standards checks. Seems there is more to do here!
Comment #51
suresh prabhu parkala commentedTried to fix cs errors. Please review.
Comment #52
daffie commentedNot all cs are fixed.
Comment #53
karishmaamin commentedTried to remove custom command fail. Please review
Comment #54
daffie commented@karishmaamin: I you think your patch is ready for a review, then please the status of the issue to "Needs review".
Comment #55
quietone commentedThe changes made in the latest patch move the PHP8 attribute to inside the doc block. According to php-attributes the annotation is to be before or after the docblock.
I confirmed that the sniff does identify these as errors. It seems like we need an issue in coder to modify the sniff?
Comment #56
longwaveYeah, this is an issue in Coder now. All the fails in https://www.drupal.org/pift-ci-job/2188125 are for things like
The attribute added between the docblock and method signature causes the sniff to trigger a false positive. Moving the attribute above the docblock doesn't help either, that just produces a different error.
Marking as postponed as there is nothing we can do here, we need a fix in Coder first.
Comment #57
longwaveOpened #3250346: InlineComment.DocBlock sniff does not understand PHP attributes
Comment #62
quietone commentedThe sniff referred to in #57 was closed in favor of #3250986: Sniff "Drupal.Commenting.InlineComment.WrongStyle" doesn't like #[\ReturnTypeWillChange], which is fixed.
Comment #64
quietone commentedComment #65
smustgrave commentedWill admit don't get the reason for going from /** to /* but if that's the rule.
The update to the .dist shows the rule being enforced and none of the changes seems off, minus what I mentioned. Think this is fine.
Comment #69
nod_Committed 9c7b3cb and pushed to 11.x. Thanks!