Part of #2571965: [meta] Fix PHP coding standards in core, stage 1.
Refer to coding standard pages, Arrays and Indenting and Whitespace.
Approach
We are testing coding standards with PHP CodeSniffer, using the Drupal coding standards from the Coder module. 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
$ composer install
$ ./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 -ps
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. The -s flag shows the sniffs when displaying results.
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.
Release notes snippet
The following coding standards checks have been enabled in core:
Drupal.Array.Array.ArrayClosingIndentation
Drupal.Array.Array.ArrayIndentation| Comment | File | Size | Author |
|---|---|---|---|
| #70 | 2937515-70-10.1.x.patch | 147.71 KB | quietone |
| #70 | interdiff-66-70-10.1.x.txt | 4.69 KB | quietone |
| #70 | 2937515-70-10.0.x.patch | 146.68 KB | quietone |
| #70 | interdiff-66-70-10.0.x.txt | 5.57 KB | quietone |
| #69 | 2937515-69-9.5.x.patch | 154.38 KB | quietone |
Issue fork drupal-2937515
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
rosk0Comment #3
SherFengChong commentedInitial patch version.
Comment #4
rosk0Good to go.
Comment #6
ivan berezhnov commentedComment #7
kala4ekReturn to RTBC, because all tests are marked as passed.
Comment #8
xjmSince this is a big cleanup, let's schedule this one for during the beta/RC phase so that it's less disruptive. We may need to generate a new patch at that time.
Comment #9
idebr commentedFeb. 7 2018 has passed, but I am not sure about the current target version (8.6.x) as xjm has tagged this is as a 'rc target'. This would indicate the target version is 8.5.x, which is currently in beta.
Comment #10
mfernea commentedI'm not sure how this got RTBC-ed! Twice! :)
Looking at the tests results there are many CS failures. Actually the phpcs.xml.dist was not modified correctly.
Altering phpcs.xml.dist by:
we are targeting all sniffs inside Drupal.Array.Array.
We should only target Drupal.Array.Array.ArrayClosingIndentation by using something like:
Looking at the patch it seems to fix all issues under Drupal.Array.Array, while it should only fix Drupal.Array.Array.ArrayClosingIndentation.
Comment #11
idebr commentedComment #12
borisson_The patch fixes the problem and it looks very good, I have some problems with some of the changes but they seem to pass phpcs on the bot (and locally).
phpcs doesn't complain about this, but this doesn't look right.
This also looks weird, but it is correct.
Most of the changes here have the same thing, they are correct but it looks weird.
Comment #13
idebr commented#12 I agree those changes look off. I'll see if there is a more sane indentation that will still pass phpcs.
Comment #14
mfernea commentedThe weird thing is that phpcs expects the array to indented with 2 more spaces than the line where the array declaration starts.
So looking at core/lib/Drupal/Core/Installer/Exception/AlreadyInstalledException.php both of these are correct:
Weird enough. And things get even more weird, as if we go for the first option, for the array items, Drupal.Array.Array.ArrayIndentation conflicts with Drupal.WhiteSpace.ScopeIndent.Incorrect. There's no way to solve both if the html code has no indentation.
I don't think that adding whitespace for the html code is a good idea, so I would extract the array declaration from the function call and put it above. What do you think?
Comment #15
berdirI see there were similar suggestions already above now that I added some comments, adding +1 to having lots of strange cases here that conflict with other sniffs and common sense :)
Yes, definitely a strange one, +1 to move that string out of the array.
this too, it's then actually on the same level as the lines above. Which are wrong and should be two spaces in.
While it might get (a lot) bigger, I'm wondering if it would make sense to combine this with correct indentation *inside* an array. But this alone results in some really weird cases where the sniffer produces false or false-looking results? Or do that part first..
also a good one, the multi-line SQL statement above is confusing it. No, I don't think this is correct, the only reason to format the SQL like that is to have it visually close together, could for example extract the query string into a $query variable or so above?
Comment #16
idebr commented#12.1 This looks a lot better with the added ArrayIndentation sniff
#12.2, #15.3 Moved the query string to a separate variable, so the code looks a bit cleaner indentation-wise
#12.3 Manually added indentation to the assertions that maintains a passing test, but makes the code more readable and passes the phpcs
#14, #15.1 Moved the replacement placeholder values to a separate values, so the code becomes a bit more readable and passes the phpcs
#15.2 Included the fixes and sniff for ArrayIndentation, since they make a lot more sense combined than separately. On the flipside, this makes the patch significantly larger
Also included an interdiff, but it is somewhat large with the new ArrayIndentation sniff
Comment #17
borisson_We usually do only one cs-fix per issue, but since these are so related, I can see the argument to do them in one issue. The testbot found 5 remaining cs failures, let's fix those as well.
Comment #18
idebr commentedThis should fix the remaining code sniffer violations.
Comment #19
borisson_The testbot agrees with this, I went trough the patch again and I agree with most of what was changed here.
This is the only thing I would change, but I think that might be out of scope. Otherwise this looks good to go for me.
We're changing these lines and they can be reflowed to something like this:
Comment #20
idebr commented#19 Agreed, considering we are touching those lines anyway, we might as well fill the lines with 80 characters.
Comment #22
idebr commentedSetting status back to 'Needs review' after #2973992: Permission issue in Nightwatch step marks all full testruns as unstable
Comment #26
klausiPatch does not apply anymore.
And we should do this against 9.1.x now, right?
Comment #27
kala4ekNah, nobody needs it.
Maybe in 3-5 years...
Comment #29
spokjeComment #31
spokjeComment #33
spokjeRebased MR on
9.3.x.Comment #34
quietone commentedI applied the patch and then ran phpcs. It returned 3 files with errors,
FILE: /var/www/html/core/modules/tour/src/TourViewBuilder.php
FILE: /var/www/html/core/modules/update/tests/src/Functional/UpdateSemverTestBase.php
FILE: /var/www/html/core/modules/user/tests/src/Functional/UserAdminTest.php
Then I reviewed the patch which was fairly easy since it is all formatting changes. Just two questions.
Comment #35
spokjeThanks @quietone for the review, I'll respond/correct (if needed) to the 2 questions in the MR.
I do have a question for you though:
Where does "the patch" come from, is it simply the diff from the MR perhaps?
If that is indeed the case, I don't understand that diff returning 3 PHPCS errors, since the MR itself doesn't seem to return any errors here:
https://dispatcher.drupalci.org/job/drupal_patches/88074/consoleFull (PHPCS output from
00:03:45.970 Checking core/includes/install.core.incuntil00:04:15.468 core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php passed.Comment #36
spokjeAnsweredReplied to both questions, hoping this will clear things up?Comment #37
quietone commented#35. Yes I applied the diff not a patch. I guess that was muscle memory typing 'patch'. The MR does not generate errors because drupalci runs commit-code-check which only runs phpcs on the files changed in the patch. It does not run phpcs on the entire code base.
Comment #38
spokjeThus spoketh @quietone in #37
Right, I always forget that.
I also did a manual run and indeed found out that indeed the three mentioned files produced a warning.
Not sure how they passed my manual check, but luckily you caught them.
Fixed them and they are now in the MR.
(Still haven't got time yet to checkout your replies in the MR, hope to get to those later today)
Comment #39
spokjeThanks @quietone for the (as usual) excellent review and explanations so even I can understand :)
Comment #40
longwaveNice work so far! Lots of comments though, there are many cases where something like
is now
and that can be wrapped back on to one line otherwise it looks wrong to me.
In general I think that pairs of brackets should match their opening line so if we start with
then it should be closed with
(although this is not always doable in some more complicated cases)
Comment #41
spokjeThanks @longwave for the review.
There's one remaining open thread about opening/not opening a follow-up issue left.
Comment #42
longwaveThanks for working on this, this is quite tedious but it will be nice to get all this properly lined up.
I did another pass at the MR and found a few I must have missed, the one that was left outstanding can just be ignored here I think - you are right that it will take a long time to solve via coding standards.
Comment #43
spokjeThanks @longwave for the review.
Comment #44
spokjeRrrrrrrrrandom Test Fail, retesting.
Comment #45
quietone commentedComment #46
daffie commentedTestbot is failing.
Comment #47
anweshasinha commentedHi,
Applied PHPCBF in thefiles and created a patch. Please review my work.
Comment #48
daffie commented@Anwesha Sinha: Could you be so kind and work on the MR. For more info about MR, please see: https://www.drupal.org/docs/develop/git/using-git-to-contribute-to-drupa...
Comment #49
longwave@anweshasinha: also in this issue we are only trying to fix the array indentation sniffs, your patch changes a large number of files and seems to cover lots of different coding standards sniffs. We also should not be changing e.g. the core/assets directory as this contains third party code that does not belong to Drupal. It also looks like phpcbf is either configured incorrectly or is just doing something wrong, because changes like this do not look correct at all:
Comment #50
anweshasinha commentedOk, I will find out and try to fix it. Thank you
Comment #51
spokjeLet's see what TestBot thinks, but I think we should be back in the green after the latest commit.
Comment #52
longwaveThanks for working on this!
I read through the whole MR again, and don't see any remaining issues - except for one change that appears to be out of scope where a double
drupalGet()has been removed. Once that has been resolved I think this is ready for RTBC :)Comment #53
spokjeThanks @longwave and @daffie for their eagle eyes on this.
- Merged latest
9.3.xinto MR- Resolved all
threatsthreadsBack to NR
Comment #54
longwaveAll known issues have been fixed and all questions answered, this has been nitpicked over by multiple contributors now, to me this is ready to go.
Comment #55
alexpottComment #56
longwaveMerged 9.3.x and fixed two conflicts, back to RTBC.
Comment #57
longwaveNeeded a further run of phpcbf and then some final manual cleanup, back to NR for someone to review the last commit.
Comment #58
quietone commentedLooks like that are more fixes to make.
Comment #59
spokjeComment #60
spokje9.4.xComment #61
longwaveI made one commit to this MR but have extensively reviewed the rest of it a number of times, the bot is happy now so I think I can still mark this RTBC.
Also added a release note snippet.
Comment #62
xjmThe 9.4.x version does not apply to 10.0.x. We might want two separate MRs here. Setting 10.0.x so that a new MR would be opened against that by default.
This changeset is large and disruptive enough also that it might be worth scheduling it as a beta target. If it ends up getting bounced around a couple more times then that's what I'd recommend. When the 9.4.x beta phase starts depends on when Drupal 10 requirements are completed, but scheduled beta targets would probably end up in the week of May 23.
Comment #63
quietone commentedI rerolled the diff and made a patch.
138 files changed, 714 insertions(+), 733 deletions(-)
Comment #64
klausiThanks, confirmed that the PHPCS run passes and that we have the change in phpcs.xml.dist.
Changes in the patch file also look good, "git diff --color-words" also only shows the rare cases where we move code around a bit.
Looks good to me!
Comment #65
alexpottCan we get a patch for 9.5.x as well? Then we can keep 9.5.x and 10.x aligned. We should land this patch during the beta phase.
Comment #66
quietone commentedYes, of course. Here is a 9.5.x patch and a diff between that and the 10.0 version.
This also needs a patch for 10.1.x which has an interdiff between 10.0 and 10.1. Needed because of #3215062: Update hook_schema for Y2038.
Comment #67
bbralaTried this, patches need a reroll unfortunately. :(
Comment #68
bbralaLets be a little more specific.
9.5.x
Patch applies. When looking through the code, (and running phpcs again) it seems this PHP file is broken:
After running phpcs in core i get this feedback:
10.0.x
Doesn't apply. Missing files, also when i force through phpstorm, there are conflicts in MediaLibraryTest
10.1.x
Same as for 10.0.x
Comment #69
quietone commentedThanks for the review.
Here is the 9.5 patch.
Comment #70
quietone commentedAnd now the patches for D10.
Comment #71
longwaveI scanned through the patches and all the changes look correct to me. Some of the interdiffs don't seem to make sense but when I check the related places in the patches they look good. Assuming the test runs agree, this is RTBC.
Comment #72
alexpottCommitted 803da73 and pushed to 10.1.x. Thanks!
Committed ea27211 and pushed to 10.0.x. Thanks!
Committed d3d2031 and pushed to 9.5.x. Thanks!
Comment #77
longwaveComment #82
quietone commented#2902636: Fix Drupal.Array.Array.ArrayIndentation coding standard was closed as a duplicate, moving credit to here.