Fix the errors/warnings reported by PHP_CodeSniffer

Created on 28 December 2023, 6 months ago
Updated 23 January 2024, 5 months ago

Problem/Motivation

Running the PHPCS test displays the following warnings and errors:

FILE: date_range_formatter/date_range_formatter.install
--------------------------------------------------------------------------------------------------------------------------------------
FOUND 5 ERRORS AFFECTING 4 LINES
--------------------------------------------------------------------------------------------------------------------------------------
  1 | ERROR | [x] Missing file doc comment
  4 | ERROR | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\Core\Entity\Entity\EntityViewDisplay.
  4 | ERROR | [x] There must be one blank line after the last USE statement; 0 found;
 11 | ERROR | [ ] Doc comment short description must be on a single line, further text should be a separate paragraph
 13 | ERROR | [x] Expected 1 blank line before function; 0 found
--------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 4 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------------------------------------------------------------


FILE: date_range_formatter/date_range_formatter.module
--------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
--------------------------------------------------------------------------------------------------------------------
 1 | ERROR | [x] Missing file doc comment
--------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------------------------------------------


FILE: date_range_formatter.info.yml
----------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
----------------------------------------------------------------------------------------------------------------------
 7 | WARNING | All dependencies must be prefixed with the project name, for example "drupal:"
----------------------------------------------------------------------------------------------------------------------


FILE: date_range_formatter/src/Plugin/Field/FieldFormatter/DateRangeFormatterRangeFormatter.php
-------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 10 ERRORS AND 3 WARNINGS AFFECTING 13 LINES
-------------------------------------------------------------------------------------------------------------------------------------------------------------
  76 | WARNING | Only string literals should be passed to t() where possible
  87 | WARNING | Line exceeds 80 characters; contains 86 characters
  91 | WARNING | Only string literals should be passed to t() where possible
 172 | ERROR   | There must be no space between ? and :
 177 | ERROR   | There must be no space between ? and :
 182 | ERROR   | There must be no space between ? and :
 187 | ERROR   | There must be no space between ? and :
 192 | ERROR   | There must be no space between ? and :
 197 | ERROR   | There must be no space between ? and :
 216 | ERROR   | There must be no space between ? and :
 217 | ERROR   | There must be no space between ? and :
 218 | ERROR   | There must be no space between ? and :
 219 | ERROR   | There must be no space between ? and :
-------------------------------------------------------------------------------------------------------------------------------------------------------------

Time: 114ms; Memory: 12MB

Steps to reproduce

Run following command:

phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml ./
šŸ“Œ Task
Status

RTBC

Version

9.0

Component

Code

Created by

šŸ‡ŗšŸ‡¦Ukraine taraskorpach Lutsk

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

  • Issue created by @taraskorpach
  • Merge request !6Fix PHPCS warnings and errors ā†’ (Merged) created by taraskorpach
  • Issue was unassigned.
  • Status changed to Needs review 6 months ago
  • šŸ‡ŗšŸ‡¦Ukraine taraskorpach Lutsk

    I've created a MR.

    FYI, I've added a phpcs.xml file to exclude warnings from the Drupal.Semantics.FunctionT.NotLiteralString rule.
    If you have any suggestions for appropriately fixing these warnings, please do not hesitate to share them.

    The issue needs review.

  • Status changed to Needs work 6 months ago
  • šŸ‡µšŸ‡­Philippines clarkssquared

    Hi

    I applied the MR !6 and it fixes most of the PHPCS issues but there is still a PHPCS errors

    āžœ  date_range_formatter git:(9.0.x) curl https://git.drupalcode.org/project/date_range_formatter/-/merge_requests/6.diff | patch -p1
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100  7372    0  7372    0     0  14690      0 --:--:-- --:--:-- --:--:-- 14892
    patching file date_range_formatter.install
    patching file date_range_formatter.module
    patching file phpcs.xml
    patching file 'src/Plugin/Field/FieldFormatter/DateRangeFormatterRangeFormatter.php'
    āžœ  date_range_formatter git:(9.0.x) āœ— ..
    āžœ  contrib git:(master) āœ— phpcs --standard=Drupal,DrupalPractice --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml date_range_formatter 
    
    FILE: /Users/clarksubing-subing/Projects/d9/d9-local/web/modules/contrib/date_range_formatter/date_range_formatter.info.yml
    ---------------------------------------------------------------------------------------------------------------------------
    FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
    ---------------------------------------------------------------------------------------------------------------------------
     7 | WARNING | All dependencies must be prefixed with the project name, for example "drupal:"
    ---------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: ...ubing-subing/Projects/d9/d9-local/web/modules/contrib/date_range_formatter/src/Plugin/Field/FieldFormatter/DateRangeFormatterRangeFormatter.php
    -----------------------------------------------------------------------------------------------------------------------------------------------------
    FOUND 0 ERRORS AND 2 WARNINGS AFFECTING 2 LINES
    -----------------------------------------------------------------------------------------------------------------------------------------------------
     70 | WARNING | Only string literals should be passed to t() where possible
     84 | WARNING | Only string literals should be passed to t() where possible
    -----------------------------------------------------------------------------------------------------------------------------------------------------
    
    Time: 272ms; Memory: 10MB
    
    āžœ  contrib git:(master) āœ— 
    
  • Status changed to Needs review 6 months ago
  • šŸ‡ŗšŸ‡¦Ukraine taraskorpach Lutsk

    I've fixed the module dependency warning and updated the MR.

    Regarding the translation warnings I mentioned above, I've added a phpcs.xml file. This means you need to run the phpcs command directly in the module folder to allow phpcs to take into account all the provided exclusions.

  • Status changed to Needs work 6 months 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.

  • Assigned to nitin_lama
  • Issue was unassigned.
  • Status changed to Needs review 6 months ago
  • šŸ‡ŗšŸ‡¦Ukraine taraskorpach Lutsk

    I don't believe that removing translation functions is the best approach here. Someone may already have translated some strings on their sites, and removing these functions could probably cause some issues. This issue requires further consideration.

    BTW, I'm currently running the PHPCS command in the project folder, and it isn't showing any issues. Could someone else try running the command?

  • šŸ‡ŗšŸ‡¦Ukraine taraskorpach Lutsk

    I am modifying some arguments in the PHPCS command to utilize the appropriate phpcs.xml file during command execution.

    Another method to bypass these errors is to use the ignore command, as in Drupal.Semantics.FunctionT.NotLiteralString.

    However, creating a phpcs.xml file seems like a more suitable approach in this context.

  • šŸ‡®šŸ‡¹Italy apaderno Brescia, šŸ‡®šŸ‡¹

    $this->t() is not the correct way to translate configuration object values. For that, there is a specific API that needs to be used, which is also documented on drupal.org.

  • šŸ‡ŗšŸ‡¦Ukraine taraskorpach Lutsk

    Yeah, but these settings are part of a formatter field form.

    As far as I know, we are unable to translate them because the issue šŸ› Entity view/form mode formatter/widget settings have no translation UI Needs review is still in progress.

  • šŸ‡®šŸ‡¹Italy apaderno Brescia, šŸ‡®šŸ‡¹

    If that is the only way to get translated strings in that specific context, I would leave the code as it is. I think it is more important to get translated strings than a PHP_CodeSniffer report without errors/warnings.
    Once šŸ› Entity view/form mode formatter/widget settings have no translation UI Needs review get fixed, the code can be changed.

  • šŸ‡ŗšŸ‡¦Ukraine taraskorpach Lutsk

    Absolutely. Because of that, I've just added a sniffer rule to avoid unnecessary warnings. We just need a review rn.

  • Status changed to Needs work 5 months ago
  • šŸ‡·šŸ‡ŗRussia zniki.ru

    Thanks for MR, please check my feedback.

    There are more changes in the MR, then we have in the phpcs report in the issue.
    I don't want to fix issues that is reported by phpcs in this issue.
    Can you please update report in the IS?

  • Assigned to taraskorpach
  • Status changed to Active 5 months ago
  • šŸ‡ŗšŸ‡¦Ukraine taraskorpach Lutsk

    I'll update the MR according to your feedback. Thanks

  • Issue was unassigned.
  • Status changed to Needs review 5 months ago
  • šŸ‡ŗšŸ‡¦Ukraine taraskorpach Lutsk

    All threads seem to have been resolved. @nikolay-shapovalov, could you take a look at this?

  • Status changed to Needs work 5 months ago
  • šŸ‡·šŸ‡ŗRussia zniki.ru

    Taras, thanks a lot, changes looks good. Please check my question.

    Can you please update IS, and provide fresh phpcs report?

  • Assigned to taraskorpach
  • Status changed to Active 5 months ago
  • šŸ‡ŗšŸ‡¦Ukraine taraskorpach Lutsk

    Yep, I'll do this soon

  • Issue was unassigned.
  • Status changed to Needs review 5 months ago
  • šŸ‡ŗšŸ‡¦Ukraine taraskorpach Lutsk

    I'm returning the entire PHPCS log to the IS as @nikolay-shapovalov has mentioned above.

    Btw, the .module file has been removed.

  • šŸ‡ŗšŸ‡¦Ukraine taraskorpach Lutsk

    Removing the PATCHES.txt file from the log

  • Status changed to RTBC 5 months ago
  • šŸ‡·šŸ‡ŗRussia zniki.ru

    Great work, thanks. RTBC.

Production build 0.69.0 2024