Postponing on #2464123: Remove the requirement that no blank line follow an inline comment.
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.SpacingAfter"/>
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 |
|---|---|---|---|
| #102 | 2572659-102.patch | 116.93 KB | ayush.khare |
| #102 | rerolldiff_97-102.txt | 113.93 KB | ayush.khare |
| #97 | reroll_diff_2572659_91-97.txt | 69.07 KB | ankithashetty |
| #97 | 2572659-97.patch | 126.47 KB | ankithashetty |
| #91 | 2572659-91.patch | 127.95 KB | ayushmishra206 |
Comments
Comment #2
attiks commentedComment #5
duaelfrAs agreed between the mentors at Drupalcon, according to issues to avoid for novices, I am untagging this issue as "Beginner". This issue contains changes across a very wide range of files and might create too many other patches to need to be rerolled at this particular time. This patch has an automated way to be rerolled later so better to implement it after Drupalcon.
Comment #6
pfrenssenComment #8
mile23Updated title and summary. Working on a patch.
Comment #9
mile23Automated patch generated by phpcbf. It's basically un-reviewable....
I suggest we split this up into a bunch of patches: core/lib, core/includes, core/modules, and core/tests.
Should we make other issues or knock them down one at a time here?
Comment #10
mile23Comment #11
jhedstromI spent a few minutes reviewing the mega patch, and didn't get very far, so +1 for smaller issues I think.
I found a lot in a short bit:
This doesn't look quite right :)
Or these.
Many of these would probably benefit from being wrapped in @code blocks, but that seems out of scope here.
False positive. Should probably be "`fseek`"?
More false positives.
Comment #12
pfrenssenYeah we definitely need to chunk this, it's not realistic to sign off on a humongous patch like this. Splitting by path will probably still be too large.
Shall we do it in chunks of 20kb max? That would be easy to review + commit. We can iterate here, and once we get to the final chunk we update the ruleset.
Comment #13
mile23Concentrating on
core/lib/Drupal/Component.phpcbf does pretty well as a starting point, but does require review.
Code that's obviously been commented out gets deleted. :-)
The sniffer misses the finer points of grammar (adding a period in the wrong place).
phpcs also gives some false positives on inline
/** @var */comments. See: #2305593: [policy] Set a standard for @var inline variable type declarationsWe see a forbidden use of var in DiffFormatter: http://cgit.drupalcode.org/drupal/tree/core/lib/Drupal/Component/Diff/Di...
...which triggers of a bunch of other issues including seeing that property's docblock as an inline comment.
So either I change it here so it's not a var, or we put this off until properties are properly declared. Ahh, scope, you cruel mistress.
Comment #14
mile23Added #2706753: Fix 'PSR2.Classes.PropertyDeclaration.ScopeMissing/VarUsed' coding standard which covers the var problem.
Comment #15
pfrenssenPatch from #13 looks good.
Comment #16
dawehnerWe should not drop this comment, this has helpful information in there.
Comment #17
dawehnerComment #18
mile23Thanks.
Added that comment back, as a proper inline comment, which is a bit ugly. Please suggest a better replacement.
Comment #19
pfrenssenIt's not the most beautiful line of documentation encountered by mankind, but it is very clear and legible :) Back to RTBC, thanks!
Comment #21
alexpottIs user input code? Tricky.
Looks odd
I don't think code should be indented and is this code?
Also how many fails are there to fix if we apply this rule to all of core... and maybe it'd be better to fix by sub -sniff in core entirely?
Comment #22
andypostAuto fix for all core
phpcbf --sniffs=Drupal.Commenting.InlineComment coreComment #24
andypostdrop is moving
Comment #26
mile23We really should limit scope here because, for instance, we have to review the patch in #24 and find things like this:
That's a fix that introduces another error: Word wrap at 80.
But as for #21, I really wish we had some better direction on what change is needed for:
Comment #28
andypostsuppose we need to split the issue into parts (tests and code)
it really pita to review such size of patches
Comment #29
alexpottSo what we can do is not enable the entire rule. See #2707641-7: Ensure core compliance to Drupal.Commenting.FunctionComment.ParamCommentIndentation (part 2) For an example of how we can break this rule up into small amounts of work by excluding specific errors from the rule.
Comment #30
alexpottSo let's do Drupal.Commenting.InlineComment.SpacingAfter
Comment #32
alexpottThere are plenty of examples like this where we break wrapping... hmmm tricky.
Not our standards so perhaps we should mark this file to be ignored?
Comment #33
alexpottLet's do this against 8.2.x first.
Comment #34
mile23Just a heads-up that I made a little script to help figure out how to work on some of these things: #2571965-63: [meta] Fix PHP coding standards in core, stage 1
Comment #35
andypostFiled related patch to #2168241: Type hints for optional methods in StatementInterface (D8) / DatabaseStatementInterface (D7)
Comment #36
pfrenssenComment #37
andypostThis fixes only
SpacingAfterComment #38
andypostAdded issues for the rest
Comment #39
rasikap commentedComment #41
rasikap commentedComment #43
rasikap commentedComment #44
rasikap commentedComment #45
dawehnerThe latest patch no longer contains a
phpcs.xml.distchange. The issue summary describes how to fix CS issues.Comment #46
rasikap commentedComment #47
rasikap commentedAdded phpcs.xml file
Comment #48
tstoeckler@rasikap We shouldn't add a new
phpcs.xmlfile but instead amend the existingphpcs.xml.dist.Comment #49
rasikap commentedComment #50
rasikap commented@tstoeckler, added the ruleset in phpcs.xml.dist itself.
Comment #51
rasikap commentedComment #53
klausiThe change in phpcs.xml.dist is on the wrong line, please insert it alphabetically with the other Durpal standard sniffs.
Then the fixes for the newly added sniff are missing? The patch should include those. While running this sniff I found a false positive, opened a Coder issue: #2851518: False positives in Drupal.Commenting.InlineComment.SpacingBefore for multi line comments.
Drupal 8.4.x currently does not even pass its own configured rules so I also filed #2851510: Fix phpcs regressions by running phpcbf.
Comment #55
mfernea commentedThis was solved in #2716685: Part 2: Fix several errors in the 'Drupal.Commenting.DocComment' coding standard.
Comment #56
mfernea commentedI was wrong. #2716685: Part 2: Fix several errors in the 'Drupal.Commenting.DocComment' coding standard fixed Drupal.Commenting.DocComment.SpacingAfter and this one targets Drupal.Commenting.InlineComment.SpacingAfter.
Comment #57
jofitzStarted again from scratch.
Comment #58
mfernea commentedIf I look at the test results there is still one issue to be solved.
Comment #59
jofitzI don't know how that one slipped through. Fixed.
Comment #60
mfernea commentedWhile the patch is correct from the technical point of view, I have some mixed feelings about some type of changes like:
Title like comments.
Comment for end of code section.
Maybe it's best to remove the comments for the end of code sections. I'm not sure.
Comment #62
idebr commented#60 While I agree with your line of thought, for these type of issues it is typically best to leave the code as is and make the smallest change possible. Once you start refactoring, the issue will quickly escalate and become hard to actually get committed :)
Patch no longer applies to 8.6.x, so it will need to be rerolled.
Comment #63
savkaviktor16@gmail.com commentedRe-rolled
Comment #64
savkaviktor16@gmail.com commentedComment #65
nkoporecTested the #64, it applies cleanly and I think the comments look good now.
Comment #66
idebr commentedThe change in phpcs.xml.dist should make it stricter. This change removes a sniff instead.
The correct change to phpcs.xml.dist was in #59:
Comment #69
Shivalik commentedReview the patch.
Comment #70
idebr commentedComment #71
klausiWe should only fix Drupal.Commenting.InlineComment.SpacingAfter, not other things.
Please provide patches that only fix this rule and nothing else here.
Comment #73
nginex commentedTagging for Drupal Global Contribution Weekend
Comment #74
jungleNo interdiff as #69 did unexpected changes. it‘s meaningless to provide interdiff
Comment #75
jungleBTW, all fixed by phpcbf, no manual changes.
Comment #76
foxtrotcharlie commentedI looked through the diff and it seems to remove all empty lines after inline comments, which is what was required.
Comment #77
alexpottThis removal is odd because the commented out code is wrong - we need to find out why the code is commented out and resolve that.
Removals like this are good.
Changes like this look wrong to me. The spacing here has meaning. The comments are not connected. I think that the rule needs fixing to allow a space between two inline comments.
The filter module is already installed. This comment should be removed.
This is a common pattern where the space has meaning. The first comment is about the entire coming section. and the next comment is specific to the following line of code. The new line has meaning and should not be removed.
tldr; personally I think we need to check whether this coding standard is actually a coding standard. As far as I can see it is not - https://www.drupal.org/node/1354
Comment #78
jungleThanks @alexpott for reviewing.
I think the first comment could be moved out as a part of the comment of the function/method.
The same, the first could be moved out as a part of the function/method comment. if one function/method has multiple sections, maybe it's a code smell. One way is to split it into small functions/methods. It's better that one function does one thing. Maybe, to move the inline comments of all sections out to the description of the function/method is reasonable. Code should be self-explained. It's untrue that the more comments, the better.
So about #77.3 and 5, my suggestion is to move out the first comment to the description of the function/method or to refactor the code itself.
Comment #79
alexpott@jungle the more I think about it the more I think this coding standard is incorrect - at least in implementation. Sure the following case could be flagged:
But doing something like
Is wrong. Space has meaning and that's a good thing. I think we should file an issue issue to either fix the coder coder so that we only flag this is the after the blank line there's code - or we file an issue to remove the rule from coder.
Comment #80
prabha1997 commentedComment #81
prabha1997 commented+++ b/core/modules/editor/tests/src/Kernel/EditorManagerTest.php
@@ -31,7 +31,6 @@ protected function setUp() {
// Install the Filter module.
I removed this comment. Plz review patch
Comment #83
daffie commented+1 For the comment #79 from @alexpott. A lot of changes from the patch look wrong.
Comment #84
nikitagupta commentedComment #85
nikitagupta commentedComment #86
nikitagupta commentedReroll the patch and worked on #79
Comment #87
nikitagupta commentedComment #89
andypostNeeds reroll after commited #2623718: Fix 'Drupal.Commenting.HookComment' coding standard
Comment #90
ayushmishra206 commentedWorking on this.
Comment #91
ayushmishra206 commentedRerolled the patch for 9.2.x.
Comment #92
andypostLatest patches missing Step 3 from issue summary - update default sniffers in
core/phpcs.xml.distwith<rule ref="Drupal.Commenting.InlineComment.SpacingAfter"/>See https://git.drupalcode.org/project/drupal/-/blob/9.2.x/core/phpcs.xml.di...
PS: updated IS with valid sniff name
Comment #93
longwaveI am in agreement with #60/#77.3/#77.5/#79 in that sometimes these blank lines are useful for readability and to split up groups of comments, so just removing them all is not the right answer and perhaps this isn't and shouldn't be a standard for us to follow, at least not with the sniff in it's current form.
Comment #94
quietone commentedI came here to review. It is not helpful that these coding standard issue do not have a Remaining Task section in the IS. It just makes it harder for a reviewer.
I thought I would apply the latest patch and have a look. It does not have changes to phpcs.xml. I then searched backwards and found that it it does not appear to be in patch form #85 onwards.
Now, reading the comments from the top everything look like typical work on an issue. However, in #77-#79 there are serious questions raised about using this sniff. Despite those concerns prabha1997, nikitagupta, ayushmishra206 made patches which are not helping to advance the issue (and do not have a phpcs.xml file as mentioned).
Going back to the serious questions. In #79 alexpott states
I agree, Drupal standards for in-line code comments states "Comments should be on a separate line immediately before the code line or block they reference. For example:" So, there isn't a strict requirement.
My two cents, when writing some tests I recall spending extra time to make in line comments that would pass this Drupal Practice standard. It was frustrating at the time but in the end I think the result was better readability. So, even though this sniff isn't a strict requirement I would be OK with it. Now, having that said that I will go with the consensus.
And the consensus seems to be to modify the sniff. Where does that work happen?
Setting to NR because the patch does not need work.
Comment #96
daffie commentedComment #97
ankithashettyJust re-rolled the patch in #91, thanks!
Comment #98
daffie commentedThe comment #92, #93 and #94 still need to be addressed.
Comment #102
ayush.khare commented10.1.x Reroll for #97
Comment #103
nikhil_110 commentedComment #104
smustgrave commented#102 appears to be missing a number of changes from #97 can it be documented why?
Also the interdiff is the same size of the patch?
Comment #105
quietone commentedThanks for the interest in this work. However, as pointed out in several comments the work here is to address comments #92, #93, #94. And #93 suggests that we don't implement this sniff at all!
I am changing the status to active so emphasize that the work here is to resolve #92, #93, #94. Also, updating credit per How is credit granted for Drupal core issues.
Comment #106
quietone commentedComment #108
nikolay shapovalov commentedI checked #94 and I totally agree that we need to update code sniff rule Drupal.Commenting.InlineComment.SpacingAfter to work with examples provided in #79.
Do we have issue for that?
Do I understand it right, before we have agreement or code sniff rule will be update, no further patches should be attached to this issue?
Do we need to update IS?
Comment #109
quietone commented@zniki.ru, thanks for the interest in coding standard issues! For some background, there is a Coding Standards project where the community discusses changes to the Drupal Coding standards. Changes agreed to there usually require a change to the Coder project to implement a new sniff or create a new one. Once, that is done, then we have issues it the core queue to implement the change. There is also the #coding_standards channel in Drupal Slack where coding standards are also discussed.
There is an issue in the Coding Standards project discussing changing this standard, #2464123: Remove the requirement that no blank line follow an inline comment. That issue is being worked on so maybe we should postpone this on that issue. I do want to avoid double work. I am adding that as a relating issue and postponing this on that issue.
Comment #110
quietone commented