Fix Drupal.Commenting.DocCommentLongArraySyntax coding standard

Created on 30 April 2017, over 7 years ago
Updated 1 July 2024, 6 months ago

Problem/Motivation

Postponed on #2857906: Enforce short array syntax for Drupal 8 api docblock comments →

This is a follow-up to #2868078: Use new array syntax in PHP files outside of /core. → .

The coding standards for arrays → require short array syntax (except for Drupal 7 core and contributed modules). Does this apply to code samples inside @code blocks? If so, then we need to

  1. Clarify this requirement in the documentation.
  2. Check array syntax inside @code blocks in our CodeSniffer sniffs.
  3. Update Drupal core to use the short syntax inside @code blocks.

Steps to reproduce

Proposed resolution

Add the new sniff, <rule ref="Drupal.Commenting.DocCommentLongArraySyntax"/> to phpcs.xml.dist
Run phpcs on core
Fix the errors found

Remaining tasks

See #46 📌 Convert code blocks in comments using array() syntax to @code/@endcode Needs review
See #47 📌 Convert use of array() syntax in sentences Needs review

Review
Commit

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Release notes snippet

📌 Task
Status

Fixed

Version

10.3 ✨

Component
Documentation  →

Last updated 1 day ago

No maintainer
Created by

🇺🇸United States benjifisher Boston area

Live updates comments and jobs are added and updated live.
  • Coding standards

    It involves compliance with, or the content of coding standards. Requires broad community agreement.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇺🇸United States smustgrave

    Comparing #24 with #8 seems to be missing a big chunk of the fixes.

    Some of them may have naturally been fixed but the few files I checked they have not.

  • 🇮🇳India kkalashnikov Ghaziabad, India

    Thanks @smustgrave this is the updated patch

  • Status changed to Needs review almost 2 years ago
  • 🇮🇳India kkalashnikov Ghaziabad, India
  • Status changed to Needs work almost 2 years ago
  • 🇺🇸United States smustgrave

    CI failure #28

  • 🇮🇳India kkalashnikov Ghaziabad, India

    Updated the patch

  • 🇮🇳India kkalashnikov Ghaziabad, India
  • 🇳🇿New Zealand quietone

    Nice to see progress here!

    Fixing PHP coding standards is done by first adding a Coder rule, or sniff. The current patch is missing that so it needs to be added.We do this so that new code does not introduce regressions. When a patch is tested various checks are run, such as coding standard checks, before the tests run.

    So, what needs to happen here is to start by just adding the sniff. Then run phpcs to find what needs to be fixed. Then fix the comments and run phpcs core until there are no errors. The latest patch will have some of those fixes but it also contains out of scope changes. This patch is to fix array syntax in comments, not code.

    I have updated the proposed resolution in the Issue Summary with some details.

  • First commit to issue fork.
  • Assigned to zniki.ru
  • Issue was unassigned.
  • Status changed to Needs review about 1 year ago
  • 🇷🇺Russia zniki.ru

    I update phpcs.xml and remove unwanted changes.
    I was not able to apply patch 32, so I made some changes to it, I added manually created interdiff.
    Please review.

  • 🇷🇺Russia zniki.ru

    Oops, made mistake when creating a patch file.
    Provide new patch file and interdiff.

  • 🇷🇺Russia zniki.ru

    Update interdiff #32 and #37.

  • Status changed to Needs work about 1 year ago
  • The Needs Review Queue Bot → tested this issue.

    While you are making the above changes, we recommend that you convert this patch to a merge request → . Merge requests are preferred over patches. Be sure to hide the old patch files as well. (Converting an issue to a merge request without other contributions to the issue will not receive credit.)

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MariaDB 10.3.22
    last update about 1 year ago
    30,560 pass
  • Status changed to Needs review about 1 year ago
  • 🇷🇺Russia zniki.ru

    I created MR as requested by Needs Review Queue Bot.

  • 🇷🇺Russia zniki.ru

    Hide patch files.

  • Status changed to RTBC about 1 year ago
  • 🇺🇸United States smustgrave

    Failure is random and unrelated, ran it a few times and sometimes it passes sometimes fails but is not impacted by this
    #3317520: Random failure in Drupal\Tests\settings_tray\FunctionalJavascript\SettingsTrayBlockFormTest::testEditModeEnableDisable →

    Large MR but not terrible to read.

    Think if anything was missed the phpcs line added would of got it.

  • 🇺🇸United States xjm

    As the person who committed the 12-gigabyte array() to [] conversion, I feel it's my very great honor to review this one as well.

  • Pipeline finished with Success
    about 1 year ago
    Total: 19443s
    #51151
  • 🇺🇸United States xjm

    Something odd is going on with the docblock of EntityFormDisplayInterface::buildForm(); there are more array closures than openings. It appears to be pseudocode rather than example code, and at least one array closure was missed. That's an existing problem and out of scope for a color-words issue like this, so tagging for a followup.

  • Status changed to Needs review about 1 year ago
  • 🇺🇸United States xjm

    I reviewed this locally with git diff --color-words to verify that all the changes were complete and correct replacements of array() with [] in API documentation.

    There was another existing issue in core/modules/views/src/Plugin/views/cache/CachePluginBase.php with a missing closing paren that's fixed here. I opened the file locally to confirm that it's fixed correctly.

    I also grepped the codebase to check for remaining array(). There are many in inline comments, but that seems to be outside the scope of this issue, which is all docblocks. So, I think we need a second followup for inline comments.

    However, there are somehow also numerous instances in docblocks that are apparently not being caught by the rule?

    [ayrton:maintainer | Thu 18:37:29] $ grep -r "\barray(" * | grep -F \* | grep -v "vendor" | grep -v "node_modules"
    core/scripts/dump-database-d7.sh:  $result = db_query('SELECT * FROM {'. $table .'}', array(), array('fetch' => PDO::FETCH_ASSOC));
    core/lib/Drupal/Core/Database/Database.php:   * array(
    core/lib/Drupal/Core/Database/Log.php:   * array(
    core/lib/Drupal/Core/Database/Log.php:   *   $logging_key = array(
    core/lib/Drupal/Core/Database/Log.php:   *     array('query' => '', 'args' => array(), 'caller' => '', 'target' => '', 'time' => 0, 'start' => 0),
    core/lib/Drupal/Core/Database/Log.php:   *     array('query' => '', 'args' => array(), 'caller' => '', 'target' => '', 'time' => 0, 'start' => 0),
    core/lib/Drupal/Core/Database/Query/Select.php:   * array(
    core/lib/Drupal/Core/Cache/UseCacheBackendTrait.php:   *   tags. For example array('node' => array(123), 'user' => array(92)).
    core/lib/Drupal/Core/Datetime/DrupalDateTime.php: * DrupalDateTime::createFromArray( array('year' => 2010, 'month' => 9, 'day' => 28) )
    core/lib/Drupal/Core/Entity/EntityAutocompleteMatcherInterface.php:   *   autocomplete API (e.g. array('value' => $value, 'label' => $label)).
    core/lib/Drupal/Core/Render/Element.php:   *   property; e.g., array('#property_name' => 'attribute_name'). If both
    core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php:   *   operation, e.g. array('width' => 50, 'height' => 100,
    core/lib/Drupal/Core/Field/FieldConfigInterface.php:   *   - NULL or array() for no default value.
    core/lib/Drupal/Core/Field/FieldDefinitionInterface.php:   *   each item being a property/value array (array() for no default value).
    core/lib/Drupal/Core/Field/FieldDefinitionInterface.php:   *   each item being a property/value array (array() for no default value).
    core/lib/Drupal/Core/Field/BaseFieldDefinition.php:   *   each item being a property/value array (array() for no default value).
    core/lib/Drupal/Core/Utility/ProjectInfo.php:   *   file. Defaults to array().
    core/lib/Drupal/Core/Utility/ProjectInfo.php:   *   file. Defaults to array().
    core/lib/Drupal/Component/Gettext/PoHeader.php:   *   array(
    core/lib/Drupal/Component/Gettext/PoHeader.php:   *   array(
    core/lib/Drupal/Component/Gettext/PoHeader.php:   *   array(
    core/modules/views_ui/admin.inc: *   array('dynamic_content', 'section') for this parameter.
    core/modules/system/tests/modules/common_test/common_test.module: * \Drupal::moduleHandler()->alter(array(TYPE1, TYPE2), ...) allows
    core/modules/page_cache/src/StackMiddleware/PageCache.php:   *   tags. For example array('node' => array(123), 'user' => array(92)).
    core/modules/views/src/Plugin/views/wizard/WizardPluginBase.php:   *   normally be found in $form_state->getValue(array('wrapper', 'select')),
    core/modules/views/src/Plugin/views/wizard/WizardPluginBase.php:   *   you would pass array('wrapper', 'select') for this parameter.
    core/modules/views/src/Plugin/views/field/Markup.php: *           array('field' => {$field}) where $field is the field in this table
    core/modules/rest/src/Plugin/ResourceInterface.php:   *   The list of supported methods. Example: array('GET', 'POST', 'PATCH').
    

    Setting NR to look into those. Several of them appear to be in sentences rather than @code/@endcode -- maybe that is the common factor? If that's confirmed, we could have a third followup to properly use @code where possible and another to fix them in sentences.

    Thanks everyone!

  • 🇺🇸United States smustgrave

    Then how come the rule didn’t catch those?

  • 🇳🇿New Zealand quietone

    @smustgrave, yes that is the question that xjm is asking those working on this issue to answer.

    To help move this along I did the following. I started by searching the vendor directory for DocCommentLongArraySyntax and found the class implementing the sniff.

    /**
     * Ensures @code annotations in doc blocks don't contain long array syntax.
     *
     * @category PHP
     * @package  PHP_CodeSniffer
     * @link     http://pear.php.net/package/PHP_CodeSniffer
     */
    class DocCommentLongArraySyntaxSniff implements Sniff
    {

    The summary line confirms what xjm suspected in #47. And as I read it then the remainder of the work in #47 can proceed.

    I have added #46 and #47 to the remaining tasks.

  • Status changed to Needs work about 1 year ago
  • 🇺🇸United States smustgrave

    For the follow ups of 46 + 47

  • Pipeline finished with Success
    6 months ago
    Total: 554s
    #197022
  • Pipeline finished with Success
    6 months ago
    Total: 543s
    #197037
  • Status changed to Needs review 6 months ago
  • 🇳🇿New Zealand quietone

    Rebased the MR. Then, I searched for the same pattern used in #47 and then checked the files and I am confident these are not detected because they are not in @code @endcode blocks. I have updated the issue summary with the followups that need to be made.

  • Status changed to RTBC 6 months ago
  • 🇺🇸United States smustgrave

    So applied the MR and ran grep -r "\barray(" * | grep -F \* | grep -v "vendor" | grep -v "node_modules"

    lib/Drupal/Core/Database/Database.php:   * array(
    lib/Drupal/Core/Database/Log.php:   * array(
    lib/Drupal/Core/Database/Log.php:   *   $logging_key = array(
    lib/Drupal/Core/Database/Log.php:   *     array('query' => '', 'args' => array(), 'caller' => '', 'target' => '', 'time' => 0, 'start' => 0),
    lib/Drupal/Core/Database/Log.php:   *     array('query' => '', 'args' => array(), 'caller' => '', 'target' => '', 'time' => 0, 'start' => 0),
    lib/Drupal/Core/Database/Query/Select.php:   * array(
    lib/Drupal/Core/Cache/UseCacheBackendTrait.php:   *   tags. For example array('node' => array(123), 'user' => array(92)).
    lib/Drupal/Core/Datetime/DrupalDateTime.php: * DrupalDateTime::createFromArray( array('year' => 2010, 'month' => 9, 'day' => 28) )
    lib/Drupal/Core/Entity/EntityAutocompleteMatcherInterface.php:   *   autocomplete API (e.g. array('value' => $value, 'label' => $label)).
    lib/Drupal/Core/Render/Element.php:   *   property; e.g., array('#property_name' => 'attribute_name'). If both
    lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php:   *   operation, e.g. array('width' => 50, 'height' => 100,
    lib/Drupal/Core/Field/FieldConfigInterface.php:   *   - NULL or array() for no default value.
    lib/Drupal/Core/Field/FieldDefinitionInterface.php:   *   each item being a property/value array (array() for no default value).
    lib/Drupal/Core/Field/FieldDefinitionInterface.php:   *   each item being a property/value array (array() for no default value).
    lib/Drupal/Core/Field/BaseFieldDefinition.php:   *   each item being a property/value array (array() for no default value).
    lib/Drupal/Core/Utility/ProjectInfo.php:   *   file. Defaults to array().
    lib/Drupal/Core/Utility/ProjectInfo.php:   *   file. Defaults to array().
    lib/Drupal/Component/Gettext/PoHeader.php:   *   array(
    lib/Drupal/Component/Gettext/PoHeader.php:   *   array(
    lib/Drupal/Component/Gettext/PoHeader.php:   *   array(
    modules/views_ui/admin.inc: *   array('dynamic_content', 'section') for this parameter.
    modules/system/tests/modules/common_test/common_test.module: * \Drupal::moduleHandler()->alter(array(TYPE1, TYPE2), ...) allows
    modules/page_cache/src/StackMiddleware/PageCache.php:   *   tags. For example array('node' => array(123), 'user' => array(92)).
    modules/views/src/Plugin/views/wizard/WizardPluginBase.php:   *   normally be found in $form_state->getValue(array('wrapper', 'select')),
    modules/views/src/Plugin/views/wizard/WizardPluginBase.php:   *   you would pass array('wrapper', 'select') for this parameter.
    modules/views/src/Plugin/views/field/Markup.php: *           array('field' => {$field}) where $field is the field in this table
    modules/rest/src/Plugin/ResourceInterface.php:   *   The list of supported methods. Example: array('GET', 'POST', 'PATCH').
    scripts/dump-database-d7.sh:  $result = db_query('SELECT * FROM {'. $table .'}', array(), array('fetch' => PDO::FETCH_ASSOC));
    

    Which spot checking appear to be outside of @code blocks.

    For #45 follow up is that covered by 📌 Convert code blocks in comments using array() syntax to @code/@endcode Needs review or 📌 Convert use of array() syntax in sentences Needs review ?

  • Status changed to Needs work 6 months ago
  • 🇺🇸United States xjm

    There is a hunk in core/modules/views/src/Plugin/views/field/FieldPluginBase.php that has out-of-scope changes. It could either be a bad merge, or someone correcting the documentation while fixing this bug.

    If it's the latter, we should make a followup issue to correct the keys; regardless, the hunk should only include the changes to array().

    Going to do a local review of this with git diff --color-words; if someone wants to fix the hunk and file a followup if appropriate, we could get this in still as a beta phase(-ish) cleanup.

    Saving issue credits.

  • 🇺🇸United States xjm

    Followups were already created.

    Tagging "Novice" for the task described in #53.

  • Status changed to RTBC 6 months ago
  • 🇺🇸United States smustgrave

    📌 Update use of arguments used in comments Active opened, already tagged for title update cuz that one is bad.

  • 🇺🇸United States smustgrave

    Oops did that before I got the novice email.

  • Pipeline finished with Success
    6 months ago
    Total: 617s
    #201098
  • 🇺🇸United States xjm

    No worries, I prefer to just land the issue in this case. :D

    • xjm → committed bd12c418 on 11.x
      Issue #2874067 by kkalashnikov, Nikolay Shapovalov, quietone, smustgrave...
    • xjm → committed 8fedc4e2 on 11.0.x
      Issue #2874067 by kkalashnikov, Nikolay Shapovalov, quietone, smustgrave...
  • 🇺🇸United States xjm

    Committed to 11.x and 11.0.x.

    It cherry-picked cleanly to 10.4.x with some auto-merging, but I'm going to open a quick 10.4.x MR here based on that cherry-pick just to ensure there aren't additional instances in D10 that would cause the rule to fail there.

  • Merge request !8433Auto-merged cherry-pick of the D11 commit. → (Closed) created by xjm
  • Pipeline finished with Failed
    6 months ago
    #201118
  • Status changed to Needs work 6 months ago
  • 🇺🇸United States xjm

    Good thing I tested that backport before pushing it; looks like the 10.4.x backport will need some additions. :D

    --------------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    --------------------------------------------------------------------------------
     258 | ERROR | Long array syntax must not be used in doc comment code
         |       | annotations
         |       | (Drupal.Commenting.DocCommentLongArraySyntax.DocLongArray)
    --------------------------------------------------------------------------------
    FILE: ...ds/project/drupal/core/modules/tour/tests/src/Functional/TourTestBasic.php
    --------------------------------------------------------------------------------
    FOUND 4 ERRORS AFFECTING 4 LINES
    --------------------------------------------------------------------------------
     19 | ERROR | Long array syntax must not be used in doc comment code
        |       | annotations
        |       | (Drupal.Commenting.DocCommentLongArraySyntax.DocLongArray)
     20 | ERROR | Long array syntax must not be used in doc comment code
        |       | annotations
        |       | (Drupal.Commenting.DocCommentLongArraySyntax.DocLongArray)
     21 | ERROR | Long array syntax must not be used in doc comment code
        |       | annotations
        |       | (Drupal.Commenting.DocCommentLongArraySyntax.DocLongArray)
     22 | ERROR | Long array syntax must not be used in doc comment code
        |       | annotations
        |       | (Drupal.Commenting.DocCommentLongArraySyntax.DocLongArray)
    --------------------------------------------------------------------------------
    FILE: ...lds/project/drupal/core/modules/tour/tests/src/Functional/TourTestBase.php
    --------------------------------------------------------------------------------
    FOUND 4 ERRORS AFFECTING 4 LINES
    --------------------------------------------------------------------------------
     23 | ERROR | Long array syntax must not be used in doc comment code
        |       | annotations
        |       | (Drupal.Commenting.DocCommentLongArraySyntax.DocLongArray)
     24 | ERROR | Long array syntax must not be used in doc comment code
        |       | annotations
        |       | (Drupal.Commenting.DocCommentLongArraySyntax.DocLongArray)
     25 | ERROR | Long array syntax must not be used in doc comment code
        |       | annotations
        |       | (Drupal.Commenting.DocCommentLongArraySyntax.DocLongArray)
     26 | ERROR | Long array syntax must not be used in doc comment code
        |       | annotations
        |       | (Drupal.Commenting.DocCommentLongArraySyntax.DocLongArray)
    --------------------------------------------------------------------------------
    Time: 10.49 secs; Memory: 8MB
    PHP CODE SNIFFER REPORT SUMMARY
    --------------------------------------------------------------------------------
    FILE                                                            ERRORS  WARNINGS
    --------------------------------------------------------------------------------
    /builds/project/drupal/core/includes/install.inc                1       0
    ...pal/core/modules/tour/tests/src/Functional/TourTestBase.php  4       0
    ...al/core/modules/tour/tests/src/Functional/TourTestBasic.php  4       0
    --------------------------------------------------------------------------------
    A TOTAL OF 9 ERRORS AND 0 WARNINGS WERE FOUND IN 3 FILES
    --------------------------------------------------------------------------------
  • First commit to issue fork.
  • Status changed to Needs review 6 months ago
  • 🇨🇦Canada b_sharpe

    Fixed up 10.4 backport, passing phpcs now.

  • Pipeline finished with Success
    6 months ago
    Total: 704s
    #201157
  • Status changed to RTBC 6 months ago
  • 🇺🇸United States smustgrave

    10.4 rebase seems fine.

  • 🇺🇸United States xjm

    Thanks @b_sharpe, nice work!

    • xjm → committed fcfcbc6d on 10.4.x
      Issue #2874067 by kkalashnikov, Nikolay Shapovalov, quietone, b_sharpe,...
  • Status changed to Fixed 6 months ago
  • 🇺🇸United States xjm

    Since 10.3.x is in RC, it is not eligible to have new rules enabled. So, instead of testing a 10.3.x backport, I backported the 10.4.x changes only for parity, with the following commands:

    [ayrton:maintainer | Mon 15:24:12] $ git cherry-pick -x 10.4.x
    Auto-merging core/modules/responsive_image/responsive_image.module
    [10.3.x e64d867c62] Issue #2874067 by kkalashnikov, Nikolay Shapovalov, quietone, b_sharpe, smustgrave, xjm, benjifisher, MerryHamster, dww: Fix Drupal.Commenting.DocCommentLongArraySyntax coding standard
     Date: Mon Jun 17 15:22:41 2024 -0500
     107 files changed, 579 insertions(+), 578 deletions(-)
    [ayrton:maintainer | Mon 15:24:20] $ git checkout HEAD^ --  core/phpcs.xml.dist  
    [ayrton:maintainer | Mon 15:27:29] $ git commit --amend -m 'Issue #2874067 by kkalashnikov, Nikolay Shapovalov, quietone, b_sharpe, smustgrave, xjm, benjifisher, MerryHamster, dww: Fix Drupal.Commenting.DocCommentLongArraySyntax coding standard'
    

    Thanks everyone!

    • xjm → committed 23f09d3f on 10.3.x
      Issue #2874067 by kkalashnikov, Nikolay Shapovalov, quietone, b_sharpe,...
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024