[meta] Fix PHP coding standards in core

Created on 21 September 2015, about 9 years ago
Updated 21 January 2023, almost 2 years ago

You can check the current state of coding standards compliance using phpcs and coder from the command line:

$ cd /path/to/drupal/root/
$ composer install
$ ./vendor/bin/phpcs -p -s --standard=./vendor/drupal/coder/coder_sniffer/Drupal core/

Meta or plans

Fixes by sniff

Other

Numbers are the errors or warnings found and the number of files. phpcs was run on 10.0 on 2022-09-29 to get the numbers.

Origin

This is a followup of #1518116: [meta] Make Core pass Coder Review → which was the original initiative from 2012 to fix coding standards in core. This new issue was made to replace the old one because the suggested workflow changed from a module-per-module approach to a sniff-per-sniff approach. The old issue has a large number of linked issues which are now obsolete.

Note that no coding standards fixes will be committed until the beta phase ends. Only when the first release candidate is released these issues will be eligible for committing.

Problem/Motivation

Drupal Core fails our automated coding standards tests.

Proposed resolution

It would be impossible to fix all coding standards at once. We will split up the work in manageable chunks. In a previous effort ( #1518116: [meta] Make Core pass Coder Review → ) we split up the work per module, but this proved to be less than ideal since it requires intimate knowledge of ALL coding standards for everyone involved in the issue: developers as well as reviewers.

Instead we will now split up the work per type of coding standard. For example in separate issues we'll deal with trailing whitespace, inline comments, line lengths, etc. This will be much easier to review, since every patch will only contain very similar fixes.

In addition to fixing the coding standards, we'll also add automatic coding standards checking to the DrupalCI test bot. Once a particular coding standard is fixed, all new patches will be automatically checked and will fail if they do not meet this standard. This way we can progressively fix all coding standards, and ensure that what is once fixed will remain fixed.

An XML file (phpcs.xml.dist) was added to core to list all coding standards currently passing and it is used by the automated coding standards check. Whenever a particular standard is fixed we will add that sniff to phpcs.xml.dist or remove the exception from it, so it will be automatically checked from that point onwards.
Eventually phpcs.xml.dist would contain all sniffs defined in Drupal CS along with their configuration from ruleset.xml. All the sniffs defined in other CS and referenced in ruleset.xml should also be present in phpcs.xml.dist.
If rulesest.xml is modified or new sniffs are defined in Drupal CS, phpcs.xml.dist should also be modified.

Approach

  1. We analyzed the available "sniffs" and put them in related groups. The goal is that each group can be easily fixed and reviewed as a whole.
  2. Choose an unclaimed sniff from the Remaining Tasks section below and create a new issue for it. You can also work on one of the active child issues.
  3. If you are creating a new issue:
    • it's best to clone one of the child issues. Pay attention to Title, Status and Parent issue fields. Parent issue field should reference this issue.
    • You may also assign the newly created issue to yourself.
    • Edit this issue summary and remove the sniff from Remaining Tasks section.
    • As always, when editing an issue summary, you should add a comment to the issue saying what you edited, so that anyone following this issue will get an email. In this case, provide a link to the new issue you filed and state which sniff you are claiming.

Remaining tasks

Work is done in the child issues on a per-sniff basis. We can conserve resources by not running all the tests on these patches, until it is ready for RTBC. This is done by modifying the drupalci.yml file. There is an example → in #3048495#comment-19 📌 Fix Drupal.Semantics.FunctionTriggerError coding standard Downport . That file also removes all the other sniffs from the phpcs.xml file and only adds the sniff being tested there.

This list contains all sniffs for which we don't have an issue yet:

  • None as at 28 March 2020
🌱 Plan
Status

Active

Version

10.1 ✨

Component
Other  →

Last updated about 5 hours ago

Created by

🇧🇬Bulgaria pfrenssen Sofia

Live updates comments and jobs are added and updated live.
  • Coding standards

    It involves compliance with, or the content of coding standards. Requires broad community agreement.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024