Follow-up to #2572659: Fix 'Drupal.Commenting.InlineComment.SpacingAfter' coding standard
Part of #2571965: [meta] Fix PHP coding standards in core, stage 1.
This relates to Drupal API documentation standards (general) and Drupal standards for in-line code comments.
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.NotCapital"/>
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 |
|---|---|---|---|
| #58 | 2719657-nr-bot.txt | 90 bytes | needs-review-queue-bot |
Issue fork drupal-2719657
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'm on it.
Comment #4
andypostThis issue has collisions with other inline comments.
phpcbf fixed most of lines but I manually edited that places to conform standards (see interdiff)
Comment #6
mile23Unfortunately neither patch applies.
Comment #7
dawehnerComment #9
ilya.no commentedCheck, please, re-rolled patch.
Comment #11
alexpottThis looks wrong. Let's add the @todo but remove the code.
Well it doesn't return FALSE. I think we should just make the comment
// Empty directory.This looks wrong.
The fseek()is odd. The PHPCS rule should be clever enough to allow comments to begin with function names imo.Hmmm this all looks odd.
Maybe
The 'settings' value is...sinceSettingsis not in the array.This comment is not correctly wrapped.
Comment wrapping.
tocan now go on the previous line.Bit of a useless comment.
Comment #12
andypost@alexpott do you think we should manually fix all this comments or use
phpcbf(suppose this wrong in that case)Because maybe better to file separate issue to actualize pointed places?
Comment #14
pfrenssenThis is blocked on #2168241: Type hints for optional methods in StatementInterface (D8) / DatabaseStatementInterface (D7).
Comment #20
quietone commentedThis was postponed on an issue that is now closed as outdated, so work can begin again.
#11.3 Here alexpott thinks the sniff should be smarter so that lower case function names can start a sentence. Should this be postponed on improvements to the sniff or add ignore lines before comments like that?
Comment #21
quietone commentedRerolled the patch in #9. Still need to address #11.
Comment #22
quietone commentedAnd now respond to #11.
1. No longer relevant, constructor removed.
2. Fixed.
3. Surrounded comment with phpcs:disable/enable
4. Looks odd to me to. Not sure what to do to improve it.
5. No longer relevant
6. Wrapping fixed.
7. Wrapping fixed.
8. No longer relevant.
The drupalci.yml file is changed to just run commit-code-check. I did that in the patch in #21 but made a mistake.
Comment #23
quietone commentedAdd periods to end of comments.
I also noticed that the sniff finds
// prefixbut not// prefix.Comment #26
mile23A do-over is easier than a re-roll.
Special call-out to
core/modules/views/tests/src/Functional/Plugin/ArgumentDefaultTest.php, where a test function was commended out with a todo, and now I've upgraded it to a test marked incomplete. This makes me wonder if #2905007: Allow run-tests.sh to report skipped/incomplete PHPUnit tests is still needed, but that's for later. My wager is that a reviewer will want to just delete the test function.The rest of the changes are pretty simple, and it looks like all the concerns from #11 have been refactored away in some form or another over time.
Comment #27
mile23Must declare visibility....
Comment #28
erikaagp commentedComment #29
alexpottWe're going to need a patch for 10.1.x / 10.0.x too.
Also I think we need to have a think about some of the comments that are being changed here.
This comment is not worthwhile.
Why not
// suffix.too - also this would be better to change all this to keys for $data.Comment #31
ayush.khare commented10.1.x Reroll for #27
Comment #32
ayush.khare commentedFixed CCF in #31
Comment #33
spokjeComment #34
himanshu_jhaloya commentedI will review the patch
Comment #36
himanshu_jhaloya commented#31 #32 patch failed to applied
Comment #37
spokjeCan we not "just" get a reroll but can we also, at the same time, address the points in #29?
Comment #38
spokjeComment #39
spokjeComment #40
spokjeComment #41
mile23Patch in #39 no longer applies. Using
git apply --rejshows us that all changes apply except the one tocore/phpunit.xml.dist, and then passescomposer phpcs. So it should be easy to reroll.MR in #35 seems broken.
In an ideal world, the MR would reflect the current changes, but it might be less work just to stick with patches. Dunno.
Comment #42
ankithashettyHello @Mile23! Could you please try against the latest core 10.1.x? The #39 patch still applies successfully.
Also, there is no change made to file
core/phpunit.xml.distin the patch.🤔Restoring back to the previous state. Thanks!
Comment #43
rpayanmI created a patch following the steps described in the IS and compared it with the patch from #39, I saw some differences, but they were discussed in the comments. So it looks good for me.
Comment #45
xjmThe test failure was a known random failure, so retesting and setting back RTBC.
Comment #46
xjmClosing the broken MR for clarity and removing credit for it.
Comment #48
xjmIs there a separate rule we can enable for comments that don't end in a period? Because a lot of these qualify.
Edit: Similarly an inline comment line length rule.
Comment #49
xjmI think ETag is actually the canonical capitalization. The method is only named
getEtag()because of our camel-casing spelling rules.I think we need to rethink how we're addressing the comments in
core/modules/system/tests/src/Kernel/Migrate/d6/MigrateSystemConfigurationTest.php.For example
This should be "UUID" when used in a sentence.
If this is used as a word in a sentence, it should be "Auto-run".
Langcode and default_langcode are not handled by the migration.This is inconsistent. Either both
langcodeanddefault_langcodeare machine names, or they should be treated as words in a sentence, i.e. "Language code" and "Default language code".For all of the above -- and other similar comments in the same file about gzip and such I think the sentence should be rephrased as:
The 'foo' property is not handled by the migration.I'm not sure if it's a "property", maybe it's a "configuration" or a "settting" or something else. Use the appropriate noun in context.
// suggestionnotimplemented() is not an implemented theme hook soI could not find
suggestionnotimplemented()anywhere in our API. It appears to be test data used inThemeTestandRendererTest. The CSpell issues will need to fix this eventually, but just adding parentheses is fine for now.Uh, what? Reading the code, I think this means:
(Though who would think + meant "or"?) Anyway. Followup for this.
This needs to use the full word, "Initialize".
There is an out-of-scope hunk in
core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.phpadding string typehints to a method.That same class rewrites a data provider to use meaningful array keys rather than inline comments on the cases, which is a good change. That could be handled in a separate, blocking issue to make things easier.
core/modules/views/views.api.phpare weird; we have the sentence fragments:// Although the table aliases will be different.I looked at this in context and the inline comment before the code snippet is:
So, I think we should changes these two to:
This should actually be changed to:
Comment #50
Aadhar_Gupta commentedChanges described in comment number 49 have been applied.
Here is the patch and inter-diff for the same -
Comment #51
Aadhar_Gupta commentedComment #52
xjmVery little of #49 is actually addressed, at least based on the interdiff posted.
Comment #55
quietone commentedConverted to an MR, ran phpbcf and then started work on #49.
#49.
1. Fixed
2. The first two are changed as asked for. The third corrects 'Langcode' but does not rework the sentences to use quotes around the variable names. I agree that it is correct and we are touching that line but I stopped because of the comment suggests it be done in the rest of the file. That would be out of scope. Even with inconsistencies across files I prefer to keep the style of comments in a file the same for readability.
3. Fixed in #3395900: Correct spelling of words in module core/tests
4. Still needs a followup.
5. Fixed.
6, 7 No longer relvant
8. No change. The suggested change doesn't make sense to me at this late hour.
9. Fixed
Comment #56
quietone commented#49.
4. Fixed.
8. Fixed
Comment #57
smustgrave commentedOf the points in #49 only 49.2 was one I had a question on. Left the question in the MR but will leave in review for others.
Comment #58
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #59
quietone commentedComment #60
smustgrave commentedMy one comment/concern has been answered. Believe this is good
Comment #65
catchCommitted/pushed to 11.x. I also committed/cherry-picked the same diff minus the actual phpcs.xml.dist change, to 11.0.x, 10.4.x, and 10.3.x to keep codebase in sync.