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: Run the test
Now you are ready to run the test! From within the core/
folder, run the following command to launch the test:
$ composer run 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 3: 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:
$ composer run phpcbf
This will fix the errors in place or composer will tell you that Script phpcbf --standard=core/phpcs.xml.dist --runtime-set installed_paths $($COMPOSER_BINARY config vendor-dir)/drupal/coder/coder_sniffer -- handling the phpcbf event returned with error code 1
meaning there was no files fixed and you need to fix them manually. 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 |
---|---|---|---|
#34 | 2937844-33-D89.patch | 8.82 KB | Spokje |
#33 | 2937844-33-D90.patch | 8.82 KB | Spokje |
Comments
Comment #2
RoSk0Comment #3
tstoecklerThis looks strange, but is actually correct.
Below this is the following:
The question is whether we need test coverage for this. I think yes.
Comment #4
borisson_I'm pretty sure these changes are not correct. This code hasn't been executing since 2013. We should either remove this code or make it work again, but keeping this as non-executing sounds like a bad idea.
This is not related to this issue though, created #2947588: Refactor \Drupal\Tests\views\Kernel\ModuleTest::testViewsGetHandler to fix that.
I agree with the remark in #3 that we need testcoverage, but that shouldn't be added in this issue.
Comment #5
borisson_Patch no longer applies, but I think we should postpone this on #2947588: Refactor \Drupal\Tests\views\Kernel\ModuleTest::testViewsGetHandler instead of rerolling this.
Comment #7
andypostFiled #3007436: Fix d7 comment migration to address #3 but new patch includes it with #2947588-11: Refactor \Drupal\Tests\views\Kernel\ModuleTest::testViewsGetHandler to make sure everything covered
Comment #9
andypostComment #13
daffie CreditAttribution: daffie commentedPatch does not apply to 9.1. Needs a reroll.
Comment #14
andypostQuick re-roll
Not sure about this hunk, but it looks useless
Comment #15
andypostFixed new
composer phpcs -- -p --sniffs=Squiz.PHP.NonExecutableCode core
Comment #16
daffie CreditAttribution: daffie commentedI think we should postpone this issue on #2947588: Refactor \Drupal\Tests\views\Kernel\ModuleTest::testViewsGetHandler.
Comment #17
andypostBlocker is in
Comment #18
ravi.shankar CreditAttribution: ravi.shankar at OpenSense Labs commentedWorking on this.
Comment #19
ravi.shankar CreditAttribution: ravi.shankar at OpenSense Labs commentedHere I have added reroll of patch #15.
Comment #20
daffie CreditAttribution: daffie commentedThe testbot returns with 6 coding standards messages. See: https://www.drupal.org/pift-ci-job/1726253.
Comment #21
mrinalini9 CreditAttribution: mrinalini9 at Srijan | A Material+ Company for Drupal India Association commentedComment #22
mrinalini9 CreditAttribution: mrinalini9 at Srijan | A Material+ Company for Drupal India Association commentedFixed coding standard issues as mentioned in #20, please review.
Comment #23
daffie CreditAttribution: daffie commentedAll code changes look good to me.
The testbot returns no coding standards messages.
The PHPCS rule is added.
For me it is RTBC.
Comment #24
xjmThanks for working on this issue. Glad to see us moving away from the weird stuff in #2.
This is a run-on sentence. It should say: "There are are errors. Therefore, there is nothing to do here.
For these, where we don't provide an inline comment, do our coding standards allow putting the curlies on the same line? E.g.:
Ideally, though, we'd be providing a comment as to why the method is empty.
Thanks!
Comment #25
Deepak Goyal CreditAttribution: Deepak Goyal at Srijan | A Material+ Company for Drupal India Association commentedComment #26
Deepak Goyal CreditAttribution: Deepak Goyal at Srijan | A Material+ Company for Drupal India Association commentedHi @xjm
Made changes as you suggested please take a look.
Comment #27
daffie CreditAttribution: daffie commentedI agree with @xjm that having a comment why a method does nothing is better then no comment. With that said and looking at the by @xjm mentioned methods. For each case it is self evident why the methods have no code. Adding a comment does not help very much. I have reviewed each case individually.
Just one nitpick. After that is it for me RTBC:
We can also remove the empty line before the line with "return;".
Comment #28
ravi.shankar CreditAttribution: ravi.shankar at OpenSense Labs commentedHere I have addressed comment #27.
Comment #29
daffie CreditAttribution: daffie commentedI agree with @xjm that having a comment why a method does nothing is better then no comment. With that said and looking at the by @xjm mentioned methods. For each case it is self evident why the methods have no code. Adding a comment does not help very much. I have reviewed each case individually.
With that said, all code changes look good to me.
The rule is added in the patch.
The testbot returns no coding standard violations.
All points of @xjm are addressed.
For me it is RTBC.
Comment #31
xjmThanks everyone for the quick turnaround here! I too checked all the cases where there is no inline comment and they seem self-explanatory (fake test fixtures, abstract base implementations, etc.)
I'm also glad we managed to resolve the issues with the early return in other patches first so this is a straightforward commit.
Tagging for release notes as we do our best to announce newly enabled rules.
Committed to 9.1.x. Thanks!
If we like, we can make a 9.0/8.9 version of #28 that does not include actually enabling the new rule yet (with the rest of the changes the same). They are all codestyle cleanups now and safe for backport.
Comment #32
SpokjeComment #33
SpokjeBackported patches for D8.9.x and D9.0.x.
Comment #34
SpokjeComment #35
SpokjeComment #36
SpokjeComment #37
longwaveLooks great to me!
Comment #38
alexpottBackported to 8.9.x as keeping the code aligned helps security patches and this results in no functional change.
Committed f8b13ab and pushed to 9.0.x. Thanks!
Committed 6946c12 and pushed to 8.9.x. Thanks!
Comment #41
alexpott