Part of #2571965: [meta] Fix PHP coding standards in core, stage 1.
This sniff refers to https://www.drupal.org/docs/develop/standards/coding-standards#naming specifically Functions and variables.
This issue is now only doing the Test files, see #15.
Update the sniff to just search tests, as below.
<rule ref="Drupal.NamingConventions.ValidVariableName"/>
<rule ref="Drupal.NamingConventions.ValidVariableName.LowerCamelName">
<!-- Views plugins do not conform to this sniff. -->
<include-pattern>**/tests/*</include-pattern>
</rule>Approach
We are testing coding standards with PHP CodeSniffer, using the Drupal coding standards from the Coder module. 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.NamingConventions.ValidVariableName.LowerCamelName 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: Install PHP CodeSniffer and the ruleset from the Coder module
$ composer install
$ ./vendor/bin/phpcs --config-set installed_paths ../../drupal/coder/coder_sniffer
Once you have installed the phpcs package, you can list all the sniffs available to you like this:
$ ./vendor/bin/phpcs --standard=Drupal -e
This will give you a big list of sniffs, and the Drupal-based ones should be present.
Step 3: Prepare the phpcs.xml file
To speed up the testing you should make a copy of the file phpcs.xml.dist (in the core/ folder) and save it as phpcs.xml. This is the configuration file for PHP CodeSniffer.
We only want this phpcs.xml file to specify the sniff we're interested in. So we need to remove all the rule items, and add only our own sniff's rule. Rule items look like this:
<rule ref="Drupal.Classes.UnusedUseStatement"/>
Remove all of them, and add only the sniff from this issue title. This will make sure that our tests run quickly, and are not going to contain any output from unrelated sniffs.
Step 4: Run the test
Now you are ready to run the test! From within the core/ folder, run the following command to launch the test:
$ cd core/
$ ../vendor/bin/phpcs -ps
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. The -s flag shows the sniffs when displaying results.
Step 5: 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:
$ ../vendor/bin/phpcbf
This will fix the errors in place. 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.
| Comment | File | Size | Author |
|---|
Issue fork drupal-2937514
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
Sparrow_1601 commentedWork in progress patch version.
Comment #3
rosk0Removed un-relevant issues from description.
Comment #9
spokjeComment #10
spokjeOne of the proposed changes by this sniff turns out to be to rename oth
\Drupal\views\ResultRow->_entityand\Drupal\views\ResultRow->_relationship_entities.IMHO that's a bad idea, see here.
We can still use this Sniff, but exclude files that make such impactful changes from being sniffed, like this:
Another option would be to file an issue in Coder to alter the sniff.
The latter would however mean that the enabling of this sniff would be delayed, possibly considerably.
Which in turn would mean that new code can be committed which offends this Code Standard.
I'm a bit on the fence with this one, but I think I'm leaning slightly towards the first, 'enable-and-exclude'-option.
Comment #11
quietone commentedI too lean to the first option (exclude those files) so that the other fixes can be made. Then, a followup needs to be made so that a solution can be figured out.
Comment #12
longwaveIf this needs to be skipped every time a variable is *used* rather than just declared I don't think this is viable, because we might introduce new code that needs to use the existing variables and hence would need new exclusions. But I haven't tested the sniff to see how it works yet.
Comment #13
spokjeComment #14
spokjeThanks @quietone and @longwave for their input.
Comment #15
longwaveHad a quick play with this one and it's going to be difficult to get into core. The biggest problems are:
We don't yet use it in core, but with a combination of
// phpcs:disable Drupal.NamingConventions.ValidVariableName.LowerCamelNamein the annotated base classes and Views base classes, and something like this in the config file:we might be able to enable this. But there might still be awkward edge cases and I don't know if we should introduce
phpcs:disableif we don't need to use it anywhere else?If we want to break this down first we could also try to introduce this for tests only to start with, and then deal with the rest of core later.
Comment #16
quietone commentedComment #18
quietone commentedHere is a patch for just the test files. I don't know how to configure phpcs.xml to ignore the non-test files.
There are 39 non test files that fail this sniff, the majority in core/lib/Drupal/Core (15) followed by core/modules/views/src/Plugin (11).
Comment #19
longwaveentitytTypeManager -> entityTypeManager
Probably should be $compiledRoute?
Comment #20
quietone commentedRight you are.
1 and 2 fixed.
Comment #21
quietone commentedSilly mistake when editing. Fixed that but this is now failing cspell on the word 'uncheck', which is not introduced here. uncheck was removed in #3212547: cspell Dictionaries changed, checking all files.
Comment #22
quietone commentedI re-installed yarn and commit-code-check is passing locally now.
Comment #23
quietone commentedAdd link to coding standards in the IS
Comment #24
quietone commentedReroll and add changes for \Drupal\Tests\jsonapi\Functional\CommentTest.
Comment #25
daffie commentedWill do a better review later
I think we can change the 4 lines to: "<rule ref="Drupal.NamingConventions.ValidVariableName"/>"
Comment #26
dhirendra.mishra commentedHere is the updated patch from #25.
Comment #27
daffie commentedThe testbot is saying that everything is ok.
Only when I run "phpcs -ps" on my local machine I get a lot of warnings.
Comment #28
longwaveThis was renamed from
compileRoutebut all the usages below in this file were not changed.The errors in #27 all relate to my comment in #15, we can't change this without some backward compatibility issues. The changes here only affect tests and so if possible we should configure phpcs so only look at tests for this sniff for now.
Comment #29
naveen433 commentedWorking on this
Comment #30
daffie commentedOpened #3224583: The testbot does not run PHPCS on all files when core/phpcs.xml.dist is changed to fix the problem with the testbot.
Comment #31
naveen433 commentedIgnore This patch.
Comment #32
daffie commented#3224583: The testbot does not run PHPCS on all files when core/phpcs.xml.dist is changed has landed. We should see now better what is wrong with the patch/MR.
Comment #36
quietone commentedComment #41
quietone commentedI updated the patch to 10.1.x and the phpcs.xml should be configured to only be doing tests for this sub sniff.
I enabled the sniff without exceptions and the Annotation directories in core/modules were excluded. However, the Annotation directories in core/lib were not. Searching the output in case there were three files found. So, perhaps coder can be modified to exclude these as well.
Comment #42
quietone commentedI messed up the parent because the other is also about NamingConventions.
Comment #44
quietone commentedComment #45
daffie commentedThe rule has been enabled in the MR.
The testbot returns green.
All code changes look good to me.
For me it is RTBC.
Comment #47
longwaveThere is a base class altered here, but only views_date_format_sql in contrib extends it and it does not use the renamed properties. Tests are not API, so it's possible that some contrib extends some of these tests, but in most cases it seems unlikely that these properties will be used. To be safe, I committed this to 10.1.x only, even if the test-only changes are eligible for backport. Also adding to release notes as we are enabling a coding standard.
Comment #49
quietone commented