Problem/Motivation
In #2571965: [meta] Fix PHP coding standards in core, stage 1 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 phpcs.xml.dist 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:
- Determine which best practices core code adheres to
- Add these into phpcs.xml.dist to ensure that we don't get any regression and introduce faults for existing passing sniffs.
- On a sniff-by-sniff basis add child issues for the remaining failing sniffs
- Decide if Core can (or should) adhere to them and fix the code or ignore the sniff as appropriate
Issue fork drupal-3310127
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
jonathan1055 commentedThe full set of sniffs in the DrupalPractice standard can be tested on the /core folder using:
This produces
However this does not include all the problems, and also does include some sniffs on files which should be excluded. An alternative (better) way is to add the single line
<rule ref="DrupalPractice"/>into core/phpcs.xml.dist, then run the command using this file as the standard:This shows the accurate list of sniffs that need fixing (or ignoring) as follows:
Comment #3
jonathan1055 commentedThe differences between the two outputs above are:
Sniffs now not reported
Additional sniffs that fail
DrupalPractice.Yaml.RoutingAccess.OpenCallback
DrupalPractice.InfoFiles.CoreVersionRequirement.CoreVersionRequirement
DrupalPractice.InfoFiles.Description.Missing
DrupalPractice.Yaml.RoutingAccess.PermissionFound
DrupalPractice.Constants.GlobalDefine.GlobalConstant
Number of failures changed
DrupalPractice.Commenting.CommentEmptyLine.SpacingAfter 423 -> 438
DrupalPractice.Constants.GlobalConstant.GlobalConstant 43 -> 71
DrupalPractice.General.DescriptionT.DescriptionT 29 -> 30
Comment #5
jonathan1055 commentedFirst commit will show all the failing DrupalPractice sniffs. This is not for commiting, just done to get a baseline before progressing.
Comment #6
jonathan1055 commentedWhy does that happen? the title gets replaced by the branch name unintentionally.
Comment #7
jonathan1055 commentedCreated a second branch including the 20 DrupalPractice sniffs that already pass in Core.
One phpunit test is included, which checks the order of newly-added sniffs. This should fail because the sniffs are not in the correct order.
Comment #9
jonathan1055 commentedThe 20 added sniffs all pass and the phpcs\SortTest shows that they have been added in the correct order.
Now reset drupalci.yml so that this
3310127-sniffs-that-passis ready for committing.Comment #10
jonathan1055 commentedMR
3310127-sniffs-that-passis ready for review to be committed. This will ensure that no new failures get committed for the 20 DrupalPractices sniffs that currently pass but are not tested for.Comment #11
erikaagp commentedComment #12
alexpottThese rules don't make any sense in the Drupal 9 / 10 context. And I'm pretty sure other one's don't either. I don't think we should be adding sniffs that check for incorrect usage of functions that no longer exist.
Comment #13
jonathan1055 commented@alexpott yes that's reasonable. I can remove those two easily.
It comes down the way we use phpcs.xml in core, see
#2912811: Sniff definitions approach in phpcs.xml.dist: exclude vs severity
and
#3135935: Keep all sniffs in phpcs.xml.dist in sync with the locked version of Coder
Comment #14
alexpott@jonathan1055 - it's not just those two. Someone should go through the entire list and work out if they are at all relevant. FWIW I'm not a huge fan of the DrupalPracticeSniffs. And question their value to core. I'd rather turn this around and work out which ones if any actually add value and decide what to include based on that.
Comment #17
jonathan1055 commentedNew branch for 10.1. Initially running all DrupalPractice sniffs to see what we get.
Comment #18
jonathan1055 commentedUpdated IS for 10.1.x
Comment #19
jonathan1055 commentedThere 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
Comment #20
jonathan1055 commentedThat's good. The dispatcher output now shows that 23 sniffs (not 5) were checked, and they all passed.
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.
Comment #21
alexpottThere'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...
Comment #22
jonathan1055 commentedThanks @alexpott
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.
Comment #23
alexpottWe 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
Comment #24
quietone commentedYes, 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.
Comment #26
bbrala