Fix the issues reported by phpcs

Created on 7 July 2023, over 1 year ago
Updated 10 July 2023, over 1 year ago

Problem/Motivation

I'm getting the following errors upon running codesniffer on the module:

C:\xampp\htdocs\backendassignment\web\modules\contrib>phpcs --standard=Drupal,DrupalPractice --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml,twig computed_breadcrumbs

FILE: C:\xampp\htdocs\backendassignment\web\modules\contrib\computed_breadcrumbs\computed_breadcrumbs.module
------------------------------------------------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 1 LINE
------------------------------------------------------------------------------------------------------------
 1 | ERROR | [x] Missing file doc comment
 1 | ERROR | [x] End of line character is invalid; expected "\n" but found "\r\n"
------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
------------------------------------------------------------------------------------------------------------


FILE: C:\xampp\htdocs\backendassignment\web\modules\contrib\computed_breadcrumbs\src\Controller\BreadcrumbsExtractor.php
------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
------------------------------------------------------------------------------------------------------------------------
 1 | ERROR | [x] End of line character is invalid; expected "\n" but found "\r\n"
------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
------------------------------------------------------------------------------------------------------------------------


FILE: C:\xampp\htdocs\backendassignment\web\modules\contrib\computed_breadcrumbs\src\EventListener.php
------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
------------------------------------------------------------------------------------------------------
 1 | ERROR | [x] End of line character is invalid; expected "\n" but found "\r\n"
------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
------------------------------------------------------------------------------------------------------


FILE: C:\xampp\htdocs\backendassignment\web\modules\contrib\computed_breadcrumbs\src\Field\ComputedBreadcrumbsItemList.php
--------------------------------------------------------------------------------------------------------------------------
FOUND 3 ERRORS AND 3 WARNINGS AFFECTING 6 LINES
--------------------------------------------------------------------------------------------------------------------------
  1 | ERROR   | [x] End of line character is invalid; expected "\n" but found "\r\n"
 12 | ERROR   | [x] Missing class doc comment
 16 | ERROR   | [ ] Missing short description in doc comment
 48 | WARNING | [x] 'TODO:' should match the format '@todo Fix problem X here.'
 62 | WARNING | [ ] Unused variable $index.
 69 | WARNING | [x] A comma should follow the last multiline array item. Found: )
--------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 4 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------------------------------------------------


FILE: C:\xampp\htdocs\backendassignment\web\modules\contrib\computed_breadcrumbs\src\Plugin\Field\FieldType\ComputedBreadcrumbsLinkItem.php
-------------------------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AND 1 WARNING AFFECTING 2 LINES
-------------------------------------------------------------------------------------------------------------------------------------------
  1 | ERROR   | [x] End of line character is invalid; expected "\n" but found "\r\n"
 16 | WARNING | [ ] Line exceeds 80 characters; contains 117 characters
-------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-------------------------------------------------------------------------------------------------------------------------------------------


FILE: C:\xampp\htdocs\backendassignment\web\modules\contrib\computed_breadcrumbs\src\Routing\BreadcrumbsResponse.php
--------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
--------------------------------------------------------------------------------------------------------------------
 1 | ERROR | [x] End of line character is invalid; expected "\n" but found "\r\n"
--------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------------------------------------------


FILE: C:\xampp\htdocs\backendassignment\web\modules\contrib\computed_breadcrumbs\tests\src\Kernel\ComputedBreadcrumbsKernelTest.php
-----------------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AND 1 WARNING AFFECTING 2 LINES
-----------------------------------------------------------------------------------------------------------------------------------
 1 | ERROR   | [x] End of line character is invalid; expected "\n" but found "\r\n"
 8 | WARNING | [x] Unused use statement
-----------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-----------------------------------------------------------------------------------------------------------------------------------

Time: 947ms; Memory: 6MB

Steps to reproduce

Run the phpcs command on the module.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

๐Ÿ“Œ Task
Status

Fixed

Version

1.1

Component

Code

Created by

๐Ÿ‡ฎ๐Ÿ‡ณIndia sidharth_soman Bangalore

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 @sidharth_soman
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia sidharth_soman Bangalore

    I've fixed all the issues except two: the formatting of the @todo comment and the 'line exceeds 80 characters' error for a comment.
    I'm keeping them both as it is for readability purposes.

    Please review the uploaded patch.

  • Issue was unassigned.
  • Status changed to Needs review over 1 year ago
  • Open on Drupal.org โ†’
    Core: 10.0.5 + Environment: PHP 8.2 & MySQL 8
    last update over 1 year ago
    Waiting for branch to pass
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia sidharth_soman Bangalore
  • Status changed to Needs work over 1 year ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom Eli-T Manchester

    Thanks for looking at this!

    Some things we need to look at:

    1. After applying your patch and running PHPCS I get a couple of errors not in your report:

    โฐelliot.wardโ™~/code/content_hub/prisoner-content-hub-backend/docroot/modules/contrib(gitโœฑfix_c_b_tests)โฑโœ˜โ‰ป ../../..//vendor/bin/phpcs --standard=Drupal,DrupalPractice --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml,twig computed_breadcrumbs
    
    
    FILE: /Users/elliot.ward/code/content_hub/prisoner-content-hub-backend/docroot/modules/contrib/computed_breadcrumbs/tests/src/Kernel/ComputedBreadcrumbsKernelTest.php
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
     6 | ERROR | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\Core\Breadcrumb\Breadcrumb.
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /Users/elliot.ward/code/content_hub/prisoner-content-hub-backend/docroot/modules/contrib/computed_breadcrumbs/src/EventListener.php
    ----------------------------------------------------------------------------------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    ----------------------------------------------------------------------------------------------------------------------------------------------------
     6 | ERROR | [x] Use statements should be sorted alphabetically. The first wrong one is Symfony\Component\EventDispatcher\EventSubscriberInterface.
    ----------------------------------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ----------------------------------------------------------------------------------------------------------------------------------------------------

    Can we fix these at the same time? If you can't see these warnings can you check you have the latest module code on the 1.1.x branch?

    2. Typo in the file comment - computer_breadcrumbs instead of computed_breadcrumbs.

    +++ b/computed_breadcrumbs.module
    @@ -1,5 +1,10 @@
     <?php
     
    +/**
    + * @file
    + * Defines the hooks for computer_breadcrumbs.
    + */
    +

    3. The line exceeding 80 characters warning can be fixed by splitting the constraints part of the annotation over multiple lines. Here's an example from core:

    /**
     * Defines the 'language' entity field item.
     *
     * @FieldType(
     *   id = "language",
     *   label = @Translation("Language"),
     *   description = @Translation("An entity field referencing a language."),
     *   default_widget = "language_select",
     *   default_formatter = "language",
     *   no_ui = TRUE,
     *   constraints = {
     *     "ComplexData" = {
     *       "value" = {
     *         "Length" = {"max" = 12}
     *       }
     *     }
     *   }
     * )
     */
    class LanguageItem extends FieldItemBase implements OptionsProviderInterface {

    5. There is no reason to not fix the @todo item

        // @todo Remove the request stack manipulation once the core issue described at https://www.drupal.org/node/2613044 is resolved.
        while ($requestStack->getCurrentRequest() === $request) {
          $requestStack->pop();
        }
    
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia sidharth_soman Bangalore

    Yep, I couldn't see the error with the sorted use statements since my drupal/coder package wasn't updated.

    I've fixed the concerns you've pointed out... and I'm uploading a new patch for it.

  • Status changed to Needs review over 1 year ago
  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 10.0.5 + Environment: PHP 8.2 & MySQL 8
    last update over 1 year ago
    Build Successful
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia sidharth_soman Bangalore

    Please review.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia sidharth_soman Bangalore

    I can also issue an MR against 1.1.x if that makes things easier for you.

  • Status changed to Needs work over 1 year ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom Eli-T Manchester

    @sidharth_soman do you use this module? Have you tested your changes?

    This code breaks the site

    foreach ($links as $index) {
      $link = $links[$index];
    

    The correct fix for the coding standard issue would be to change

    foreach ($links as $index => $link) {
    

    to

    foreach ($links as $link) {
    
  • Open on Drupal.org โ†’
    Core: 10.0.5 + Environment: PHP 8.2 & MySQL 8
    last update over 1 year ago
    Waiting for branch to pass
  • @sidharth_soman opened merge request.
  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 10.0.5 + Environment: PHP 8.2 & MySQL 8
    last update over 1 year ago
    Build Successful
  • @sidharth_soman opened merge request.
  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 10.0.5 + Environment: PHP 8.2 & MySQL 8
    last update over 1 year ago
    Build Successful
  • Status changed to Fixed over 1 year ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom Eli-T Manchester

    Thanks! Committed to 1.1.x.

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

Production build 0.71.5 2024