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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

RoSk0 created an issue. See original summary.

RoSk0’s picture

tstoeckler’s picture

  1. +++ b/core/modules/node/src/Plugin/migrate/source/d7/NodeType.php
    @@ -40,7 +40,7 @@ public function query() {
    -    return [
    +    $fields = [
    

    This looks strange, but is actually correct.

    Below this is the following:

    
        if ($this->moduleExists('comment')) {
          $fields += $this->getCommentFields();
        }
        return $fields;
    

    The question is whether we need test coverage for this. I think yes.

borisson_’s picture

+++ b/core/modules/system/tests/src/Functional/FileTransfer/TestFileTransfer.php
index 9fe9df6..9d1fcc1 100644
--- a/core/modules/views/tests/src/Kernel/ModuleTest.php

--- a/core/modules/views/tests/src/Kernel/ModuleTest.php
+++ b/core/modules/views/tests/src/Kernel/ModuleTest.php

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.

borisson_’s picture

Patch no longer applies, but I think we should postpone this on #2947588: Refactor \Drupal\Tests\views\Kernel\ModuleTest::testViewsGetHandler instead of rerolling this.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

andypost’s picture

Filed #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

Status: Needs review » Needs work

The last submitted patch, 7: 2937844-7.patch, failed testing. View results

andypost’s picture

Status: Needs work » Needs review

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

daffie’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

Patch does not apply to 9.1. Needs a reroll.

andypost’s picture

Quick re-roll

+++ b/core/modules/views/tests/src/Kernel/ModuleTest.php
@@ -77,61 +108,6 @@ public function testViewsGetHandler() {
-    // @todo Reinstate these tests when the debug() in views_get_handler() is
-    //   restored.
-    return;
...
-  public function customErrorHandler($error_level, $message, $filename, $line, $context) {

Not sure about this hunk, but it looks useless

andypost’s picture

FileSize
772 bytes
15.51 KB

Fixed new composer phpcs -- -p --sniffs=Squiz.PHP.NonExecutableCode core

FILE: ...lTests/Installer/InstallerExistingBrokenDatabaseSettingsTest.php
----------------------------------------------------------------------
FOUND 0 ERRORS AND 2 WARNINGS AFFECTING 2 LINES
----------------------------------------------------------------------
 51 | WARNING | Empty return statement not required here
 59 | WARNING | Empty return statement not required here
----------------------------------------------------------------------
daffie’s picture

andypost’s picture

Status: Postponed » Needs work

Blocker is in

ravi.shankar’s picture

Assigned: Unassigned » ravi.shankar

Working on this.

ravi.shankar’s picture

Assigned: ravi.shankar » Unassigned
Status: Needs work » Needs review
FileSize
5.95 KB

Here I have added reroll of patch #15.

daffie’s picture

Status: Needs review » Needs work

The testbot returns with 6 coding standards messages. See: https://www.drupal.org/pift-ci-job/1726253.

mrinalini9’s picture

Assigned: Unassigned » mrinalini9
mrinalini9’s picture

Assigned: mrinalini9 » Unassigned
Status: Needs work » Needs review
FileSize
8.67 KB
1.91 KB

Fixed coding standard issues as mentioned in #20, please review.

daffie’s picture

Status: Needs review » Reviewed & tested by the community

All code changes look good to me.
The testbot returns no coding standards messages.
The PHPCS rule is added.
For me it is RTBC.

xjm’s picture

Status: Reviewed & tested by the community » Needs work

Thanks for working on this issue. Glad to see us moving away from the weird stuff in #2.

+++ b/core/tests/Drupal/FunctionalTests/Installer/InstallerExistingConfigNoSystemSiteTest.php
@@ -22,7 +22,7 @@ protected function prepareEnvironment() {
+    // There are errors therefore there is nothing to do here.

This is a run-on sentence. It should say: "There are are errors. Therefore, there is nothing to do here.

+++ b/core/lib/Drupal/Core/Database/StatementEmpty.php
@@ -50,7 +50,6 @@ public function rowCount() {
   public function setFetchMode($mode, $a1 = NULL, $a2 = []) {
-    return;
   }

+++ b/core/lib/Drupal/Core/Entity/EntityListBuilder.php
@@ -233,7 +233,6 @@ public function render() {
   protected function getTitle() {
-    return;
   }

+++ b/core/modules/system/tests/src/Functional/FileTransfer/TestFileTransfer.php
@@ -61,7 +61,6 @@ public function isFile($path) {
   public function chmodJailed($path, $mode, $recursive) {
-    return;
   }

+++ b/core/tests/Drupal/Tests/Core/Database/Stub/StubConnection.php
@@ -68,7 +68,6 @@ public function databaseType() {
   public function createDatabase($database) {
-    return;
   }

+++ b/core/tests/fixtures/database_drivers/custom/fake/Connection.php
@@ -50,7 +50,6 @@ public function databaseType() {
   public function createDatabase($database) {
-    return;
   }

For these, where we don't provide an inline comment, do our coding standards allow putting the curlies on the same line? E.g.:

function myMethod() {}

Ideally, though, we'd be providing a comment as to why the method is empty.

Thanks!

Deepak Goyal’s picture

Assigned: Unassigned » Deepak Goyal
Deepak Goyal’s picture

Assigned: Deepak Goyal » Unassigned
Status: Needs work » Needs review
FileSize
9.09 KB
2.8 KB

Hi @xjm

Made changes as you suggested please take a look.

daffie’s picture

Status: Needs review » Needs work

I 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:

+++ b/core/lib/Drupal/Component/Gettext/PoStreamReader.php
@@ -507,7 +507,6 @@ private function readLine() {
 
-    return;

We can also remove the empty line before the line with "return;".

ravi.shankar’s picture

Status: Needs work » Needs review
FileSize
9.21 KB
493 bytes

Here I have addressed comment #27.

daffie’s picture

Status: Needs review » Reviewed & tested by the community

I 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.

  • xjm committed 1220c07 on 9.1.x
    Issue #2937844 by andypost, ravi.shankar, mrinalini9, Deepak Goyal,...
xjm’s picture

Title: Fix 'Squiz.PHP.NonExecutableCode' coding standard » [8.9/9.0 backport] Fix 'Squiz.PHP.NonExecutableCode' coding standard
Version: 9.1.x-dev » 8.9.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)
Issue tags: +9.1.0 release notes

Thanks 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.

Spokje’s picture

Assigned: Unassigned » Spokje
Spokje’s picture

FileSize
8.82 KB

Backported patches for D8.9.x and D9.0.x.

Spokje’s picture

Spokje’s picture

Status: Patch (to be ported) » Needs review
Spokje’s picture

Assigned: Spokje » Unassigned
longwave’s picture

Status: Needs review » Reviewed & tested by the community

Looks great to me!

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Backported 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!

  • alexpott committed f8b13ab on 9.0.x
    Issue #2937844 by andypost, ravi.shankar, Spokje, mrinalini9, Deepak...

  • alexpott committed 6946c12 on 8.9.x
    Issue #2937844 by andypost, ravi.shankar, Spokje, mrinalini9, Deepak...
alexpott’s picture

Title: [8.9/9.0 backport] Fix 'Squiz.PHP.NonExecutableCode' coding standard » Fix 'Squiz.PHP.NonExecutableCode' coding standard

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.