Fix the issues reported by phpcs

Created on 20 April 2023, over 1 year ago
Updated 26 October 2023, about 1 year ago

Problem/Motivation

Running phpcs --standard=Drupal,DrupalPractice --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md reports the following issues that should be fixed.

FILE: ./xmlsitemap_taxonomy/xmlsitemap_taxonomy.test
 50 | ERROR | The array declaration extends to column 101 (the limit is 80). The array content should be split up over multiple
    |       | lines

FILE: ./xmlsitemap_taxonomy/xmlsitemap_taxonomy.install
 15 | WARNING | Unused variable $vid.

FILE: ./tests/xmlsitemap_views_fields.test
 8 | ERROR | [x] Missing class doc comment

FILE: ./xmlsitemap.module
  219 | WARNING | [ ] Open page callback found, please add a comment before the line why there is no access restriction
  225 | WARNING | [ ] Open page callback found, please add a comment before the line why there is no access restriction
  293 | ERROR   | [x] Expected 1 blank line after function; 2 found
  585 | ERROR   | [ ] The array declaration extends to column 89 (the limit is 80). The array content should be split up over
      |         |     multiple lines
  675 | ERROR   | [ ] The array declaration extends to column 222 (the limit is 80). The array content should be split up over
      |         |     multiple lines
  774 | WARNING | [x] There must be no blank line following an inline comment
  774 | WARNING | [ ] There must be no blank line following an inline comment
  840 | ERROR   | [ ] The array declaration extends to column 229 (the limit is 80). The array content should be split up over
      |         |     multiple lines
  907 | WARNING | [ ] Unused variable $smid.
 1035 | WARNING | [ ] Unused variable $bundle_key.
 1096 | ERROR   | [ ] The array declaration extends to column 167 (the limit is 80). The array content should be split up over
      |         |     multiple lines
 1097 | ERROR   | [ ] The array declaration extends to column 197 (the limit is 80). The array content should be split up over
      |         |     multiple lines
 1165 | ERROR   | [ ] The array declaration extends to column 119 (the limit is 80). The array content should be split up over
      |         |     multiple lines
 1215 | ERROR   | [ ] The array declaration extends to column 82 (the limit is 80). The array content should be split up over
      |         |     multiple lines
 1468 | ERROR   | [x] Separate the @codingStandardsIgnoreLine and @return sections by a blank line.
 1468 | ERROR   | [x] Expected "object" but found "object." for function return type
 1480 | ERROR   | [ ] The array declaration extends to column 99 (the limit is 80). The array content should be split up over
      |         |     multiple lines
 1520 | ERROR   | [ ] The array declaration extends to column 225 (the limit is 80). The array content should be split up over
      |         |     multiple lines

FILE: ./xmlsitemap.test
  59 | ERROR   | The array declaration extends to column 168 (the limit is 80). The array content should be split up over multiple
     |         | lines
 141 | ERROR   | The array declaration extends to column 158 (the limit is 80). The array content should be split up over multiple
     |         | lines
 227 | ERROR   | The array declaration extends to column 116 (the limit is 80). The array content should be split up over multiple
     |         | lines
 238 | ERROR   | The array declaration extends to column 116 (the limit is 80). The array content should be split up over multiple
     |         | lines
 406 | WARNING | Do not call theme functions directly, use theme('dblog_message', ...) instead
 406 | ERROR   | The array declaration extends to column 83 (the limit is 80). The array content should be split up over multiple
     |         | lines
 522 | WARNING | Do not use UPDATE queries with db_query(), use db_update() instead
 783 | WARNING | Unused variable $link1.
 784 | WARNING | Unused variable $link2.
 865 | WARNING | Line exceeds 80 characters; contains 104 characters
 915 | ERROR   | The array declaration extends to column 118 (the limit is 80). The array content should be split up over multiple
     |         | lines

FILE: ./xmlsitemap_menu/xmlsitemap_menu.test
 50 | ERROR | The array declaration extends to column 97 (the limit is 80). The array content should be split up over multiple
    |       | lines

FILE: ./xmlsitemap_user/xmlsitemap_user.test
 50 | ERROR | The array declaration extends to column 96 (the limit is 80). The array content should be split up over multiple
    |       | lines

FILE: ./xmlsitemap.install
  53 | ERROR   | [ ] The array declaration extends to column 299 (the limit is 80). The array content should be split up over
     |         |     multiple lines
  68 | ERROR   | [x] Each index in a multi-line array must be on a new line
  95 | ERROR   | [ ] Do not use t() or st() in installation phase hooks, use $t = get_t() to retrieve the appropriate localization
     |         |     function name
 349 | WARNING | [x] There must be no blank line following an inline comment
 349 | WARNING | [ ] There must be no blank line following an inline comment

FILE: ./xmlsitemap.variable.inc
   1 | ERROR | [x] The PHP open tag must be followed by exactly one blank line
  25 | ERROR | [ ] The array declaration extends to column 87 (the limit is 80). The array content should be split up over multiple
     |       |     lines
  72 | ERROR | [ ] The array declaration extends to column 143 (the limit is 80). The array content should be split up over
     |       |     multiple lines
 101 | ERROR | [x] Functions must not contain multiple empty lines in a row; found 2 empty lines

FILE: ./xmlsitemap.generate.inc
 321 | ERROR | The array declaration extends to column 119 (the limit is 80). The array content should be split up over multiple
     |       | lines
 328 | ERROR | The array declaration extends to column 88 (the limit is 80). The array content should be split up over multiple
     |       | lines
 329 | ERROR | The array declaration extends to column 94 (the limit is 80). The array content should be split up over multiple
     |       | lines
 333 | ERROR | The array declaration extends to column 120 (the limit is 80). The array content should be split up over multiple
     |       | lines
 427 | ERROR | The array declaration extends to column 116 (the limit is 80). The array content should be split up over multiple
     |       | lines
 437 | ERROR | The array declaration extends to column 91 (the limit is 80). The array content should be split up over multiple
     |       | lines
 441 | ERROR | The array declaration extends to column 117 (the limit is 80). The array content should be split up over multiple
     |       | lines

FILE: ./xmlsitemap_i18n/xmlsitemap_i18n.test
 76 | ERROR | The array declaration extends to column 86 (the limit is 80). The array content should be split up over multiple
    |       | lines

FILE: ./xmlsitemap_custom/xmlsitemap_custom.drush.inc
 32 | ERROR | The array declaration extends to column 108 (the limit is 80). The array content should be split up over multiple
    |       | lines
 34 | ERROR | The array declaration extends to column 227 (the limit is 80). The array content should be split up over multiple
    |       | lines

FILE: ./xmlsitemap_custom/xmlsitemap_custom.scan.inc
  1 | ERROR | [x] The PHP open tag must be followed by exactly one blank line
  7 | ERROR | [x] Whitespace found at end of line
 38 | ERROR | [ ] Concatenating translatable strings is not allowed, use placeholders instead and only one string literal
 39 | ERROR | [ ] Concatenating translatable strings is not allowed, use placeholders instead and only one string literal

FILE: ./xmlsitemap_custom/xmlsitemap_custom.test
  32 | ERROR | The array declaration extends to column 96 (the limit is 80). The array content should be split up over multiple
     |       | lines
  70 | ERROR | The array declaration extends to column 93 (the limit is 80). The array content should be split up over multiple
     |       | lines
  97 | ERROR | The array declaration extends to column 81 (the limit is 80). The array content should be split up over multiple
     |       | lines
 121 | ERROR | The array declaration extends to column 91 (the limit is 80). The array content should be split up over multiple
     |       | lines
 128 | ERROR | The array declaration extends to column 91 (the limit is 80). The array content should be split up over multiple
     |       | lines

FILE: ./xmlsitemap_custom/xmlsitemap_custom.admin.inc
 15 | ERROR | The array declaration extends to column 83 (the limit is 80). The array content should be split up over multiple
    |       | lines
 45 | ERROR | The array declaration extends to column 159 (the limit is 80). The array content should be split up over multiple
    |       | lines
 46 | ERROR | The array declaration extends to column 165 (the limit is 80). The array content should be split up over multiple
    |       | lines

FILE: ./xmlsitemap_engines/tests/xmlsitemap_engines.test
  43 | ERROR | The array declaration extends to column 96 (the limit is 80). The array content should be split up over multiple
     |       | lines
  51 | ERROR | The array declaration extends to column 96 (the limit is 80). The array content should be split up over multiple
     |       | lines
 122 | ERROR | The array declaration extends to column 137 (the limit is 80). The array content should be split up over multiple
     |       | lines
 123 | ERROR | The array declaration extends to column 106 (the limit is 80). The array content should be split up over multiple
     |       | lines
 152 | ERROR | The array declaration extends to column 115 (the limit is 80). The array content should be split up over multiple
     |       | lines
 153 | ERROR | The array declaration extends to column 87 (the limit is 80). The array content should be split up over multiple
     |       | lines

FILE: ./xmlsitemap_engines/tests/xmlsitemap_engines_test.module
 36 | ERROR | The array declaration extends to column 106 (the limit is 80). The array content should be split up over multiple
    |       | lines

FILE: ./xmlsitemap_engines/xmlsitemap_engines.module
 161 | ERROR | The array declaration extends to column 144 (the limit is 80). The array content should be split up over multiple
     |       | lines

FILE: ./xmlsitemap_engines/xmlsitemap_engines.variable.inc
  1 | ERROR | [x] The PHP open tag must be followed by exactly one blank line
 16 | ERROR | [ ] The array declaration extends to column 374 (the limit is 80). The array content should be split up over multiple
    |       |     lines
 39 | ERROR | [ ] The array declaration extends to column 128 (the limit is 80). The array content should be split up over multiple
    |       |     lines

FILE: ./xmlsitemap.xmlsitemap.inc
  27 | ERROR   | [ ] Missing member variable doc comment
  28 | ERROR   | [ ] Missing member variable doc comment
  29 | ERROR   | [ ] Missing member variable doc comment
  30 | ERROR   | [ ] Missing member variable doc comment
  68 | ERROR   | [ ] Inline doc block comments are not allowed; use "/* Comment */" or "// Comment" instead
  72 | ERROR   | [x] Missing function doc comment
  75 | WARNING | [ ] Exceptions should not be translated
  80 | ERROR   | [ ] Inline doc block comments are not allowed; use "/* Comment */" or "// Comment" instead
  84 | ERROR   | [x] Missing function doc comment
  88 | WARNING | [ ] Exceptions should not be translated
 151 | ERROR   | [ ] Inline doc block comments are not allowed; use "/* Comment */" or "// Comment" instead
 155 | ERROR   | [x] Missing function doc comment
 186 | ERROR   | [ ] Inline doc block comments are not allowed; use "/* Comment */" or "// Comment" instead
 197 | ERROR   | [x] Missing function doc comment
 228 | ERROR   | [ ] Inline doc block comments are not allowed; use "/* Comment */" or "// Comment" instead
 232 | ERROR   | [x] Missing function doc comment
 236 | WARNING | [ ] Exceptions should not be translated
 255 | ERROR   | [ ] Missing member variable doc comment
 392 | WARNING | [ ] Unused variable $info.

FILE: ./xmlsitemap_node/xmlsitemap_node.module
 173 | WARNING | Doc comment indicates hook_form_alter() but function signature is "xmlsitemap_node_form_node_form_alter" instead
     |         | of "xmlsitemap_node_form_alter". Did you mean hook_form_FORM_ID_alter()?
 203 | ERROR   | The array declaration extends to column 175 (the limit is 80). The array content should be split up over multiple
     |         | lines

FILE: ./xmlsitemap.admin.inc
  87 | ERROR | [ ] The array declaration extends to column 148 (the limit is 80). The array content should be split up over
     |       |     multiple lines
  88 | ERROR | [ ] The array declaration extends to column 154 (the limit is 80). The array content should be split up over
     |       |     multiple lines
 328 | ERROR | [x] Array closing indentation error, expected 4 spaces but found 6
 576 | ERROR | [ ] The array declaration extends to column 164 (the limit is 80). The array content should be split up over
     |       |     multiple lines

Steps to reproduce

Run phpcs --standard=Drupal,DrupalPractice --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md.

Proposed resolution

Fix the issues that can be fixed, or that are really issues for a Drupal 7 module.

📌 Task
Status

Closed: won't fix

Version

2.0

Component

xmlsitemap_node.module

Created by

🇮🇳India vimal_nadar

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.

  • Needs reroll

    The patch will have to be re-rolled with new suggestions/changes described in the comments in the issue.

Sign in to follow issues

Comments & Activities

  • Issue created by @vimal_nadar
  • Issue was unassigned.
  • Status changed to Needs review over 1 year ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 7.x + Environment: PHP 5.3 & MySQL 5.5
    last update over 1 year ago
    PHPLint Failed
  • Open in Jenkins → Open on Drupal.org →
    Core: 7.x + Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    45 pass
  • Status changed to RTBC over 1 year ago
  • 🇬🇧United Kingdom lesleyfernandes

    The changes are fine. Maintainers should check if it is good to go or not.

  • Status changed to Needs work over 1 year ago
  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    The issue summary should always describe what the issue is trying to fix and, in the case, of coding standards issues, show which command has been used, which arguments have been used, and which report that command shown.

  • 🇮🇹Italy apaderno Brescia, 🇮🇹
    -  $node_type = isset($form['#node_type']->type) ? $form['#node_type']->type : '';
    +  $node_type = $form['#node_type']->type ?? '';

    The minimum PHP version required by Drupal 7 is not PHP 7. The existing code is correct.

  • 🇮🇳India Akram Khan Cuttack, Odisha

    hi @apaderno then is it okey to keep "Use null coalesce operator instead of ternary operator." error message ?

  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    Since the rulesets are not applied to Drupal 8/9/10 code, yes, it is fine.
    phpcs does not have setting for the PHP version used by the code, and it cannot even know to which PHP version each rule in the ruleset applies.

  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    Also, the issue summary still needs to be updated, as per my previous comment.

  • Status changed to Needs review over 1 year ago
  • 🇮🇳India vimal_nadar

    Updated the issue summary with required details.

  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    No standard is passed to phpcs. If I run phpcs --standard=Drupal,DrupalPractice --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md for the 7.x-2.x branch, I get a longer report.

  • Status changed to Needs work over 1 year ago
  • 🇮🇹Italy apaderno Brescia, 🇮🇹
  • Assigned to vimal_nadar
  • Issue was unassigned.
  • Status changed to Needs review over 1 year ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 7.x + Environment: PHP 5.3 & MySQL 5.5
    last update over 1 year ago
    PHPLint Failed
  • 🇮🇳India vimal_nadar

    Resolved all the phpcs warning and error messages.

  • Open in Jenkins → Open on Drupal.org →
    Core: 7.x + Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    44 pass, 1 fail
  • Open in Jenkins → Open on Drupal.org →
    Core: 7.x + Environment: PHP 5.3 & MySQL 5.5
    last update over 1 year ago
    PHPLint Failed
  • Open in Jenkins → Open on Drupal.org →
    Core: 7.x + Environment: PHP 8.0 & MySQL 5.7
    last update over 1 year ago
    44 pass, 1 fail
  • Status changed to Needs work over 1 year ago
  • 🇮🇹Italy apaderno Brescia, 🇮🇹
  • 🇸🇰Slovakia poker10

    Thanks for the detailed review @apaderno!

    I am going to add a few more points:

    -    $sitemaps[$smid]->context = unserialize($sitemap->context);
    +    $sitemaps[$smid]->context = unserialize($sitemap->context, ['allowed_classes' => FALSE]);
    

    This is a functional change a has nothing to do with a coding standards. Therefore it must be done in a separate issue if it is needed (preferably also with a test demonstrating the bug you are trying to fix).

       public function testDuplicatePaths() {
    -    $link1 = $this->addSitemapLink(array('loc' => 'duplicate'));
    -    $link2 = $this->addSitemapLink(array('loc' => 'duplicate'));
         $this->regenerateSitemap();
    

    The fact that the variables are not used anywhere does not mean you can delete the whole statements.

    -function xmlsitemap_form_locale_languages_overview_form_alter(&$form, $form_state) {
    +function xmlsitemap_i18n_form_locale_languages_overview_form_alter(&$form, $form_state) {
       array_unshift($form['#submit'], 'xmlsitemap_form_submit_flag_regenerate');
     }
    

    This is a functional change a has nothing to do with a coding standards. Therefore it must be done in a separate issue if it is needed (preferably also with a test demonstrating the bug you are trying to fix).

    I would also prefer to keep the "one-liners" untouched, as often these have better readability.

  • Assigned to vimal_nadar
  • Issue was unassigned.
  • Status changed to Needs review over 1 year ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 7.x + Environment: PHP 5.3 & MySQL 5.5
    last update over 1 year ago
    PHPLint Failed
  • 🇮🇳India vimal_nadar

    @apaderno and @poker10 made required changes in this patch.

  • Status changed to Needs work over 1 year ago
  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    I apologize: I meant to give a more complete review the same day I posted my previous comment, but I have forgot to do so.

    +    // This page callback outputs the sitemap in chunks using the
    +    // xmlsitemap_output_chunk() function. There is no access restriction for
    +    // this page because it needs to be accessible to search engine bots and
    +    // other automated tools that crawl the site.
         'access callback' => TRUE,
         'type' => MENU_CALLBACK,
         'file' => 'xmlsitemap.pages.inc',
       );

    The second sentence is probably sufficient.

    -        foreach ($info['bundles'] as $bundle_key => $bundle) {
    +        foreach ($info['bundles'] as $bundle) {

    While $bundle_key is not used, sometimes foreach loops are written so to document what the array contains. That is not necessarily an issue.

    -    return $this->assertFalse($match, $message ? $message : t('HTTP response not expected !code, actual !curl_code', array('!code' => $code, '!curl_code' => $curl_code)), t('Browser'));
    +    return $this->assertFalse($match, $message ? $message : t('HTTP response not expected !code, actual !curl_code', array(
    +      '!code' => $code,
    +      '!curl_code' => $curl_code,
    +    ),
    +    ), t('Browser'));

    The changed code is not the same as the existing code. There is an extra comma, which should not be there as only PHP 8 allow it.

    - throw new XMLSitemapGenerationException(t('Could not open file @file for writing.', array('@file' => $uri)));
    + throw new XMLSitemapGenerationException('Could not open file %file for writing.', array(
    + '%file' => $uri,
    + ));

    If the t() call is removed, the second argument passed to the exception constructor needs to be removed too, as exception constructors usually expect a single argument: the description of what happened.

    -    watchdog('xmlsitemap', 'Files: <pre>' . var_export($files, TRUE), WATCHDOG_NOTICE);
    -    watchdog('xmlsitemap', 'Custom: <pre>' . var_export($custom_links, TRUE), WATCHDOG_NOTICE);
    +    $files_message = t('Files: @files', ['@files' => '<pre>' . var_export($files, TRUE) . '</pre>']);
    +    watchdog('xmlsitemap', $files_message, WATCHDOG_NOTICE);

    The second argument passed to watchdog() is not a translatable string. It is sufficient the arguments Drupal 7 core passes to watchdog().

    if ($stack > variable_get('actions_max_stack', 35)) {
        watchdog('actions', 'Stack overflow: too many calls to actions_do(). Aborting to prevent infinite recursion.', array(), WATCHDOG_ERROR);
        return;
    }

    Eventually, those function calls are missing an argument, but I would rather fill a different issue.

    -    $this->submit_url = url('ping', array('absolute' => TRUE, 'query' => array('sitemap' => ''))) . '[sitemap]';
    +    $this->submit_url = url('ping', array(
    +      'absolute' => TRUE,
    +      'query' => array('sitemap' => ''),
    +    )) . '[sitemap]';

    Like other changes introduced in this patch, this change does not make the code more readable.

    I would rather suggest maintainers to ignore this issue. Reviewing all the introduced changes is time-consuming and the patch does not seem provided by by someone who has knowledge of how code should be modified.
    Changing the code to respect the coding standards can be done when a function is edited for another reason. There is no need to resolve all the coding standards issues in a single issue, especially when the changes are done just to see an automatic tool giving an empty report.

  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    Even the last provided patch still has the same errors. What I reported in my previous comment has not been fixed.

  • First commit to issue fork.
  • Status changed to Needs review about 1 year ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 7.x + Environment: PHP 8.1 & MySQL 5.7
    last update about 1 year ago
    45 pass
  • 🇮🇳India shefali99

    The provided patch file "fix_coding_standards-3355437-23.patch" fixes the reported issue as per comment #21.

  • Status changed to Closed: won't fix about 1 year ago
  • 🇸🇰Slovakia poker10

    @shefali99 Thanks for working on this, unfortunatelly the patch still has multiple issues (selecting only few of them):

    -  $term->xmlsitemap['language'] = isset($term->language) ? $term->language : LANGUAGE_NONE;
    +  $term->xmlsitemap['language'] = $term->language ?? LANGUAGE_NONE;
    

    We cannot use the Null coalescing operator (used on multiple places in the patch), because we are not going to drop BC intentionally.

    -function xmlsitemap_form_locale_languages_overview_form_alter(&$form, $form_state) {
    +function xmlsitemap_i18n_xmlsitemap_form_locale_languages_overview_form_alter(&$form, $form_state) {
       array_unshift($form['#submit'], 'xmlsitemap_form_submit_flag_regenerate');
     }
    

    I already commented this change previously as a non-coding standards issue, so this change shouldn't be here at all.

    -      throw new XMLSitemapGenerationException(t('Unknown error occurred while writing to file @file.', array('@file' => $this->uri)));
    +      throw new XMLSitemapGenerationException('Unknown error occurred while writing to file ' . $this->uri);
    

    This is also unrelated to the coding standards.

    -  $batch['operations'][] = array('xmlsitemap_batch_variable_set', array(array('xmlsitemap_rebuild_needed' => FALSE)));
    +  $batch['operations'][] = array('xmlsitemap_batch_variable_set', array(array(
    +    'xmlsitemap_rebuild_needed' => FALSE,
    +  ),
    +  ),
    +  );
    

    I am not sure this is a correct formatting.

    -    $operations['edit'] = xmlsitemap_get_operation_link('admin/config/search/xmlsitemap/edit/' . $smid, array('title' => t('Edit'), 'modal' => TRUE));
    -    $operations['delete'] = xmlsitemap_get_operation_link('admin/config/search/xmlsitemap/delete/' . $smid, array('title' => t('Delete'), 'modal' => TRUE));
    +    $operations['edit'] = xmlsitemap_get_operation_link('admin/config/search/xmlsitemap/edit/' . $smid, array(
    +      '
    +    title' => t('Edit'),
    +      'modal' => TRUE,
    +    ));
    

    That newline with a single quote is also wrong formatting / typo.

    ------------

    D7 contrib modules and D7 core itself contains a lot of coding standard issues and personally I do not think that it is useful to invest so much time in fixing these in the current D7 phase (EOL in Jan 2025). So I agree with @apaderno, that this issue is probably too much (either for contributors and also for reviewers/maintainers). Let's just focus on other issues, where we can still deliver some fixes and improvements. Thanks!

Production build 0.71.5 2024