Fix the issues reported by phpcs

Created on 1 February 2023, over 1 year ago
Updated 3 August 2023, about 1 year ago

Problem/Motivation

Getting following error/warnings.

FILE: C:\xampp\htdocs\abc\drupal\modules\contextly\contextly.install
----------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
----------------------------------------------------------------------
7 | WARNING | @author tags are not usually used in Drupal, because
| | over time multiple contributors will touch the code
| | anyway
----------------------------------------------------------------------

FILE: ...\xampp\htdocs\abc\drupal\modules\contextly\contextly.routing.yml
----------------------------------------------------------------------
FOUND 0 ERRORS AND 2 WARNINGS AFFECTING 2 LINES
----------------------------------------------------------------------
7 | WARNING | The administration page callback should probably use
| | "administer site configuration" - which implies the
| | user can change something - rather than "access
| | administration pages" which is about viewing but not
| | changing configurations.
17 | WARNING | The administration page callback should probably use
| | "administer site configuration" - which implies the
| | user can change something - rather than "access
| | administration pages" which is about viewing but not
| | changing configurations.
----------------------------------------------------------------------

FILE: ...bc\drupal\modules\contextly\src\Contextly\ContextlyDrupalKit.php
----------------------------------------------------------------------
FOUND 4 ERRORS AND 1 WARNING AFFECTING 3 LINES
----------------------------------------------------------------------
157 | WARNING | t() calls should be avoided in classes, use
| | \Drupal\Core\StringTranslation\StringTranslationTrait
| | and $this->t() instead
269 | ERROR | Type hint "array" missing for $query
269 | ERROR | Type hint "array" missing for $data
269 | ERROR | Type hint "array" missing for $headers
401 | ERROR | Type hint "\ContextlyKitApiTokenInterface" missing
| | for $token
----------------------------------------------------------------------

FILE: ...htdocs\abc\drupal\modules\contextly\src\ContextlyBaseService.php
----------------------------------------------------------------------
FOUND 0 ERRORS AND 9 WARNINGS AFFECTING 9 LINES
----------------------------------------------------------------------
20 | WARNING | The class short comment should describe what the
| | class does and not simply repeat the class name
32 | WARNING | t() calls should be avoided in classes, use
| | \Drupal\Core\StringTranslation\StringTranslationTrait
| | and $this->t() instead
54 | WARNING | t() calls should be avoided in classes, use
| | \Drupal\Core\StringTranslation\StringTranslationTrait
| | and $this->t() instead
92 | WARNING | t() calls should be avoided in classes, use
| | \Drupal\Core\StringTranslation\StringTranslationTrait
| | and $this->t() instead
121 | WARNING | t() calls should be avoided in classes, use
| | \Drupal\Core\StringTranslation\StringTranslationTrait
| | and $this->t() instead
130 | WARNING | t() calls should be avoided in classes, use
| | \Drupal\Core\StringTranslation\StringTranslationTrait
| | and $this->t() instead
344 | WARNING | Unused variable $all.
346 | WARNING | NodeType::loadMultiple calls should be avoided in
| | classes, use dependency injection instead
480 | WARNING | \Drupal calls should be avoided in classes, use
| | dependency injection instead
----------------------------------------------------------------------

FILE: ...c\drupal\modules\contextly\src\ContextlyBaseServiceInterface.php
----------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
----------------------------------------------------------------------
11 | WARNING | The class short comment should describe what the
| | class does and not simply repeat the class name
----------------------------------------------------------------------

FILE: ...ocs\abc\drupal\modules\contextly\src\ContextlyLibraryService.php
----------------------------------------------------------------------
FOUND 0 ERRORS AND 2 WARNINGS AFFECTING 2 LINES
----------------------------------------------------------------------
11 | WARNING | The class short comment should describe what the
| | class does and not simply repeat the class name
26 | WARNING | Unused variable $js_path.
----------------------------------------------------------------------

FILE: ...mpp\htdocs\abc\drupal\modules\contextly\src\ContextlyWidgets.php
----------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
----------------------------------------------------------------------
9 | WARNING | The class short comment should describe what the class
| | does and not simply repeat the class name
----------------------------------------------------------------------

FILE: ...s\abc\drupal\modules\contextly\src\ContextlyWidgetsInterface.php
----------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
----------------------------------------------------------------------
7 | WARNING | The class short comment should describe what the class
| | does and not simply repeat the class name
----------------------------------------------------------------------

FILE: ...ontextly\src\Controller\ContextlyAjaxEditorRequestController.php
----------------------------------------------------------------------
FOUND 1 ERROR AND 1 WARNING AFFECTING 2 LINES
----------------------------------------------------------------------
11 | WARNING | The class short comment should describe what the
| | class does and not simply repeat the class name
24 | ERROR | The $_POST super global must not be accessed
| | directly; inject the request_stack service and use
| | $stack->getCurrentRequest()->request instead
----------------------------------------------------------------------

FILE: ...ocs\abc\drupal\modules\contextly\src\Form\ContextlyAdminForm.php
----------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
----------------------------------------------------------------------
13 | WARNING | The class short comment should describe what the
| | class does and not simply repeat the class name
----------------------------------------------------------------------

FILE: ...\abc\drupal\modules\contextly\src\Form\ContextlyAdminFormOld.php
----------------------------------------------------------------------
FOUND 1 ERROR AND 1 WARNING AFFECTING 2 LINES
----------------------------------------------------------------------
10 | WARNING | The class short comment should describe what the
| | class does and not simply repeat the class name
11 | ERROR | Class name doesn't match filename; expected "class
| | ContextlyAdminFormOld"
----------------------------------------------------------------------

FILE: ...bc\drupal\modules\contextly\src\Form\ContextlyImageAdminForm.php
----------------------------------------------------------------------
FOUND 0 ERRORS AND 4 WARNINGS AFFECTING 3 LINES
----------------------------------------------------------------------
13 | WARNING | The class short comment should describe what the
| | class does and not simply repeat the class name
65 | WARNING | NodeType::loadMultiple calls should be avoided in
| | classes, use dependency injection instead
91 | WARNING | Unused variable $node_types.
91 | WARNING | NodeType::loadMultiple calls should be avoided in
| | classes, use dependency injection instead
----------------------------------------------------------------------

FILE: ...rupal\modules\contextly\src\Form\ContextlyNodeTypesAdminForm.php
----------------------------------------------------------------------
FOUND 0 ERRORS AND 2 WARNINGS AFFECTING 2 LINES
----------------------------------------------------------------------
10 | WARNING | The class short comment should describe what the
| | class does and not simply repeat the class name
57 | WARNING | t() calls should be avoided in classes, use
| | \Drupal\Core\StringTranslation\StringTranslationTrait
| | and $this->t() instead
----------------------------------------------------------------------

FILE: ...abc\drupal\modules\contextly\src\Form\ContextlyTagsAdminForm.php
----------------------------------------------------------------------
FOUND 0 ERRORS AND 7 WARNINGS AFFECTING 6 LINES
----------------------------------------------------------------------
13 | WARNING | The class short comment should describe what the
| | class does and not simply repeat the class name
43 | WARNING | t() calls should be avoided in classes, use
| | \Drupal\Core\StringTranslation\StringTranslationTrait
| | and $this->t() instead
55 | WARNING | t() calls should be avoided in classes, use
| | \Drupal\Core\StringTranslation\StringTranslationTrait
| | and $this->t() instead
61 | WARNING | NodeType::loadMultiple calls should be avoided in
| | classes, use dependency injection instead
62 | WARNING | Unused variable $a.
96 | WARNING | Unused variable $node_types.
96 | WARNING | NodeType::loadMultiple calls should be avoided in
| | classes, use dependency injection instead
----------------------------------------------------------------------

FILE: ...\xampp\htdocs\abc\drupal\modules\contextly\src\MetaGenerator.php
----------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
----------------------------------------------------------------------
11 | WARNING | The class short comment should describe what the
| | class does and not simply repeat the class name
----------------------------------------------------------------------

FILE: ...docs\abc\drupal\modules\contextly\src\MetaGeneratorInterface.php
----------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
----------------------------------------------------------------------
9 | WARNING | The class short comment should describe what the class
| | does and not simply repeat the class name
----------------------------------------------------------------------

FILE: ...s\abc\drupal\modules\contextly\src\Routing\ConditionalRoutes.php
----------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
----------------------------------------------------------------------
10 | WARNING | @author tags are not usually used in Drupal, because
| | over time multiple contributors will touch the code
| | anyway
----------------------------------------------------------------------

Time: 1 secs; Memory: 14MB

Steps to reproduce

Run following command -

phpcs --standard=Drupal,DrupalPractice --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml modules/contrib/contextly/

Proposed resolution

Above error/warnings need to be fixed.

📌 Task
Status

Fixed

Version

2.0

Component

Code

Created by

🇮🇳India Charchil Khandelwal

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 @Charchil Khandelwal
  • @charchil-khandelwal opened merge request.
  • Issue was unassigned.
  • Status changed to Needs review over 1 year ago
  • 🇮🇳India Charchil Khandelwal

    Created MR for the above issue.
    Some warnings and errors are fixed now.
    Please review.

  • Status changed to Needs work over 1 year ago
  • FILE: ...bc\drupal\modules\contextly\src\Contextly\ContextlyDrupalKit.php
    ----------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    ----------------------------------------------------------------------
    401 | ERROR | Type hint "\ContextlyKitApiTokenInterface" missing for
    | | $token
    ----------------------------------------------------------------------

    FILE: ...htdocs\abc\drupal\modules\contextly\src\ContextlyBaseService.php
    ----------------------------------------------------------------------
    FOUND 0 ERRORS AND 2 WARNINGS AFFECTING 2 LINES
    ----------------------------------------------------------------------
    346 | WARNING | NodeType::loadMultiple calls should be avoided in
    | | classes, use dependency injection instead
    480 | WARNING | \Drupal calls should be avoided in classes, use
    | | dependency injection instead
    ----------------------------------------------------------------------

    FILE: ...ontextly\src\Controller\ContextlyAjaxEditorRequestController.php
    ----------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    ----------------------------------------------------------------------
    24 | ERROR | The $_POST super global must not be accessed directly;
    | | inject the request_stack service and use
    | | $stack->getCurrentRequest()->request instead
    ----------------------------------------------------------------------

    FILE: ...bc\drupal\modules\contextly\src\Form\ContextlyImageAdminForm.php
    ----------------------------------------------------------------------
    FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
    ----------------------------------------------------------------------
    65 | WARNING | NodeType::loadMultiple calls should be avoided in
    | | classes, use dependency injection instead
    ----------------------------------------------------------------------

    FILE: ...abc\drupal\modules\contextly\src\Form\ContextlyTagsAdminForm.php
    ----------------------------------------------------------------------
    FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
    ----------------------------------------------------------------------
    61 | WARNING | NodeType::loadMultiple calls should be avoided in
    | | classes, use dependency injection instead
    ----------------------------------------------------------------------

    Need to fix these issues.

  • Assigned to mukesh88
  • Issue was unassigned.
  • Status changed to Needs review over 1 year ago
  • Status changed to Needs work over 1 year ago
  • 🇮🇹Italy apaderno Brescia, 🇮🇹
       // @todo: remove if field is exist on admin form.
       const KIT_PATH = '/libraries/contextly-kit';

    That line must be changed to and phpcs should have reported it.

    -    $module_path = drupal_get_path('module', 'contextly');
    -    $js_path = $module_path . '/js/';
    -
    +    //$module_path = drupal_get_path('module', 'contextly');
    +    //$js_path = $module_path . '/js/';

    The code should be removed, if it is not necessary. Why has it been commented out? What error did phpcs report for those lines?

    +  /**
    +   * Constructs a ContextlyAjaxEditorRequestController object.
    +   *
    +   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
    +   *   The request stack.
    +   */
    +  public function __construct(RequestStack $request_stack) {

    The class namespace is missing.

    -      $params = $_POST;
    +      $params = $this->requestStack->getCurrentRequest()->query->all();

    query->all() is the equivalent of $_GET. It should be request->all().

    -    $node_types = NodeType::loadMultiple();
    +    // $node_types = NodeType::loadMultiple();

    Either the code is fixed or it is left as it is. We do not comment out the lines reported by phpcs.

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

    Providing patch with update.
    Please review.

  • Status changed to Needs work over 1 year ago
  • 🇮🇹Italy apaderno Brescia, 🇮🇹
       Contextly.editor = {
    -    isLoaded: false,
    -    isLoading: false,
    -    hasFailed: false,
    +    isLoaded: FALSE,
    +    isLoading: FALSE,
    +    hasFailed: FALSE,

    Those lines are already correct, since that is JavaScript code.
    Then, the report shown in the issue summary is not for JavaScript files.

    -  // @todo: remove if field is exist on admin form.
    +  use StringTranslationTrait;
    +
    +  // @todo remove if field is exist on admin form.
       const KIT_PATH = '/libraries/contextly-kit';

    What follows @todo is a sentence: It starts with a capitalized word and it ends with a period.

         if (empty($url)) {
    -      throw $this->kit->newDrupalException(t('Unable to generate URL for the node #@nid.', [
    +      throw $this->kit->newDrupalException($this->t('Unable to generate URL for the node #@nid.', [
             '@nid' => $node->id(),
           ]));

    An exception message is not translated.

    -    // TODO Take care about langcode.
    +    // @todo Take care about langcode.

    The correct phrase is take care of.

     /**
      * The contextly drupal assets list class.
      */
    -class ContextlyDrupalAssetsList extends ContextlyKitAssetsPackage {
    +class ContextlyDrupalAssetsList extends \ContextlyKitAssetsPackage {

    Those changes are not required from the phpcs report. They also seem wrong.

     /**
    - * Class ContextlyBaseService.
    + * Class provide functionality for ContextlyBaseService.
      */
     class ContextlyBaseService implements ContextlyBaseServiceInterface, ContainerAwareInterface {

    It is not necessary to start a short description with Class; rather, it should start with a verb.

     /**
    - * Interface ContextlyBaseServiceInterface.
    + * Provide Interface for ContextlyBaseServiceInterface.
      */
     interface ContextlyBaseServiceInterface {
    
     /**
    - * Class ContextlyLibraryService.
    + * Class provide functionality for ContextlyLibraryService.
      */
     class ContextlyLibraryService implements ContainerAwareInterface {
    

    No, an interface doesn't provide an interface for itself, nor do a class provide functionality for itself.

    -    // @todo: check $node why is in url.
    +    // @todo check $node why is in url.

    Leaving out that the comment does not make sense, what follows @todo is a sentence: It starts with a capitalized word and it ends with a period. url is not correctly spelled, since it is an acronym.

    +    $entityManager = $this->container->get('entity_type.manager');
    +    $node_types = $entityManager->getStorage('node_type')->loadMultiple();
    +    // $a = $config->get();

    Code is either fixed or removed; it is not commented out. That is not a change coding standards require.

     /**
      * Description of ConditionalRoutes.
      *
    - * @author dj
    + * Author dj.
      */
     class ConditionalRoutes {
    

    Actually, the existing line is correct. @author is a tag and it spelled like that. It is Drupal that does not use that tag, but in that case it should be removed.
    If anything needs to be changed, that is the short description.

  • Status changed to Needs review over 1 year ago
  • 🇮🇳India TanujJain-TJ

    Adding a new patch addressing points from #11, please review.

  • Status changed to Fixed about 1 year ago
Production build 0.71.5 2024