Problem/Motivation

As discussed in #3134731: Update coder to 8.3.9 with @longwave and @jonathan1055, we propose to have phpcs.xml.dist sorted by rule and by sniff. This will assist any work when a new sniff is added. A test will be written to ensure that any patch which adds a new sniff puts it in the correct place.

Furthermore in another issue #3135935: Keep all sniffs in phpcs.xml.dist in sync with the locked version of Coder a proposal to redesign phpcs.xml.dist and write a test to ensure all sniffs (either commented out or not) are in sync with the full list got from the locked version of coder.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3135933

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

jungle created an issue. See original summary.

jungle’s picture

Title: Get phpcs.xml.dist (commented out or not) sorted » Get all sniffers/rules in phpcs.xml.dist (commented out or not) sorted
jungle’s picture

Title: Get all sniffers/rules in phpcs.xml.dist (commented out or not) sorted » Get all sniffs/rules in phpcs.xml.dist (commented out or not) sorted
Issue summary: View changes

- sniffers
+ sniffs

jungle’s picture

$ ./vendor/bin/phpcs --standard=Drupal,DrupalPractice --runtime-set installed_paths vendor/drupal/coder/coder_sniffer -e | grep sniff 

The Drupal standard contains 115 sniffs
Drupal (71 sniffs)
Generic (13 sniffs)
MySource (1 sniff)
PEAR (3 sniffs)
PSR2 (2 sniffs)
Squiz (24 sniffs)
Zend (1 sniff)
The DrupalPractice standard contains 37 sniffs
DrupalPractice (37 sniffs)

The categories/sub-categories of sniffs won't be sorted globally, just keep it inline with the output above. The Drupal standard with its sub-categories (sub=categories are sorted by default) at the top, followed by the Drupal DrupalPractice standard.

  <!-- Internal sniffs -->
  <rule ref="Internal.NoCodeFound">
    <!-- No PHP code in *.yml -->
    <exclude-pattern>*.yml</exclude-pattern>
  </rule>

Internal sniffs will be moved to the above of the Drupal standard.

jungle’s picture

StatusFileSize
new1.84 KB

First iteration

Before:

$ cat core/phpcs.xml.dist | grep sniff
  <!-- Only include specific sniffs that pass. This ensures that, if new sniffs are added, HEAD does not fail.-->
  <!-- Drupal sniffs -->
  <!-- Drupal Practice sniffs -->
  <!-- Generic sniffs -->
  <!-- Internal sniffs -->
  <!-- MySource sniffs -->
  <!-- PEAR sniffs -->
  <!-- PEAR sniffs -->
  <!-- The sniffs inside PEAR.Functions.FunctionCallSignature silenced below are
  <!-- PSR-2 sniffs -->
  <!-- Squiz sniffs -->
  <!-- Zend sniffs -->

After:

$ cat core/phpcs.xml.dist | grep sniff
  <!-- Only include specific sniffs that pass. This ensures that, if new sniffs are added, HEAD does not fail.-->
  <!-- Internal sniffs -->
  <!-- Drupal sniffs -->
  <!-- Generic sniffs -->
  <!-- MySource sniffs -->
  <!-- PEAR sniffs -->
  <!-- The sniffs inside PEAR.Functions.FunctionCallSignature silenced below are
  <!-- PSR-2 sniffs -->
  <!-- Squiz sniffs -->
  <!-- Zend sniffs -->
  <!-- Drupal Practice sniffs -->
jungle’s picture

StatusFileSize
new4.3 KB
new2.6 KB

2nd intention: sort Drupal sniffs

To verify:

$ cat core/phpcs.xml.dist | grep Drupal. | grep \<rule | grep -v DrupalPractice > unsorted.txt
$ cat core/phpcs.xml.dist | grep Drupal. | grep \<rule | grep -v DrupalPractice | sort > sorted.txt

unsorted.txt should be identical with sorted.txt

jungle’s picture

StatusFileSize
new6.26 KB
new2.44 KB

3rd iteration

1. Nothing to do with generic sniffs.

$ cat core/phpcs.xml.dist | grep Generic. | grep rule > before.txt
$ cat core/phpcs.xml.dist | grep Generic. | grep rule | sort> after.txt

Before.txt is identical with after.txt

2. MySource: only 1 sniff, skipped

3. Sorted PEAR sniffs: to verify:

$ cat core/phpcs.xml.dist | grep PEAR. | grep rule > unsorted.txt
$  cat core/phpcs.xml.dist | grep PEAR. | grep rule | sort > sorted.txt

unsorted.txt should be identical with sorted.txt

jungle’s picture

Status: Active » Needs review
StatusFileSize
new8.22 KB
new2.1 KB

4th iteration (The last)

1. Nothing to do with PSR2 sniffs,

$ cat core/phpcs.xml.dist | grep PSR2. | grep rule > before.txt
$ cat core/phpcs.xml.dist | grep PSR2. | grep rule | sort > after.txt

Before.txt is identical with after.txt

2. Sorted Squiz sniffs: to verify:

$ cat core/phpcs.xml.dist | grep Squiz. | grep rule > unsorted.txt
$  cat core/phpcs.xml.dist | grep Squiz. | grep rule | sort > sorted.txt

unsorted.txt should be identical with sorted.txt

3. Zend sniffs, one sniff only, skipped
4. Drupal Practice sniffs, one sniff only, skipped

alexpott’s picture

Issue tags: +Needs tests

Having another issue to add a test for the changes added here feels unnecessary. If this issue added the test then we can be sure that we'll never need to redo this work. I think this issue should add the sorting test because that's what it fixes.

jungle’s picture

@alexpott thanks for your review!

From my understanding, the test in other issue planed is to test if all sniffs in phpcs.xml.dist in sync with the list got from the current version of coder locked in composer.lock. The test needed here is to test sorting.

jonathan1055’s picture

Nice work @jungle.

@alexpott Yes you are right if the sniffs are to be kept in alphabetic order then this issue should also contain a new test to ensure that.

However, I think no more work should be done here until we decide on the questions asked in the other issue #3135935: Keep all sniffs in phpcs.xml.dist in sync with the locked version of Coder. Until we know whether all sniffs (including the ignored/commented out ones) are in the file, or just the active rules which Core can pass on, then that will affect the processing of the sorted test. So let's have the discussion over there first.

andypost’s picture

Related issues: +#3123061: Create a sniff to make sure exception messages are not translated

The related sniff has false positives in coder 8.3.9

jonathan1055’s picture

Hi andypost,
I don't understand why #3123061: Create a sniff to make sure exception messages are not translated is related to this issue?

andypost’s picture

@jonathan1055 because inclusion of the sniffer depends on coder release, for 8.3.9 DrupalPractice.General.ExceptionT should be exuded as having false positives

jonathan1055’s picture

Title: Get all sniffs/rules in phpcs.xml.dist (commented out or not) sorted » Sort sniffs/rules in phpcs.xml.dist and write test to keep them sorted
Issue summary: View changes
Related issues: -#3123061: Create a sniff to make sure exception messages are not translated

The sniff DrupalPractice.General.ExceptionT was not being added here. It does not appear in the patches above - we are only sorting the existing phpcs.xml.dist here. (maybe the issue title was misleading so I've altered it and updated the issue summary)

jungle’s picture

Status: Postponed » Needs review
StatusFileSize
new8.66 KB
new14.41 KB

Re #11, @alexpott commented on #3135935: Keep all sniffs in phpcs.xml.dist in sync with the locked version of Coder, I think we should continue. The patch sorted:

  1. Level - 1: <file>, <description>, <rule> etc. tags sorted.
  2. Level - 2: excluded sniffs ( with <exclude> tags) sorted.
jungle’s picture

StatusFileSize
new364 bytes
new14.43 KB

Trying to make the bot work

jonathan1055’s picture

Hi Jungle,
No I think you have mis-understood. Alexpott said in #3135935-5: Keep all sniffs in phpcs.xml.dist in sync with the locked version of Coder

It might be time to swap it round

meaning that we may not have all the sniffs in the file, but now start to list the ones that are excluded. If that is the case then that issue needs to be done first. Unless there is a good reaons to sorting the sniffs now, then remove and change them all in the other issue?

jungle’s picture

Re #18, from my understanding, the two issues are not tightly relevant/related/bundled.

  1. This one focuses on getting the "XML tree" -- phpcs.xml.dist, sorted. Sorted alphabetically will make CRUD on this file easier, no matter whether we will continue with #3135935 or not
  2. That one focuses on keeping sniffs in sync with sniffs from coder locked in composer.lock,
  3. Once the phpcs.xml.dist file gets sorted, it will make the sync thing easier, more or less
  4. Yes, not all sniffs are in the phpcs.xml.dist file, while keeping them in sync, missing ones will be added to the file, commented ones will be converted to excluded ones if possible per @longwave's comment somewhere I couldn't remember that, using the solution below if applicable
     <rule ref="Drupal"> 
       <exclude name="Drupal.FooSniff"/>
     </rule>
    

I am not a native English speaker, please bear with me at sometimes, Thank you @jonathan1055!

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
Status: Needs review » Needs work
Issue tags: +Coding standards, +Bug Smash Initiative
spokje’s picture

Used 3135933-17.patch as base for a re-roll in the new MR.

spokje’s picture

Issue tags: -Needs tests

Issue has tests, removing tag.

spokje’s picture

Assigned: spokje » Unassigned
Status: Needs work » Needs review

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.

spokje’s picture

Issue tags: -Bug Smash Initiative

Rebased against 9.3.x.

spokje’s picture

Something went very wrong when rebasing the previous MR on 9.3.x.
Close it and opened a new one.

phenaproxima’s picture

Status: Needs review » Needs work

This largely makes sense to me; I just have some nitpicks and a couple of questions to keep the test robust easy to understand.

spokje’s picture

Assigned: Unassigned » spokje

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.

spokje’s picture

Assigned: spokje » Unassigned
ravi.shankar’s picture

Status: Needs work » Needs review
StatusFileSize
new14.26 KB
new7.42 KB

Added a patch for Drupal 9.5.x. from MR, added reroll diff between MR and patch.

Status: Needs review » Needs work

The last submitted patch, 36: 3135933-36.patch, failed testing. View results

longwave’s picture

The new test is failing because two of the exclude-pattern lines are out of order.

ravi.shankar’s picture

Status: Needs work » Needs review
StatusFileSize
new14.26 KB
new952 bytes

Trying to fix failed tests of patch #36.

Status: Needs review » Needs work

The last submitted patch, 40: 3135933-40.patch, failed testing. View results

quietone’s picture

Status: Needs work » Needs review
StatusFileSize
new2.94 KB
new15.87 KB

Rearranging phpcs.xml.dist so it is sorted.

borisson_’s picture

Status: Needs review » Reviewed & tested by the community

This test will work differently on php 7.x vs 8.x, because of the behavior change of sorting with items that have same weight. I'm not sure if that means that we should only commit this to the 10.x branch? Otherwise we're introducing a known random failure target.

If this is a worry for nothing, this looks great and it will make maintaining order in the phpcs config file much better. +1

longwave’s picture

Not sure why would items have the same weight here? We are sorting by string values, and there should be no duplicates.

alexpott’s picture

Version: 9.5.x-dev » 9.4.x-dev
Status: Reviewed & tested by the community » Fixed

I ran the test on PHP 7 a few times and it passes just fine.

Committed and pushed 6ef6ffcf46 to 10.1.x and 8cd67cbd9f to 10.0.x and a0586047b7 to 9.5.x and 6b96b54fc5 to 9.4.x. Thanks!

Backported to 9.4.x because this is a dev thing and if we do choose to backport a coding standard fix it'll make it easier.

  • alexpott committed 6ef6ffc on 10.1.x
    Issue #3135933 by Spokje, jungle, ravi.shankar, quietone, jonathan1055,...

  • alexpott committed 8cd67cb on 10.0.x
    Issue #3135933 by Spokje, jungle, ravi.shankar, quietone, jonathan1055,...

  • alexpott committed a058604 on 9.5.x
    Issue #3135933 by Spokje, jungle, ravi.shankar, quietone, jonathan1055,...

  • alexpott committed 6b96b54 on 9.4.x
    Issue #3135933 by Spokje, jungle, ravi.shankar, quietone, jonathan1055,...

Status: Fixed » Closed (fixed)

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