[META] Fix DrupalPractice best practice in Core

Created on 16 September 2022, over 2 years ago
Updated 15 March 2023, almost 2 years ago

Problem/Motivation

In 🌱 [meta] Fix PHP coding standards in core Active we are gradually fixing the coding standards in the Drupal standard. This issue is all about fixing the best practices in the DrupalPractice standard.

There are 40 sniffs in the DrupalPractice standard, and they can be listed using:

$ cd /path/to/drupal/root/
$ composer install
$ ./vendor/bin/phpcs --standard=./vendor/drupal/coder/coder_sniffer/DrupalPractice -e

This produces

The DrupalPractice standard contains 40 sniffs
DrupalPractice (40 sniffs)
--------------------------
  DrupalPractice.CodeAnalysis.VariableAnalysis
  DrupalPractice.Commenting.AuthorTag
  DrupalPractice.Commenting.CommentEmptyLine
  DrupalPractice.Commenting.ExpectedException
  DrupalPractice.Constants.GlobalConstant
  DrupalPractice.Constants.GlobalDefine
  DrupalPractice.FunctionCalls.CheckPlain
  DrupalPractice.FunctionCalls.CurlSslVerifier
  DrupalPractice.FunctionCalls.DbQuery
  DrupalPractice.FunctionCalls.DbSelectBraces
  DrupalPractice.FunctionCalls.DefaultValueSanitize
  DrupalPractice.FunctionCalls.FormErrorT
  DrupalPractice.FunctionCalls.InsecureUnserialize
  DrupalPractice.FunctionCalls.LCheckPlain
  DrupalPractice.FunctionCalls.MessageT
  DrupalPractice.FunctionCalls.TCheckPlain
  DrupalPractice.FunctionCalls.Theme
  DrupalPractice.FunctionCalls.VariableSetSanitize
  DrupalPractice.FunctionDefinitions.AccessHookMenu
  DrupalPractice.FunctionDefinitions.FormAlterDoc
  DrupalPractice.FunctionDefinitions.HookInitCss
  DrupalPractice.FunctionDefinitions.InstallT
  DrupalPractice.General.AccessAdminPages
  DrupalPractice.General.ClassName
  DrupalPractice.General.DescriptionT
  DrupalPractice.General.ExceptionT
  DrupalPractice.General.FormStateInput
  DrupalPractice.General.LanguageNone
  DrupalPractice.General.OptionsT
  DrupalPractice.General.VariableName
  DrupalPractice.InfoFiles.CoreVersionRequirement
  DrupalPractice.InfoFiles.Description
  DrupalPractice.InfoFiles.NamespacedDependency
  DrupalPractice.Objects.GlobalClass
  DrupalPractice.Objects.GlobalDrupal
  DrupalPractice.Objects.GlobalFunction
  DrupalPractice.Objects.StrictSchemaDisabled
  DrupalPractice.Objects.UnusedPrivateMethod
  DrupalPractice.Variables.GetRequestData
  DrupalPractice.Yaml.RoutingAccess

The core 10.1 file does not add the complete "DrupalPractice" ruleset but instead specifically includes just five rules:

DrupalPractice.CodeAnalysis.VariableAnalysis
DrupalPractice.Commenting.ExpectedException
DrupalPractice.General.ExceptionT
DrupalPractice.InfoFiles.NamespacedDependency
DrupalPractice.Objects.GlobalFunction

The other 35 best practices are not tested for at all. The initial tasks are therefore:

  1. Determine which best practices core code adheres to
  2. Add these into phpcs.xml.dist to ensure that we don't get any regression and introduce faults for existing passing sniffs.
  3. On a sniff-by-sniff basis add child issues for the remaining failing sniffs
  4. Decide if Core can (or should) adhere to them and fix the code or ignore the sniff as appropriate
πŸ“Œ Task
Status

Needs work

Version

10.1 ✨

Component
OtherΒ  β†’

Last updated about 1 hour ago

Created by

πŸ‡¬πŸ‡§United Kingdom jonathan1055

Live updates comments and jobs are added and updated live.
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.

  • @jonathan1055 opened merge request.
  • πŸ‡¬πŸ‡§United Kingdom jonathan1055

    New branch for 10.1. Initially running all DrupalPractice sniffs to see what we get.

  • πŸ‡¬πŸ‡§United Kingdom jonathan1055

    Updated IS for 10.1.x

  • Status changed to Needs review almost 2 years ago
  • πŸ‡¬πŸ‡§United Kingdom jonathan1055

    There are 40 sniffs in DrupalPractice. 5 are already in phpcs.xml.dist and being checked for. 15 have failures. 2 have been suggested as no longer relevant to 10.x (see comment #12). That leaves 40 - 5 - 15 -2 = 18 which already pass in core 10.1 but are not tested for. The next commit adds these 18 sniffs into phpcs.xml.dist

  • πŸ‡¬πŸ‡§United Kingdom jonathan1055

    That's good. The dispatcher output now shows that 23 sniffs (not 5) were checked, and they all passed.

    DrupalPractice (23 sniffs)
    --------------------------
      DrupalPractice.CodeAnalysis.VariableAnalysis
      DrupalPractice.Commenting.AuthorTag
      DrupalPractice.Commenting.ExpectedException
      DrupalPractice.FunctionCalls.CheckPlain
      DrupalPractice.FunctionCalls.CurlSslVerifier
      DrupalPractice.FunctionCalls.DbQuery
      DrupalPractice.FunctionCalls.DbSelectBraces
      DrupalPractice.FunctionCalls.DefaultValueSanitize
      DrupalPractice.FunctionCalls.FormErrorT
      DrupalPractice.FunctionCalls.LCheckPlain
      DrupalPractice.FunctionCalls.MessageT
      DrupalPractice.FunctionDefinitions.AccessHookMenu
      DrupalPractice.FunctionDefinitions.FormAlterDoc
      DrupalPractice.FunctionDefinitions.HookInitCss
      DrupalPractice.FunctionDefinitions.InstallT
      DrupalPractice.General.AccessAdminPages
      DrupalPractice.General.ClassName
      DrupalPractice.General.ExceptionT
      DrupalPractice.General.FormStateInput
      DrupalPractice.General.LanguageNone
      DrupalPractice.General.VariableName
      DrupalPractice.InfoFiles.NamespacedDependency
      DrupalPractice.Objects.GlobalFunction
    

    The 18 additional sniffs could now be committed to the phpcs.xml.dist file, as in the MR, so that we do not accidentally introduce new failures for these in future commits.

  • Status changed to Needs work almost 2 years ago
  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    There's still one in that list that make no sense. Like most of the function call ones are checking for functions that don't exist. Perhaps they should have other equivalents but someone needs to do that work. As I wrote before in #14 I think we should swap it around and chose to add each sniff based on the value it gives to us not blindly just because they pass.

    A quick check reveals that DrupalPractice.FunctionCalls.CheckPlain, DrupalPractice.FunctionCalls.LCheckPlain, DrupalPractice.General.AccessAdminPages, DrupalPractice.General.VariableName are bogus for Drupal 10 and there are more...

  • πŸ‡¬πŸ‡§United Kingdom jonathan1055

    Thanks @alexpott

    we should swap it around and chose to add each sniff based on the value it gives to us not blindly just because they pass

    Yes OK, I accept that. I just wanted to make a new branch for 10.1 and update the issue to get it to the same point we had reached in 9.5.

    I will go through all 35 sniffs in DrupalPractice that are not yet included in the core phpcs.xml.dist and come back with a suggested list to add, and reasons for excluding the others. I think we need to document why sniffs are excluded so that someone in future does not have to go through the same process.

  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    I think we need to document why sniffs are excluded...

    We also need to do something about upstream - like I think coder shouldn't be running quite a few of these rules on Drupal 9 / 10 code

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    Yes, it will help to be sure we are running the right sniffs and to add some documentation.

    I made a child issue for the remaining work of implementing DrupalPractice.CodeAnalysis.VariableAnalysis and moved the few remaining issues there.

Production build 0.71.5 2024