Fix the warnings/errors reported by PHP_CodeSniffer

Created on 22 June 2023, over 1 year ago
Updated 2 January 2024, 11 months ago

Problem/Motivation

Issue found related to PHP_CodeSniffer in this module.

FILE: /Users/ashutoshahirwal/Sites/lando/contribution/web/modules/custom/ekam/ekam.theme
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 59 ERRORS AND 4 WARNINGS AFFECTING 40 LINES
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 | ERROR | [x] Missing file doc comment
3 | WARNING | [x] Unused use statement
4 | WARNING | [x] Unused use statement
10 | ERROR | [x] There should be no white space after an opening "{"
10 | ERROR | [x] Comments may not appear after statements
28 | ERROR | [x] Expected 1 space after "="; 2 found
28 | ERROR | [x] Whitespace found at end of line
29 | ERROR | [x] Expected one space after the comma, 0 found
29 | ERROR | [x] Expected one space after the comma, 0 found
31 | ERROR | [x] Concat operator must be surrounded by a single space
62 | ERROR | [x] Short array syntax must be used to define arrays
111 | ERROR | [ ] Missing short description in doc comment
112 | ERROR | [ ] Missing parameter comment
112 | ERROR | [ ] Missing parameter type
113 | ERROR | [ ] Missing parameter comment
116 | ERROR | [x] Expected 1 space after IF keyword; 0 found
116 | ERROR | [x] Expected 1 space after closing parenthesis; found 0
117 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 2
121 | ERROR | [ ] Missing short description in doc comment
122 | ERROR | [ ] Missing parameter comment
125 | ERROR | [x] Expected 1 space after IF keyword; 0 found
125 | ERROR | [x] Expected 1 space after closing parenthesis; found 0
130 | ERROR | [ ] Missing short description in doc comment
131 | ERROR | [ ] Missing parameter comment
131 | ERROR | [ ] Missing parameter type
132 | ERROR | [ ] Missing parameter comment
152 | WARNING | [x] A comma should follow the last multiline array item. Found: )
165 | WARNING | [x] A comma should follow the last multiline array item. Found: field
170 | ERROR | [x] Doc comment short description must end with a full stop
172 | ERROR | [x] Expected 1 space before opening brace; found 0
173 | ERROR | [x] No space found before comment text; expected "// need to clean this up a bit at some point.." but found "//need to clean this up a bit at some point.."
173 | ERROR | [x] Inline comments must start with a capital letter
174 | ERROR | [x] No space found before comment text; expected "// get the url because" but found "//get the url because"
175 | ERROR | [x] No space found before comment text; expected "// $url = \Drupal::service('path.current')->getPath();" but found "//$url = \Drupal::service('path.current')->getPath();"
177 | ERROR | [x] No space found before comment text; expected "// clean whitespace" but found "//clean whitespace"
177 | ERROR | [x] Inline comments must start with a capital letter
177 | ERROR | [x] Inline comments must end in full-stops, exclamation marks, question marks, colons, or closing parentheses
179 | ERROR | [x] No space found before comment text; expected "// if there is a question mark" but found "//if there is a question mark"
179 | ERROR | [x] Inline comments must start with a capital letter
179 | ERROR | [x] Inline comments must end in full-stops, exclamation marks, question marks, colons, or closing parentheses
181 | ERROR | [x] No space found before comment text; expected "// explode into an array" but found "//explode into an array"
181 | ERROR | [x] Inline comments must start with a capital letter
181 | ERROR | [x] Inline comments must end in full-stops, exclamation marks, question marks, colons, or closing parentheses
183 | ERROR | [x] No space found before comment text; expected "// adding the different paths to a long unique id" but found "//adding the different paths to a long unique id"
183 | ERROR | [x] Inline comments must start with a capital letter
183 | ERROR | [x] Inline comments must end in full-stops, exclamation marks, question marks, colons, or closing parentheses
185 | ERROR | [x] Expected 1 space after IF keyword; 0 found
185 | ERROR | [x] Expected 1 space after closing parenthesis; found 0
186 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 6
187 | ERROR | [x] Expected newline after closing brace
187 | ERROR | [x] Expected 1 space after ELSEIF keyword; 0 found
187 | ERROR | [x] Expected 1 space after closing parenthesis; found 0
188 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 6
190 | ERROR | [x] No space found before comment text; expected "// if there is no index or url." but found "//if there is no index or url."
190 | ERROR | [x] Inline comments must start with a capital letter
191 | ERROR | [x] Expected 1 space after IF keyword; 0 found
191 | ERROR | [x] Expected 1 space after closing parenthesis; found 0
192 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 6
194 | ERROR | [x] No space found before comment text; expected "// this is here because the id search is used on the search page." but found "//this is here because the id search is used on the search
| | page."
194 | ERROR | [x] Inline comments must start with a capital letter
195 | ERROR | [x] Expected 1 space after IF keyword; 0 found
195 | ERROR | [x] Expected 1 space after closing parenthesis; found 0
196 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 6
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 53 MARKED SNIFF VIOLATIONS AUTOMATICALLY
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Time: 375ms; Memory: 14MB

Steps to reproduce

Use the below command with arguments.

phpcs --standard=Drupal,DrupalPractice --extensions=php,module,inc,install,test,profile,theme,info,txt,md,css,js,yml web/modules/custom/ekam

Proposed resolution

Fix the issue reported by phpcs.

📌 Task
Status

Needs work

Version

8.2

Component

Code

Created by

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

Comments & Activities

  • Issue created by @sonam_sharma
  • Issue was unassigned.
  • 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 and arguments have been used and which report that command shown. In this way, project maintainers can verify the patch/MR fixes all the warnings/errors.

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

    I have updated issue summary.

  • 🇮🇹Italy apaderno Brescia, 🇮🇹
  • Status changed to RTBC over 1 year ago
  • 🇵🇭Philippines paraderojether

    Hi

    I reviewed patch #2, applied against Ekam - Blogging, News Drupal theme 8.0.x-dev, and confirmed it fixes the issues reported by phpcs.

    I added screenshots for reference.
    Thank you.

  • Status changed to Needs work over 1 year ago
  • 🇮🇹Italy apaderno Brescia, 🇮🇹
    --- a/ekam.theme
    +++ b/ekam.theme
    @@ -1,13 +1,16 @@
     <?php
     
    -use Drupal\Core\Form\FormStateInterface;
    -use Drupal\Core\Template\Attribute;
    +/**
    + * @file
    + * Functions to support theming in the pachu theme.
    + */

    The theme name reported in the ekam.info.yml file is Ekam - Bloging, news Drupal theme. Since that seems more a description, I would use Ekam as theme name.
    The used description is for the pachu theme, which is not this theme.

    -  if ($hook == 'menu__main') { // We're doing that for main menu.
    +  if ($hook == 'menu__main') {
    +    // We're doing that for main menu.
         // Get the current path.
         $current_path = \Drupal::request()->getRequestUri();

    Those comments are not necessary, as they describe what is already clear from the code. There is no need to say the code is getting the current path when it is setting the $current_path variable, nor to say the code is for the main menu when there is $hook == 'menu_main'.

    -  $pt =  \Drupal::hasService('extension.path.resolver') ? 
    -  \Drupal::service('extension.path.resolver')->getPath('theme','ekam') : drupal_get_path('theme','ekam');
    +  $pt = \Drupal::hasService('extension.path.resolver') ?
    +  \Drupal::service('extension.path.resolver')->getPath('theme', 'ekam') : drupal_get_path('theme', 'ekam');
    

    I would leave the code as it is, since code lines are allowed to exceed 80 characters, if they are easier to understand.
    If the code is going to be split in two lines, the second line must be indented, or it seems an independent line, not a continuation of the previous line.

     /**
    - * @param $suggestions
    - * @param array $variables
    + * Param $suggestions, Param array $variables.
      */
    

    Parameters are described with @param, not Param. That is not the comment used for hook implementations, though.

     /**
    - * @param array $variables
    + * Param array $variables.
      */
     function ekam_preprocess_form_element(&$variables) {
    

    The documentation comment should be similar to the following one, used for claro_preprocess_menu_local_action().

    /**
     * Implements hook_preprocess_HOOK() for menu-local-action templates.
     */
    
     /**
    - * @param $suggestions
    - * @param array $variables
    + * Param $suggestions, Param array $variables.
      */
     function ekam_theme_suggestions_views_view_alter(array &$suggestions, array $variables) {
    

    It is the same wrong change already reported.

     /**
    - * This function generates the body id for the preprocess_html function
    + * This function generates the body id for the preprocess_html function.
      */
    

    The @return line is missing.
    the preprocess_html function is a wrong description, since that is either a hook or a template function. There is not any preprocess_html() function.

    +  // Get the url because.
    +  // $url = \Drupal::service('path.current')->getPath();
       $url = trim($_SERVER['REQUEST_URI']);
    -  //clean whitespace
    +  // Clean whitespace.
       $url = str_replace(" ", "", $url);
    -  //if there is a question mark
    +  // If there is a question mark.
       $url = str_replace("?", "/", $url);
    -  //explode into an array
    +  // Explode into an array.
       $url_array = explode('/', $url);
    -  //adding the different paths to a long unique id
    +  // Adding the different paths to a long unique id.

    Except for Need to clean this up a bit at some point.. which should be @todo Clean this up., the other comments are not necessary.

    +  // This is here because the id search is used on the search page.
    +  if ($body_id == "search") {
    +    $body_id = "search-page";
       }

    That comment can be removed.

    As a side note, the patch/MR should be created for the 8.2.x branch.

Production build 0.71.5 2024