Fix the issues reported by PHP_CodeSniffer

Created on 22 November 2023, about 1 year ago
Updated 15 August 2024, 6 months ago

Problem/Motivation

Resolved these coding standard issues. This report is generated using the following command :

vendor/bin/phpcs--standard=Drupal,DrupalPractice --extensions=php,module,inc,install,test,profile,theme,info,txt,md,yml unix_time_conversion
FILE: unix_time_conversion/unix_time_conversion.module
-----------------------------------------------------------------------------------------------------------------------
FOUND 4 ERRORS AFFECTING 4 LINES
-----------------------------------------------------------------------------------------------------------------------
  1 | ERROR | [x] End of line character is invalid; expected "\n" but found "\r\n"
  8 | ERROR | [x] There must be one blank line after the last USE statement; 2 found;
 14 | ERROR | [x] Expected 1 blank line before function; 2 found
 19 | ERROR | [x] Concat operator must be surrounded by a single space
-----------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 4 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-----------------------------------------------------------------------------------------------------------------------

FILE: unix_time_conversion/unix_time_conversion.helper_functions.inc
-------------------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
-------------------------------------------------------------------------------------------------------------------------------------
 3 | ERROR | [x] You must use "/**" style comments for a file comment
-------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-------------------------------------------------------------------------------------------------------------------------------------

FILE: unix_time_conversion/src/Form/UnixTimeConversionSettings.php
-----------------------------------------------------------------------------------------------------------------------------------
FOUND 3 ERRORS AND 6 WARNINGS AFFECTING 8 LINES
-----------------------------------------------------------------------------------------------------------------------------------
  14 | WARNING | [ ] The class short comment should describe what the class does and not simply repeat the class name
  22 | WARNING | [x] A comma should follow the last multiline array item. Found: 'unix_time_conversion.settings'
  29 | ERROR   | [ ] Public method name "UnixTimeConversionSettings::getFormID" is not in lowerCamel format
  39 | WARNING | [ ] \Drupal calls should be avoided in classes, use dependency injection instead
  78 | ERROR   | [x] Array closing indentation error, expected 6 spaces but found 10
  84 | WARNING | [x] There must be no blank line following an inline comment
  84 | WARNING | [ ] There must be no blank line following an inline comment
 124 | WARNING | [ ] \Drupal calls should be avoided in classes, use dependency injection instead
 125 | ERROR   | [x] Object operator not indented correctly; expected 6 spaces but found 8
-----------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 4 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-----------------------------------------------------------------------------------------------------------------------------------

FILE: unix_time_conversion/src/Form/DateToUnixTimestampBlockForm.php
---------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 3 ERRORS AND 6 WARNINGS AFFECTING 8 LINES
---------------------------------------------------------------------------------------------------------------------------------------------------------
   7 | WARNING | [x] Unused use statement
   8 | WARNING | [x] Unused use statement
   8 | ERROR   | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\Component\Utility\UrlHelper.
  10 | WARNING | [x] Unused use statement
  11 | WARNING | [x] Unused use statement
  13 | WARNING | [x] Unused use statement
  14 | WARNING | [x] Unused use statement
  93 | ERROR   | [x] Missing function doc comment
 101 | ERROR   | [x] No space found before comment text; expected "// $date = explode('-',$date_value);" but found "//$date = explode('-',$date_value);"
---------------------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 9 MARKED SNIFF VIOLATIONS AUTOMATICALLY
---------------------------------------------------------------------------------------------------------------------------------------------------------

FILE: unix_time_conversion/src/Form/UnixTimestampToDateBlockForm.php
-------------------------------------------------------------------------------------------------------------------------------------
FOUND 3 ERRORS AND 6 WARNINGS AFFECTING 8 LINES
-------------------------------------------------------------------------------------------------------------------------------------
  1 | ERROR   | [x] End of line character is invalid; expected "\n" but found "\r\n"
  7 | WARNING | [x] Unused use statement
  8 | WARNING | [x] Unused use statement
  8 | ERROR   | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\Component\Utility\UrlHelper.
 10 | WARNING | [x] Unused use statement
 11 | WARNING | [x] Unused use statement
 13 | WARNING | [x] Unused use statement
 14 | WARNING | [x] Unused use statement
 86 | ERROR   | [x] Missing function doc comment
-------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 9 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-------------------------------------------------------------------------------------------------------------------------------------

FILE: unix_time_conversion/src/Element/TimeElement.php
-----------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AND 1 WARNING AFFECTING 1 LINE
-----------------------------------------------------------------------------------------------------------------------
 7 | WARNING | [x] Unused use statement
 7 | ERROR   | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\Core\Render\Element.
-----------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-----------------------------------------------------------------------------------------------------------------------

FILE: unix_time_conversion/src/Plugin/Block/UnixTimestampToDateBlock.php
-----------------------------------------------------------------------------------------------------------------------------------------
FOUND 2 ERRORS AND 1 WARNING AFFECTING 3 LINES
-----------------------------------------------------------------------------------------------------------------------------------------
  1 | ERROR   | [x] End of line character is invalid; expected "\n" but found "\r\n"
  7 | ERROR   | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\Core\Block\BlockBase.
 30 | WARNING | [ ] \Drupal calls should be avoided in classes, use dependency injection instead
-----------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-----------------------------------------------------------------------------------------------------------------------------------------

FILE: unix_time_conversion/src/Plugin/Block/DateToUnixTimestampBlock.php
-----------------------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AND 1 WARNING AFFECTING 2 LINES
-----------------------------------------------------------------------------------------------------------------------------------------
  7 | ERROR   | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\Core\Block\BlockBase.
 30 | WARNING | [ ] \Drupal calls should be avoided in classes, use dependency injection instead
-----------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-----------------------------------------------------------------------------------------------------------------------------------------

Steps to reproduce

1. Run PHP coding standards to check the VIOLATIONS

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

šŸ“Œ Task
Status

Needs work

Version

10.0

Component

Code

Created by

šŸ‡®šŸ‡³India adwivedi008

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

Merge Requests

Comments & Activities

  • Issue created by @adwivedi008
  • šŸ‡®šŸ‡³India adwivedi008

    Resolved above mentioned issues

    Please approve

  • Status changed to Needs work about 1 year ago
  • šŸ‡®šŸ‡¹Italy apaderno Brescia, šŸ‡®šŸ‡¹

    A summary must describe what needs to be changed, and eventually why. A screenshot is not sufficient, even in the case a patch/MR is provided.
    In the case of bug reports, the steps to reproduce the bug should be provided too. In the case of coding standards issues found using an automatic tool, the report given by that tool must be quoted too, including the arguments passed to that tool, if it is a command line tool.

  • šŸ‡®šŸ‡¹Italy apaderno Brescia, šŸ‡®šŸ‡¹
  • Status changed to Needs review about 1 year ago
  • šŸ‡®šŸ‡³India adwivedi008

    @apanderno Hi, I have updated the issue summary steps to reproduce and also the command using which the report is generated.
    Moving the issue status to needs review
    Please review.

  • Status changed to Needs work about 1 year ago
  • šŸ‡®šŸ‡¹Italy apaderno Brescia, šŸ‡®šŸ‡¹
    +  /**
    +   * Function to Calculate Time.
    +   */
       public function calculateTime(&$form, FormStateInterface $form_state) {
    

    A method description must not start with Function. (This holds true for functions too.)
    Given the method parameters, that method seems a from handler/callback, which then should have a different description, basing on exactly what that method is.

     /**
    - * Class UnixTimeConversionSettings.
    + * Class Unix Time Conversion Settings.
      *
      * @package Drupal\unix_time_conversion\Form\UnixTimeConversionSettings
      */
     class UnixTimeConversionSettings extends ConfigFormBase {
     

    That description is still repeating the class name, not describing what the class does. Class descriptions must not start with Class. (The code makes already clear it is a class.)

    +  /**
    +   * The config factory service.
    +   *
    +   * @var \Drupal\Core\Config\ConfigFactoryInterface
    +   */
    +  protected $configFactory;

    The config factory. is sufficient (and what Drupal core uses as description.)

    +  /**
    +   * Constructs a new UnixTimeConversionSettings object.
    +   *
    +   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
    +   *   The config factory service.
    +   */
    +  public function __construct(ConfigFactoryInterface $config_factory) {

    The documentation comment for constructors is not mandatory anymore, If it is given, the description must be Constructs a new [class name] object. where [class name] includes the class namespace.

    -    // Form constructor.
    -    $form = parent::buildForm($form, $form_state);
    -    $config = \Drupal::configFactory()->getEditable('unix_time_conversion.settings');
    +    $config = $this->configFactory->getEditable('unix_time_conversion.settings');
         // Include the helper functions file.
         module_load_include('inc', 'unix_time_conversion', 'unix_time_conversion.helper_functions');
         $form = [];

    Calling parent::buildForm() is correct. What it is not correct is setting $form to an empty array after that. That is a bug, though, not something PHP_CodeSniffer reported; this issue should not remove neither of those lines.

    +    $this->configFactory->getEditable('unix_time_conversion.settings')
    +      ->set('unix_time_conversion_timestamp_field_title', $form_state->getValue('unix_time_conversion_timestamp_field_title'))
    +      ->set('unix_time_conversion_timestamp_field_description', $form_state->getValue('unix_time_conversion_timestamp_field_description'))
    +      ->set('unix_time_conversion_time_to_date_output_format', $form_state->getValue('unix_time_conversion_time_to_date_output_format'))
    +      ->set('unix_time_conversion_date_field_title', $form_state->getValue('unix_time_conversion_date_field_title'))
    +      ->set('unix_time_conversion_time_field_title', $form_state->getValue('unix_time_conversion_time_field_title'))
    +      ->save();

    The correct code calls $this->config('unix_time_conversion.settings').

    +/**
    + * Class UnixTimeConversionForm.
    + *
    + * @package Drupal\unix_time_conversion\Form\UnixTimeConversionForm
    + */
    +class UnixTimestampToDateBlockForm extends FormBase {

    The description is merely repeating the class name.

    +  /**
    +   * {@inheritdoc}
    +   */
    +  public function validateForm(array &$form, FormStateInterface $form_state) {
    +
    +  }
    +
    +  /**
    +   * {@inheritdoc}
    +   */
    +  public function submitForm(array &$form, FormStateInterface $form_state) {
    +
    +  }

    The second method is not necessary, if it does not contain code, as the parent class already defines it.
    The first method is not necessary, if it does not contain code. Other classes that extend ConfigFormBase do not define it. (See AccountSettingsForm as example.)

    +  /**
    +   * The form builder service.
    +   *
    +   * @var \Drupal\Core\Form\FormBuilderInterface
    +   */
    +  protected $formBuilder;

    The form builder. is sufficient.

    +/**
    + * @file
      * To change this license header, choose License Headers in Project Properties.
    + *
      * To change this template file, choose Tools | Templates
      * and open the template in the editor.
      */

    Since that comment is edited, it should be corrected: It must describe what that file contains. Those are instructions that are true for a specific editor or IDE.

    +/**
    + * @file
    + * Defines necessary menu's, Permissions, age-calculator form & ajax callbacks.
    + */

    The usual description for a .module file is Hook implementations for the [module name] module. where [module name] is the name of the module given in its .info.yml file.

  • Assigned to pray_12
  • šŸ‡®šŸ‡³India pray_12

    Hi,
    The above patch #8 does not address #7 comments and has many errors and warnings.
    I have addressed #7 comments using patch #2 .
    Added both the interdiff file and updated patch file.

  • Issue was unassigned.
  • Status changed to Needs review 12 months ago
  • Hi,
    Applied patch #10, the patch applies cleanly and there are no phpcs errors found.

  • Status changed to RTBC 6 months ago
  • Hi @everyone,

    Applied comment #11's patch, confirmed it fixed all reported errors/warnings and it addressed the feedback of comment #7.

    āžœ  unix_time_conversion git:(main) āœ— curl https://www.drupal.org/files/issues/2024-02-09/3403470-10.patch | patch -p1
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100 22400  100 22400    0     0  39251      0 --:--:-- --:--:-- --:--:-- 40000
    patching file src/Element/TimeElement.php
    patching file src/Form/DateToUnixTimestampBlockForm.php
    patching file src/Form/UnixTimeConversionSettings.php
    patching file src/Form/UnixTimestampToDateBlockForm.php
    patching file src/Plugin/Block/DateToUnixTimestampBlock.php
    patching file src/Plugin/Block/UnixTimestampToDateBlock.php
    patching file unix_time_conversion.helper_functions.inc
    patching file unix_time_conversion.module
    āžœ  unix_time_conversion git:(main) āœ— ..
    āžœ  contrib git:(main) āœ— phpcs --standard=Drupal,DrupalPractice --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml,twig unix_time_conversion
    āžœ  contrib git:(main) āœ—

    Will now move this to RTBC

    Thanks,
    Jake

  • Status changed to Needs work 6 months ago
  • šŸ‡®šŸ‡¹Italy apaderno Brescia, šŸ‡®šŸ‡¹

    Let's create a merge request, now that patches are no longer tested.

  • Pipeline finished with Success
    6 months ago
    Total: 157s
    #255088
Production build 0.71.5 2024