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.

Issue fork drupal-2937514

Command icon 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

Sparrow_1601 created an issue. See original summary.

Sparrow_1601’s picture

Assigned: Sparrow_1601 » Unassigned
Status: Active » Needs work
Issue tags: -undefined
StatusFileSize
new471.17 KB

Work in progress patch version.

rosk0’s picture

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.

spokje’s picture

Assigned: Unassigned » spokje
spokje’s picture

One of the proposed changes by this sniff turns out to be to rename oth \Drupal\views\ResultRow->_entity and \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:

  <rule ref="Drupal.NamingConventions.ValidVariableName.LowerCamelName">
    <exclude-pattern>core/modules/views/src/ResultRow\.php</exclude-pattern>
  </rule>

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.

quietone’s picture

I 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.

longwave’s picture

If 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.

spokje’s picture

Assigned: spokje » Unassigned
spokje’s picture

Thanks @quietone and @longwave for their input.

longwave’s picture

Had a quick play with this one and it's going to be difficult to get into core. The biggest problems are:

  • Our annotated classes specify properties with underscores, these map directly on to class properties and we can't change any of this without breaking BC.
  • Views has a lot of historical property usage with snake_case names that we similarly can't change.

We don't yet use it in core, but with a combination of // phpcs:disable Drupal.NamingConventions.ValidVariableName.LowerCamelName in the annotated base classes and Views base classes, and something like this in the config file:

  <rule ref="Drupal.NamingConventions.ValidVariableName"/>
  <rule ref="Drupal.NamingConventions.ValidVariableName.LowerCamelName">
    <!-- Views plugins do not conform to this sniff. -->
    <exclude-pattern>**/src/Plugin/views/*/*.php</exclude-pattern>
  </rule>

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:disable if 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.

quietone’s picture

Issue tags: +Coding standards

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

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Status: Needs work » Needs review
StatusFileSize
new25.23 KB

Here 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).

longwave’s picture

  1. +++ b/core/modules/field_ui/tests/src/FunctionalJavascript/ManageDisplayTest.php
    @@ -36,12 +36,12 @@ class ManageDisplayTest extends WebDriverTestBase {
    +  protected $entitytTypeManager;
    

    entitytTypeManager -> entityTypeManager

  2. +++ b/core/tests/Drupal/Tests/Core/Routing/CompiledRouteLegacyTest.php
    @@ -17,13 +17,13 @@ class CompiledRouteLegacyTest extends UnitTestCase {
    +  private $compileRoute;
    

    Probably should be $compiledRoute?

quietone’s picture

StatusFileSize
new25.23 KB

Right you are.

1 and 2 fixed.

quietone’s picture

StatusFileSize
new1.7 KB
new25.3 KB

Silly 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.

quietone’s picture

I re-installed yarn and commit-code-check is passing locally now.

quietone’s picture

Issue summary: View changes

Add link to coding standards in the IS

quietone’s picture

Issue summary: View changes
StatusFileSize
new3.59 KB
new27.21 KB

Reroll and add changes for \Drupal\Tests\jsonapi\Functional\CommentTest.

daffie’s picture

Status: Needs review » Needs work

Will do a better review later

+++ b/core/phpcs.xml.dist
@@ -115,8 +115,6 @@
   <rule ref="Drupal.NamingConventions.ValidVariableName">
-    <!-- Sniff for: LowerStart -->
-    <exclude name="Drupal.NamingConventions.ValidVariableName.LowerCamelName"/>
   </rule>

I think we can change the 4 lines to: "<rule ref="Drupal.NamingConventions.ValidVariableName"/>"

dhirendra.mishra’s picture

Status: Needs work » Needs review
StatusFileSize
new542 bytes
new27.29 KB

Here is the updated patch from #25.

daffie’s picture

Status: Needs review » Needs work
StatusFileSize
new43.03 KB

The testbot is saying that everything is ok.
Only when I run "phpcs -ps" on my local machine I get a lot of warnings.

longwave’s picture

+++ b/core/tests/Drupal/Tests/Core/Routing/CompiledRouteLegacyTest.php
@@ -17,13 +17,13 @@ class CompiledRouteLegacyTest extends UnitTestCase {
+  private $compiledRoute;

This was renamed from compileRoute but 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.

naveen433’s picture

Assigned: Unassigned » naveen433

Working on this

daffie’s picture

naveen433’s picture

Assigned: naveen433 » Unassigned
Status: Needs work » Needs review
StatusFileSize
new2.04 MB
new43.86 KB

Ignore This patch.

daffie’s picture

Status: Needs review » Needs work

#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.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Title: Fix Drupal.NamingConventions.ValidVariableName.LowerCamelName » Fix Drupal.NamingConventions.ValidVariableName.LowerCamelName in tests
Parent issue: #2571965: [meta] Fix PHP coding standards in core, stage 1 » #3123066: [meta] Fix 'Drupal.NamingConventions.ValidFunctionName' coding standard

quietone’s picture

Issue summary: View changes

I 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.

$ grep FILE a.out| awk -F: '{print $2}'  | grep Annotation | sort -u | nl 
     1   ...html/core/lib/Drupal/Core/Entity/Annotation/ConfigEntityType.php
     2   ...tml/core/lib/Drupal/Core/Entity/Annotation/ContentEntityType.php
     3   ...var/www/html/core/lib/Drupal/Core/Field/Annotation/FieldType.php
quietone’s picture

I messed up the parent because the other is also about NamingConventions.

quietone’s picture

Status: Needs work » Needs review
daffie’s picture

Status: Needs review » Reviewed & tested by the community

The rule has been enabled in the MR.
The testbot returns green.
All code changes look good to me.
For me it is RTBC.

  • longwave committed ba8126bd on 10.1.x
    Issue #2937514 by Spokje, quietone, dhirendra.mishra, Sparrow_1601,...
longwave’s picture

Category: Plan » Task
Status: Reviewed & tested by the community » Fixed
Issue tags: +10.1.0 release notes

There 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.

Status: Fixed » Closed (fixed)

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