Part of #2571965: [meta] Fix PHP coding standards in core, stage 1.

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.

Comments

SherFengChong created an issue. See original summary.

SherFengChong’s picture

Assigned: SherFengChong » Unassigned
Status: Active » Needs review
StatusFileSize
new120.15 KB

Initial patch version.

rosk0’s picture

Status: Needs review » Reviewed & tested by the community

Good to go.

catch’s picture

Status: Reviewed & tested by the community » Needs review
+++ b/core/core.api.php
@@ -817,12 +817,12 @@
  *   generic method to access any service. Examples:
- *   @code
+ * @code
  *   // Retrieve the entity.manager service object (special method exists).
  *   $manager = \Drupal::entityManager();
  *   // Retrieve the service object for machine name 'foo.bar'.
  *   $foobar = \Drupal::service('foo.bar');
- *   @endcode
+ * @endcode
  *

I think we need to double check these aren't false positives - the indentation is due to the inclusion of the @code examples in lists. Not sure if the coding standards themselves are explicit either way?

borisson_’s picture

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

Patch no longer applies.

savkaviktor16@gmail.com’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new120.35 KB

Re-rolled

borisson_’s picture

Status: Needs review » Reviewed & tested by the community

The testbot agrees with this patch, and I ran composer phpcs locally and got no remaining errors.

Looks great!

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

Looking at https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Component%21Rend... I think the things being fixed here a not quite right. There's no need to for @code, @link etc to not be indented.

We need to fix the PHPCS rules in coder before implementing this.

idebr’s picture

Title: Fix 'Drupal.Commenting.DocCommentAlignment' coding standard » [PP-1] Fix 'Drupal.Commenting.DocCommentAlignment' coding standard
Status: Needs work » Postponed
jhodgdon’s picture

The changes in the patch in #7 will result in things being totally broken on api.drupal.org. @code and @link need to be indented the same depth as the surrounding text, not moved to the left as they are in that patch. I guess someone figured that out, but we definitely do not want to commit that patch!!

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.

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.

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

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

longwave’s picture

Title: [PP-1] Fix 'Drupal.Commenting.DocCommentAlignment' coding standard » Fix 'Drupal.Commenting.DocCommentAlignment' coding standard
Status: Postponed » Needs review
StatusFileSize
new20.63 KB

The linked issue in #10 is fixed so we can enable this sniff and fix the existing alignment issues.

jhodgdon’s picture

I checked over the patch. Everything in there looks good to me. If the tests pass, including the new Coder sniff, this is RTBC.

quietone’s picture

Status: Needs review » Reviewed & tested by the community

Setting to RTBC based on the comment in #18.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed e3dfd1a and pushed to 9.2.x. Thanks!

  • alexpott committed e3dfd1a on 9.2.x
    Issue #2937858 by SherFengChong, Saviktor, longwave, jhodgdon, catch:...

Status: Fixed » Closed (fixed)

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