Part of meta-issue #2571965: [meta] Fix PHP coding standards in core, stage 1

Step 1: Preparation

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 & configure PHPCS

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 -p

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.

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.

Comments

eiriksm created an issue. See original summary.

eiriksm’s picture

Status: Active » Needs review
StatusFileSize
new795 bytes
new0 bytes

Here is a rule-update only patch (that should fail) and a complete update.

Status: Needs review » Needs work

The last submitted patch, 2: fix-2902636-2.patch, failed testing. View results

mfernea’s picture

Hi @eiriksm, cs violations don't fail the tests. So you can add only one patch. Note that the second patch is empty.

eiriksm’s picture

True. But you can see them listed in the results. Where i see i forgot to remove a couple of other array rules.

Strange that the patch was 0b I'll try to upload it again maybe later today.

Thanks!

mfernea’s picture

Now I see what you mean. In fact it's a good idea.

eiriksm’s picture

Status: Needs work » Needs review
StatusFileSize
new917 bytes
new111.15 KB

Ok, trying again :)

eiriksm’s picture

StatusFileSize
new122.47 KB

So, so manual fixes were required I guess (https://www.drupal.org/pift-ci-job/744001).

Trying again.

Status: Needs review » Needs work

The last submitted patch, 9: fix-2902636-9.patch, failed testing. View results

eiriksm’s picture

Status: Needs work » Needs review
StatusFileSize
new815 bytes
new122.47 KB

Oops. syntax errors, not good.

Status: Needs review » Needs work

The last submitted patch, 11: fix-2902636-11.patch, failed testing. View results

eiriksm’s picture

Status: Needs work » Needs review
StatusFileSize
new122.47 KB

Status: Needs review » Needs work

The last submitted patch, 13: fix-2902636-13.patch, failed testing. View results

eiriksm’s picture

Status: Needs work » Needs review
StatusFileSize
new1.15 KB
new121.79 KB

New take

andypost’s picture

Related issues: +#2911280: RectangleTest.php takes a very long time to scan for coding standards

Part of patch will not be needed after fix of related #2911280: RectangleTest.php takes a very long time to scan for coding standards

andypost’s picture

Status: Needs review » Needs work
Related issues: -#2911280: RectangleTest.php takes a very long time to scan for coding standards

This needs work to update core/phpcs.xml.dist with sniffer & exclude other sub-sniffs

mariiadeny’s picture

StatusFileSize
new98.63 KB
new26.23 KB
andypost’s picture

Status: Needs work » Needs review
borisson_’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

This needs a reroll but looks solid, we should probably also prepare a patch for 8.4 without the phpcs changes.

jofitz’s picture

Assigned: eiriksm » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new98.63 KB

Re-rolled.

The last submitted patch, 2: fix-2902636-2.patch, failed testing. View results

neclimdul’s picture

StatusFileSize
new99.06 KB
neclimdul’s picture

Status: Needs review » Needs work

Drive by review:
1) when testing the reroll there where quite a few array indentation failures for me. Doesn't seem related to the reroll though.
2) it seems the standard that exclusion lists like is in this patch include a comment with the tests being run but this patch doesn't include that.

ivan berezhnov’s picture

Issue tags: +CSKyiv18

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.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.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.

izus’s picture

Status: Needs work » Needs review
StatusFileSize
new166.18 KB

hi,
i gave this a new try
hope it'll be green

borisson_’s picture

Status: Needs review » Reviewed & tested by the community

This looks great and it has the required changes to the phpcs file.

The last submitted patch, 23: 2902636-23.patch, failed testing. View results

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

Already needs a reroll... :(

This is likely to be a tricky on to get in.

alexpott’s picture

Also +1 to get this done it will will fix lots of common errors and help patch review of big arrays way easier. One less thing to visually check.

izus’s picture

StatusFileSize
new166.22 KB

rerolled #28

izus’s picture

Status: Needs work » Needs review
borisson_’s picture

Status: Needs review » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 33: 2902636-33.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

alexpott’s picture

  1. Thanks everyone for working on this patch. Patches like this are tricky but core adhering to its coding standards is an important thing especially once we get to a place where all the rules are applied. Unfortunately this patch is making some changes to apply the rule but continue to break our coding standards. I'm not sure we should do that. See the partial review below.
  2. +++ b/core/lib/Drupal/Core/Installer/Exception/AlreadyInstalledException.php
    @@ -24,9 +24,9 @@ public function __construct(TranslationInterface $string_translation) {
    -      ':base-url' => $GLOBALS['base_url'],
    -      ':update-url' => $GLOBALS['base_path'] . 'update.php',
    -    ]);
    +  ':base-url' => $GLOBALS['base_url'],
    +  ':update-url' => $GLOBALS['base_path'] . 'update.php',
    +]);
    

    This change doesn't really look correct. In fact the whole way the translatable text contains HTML is super interesting. I think we should open a separate blocking issue to address this. We don't usually translate exception messages so I think we should go back to the original issue and see discussions there.

  3. +++ b/core/modules/comment/src/CommentStorage.php
    @@ -183,12 +183,12 @@ public function getNewCommentPageNumber($total_comments, $new_comments, Fieldabl
    -        ':status' => CommentInterface::PUBLISHED,
    -        ':entity_id' => $entity->id(),
    -        ':field_name' => $field_name,
    -        ':entity_type' => $entity->getEntityTypeId(),
    -        ':thread' => $first_thread,
    -      ])->fetchField();
    +                          ':status' => CommentInterface::PUBLISHED,
    +                          ':entity_id' => $entity->id(),
    +                          ':field_name' => $field_name,
    +                          ':entity_type' => $entity->getEntityTypeId(),
    +                          ':thread' => $first_thread,
    +                        ])->fetchField();
    

    This looks odd.

  4. +++ b/core/modules/contact/tests/src/Functional/ContactSitewideTest.php
    @@ -282,7 +282,7 @@ public function testSiteWideContact() {
    -      ]
    +    ]
         );
    

    Let's bring the ); up to the ]

  5. +++ b/core/modules/content_translation/tests/src/Functional/ContentTranslationOutdatedRevisionTranslationTest.php
    @@ -35,10 +35,10 @@ public function testFlagAsOutdatedHidden() {
    +      $entity->getEntityTypeId() => $id,
    +      'source' => 'en',
    +      'target' => 'it',
    +    ],
           [
             'language' => ConfigurableLanguage::load('it'),
             'absolute' => FALSE,
    
    @@ -54,10 +54,10 @@ public function testFlagAsOutdatedHidden() {
    +      $entity->getEntityTypeId() => $id,
    +      'source' => 'en',
    +      'target' => 'fr',
    +    ],
           [
             'language' => ConfigurableLanguage::load('fr'),
             'absolute' => FALSE,
    
    +++ b/core/modules/content_translation/tests/src/Functional/ContentTranslationRevisionTranslationDeletionTest.php
    @@ -57,10 +57,10 @@ public function doTestOverview($index) {
    -        $entity->getEntityTypeId() => $id,
    -        'source' => 'en',
    -        'target' => 'it',
    -      ],
    +      $entity->getEntityTypeId() => $id,
    +      'source' => 'en',
    +      'target' => 'it',
    +    ],
           [
             'language' => ConfigurableLanguage::load('it'),
             'absolute' => FALSE,
    
    @@ -182,9 +182,9 @@ public function doTestOverview($index) {
    -        'node' => $id,
    -        'node_revision' => $it_revision->getRevisionId(),
    -      ],
    +      'node' => $id,
    +      'node_revision' => $it_revision->getRevisionId(),
    +    ],
           [
             'language' => ConfigurableLanguage::load('it'),
             'absolute' => FALSE,
    

    This looks odd.

  6. +++ b/core/modules/filter/tests/src/Kernel/FilterKernelTest.php
    @@ -335,12 +335,12 @@ public function testLineBreakFilter() {
    -        "<script>aaa\nbbb\n\nccc</script>" => TRUE,
    -        "<style>aaa\nbbb\n\nccc</style>" => TRUE,
    -        "<pre>aaa\nbbb\n\nccc</pre>" => TRUE,
    -        "<object>aaa\nbbb\n\nccc</object>" => TRUE,
    -        "<iframe>aaa\nbbb\n\nccc</iframe>" => TRUE,
    -      ],
    +  "<script>aaa\nbbb\n\nccc</script>" => TRUE,
    +  "<style>aaa\nbbb\n\nccc</style>" => TRUE,
    +  "<pre>aaa\nbbb\n\nccc</pre>" => TRUE,
    +  "<object>aaa\nbbb\n\nccc</object>" => TRUE,
    +  "<iframe>aaa\nbbb\n\nccc</iframe>" => TRUE,
    +],
    

    I think this needs a different fix. Maybe by creating the array key outside of the array.

  7. +++ b/core/modules/filter/tests/src/Kernel/FilterKernelTest.php
    @@ -566,19 +566,19 @@ public function testUrlFilter() {
           '
     http://example.com or www.example.com
     ' => [
    -        '<a href="http://example.com">http://example.com</a>' => TRUE,
    -        '<a href="http://www.example.com">www.example.com</a>' => TRUE,
    -      ],
    +  '<a href="http://example.com">http://example.com</a>' => TRUE,
    +  '<a href="http://www.example.com">www.example.com</a>' => TRUE,
    +],
    ...
     person@example.com or mailto:person2@example.com or ' . $email_with_plus_sign . ' or ' . $long_email . ' but not ' . $too_long_email . '
     ' => [
    -        '<a href="mailto:person@example.com">person@example.com</a>' => TRUE,
    -        '<a href="mailto:person2@example.com">mailto:person2@example.com</a>' => TRUE,
    -        '<a href="mailto:' . $long_email . '">' . $long_email . '</a>' => TRUE,
    -        '<a href="mailto:' . $too_long_email . '">' . $too_long_email . '</a>' => FALSE,
    -        '<a href="mailto:' . $email_with_plus_sign . '">' . $email_with_plus_sign . '</a>' => TRUE,
    -      ],
    +  '<a href="mailto:person@example.com">person@example.com</a>' => TRUE,
    +  '<a href="mailto:person2@example.com">mailto:person2@example.com</a>' => TRUE,
    +  '<a href="mailto:' . $long_email . '">' . $long_email . '</a>' => TRUE,
    +  '<a href="mailto:' . $too_long_email . '">' . $too_long_email . '</a>' => FALSE,
    +  '<a href="mailto:' . $email_with_plus_sign . '">' . $email_with_plus_sign . '</a>' => TRUE,
    +],
    

    I would fix the array key here to be

    "\nhttp://example.com or www.example.com\n" => [
            '<a href="http://example.com">http://example.com</a>' => TRUE,
            '<a href="http://www.example.com">www.example.com</a>' => TRUE,
          ],
    

    Same for the MAILTO URLs test...

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.

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.

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.

mile23’s picture

This is a duplicate of #2937515: Fix Drupal.Array.Array.[ArrayClosingIndentation, ArrayIndentation] coding standard

Current config in 9.5.x:

  <rule ref="Drupal.Arrays.Array">
    <!-- Sniff for these errors: ArrayClosingIndentation, ArrayIndentation, CommaLastItem -->
    <exclude name="Drupal.Arrays.Array.LongLineDeclaration"/>
  </rule>
quietone’s picture

@Mile23, thanks. I don't know how I missed that this is a duplicate.

I have moved credit to other issue.