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 |
---|---|---|---|
#78 | drupal-coding-standards-2715485-78.patch | 32.97 KB | mfernea |
#73 | drupal-coding-standards-2715485-73.patch | 1.28 KB | mfernea |
#59 | drupal-coding-standards-2715485-59.patch | 34.32 KB | mfernea |
Comments
Comment #2
Mile23Le Patch.
Comment #3
Mile23Kicking the testbot...
Comment #4
Mile23Comment #5
Mile23Ah well, no testbot.
There are a bunch of @todos in the patch, so I followed up on them, and added this one to connect the dots: #2715507: @todo: Modify core/profiles/standard/src/Tests/StandardTest.php
Comment #6
pfrenssenComment #7
dawehnerIMHO we should move that onto the previous line
Let's remove this code
Let's remove this pointless comment as well
Move on top
Comment #8
vprocessor CreditAttribution: vprocessor at Skilld commentedComment #9
vprocessor CreditAttribution: vprocessor at Skilld commentedrerolled & fixed according with dawehner comment #7
Comment #10
vprocessor CreditAttribution: vprocessor at Skilld commentedComment #12
vprocessor CreditAttribution: vprocessor at Skilld commentedComment #13
vprocessor CreditAttribution: vprocessor at Skilld commentedrerolled
Comment #14
Mile23So. Much. Commented. Code. :-(
If Simpletest had a way to mark a test as incomplete, I'd say we should do that here. Otherwise we should move the @todo to the class docblock.
Also phpcs says this.. (note that it's commented code..)
Comment #15
dawehnerTo be clear, removing this test is 100% fine, we don't have this PHP argument_default plugin anymore.
Novice to fix the issue pointed out by mile23
Comment #17
mradcliffeTasks that remain:
1. Make the fix suggested by mile23, which is to remove the argument default test and move the doc block.
Comment #18
imalabyaAdded patch for 8.3.x
Comment #19
imalabyaRe-uploading. Missed phpcs.xml.dist changes.
Comment #20
imalabyaJust noticed. Missed out @dawehner comments #7
Final patch from my end now.
Comment #21
dawehner@malavya Ensure to create interdiffs in the future.
Comment #23
klausiNot really useful to keep this comment without explaining what this even is? I would remove it.
That comment is not really useful, let's just remove it?
Comment #24
pfrenssenSo this seems to detect a whole bunch of commented out code that is left in our code base. Let's make some followups to get rid of it.
Comment #25
Mile238.3.x is in alpha now.
Comment #26
pfrenssenYes but coding standards fixes are acceptable right up to 8.3.0-RCx.
Comment #27
tameeshb CreditAttribution: tameeshb at Google Summer of Code commentedRerolled and made changes from #24
Comment #28
tameeshb CreditAttribution: tameeshb at Google Summer of Code commentedComment #29
klausiThanks, looks like you forgot to fix CommentResourceTestBase.php which shows up with 6 errors after I run phpcs on core.
Comments should not appear after statements. I think we should move the commented out message just as normal comment to the line before.
Comment #30
himanshu-dixit CreditAttribution: himanshu-dixit as a volunteer commentedHere is the patch that i made. I also changed, this line
to
I don't understand what's the need for
,
here.Comment #31
klausiThe comment was just commented out code from the assertion call where you can pass a third parameter as message.
Cool, can you also remove "');" at the end?
Comment #32
himanshu-dixit CreditAttribution: himanshu-dixit as a volunteer commentedHere is the new patch.
Comment #33
klausiNo, you should remove the quotes and the "," to make it look like a normal comment.
And I don't see the required changes to CommentResourceTestBase in this patch. Can you run ../vendor/bin/phpcs in the core directory and make sure its output is empty before you post the next patch. Thanks!
Comment #34
rajeevkComment #36
mfernea CreditAttribution: mfernea at AmeXio commentedThis needs work as the required phpcs.xml.dist are not correct.
Comment #37
harsha012 CreditAttribution: harsha012 as a volunteer and at Red Crackle commentedfixed the phpcs.xml.dist
Comment #38
mfernea CreditAttribution: mfernea at AmeXio commentedPlease check the test results. There are 378 cs violations.
Comment #39
mfernea CreditAttribution: mfernea at AmeXio commentedActually we are not excluding enough sniffs. I would go for these lines in phpcs.xml.dist:
Other notes:
Comment #40
harsha012 CreditAttribution: harsha012 as a volunteer and at Red Crackle commented@mfernea not sure about the below line
fixed 378 cs issue and also fixed the other point.
Comment #42
mfernea CreditAttribution: mfernea at AmeXio commentedHmm... actually you should have only modified phpcs.xml.dist as you did, but not fix the 378 cs issues. They wouldn't have been raised again. Also, they are not related to the sniffs we are fixing in this issue, mentioned in the title.
My suggestion would be to start from the patch at #37, do the modifications from #39 and see what else pops up. Also post an interdiff between the new patch and #37.
Looking at the interdiff between #37 and #40 regarding the issues mentioned in #39 the modifications look good.
Regarding ArgumentDefaultTest.php:
I would only add the space after "//":
//function testArgumentDefaultPhp() {}
->// function testArgumentDefaultPhp() {}
Comment #43
harsha012 CreditAttribution: harsha012 as a volunteer and at Red Crackle commented@mfernea
fixed as per your comment.
Comment #44
harsha012 CreditAttribution: harsha012 as a volunteer and at Red Crackle commentedComment #45
mfernea CreditAttribution: mfernea at AmeXio commentedLooks good.
In the latest patch testArgumentDefaultNode function's comment was removed and it triggers a cs error. The comment was:
Also, if I look at the test results I notice one extra cs issue.
Comment #46
harsha012 CreditAttribution: harsha012 as a volunteer and at Red Crackle commented@mfernea
fixed as per the comment
Comment #48
cburschkaTest failure from #2857843: Random fail in Drupal\KernelTests\Core\Entity\ContentEntityChangedTest::testChanged.
Comment #49
cburschkaIs this identical to #2719649: Fix 'Drupal.Commenting.InlineComment.SpacingBefore' coding standard? The patch seems to be targeting the same lines.
Comment #50
mfernea CreditAttribution: mfernea at AmeXio commentedGood work! The patch looks fine. Applies cleanly. No cs errors.
@harsha012: Maybe one more thing, but less important. Add back the empty line after:
@cburschka: If I apply this patch there are still a lot of SpacingBefore issues. SpacingBefore focuses on indentation.
Comment #52
mfernea CreditAttribution: mfernea at AmeXio commentedQuick re-roll. I also added back the blank line mentioned at #50.
Comment #54
jofitz CreditAttribution: jofitz at ComputerMinds commentedRe-rolled patch from #46 and re-added the blank line mentioned in #50 (same as was attempted in #52).
Comment #55
tiago.urbano CreditAttribution: tiago.urbano at CI&T commentedComments should start with capital letter and end with full stops.
Comment #56
mfernea CreditAttribution: mfernea at AmeXio commented@tiago.urbano We are not targeting those sniffs in this patch. As you can see in the phpcs.xml.dist Drupal.Commenting.InlineComment.InvalidEndChar and Drupal.Commenting.InlineComment.NotCapital are excluded. They are handled in other issues.
Patch looks good. No cs issues. RTBC.
Comment #57
xjmSo this patch mixes several different kinds of fixes to comments and fixes several rules in a single patch, which isn't the best issue scope per https://www.drupal.org/core/scope. It's mostly adding a space between the double slash and words, but there are some places that are moved to the line before and several places that are removing commented code entirely. We shouldn't do that -- each place where there is commented code should probably have a git blame and be discussed separately.
Keep in mind that coding standards issues are often best reviewed with word diffs.
git diff --color-words
would show nothing for the majority of this patch where it's just fixing whitespace, but then there are a number of places where it shows something and then the reviewer has to switch contexts to think about the lines and do research into why they are being removed.Aside: This is a terrifying comment to read.
Can we make this patch a patch that just fixes the comment initial whitespace rule, have a different issue that fixes the "comment on its own line" rule, and have separate issues that discuss removing the commented-out code rather than doing it in this patch, with research into why the commented code is there in the first place?
I've removed issue credit for @tiago.urbano because that review is out of scope for this issue, and attaching screenshots of patches applied to code is not helpful. I've also removed credit for @RajeevK since they only attached a patch that did not apply and did not address feedback. In the future, please read the issue carefully to ensure you are helping make progress on it, so that you can receive issue credit. Thanks!
Thanks everyone for your ongoing work to clean up these coding standards and enable our coding rules for checking; it's really appreciated and will make it so we don't have to worry about these sorts of issues in the future.
Comment #58
mfernea CreditAttribution: mfernea at AmeXio commentedI update the issue title and opened #2909163: Fix 'Drupal.Commenting.InlineComment.WrongStyle' coding standard as a follow-up.
For commented code we can add the required space when missing.
I'm not sure what to do with commented code, as there are a lot of them. How to create new issues? Grouped or individual? Both options have downsides :).
Comment #59
mfernea CreditAttribution: mfernea at AmeXio commentedHere is the updated patch.
Comment #60
xjmI would lean toward one issue per API for the commented code followups. This adds some overhead for issue creation etc., but then we can use git blame, relate the past issues that commented the code, file followups if needed, and get input from the relevant subsystem maintainer. I think maybe a meta is a good idea. Then we could file issues just for the two we decided to remove here, and let the meta take care of others since this is only tangentially connected to this coding standard.
Comment #61
xjmThe updated patch looks like a good scope to me if some others want to review/confirm for RTBC ('cause then I can commit it). Thanks!
Comment #62
mfernea CreditAttribution: mfernea at AmeXio commentedActually, the modifications that started the discussion belong to #2901572: Fix 'Drupal.Commenting.PostStatementComment' coding standard and not to #2909163: Fix 'Drupal.Commenting.InlineComment.WrongStyle' coding standard. I will add comments there on how to approach those issues.
Comment #63
andriyun CreditAttribution: andriyun at Skilld, Drupal Ukraine Community commentedPatch is pretty clear for me and contains only issue sniff related changes.
Agree that we should fix other issues related to inline comment issues in proper sniff treads
Let's be sure that we create all following issues.
There is the list related follow-up issues:
+1 to RTBC
Comment #64
andriyun CreditAttribution: andriyun at Skilld, Drupal Ukraine Community commentedComment #67
xjmAlright, thanks, this is the patch I'd expect.
../vendor/bin/phpcs -p
on 8.5.x with the patch applied and confirmed there were no failures.Committed to 8.5.x and cherry-picked to 8.4.x as a coding standards cleanup during RC.
Posted #2909362: [meta] Commented-out code in Drupal to address commented-out code. We can probably redirect some of the other issues' followups there as well.
Thanks everyone! See you in the other issues. It will be helpful if you can mention in the summaries of those issues who from this issue should also receive credit (if they participated in the discussion for how to fix that part). Thanks!
Comment #70
xjmOh darnit. This one also has diff component files changes in it. Let's please remove those hunks before committing. Thanks!
Comment #71
mfernea CreditAttribution: mfernea at AmeXio commentedI will handle this shortly.
Comment #72
mfernea CreditAttribution: mfernea at AmeXio commented@xjm: you reverted the revert on 8.5.x, I guess you meant to revert from both branches, no?
Comment #73
mfernea CreditAttribution: mfernea at AmeXio commentedThis would be the patch if the commit was reverted, not reverted twice. So, I'm not setting the NR status yet.
Comment #76
xjm#fail. Thanks @mfernea. That'll teach me to use relative refs rather than the actual commit hash. :P
#73 is kind of an interdiff. Let's get the rest of the patch, without that bit. :) Thanks!
Comment #77
xjmOr, we could keep going for more reverts of reverts of reverts... ;) but probably best not.
Comment #78
mfernea CreditAttribution: mfernea at AmeXio commentedHere is the updated patch.
Comment #79
andriyun CreditAttribution: andriyun as a volunteer and at Drupal Ukraine Community for Drupal Ukraine Community commentedOk
No more changes for Diff component in patch.
git diff --color-words
show phpcs.xml.dist and few removed
/
So move issue to RTBC
Comment #81
catchWe should remove the inline comment here - it's just commenting out the assertion message. So either kill the message altogether or uncomment it.
Comment #82
mfernea CreditAttribution: mfernea at AmeXio commentedWe agreed with @xjm to just fix the cs issues here and open child issues of #2909362: [meta] Commented-out code in Drupal for these lines. So for the above mentioned line we have #2911166: Undo accidental commenting of message in EntityDefinitionUpdateTest. Isn't it ok?
Comment #83
mfernea CreditAttribution: mfernea at AmeXio commentedComment #84
xjmYep, back to RTBC. :) Sorry for the confusion!
Comment #86
catchFair enough.
Committed/pushed to 8.5.x and cherry-picked to 8.4.x. Thanks!
Comment #88
Malevi4 CreditAttribution: Malevi4 at Drupal Ukraine Community commentedHi guys I have additional changes but for 8.5.x, please verify it.
Comment #89
Malevi4 CreditAttribution: Malevi4 at Drupal Ukraine Community commentedComment #91
mfernea CreditAttribution: mfernea at AmeXio commentedThis issue is fixed.
@Malevi4: changes in your patch relate to the sniff fixed in #2719649: Fix 'Drupal.Commenting.InlineComment.SpacingBefore' coding standard.
Comment #92
Malevi4 CreditAttribution: Malevi4 at Drupal Ukraine Community commentedThanks, I will move it to #2719649: Fix 'Drupal.Commenting.InlineComment.SpacingBefore' coding standard.
Comment #93
Malevi4 CreditAttribution: Malevi4 at Drupal Ukraine Community commented