Fix the issues reported by phpcs

Created on 26 December 2022, almost 2 years ago
Updated 9 April 2024, 8 months ago

getting issues and warnings in PHPCS

FILE: /Applications/MAMP/htdocs/d9/web/modules/contrib/xmlrpc/xmlrpc.module
------------------------------------------------------------------------------------------------------------------------------------------
FOUND 3 ERRORS AFFECTING 3 LINES
------------------------------------------------------------------------------------------------------------------------------------------
32 | ERROR | Long array syntax must not be used in doc comment code annotations
33 | ERROR | Long array syntax must not be used in doc comment code annotations
57 | ERROR | All functions defined in a module file must be prefixed with the module's name, found "xmlrpc" but expected "xmlrpc_xmlrpc"
------------------------------------------------------------------------------------------------------------------------------------------

FILE: /Applications/MAMP/htdocs/d9/web/modules/contrib/xmlrpc/xmlrpc_example/src/Tests/XmlRpcExampleTest.php
------------------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 14 WARNINGS AFFECTING 14 LINES
------------------------------------------------------------------------------------------------------------------------------------------
76 | WARNING | t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
77 | WARNING | t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
81 | WARNING | t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
82 | WARNING | t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
85 | WARNING | t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
94 | WARNING | t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
107 | WARNING | t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
109 | WARNING | t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
112 | WARNING | t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
113 | WARNING | t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
136 | WARNING | t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
137 | WARNING | t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
142 | WARNING | t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
143 | WARNING | t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
----------------------------------------------------------------------------------------------------------------------

๐Ÿ“Œ Task
Status

Fixed

Version

1.0

Component

Code

Created by

๐Ÿ‡ฎ๐Ÿ‡ณIndia noorulshameera

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.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Charchil Khandelwal

    Charchil Khandelwal โ†’ made their first commit to this issueโ€™s fork.

  • Status changed to RTBC almost 2 years ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Charchil Khandelwal

    Patch #2 working fine in my system.
    Create MR for this.
    Moving it to RTBC +.

    Thank you.

  • Status changed to Needs work over 1 year ago
  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands megachriz

    Changing the name of the function xmlrpc() to xmlrpc_call() is an API change, so I strongly recommend not to change this, even if it is flagged as a coding standard issue.

    The t() calls in tests could be completely removed, t() calls in tests are usually not needed. They could be replaced with strings. For example:

    $this->assertSession()->pageTextContains(t('The XML-RPC server returned this response: @num', ['@num' => 77]));
    

    could be replaced with:

    $this->assertSession()->pageTextContains('The XML-RPC server returned this response: 77');
    
  • ๐Ÿ‡ซ๐Ÿ‡ทFrance fgm Paris, France

    ISTR we can quiet the naming warning by moving the function to a .inc file included from the module, can't we ?

  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands megachriz

    @fgm
    Based on the coding standard message that sounds like it would work, but I wonder if including an extra file on every request is worth doing for just one coding standard issue (an issue I would call a false positive)?

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly apaderno Brescia, ๐Ÿ‡ฎ๐Ÿ‡น

    I think this is an edge case. The function name is the machine name of the module; there is no risk of name conflict because no other module should have a xmlrpc() function.
    Plus, there are surely exceptions to how functions are named, for example a module that implements hooks on behalf of another module.

    Moving the function in a different file would not have any effect, since the coding standards are valid also for any .inc file a module could use.

  • Status changed to Needs review over 1 year ago
  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands megachriz

    Here's a patch to fix the CS issues. I found out that with phpcs:ignore or phpcs:disable you can ignore one specific type of coding standard violation. phpcs:ignore would need to put directly above the line where the coding standard violation is, but that resulted into the coding violation "Missing function doc comment", so that's why I chose to use phpcs:disable instead.

    Firstly, phpcs skipped the .module file locally, so I found out that I had to use this command:

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

    The -s option is to get the sniff code of the violation.

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly apaderno Brescia, ๐Ÿ‡ฎ๐Ÿ‡น

    The last patch seems good to go, to me.

  • Status changed to Needs work over 1 year ago
  • ๐Ÿ‡ซ๐Ÿ‡ทFrance fgm Paris, France

    Mostly good with using an ignore. However, I think you can put it at the function phpdoc level rather than at the file level, which makes it more focused.

  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands megachriz

    @fgm

    However, I think you can put it at the function phpdoc level rather than at the file level

    I tried that first, but I got an other coding standard violation in that case: "Missing function doc comment" (see #10).

  • ๐Ÿ‡ซ๐Ÿ‡ทFrance fgm Paris, France

    Did you try putting it at the end of the function line, like this ?

    function xmlrpc() { // phpcs:ignore Drupal.NamingConventions.ValidFunctionName.InvalidPrefix
    
  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands megachriz

    @fgm
    I didn't yet, but when I do that:

    function xmlrpc($url, array $args, array $headers = []) { // phpcs:ignore Drupal.NamingConventions.ValidFunctionName.InvalidPrefix
      module_load_include('inc', 'xmlrpc');
      return _xmlrpc($url, $args, $headers);
    }
    

    I get on phpcs --standard=Drupal . --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml -s:

    ---------------------------------------------------------------------------------------------------------------------------------
    FOUND 1 ERROR AND 1 WARNING AFFECTING 1 LINE
    ---------------------------------------------------------------------------------------------------------------------------------
     59 | ERROR   | [x] There should be no white space after an opening "{"
        |         |     (Drupal.WhiteSpace.OpenBracketSpacing.OpeningWhitespace)
     59 | WARNING | [ ] Line exceeds 80 characters; contains 130 characters (Drupal.Files.LineLength.TooLong)
    ---------------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ---------------------------------------------------------------------------------------------------------------------------------
    
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly apaderno Brescia, ๐Ÿ‡ฎ๐Ÿ‡น

    phpcs:ignore is applied to the next line, not to the line containing it.

  • Status changed to Needs review over 1 year ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Akram Khan Cuttack, Odisha

    added updated patch and address comment #14

  • Status changed to Needs work over 1 year ago
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly apaderno Brescia, ๐Ÿ‡ฎ๐Ÿ‡น
    +// phpcs:disable Drupal.NamingConventions.ValidFunctionName.InvalidPrefix
    +

    +// phpcs:ignore Drupal.NamingConventions.ValidFunctionName.InvalidPrefix

    Either phpcs:disable or phpcs:ignore is used. The first disables rules on all the lines following the line containing it until the line preceding phpcs:enable, the second disables rules only for the line following the line containing it. (That means phpcs:ignore has been also misplaced.)

  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands megachriz

    I think we'll need to go for the patch in #10 and call it a day. Because putting // phpcs:ignore Drupal.NamingConventions.ValidFunctionName.InvalidPrefix above the function results into an other coding standard violation.

  • ๐Ÿ‡ซ๐Ÿ‡ทFrance fgm Paris, France

    AIUI it needs to be at the end of the line you want to ignore, not above it.

  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands megachriz

    @fgm
    You are correct. I see that if I put the ignore on the line it should ignore that coding standard violation is indeed ignored. The issue is though that it results into two other coding standard violations.
    In examples I've seen the ignore line to be placed above the line it should ignore. That works too. But in this case that also results into an other coding standard violation: "Missing function doc comment".

    So because of these results I think we should disable the rule near the top of the file. Two lines above the function docblock is also a possibility - in that case the "phpcs:disable" line is between xmlrpc_help() and xmlrpc(). Do you like that better, @fgm?

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly apaderno Brescia, ๐Ÿ‡ฎ๐Ÿ‡น

    The examples given in the PHP_CodeSniffer documentation are clear.

    // phpcs:ignore
    $foo = [1,2,3];
    bar($foo, false);
    
    // phpcs:disable Generic.Commenting.Todo.Found
    $xmlPackage = new XMLPackage;
    $xmlPackage['error_code'] = get_default_error_code_value();
    // TODO: Add an error message here.
    $xmlPackage->send();
    // phpcs:enable

    It is not true the comment must be in the same line.

  • Status changed to Needs review over 1 year ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia chaitanyadessai Goa

    Patch added please review.

  • Status changed to Needs work over 1 year ago
  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands megachriz

    @chaitanyadessai
    Thanks for your contribution, but using $this->t() in tests is rarely needed. So the changes in the tests were already good in the previous two patches.
    The only thing were this issue hangs on is where to place the phpcs comment to ignore/disable a coding standard violation.

    Options considered:

    1. Near the top of the file, using phpcs::disable Drupal.NamingConventions.ValidFunctionName.InvalidPrefix, but that is less focused.
    2. Above the function docblock, using phpcs::disable Drupal.NamingConventions.ValidFunctionName.InvalidPrefix, still less focused.
    3. Above the function itself, using phpcs::ignore Drupal.NamingConventions.ValidFunctionName.InvalidPrefix, but that causes another coding standard violation: "Missing function doc comment".
    4. On the same line as the function, using phpcs::ignore Drupal.NamingConventions.ValidFunctionName.InvalidPrefix, but that causes two other coding standard violations.

    I would choose option 1. Options 3 and 4 are out of the question in my opinion as they create new problems.

  • Status changed to Needs review over 1 year ago
  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands megachriz

    Okay, looked at this again and I changed my mind. I think from the options listed at #25, option 2 is better. This is the closest focus with the function we can get. And ending with phpcs:enable, so if in the future code is added to the module it checks for the naming conventions again.

  • Status changed to RTBC over 1 year ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Anmol_Specbee

    Patch #26 seems to be working as expected and can be moved to RTBC.

  • Status changed to Fixed over 1 year ago
  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands megachriz

    @Anmol_Specbee
    Thanks for checking, committed #26.

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

Production build 0.71.5 2024