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

This issue is to add the sniffs Squiz.Arrays.ArrayDeclaration.NoKeySpecified and Squiz.Arrays.ArrayDeclaration.KeySpecified

The relevant coding standard is Arrays which does not specify the changes being made in the sniffs. In other words, these sniffs are not part of the Drupal coding standards.

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

mfernea created an issue. See original summary.

mfernea’s picture

Status: Active » Postponed
mfernea’s picture

mfernea’s picture

Title: Fix 'Squiz.Arrays.ArrayDeclaration.NoKeySpecified' coding standard » Fix 'Squiz.Arrays.ArrayDeclaration.NoKeySpecified' and 'Squiz.Arrays.ArrayDeclaration.KeySpecified' coding standard

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.

idebr’s picture

Status: Postponed » Active

#2901739: Fix 'Squiz.Arrays.ArrayDeclaration' coding standard was committed, so this is no longer postponed.

mfernea’s picture

Assigned: Unassigned » mfernea
mfernea’s picture

Assigned: mfernea » Unassigned
Status: Active » Needs review
StatusFileSize
new7.81 KB

Here is the patch. I'm not sure what to do about files in /lib/Drupal/Component/Transliteration/data.

Status: Needs review » Needs work

The last submitted patch, 8: drupal-coding-standards-2908266-8.patch, failed testing. View results

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.

andypost’s picture

Status: Needs work » Needs review
StatusFileSize
new8.52 KB
new658 bytes
new658 bytes

Reroll for 8.7.x + fixes
- core/modules/system/tests/src/Kernel/Theme/FunctionsTest.php
- core/tests/Drupal/KernelTests/Core/Menu/LocalActionManagerTest.php

interdiff & "no-translit" patch is workaround for /lib/Drupal/Component/Transliteration/data I think it needs follow-up to discus & fix this tables

Status: Needs review » Needs work

The last submitted patch, 11: 2908266-11-no-translit.patch, failed testing. View results

andypost’s picture

Status: Needs work » Needs review
StatusFileSize
new8.6 KB

proper no-translit patch

andypost’s picture

StatusFileSize
new607 bytes
new8.63 KB

Status: Needs review » Needs work

The last submitted patch, 14: 2908266-14.patch, failed testing. View results

andypost’s picture

Status: Needs work » Needs review

testbot flux

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.

daffie’s picture

Status: Needs review » Needs work

Patch still applies to 9.1.
The patch fixes a lot of errors, but not all of them.
The remaining error:

FILE: /var/www/drupalvm/drupal/core/modules/migrate/src/Plugin/migrate/destination/EntityFieldStorageConfig.php
---------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
---------------------------------------------------------------------------------------------------------------
 77 | ERROR | Key specified for array entry; first entry has no key
---------------------------------------------------------------------------------------------------------------
munish.kumar’s picture

Assigned: Unassigned » munish.kumar
andypost’s picture

Assigned: munish.kumar » Unassigned
Status: Needs work » Needs review
StatusFileSize
new757 bytes
new9.37 KB

Fixed

andypost’s picture

@munish.kumar sorry, please help testing the patch

munish.kumar’s picture

@andypost thats alright, you have uploaded it so quickly. Yeah sure will help with the testing.

munish.kumar’s picture

Tested the latest patch, Applied successfully to 9.1.x branch also it fixed the #20 issue. Here is the result.

Munish-MacPro:drupal9 munish.kumar$ git checkout 9.1.x
Already on '9.1.x'
Your branch is up to date with 'origin/9.1.x'.
Munish-MacPro:drupal9 munish.kumar$ git pull origin 9.1.x
From https://git.drupalcode.org/project/drupal
 * branch                  9.1.x      -> FETCH_HEAD
Already up to date.
Munish-MacPro:drupal9 munish.kumar$ curl https://www.drupal.org/files/issues/2020-05-29/2908266-21.patch | git apply -v
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  9598  100  9598    0     0  16406      0 --:--:-- --:--:-- --:--:-- 16406
Checking patch core/modules/contextual/tests/src/Kernel/ContextualUnitTest.php...
Checking patch core/modules/language/language.admin.inc...
Checking patch core/modules/migrate/src/Plugin/migrate/destination/EntityFieldStorageConfig.php...
Checking patch core/modules/settings_tray/tests/src/FunctionalJavascript/SettingsTrayBlockFormTest.php...
Checking patch core/modules/system/tests/src/Functional/FileTransfer/FileTransferTest.php...
Checking patch core/modules/system/tests/src/Kernel/Theme/FunctionsTest.php...
Checking patch core/modules/views/tests/src/Unit/PluginBaseTest.php...
Checking patch core/phpcs.xml.dist...
Checking patch core/tests/Drupal/KernelTests/Core/Menu/LocalActionManagerTest.php...
Checking patch core/tests/Drupal/Tests/Component/Serialization/YamlTestBase.php...
Checking patch core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityTypeTest.php...
Checking patch core/tests/Drupal/Tests/Core/Render/RendererTest.php...
Applied patch core/modules/contextual/tests/src/Kernel/ContextualUnitTest.php cleanly.
Applied patch core/modules/language/language.admin.inc cleanly.
Applied patch core/modules/migrate/src/Plugin/migrate/destination/EntityFieldStorageConfig.php cleanly.
Applied patch core/modules/settings_tray/tests/src/FunctionalJavascript/SettingsTrayBlockFormTest.php cleanly.
Applied patch core/modules/system/tests/src/Functional/FileTransfer/FileTransferTest.php cleanly.
Applied patch core/modules/system/tests/src/Kernel/Theme/FunctionsTest.php cleanly.
Applied patch core/modules/views/tests/src/Unit/PluginBaseTest.php cleanly.
Applied patch core/phpcs.xml.dist cleanly.
Applied patch core/tests/Drupal/KernelTests/Core/Menu/LocalActionManagerTest.php cleanly.
Applied patch core/tests/Drupal/Tests/Component/Serialization/YamlTestBase.php cleanly.
Applied patch core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityTypeTest.php cleanly.
Applied patch core/tests/Drupal/Tests/Core/Render/RendererTest.php cleanly.
Munish-MacPro:drupal9 munish.kumar$ phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md /drupal9/core/modules/migrate/src/Plugin/migrate/destination/EntityFieldStorageConfig.php
Munish-MacPro:drupal9 munish.kumar$ 
daffie’s picture

@munish.kumar: Thank you for your review. What you did was great, only for a full review you should also change the status of the issue. There is a easy to follow review process, that explains everything you should do when doing a review. For a guide: https://www.drupal.org/patch/review.
We all like to have our patches reviewed by somebody else. Doing a review of somebody else's is far less popular thing to do. Doing reviews of other people's patches build community credits and also gets rewarded with commit credits! Its usually less work to do a review then to write a patch, so more commit credits for the same amount of time invested. How to get commit credits: https://www.drupal.org/core/maintainers/issue-credit.

By the way: the patch look great and is for me RTBC, but I leave it to @munish.kumar to change to status.

munish.kumar’s picture

Status: Needs review » Reviewed & tested by the community

@daffie, Thanks for your valuable feedback. Yeah, I have reviewed the patch and it works as expected. So moving to RTBC.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

So are we sure that we want to adopt this as a standard? It's not part of https://www.drupal.org/docs/develop/standards/coding-standards#array and it's not very pretty. I'm not convinced this is a good coding standard. Yes it is part of Coder's ruleset but that does not 100% implement https://www.drupal.org/docs/develop/standards/coding-standards - it has more opinions.

+++ b/core/modules/language/language.admin.inc
@@ -55,7 +55,7 @@ function template_preprocess_language_negotiation_configure_form(&$variables) {
         $table[$id]['title'] = [
           '#prefix' => '<strong>',
-          $form[$type]['title'][$id],
+          0 => $form[$type]['title'][$id],
           '#suffix' => '</strong>',
         ];

+++ b/core/modules/migrate/src/Plugin/migrate/destination/EntityFieldStorageConfig.php
@@ -73,7 +73,7 @@ public function rollback(array $destination_identifier) {
       $destination_identifier = [
-        implode('.', $destination_identifier),
+        0 => implode('.', $destination_identifier),
         'langcode' => $language,
       ];

There are the only runtime fixes apart from the ones excluded in transliteration data - and I'm really really not sure that doing #3007415: Fix transliteration data to conform Squiz.Arrays.ArrayDeclaration standard is worth it. It's not like you look to core/lib/Drupal/Component/Transliteration/data/x87.php for how to write core code.

The change to template_preprocess_language_negotiation_configure_form() doesn't really look correct. This is a render array. So we can give the thing a meaning full key. Like:

        $table[$id]['title']['data'] = $form[$type]['title'][$id];
        $table[$id]['title']['#prefix'] =  '<strong>';
        $table[$id]['title']['#suffix'] =  '</strong>';

Or as $form[$type]['title'][$id] is a render array we can even do:

        $table[$id]['title'] = $form[$type]['title'][$id];
        $table[$id]['title']['#prefix'] = isset($table[$id]['title']['#prefix']) ? '<strong>' . $table[$id]['title']['#prefix'] : '<strong>';
        $table[$id]['title']['#suffix'] = isset($table[$id]['title']['#suffix']) ?  $table[$id]['title']['#suffix'] . '</strong>' : '</strong>';

Or we could even do

        $table[$id]['title']['data'] = $form[$type]['title'][$id];
        $table[$id]['title']['#wrapper_attributes']['class'] = 'language-negotiation-title';

And then use CSS to style this as the choice of bold is a visual thing to make the titles stand out from the description and this feels like something that should be controlled by the theme.

sanjayk’s picture

Assigned: Unassigned » sanjayk
Status: Needs work » Needs review
StatusFileSize
new9.91 KB
new1.01 KB

Fixed changes as suggested #28.

sanjayk’s picture

Assigned: sanjayk » Unassigned

Forgot to unassigned, kindly review #29 patch.

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.

quietone’s picture

Before work continues here the question in #28 needs to be addressed.

longwave’s picture

I think I agree with #28, I am not sure I see the point of this standard in core. To me it makes code less readable in the places touched by this patch, rather than more readable which is what coding standards should aim for.

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

Issue summary: View changes

I can confirm that the changes made by these sniffs are not part of the coding standards. I searched and could not find an issue in the Coding Standards project about these either.

What is the next step here? I suppose a) create an issue in Coding Standards and postpone this on that or b) close this as won't fix.

My two cents is that I agree with #28 and #33 that this does not add value.

daffie’s picture

Status: Needs review » Closed (won't fix)

We have a core committer and 3 long term contributers (including myself) who think this is something we should not do. Therefor marking this issue as "closed won't fix".