Dependency injection needed in controller file

Created on 17 July 2023, over 1 year ago
Updated 20 August 2024, 3 months ago

Problem/Motivation

PHPCS reports errors regarding global service calls in the controller file.

Steps to reproduce

Run the phpcs command on the module.

Proposed resolution

Inject the services (request and time) via DI.

Remaining tasks

User interface changes

API changes

Data model changes

๐Ÿ“Œ Task
Status

RTBC

Version

1.0

Component

Code

Created by

๐Ÿ‡ฎ๐Ÿ‡ณIndia sidharth_soman Bangalore

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @sidharth_soman
  • Issue was unassigned.
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia sidharth_soman Bangalore
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia dev16.addweb

    silvi.addweb โ†’ made their first commit to this issueโ€™s fork.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia aman1248

    Amandeep123 โ†’ made their first commit to this issueโ€™s fork.

  • Status changed to Needs review 6 months ago
  • Status changed to RTBC 3 months ago
  • ๐Ÿ‡ต๐Ÿ‡ญPhilippines cleavinjosh

    Hi @silvi.addweb,

    I applied MR!5, it was applied smoothly and fixed the issues related to dependency injection in the MVController file.

    โžœ  most_viewed git:(8.x-1.x) curl https://git.drupalcode.org/project/most_viewed/-/merge_requests/5.diff | patch -p1
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100  2854    0  2854    0     0   8610      0 --:--:-- --:--:-- --:--:--  8648
    patching file src/Controller/MVController.php
    โžœ  most_viewed git:(8.x-1.x) โœ— ..
    โžœ  contrib phpcs --standard=Drupal,DrupalPractice --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml most_viewed
    
    FILE: /Users/interns/Demo-site/drupal-org-issues/web/modules/contrib/most_viewed/most_viewed.module
    --------------------------------------------------------------------------------------------------------------------------
    FOUND 4 ERRORS AND 1 WARNING AFFECTING 5 LINES
    --------------------------------------------------------------------------------------------------------------------------
      1 | ERROR   | [x] The PHP open tag must be followed by exactly one blank line
      7 | WARNING | [ ] Global constants should not be used, move it to a class or interface
     10 | ERROR   | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\Core\Database\Database.
     43 | ERROR   | [x] Short array syntax must be used to define arrays
     58 | ERROR   | [x] Short array syntax must be used to define arrays
    --------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 4 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    --------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /Users/interns/Demo-site/drupal-org-issues/web/modules/contrib/most_viewed/most_viewed.views.inc
    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    FOUND 30 ERRORS AND 2 WARNINGS AFFECTING 18 LINES
    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
       1 | ERROR   | [x] The PHP open tag must be followed by exactly one blank line
      32 | ERROR   | [x] Line indented incorrectly; expected at least 2 spaces, found 0
      32 | ERROR   | [x] 6 spaces found before inline comment; expected "// 'handler' => 'views_handler_field_numeric'," but found "//      'handler' => 'views_handler_field_numeric',"
      33 | ERROR   | [x] Line indented incorrectly; expected at least 2 spaces, found 0
      37 | ERROR   | [x] Line indented incorrectly; expected at least 4 spaces, found 0
      37 | ERROR   | [x] 6 spaces found before inline comment; expected "// 'handler' => 'views_handler_filter_numeric'," but found "//      'handler' => 'views_handler_filter_numeric',"
      41 | ERROR   | [x] Line indented incorrectly; expected at least 4 spaces, found 0
      41 | ERROR   | [x] 6 spaces found before inline comment; expected "// 'handler' => 'views_handler_sort'," but found "//      'handler' => 'views_handler_sort',"
      65 | ERROR   | [x] Line indented incorrectly; expected at least 2 spaces, found 0
      65 | ERROR   | [x] 6 spaces found before inline comment; expected "// 'handler' => 'views_handler_field'," but found "//      'handler' => 'views_handler_field',"
      70 | ERROR   | [x] Line indented incorrectly; expected at least 4 spaces, found 0
      70 | ERROR   | [x] 6 spaces found before inline comment; expected "// 'handler' => 'views_handler_filter_string'," but found "//      'handler' => 'views_handler_filter_string',"
      74 | ERROR   | [x] Line indented incorrectly; expected at least 4 spaces, found 0
      74 | ERROR   | [x] 6 spaces found before inline comment; expected "// 'handler' => 'views_handler_sort'," but found "//      'handler' => 'views_handler_sort',"
     100 | ERROR   | [x] Line indented incorrectly; expected at least 2 spaces, found 0
     100 | ERROR   | [x] 6 spaces found before inline comment; expected "// 'handler' => 'views_handler_field_date'," but found "//      'handler' => 'views_handler_field_date',"
     105 | ERROR   | [x] Line indented incorrectly; expected at least 4 spaces, found 0
     105 | ERROR   | [x] 6 spaces found before inline comment; expected "// 'handler' => 'views_handler_filter_date'," but found "//      'handler' => 'views_handler_filter_date',"
     109 | ERROR   | [x] Line indented incorrectly; expected at least 4 spaces, found 0
     109 | ERROR   | [x] 6 spaces found before inline comment; expected "// 'handler' => 'views_handler_sort_date'," but found "//      'handler' => 'views_handler_sort_date',"
     114 | ERROR   | [x] Line indented incorrectly; expected 2 spaces, found 0
     114 | ERROR   | [x] 2 spaces found before inline comment; expected "// $data['node']['most_viewed_sort'] = [" but found "//  $data['node']['most_viewed_sort'] = ["
     115 | ERROR   | [x] Line indented incorrectly; expected 2 spaces, found 0
     115 | ERROR   | [ ] Comment indentation error, expected only 2 spaces
     116 | ERROR   | [x] Line indented incorrectly; expected 2 spaces, found 0
     117 | ERROR   | [x] Line indented incorrectly; expected 2 spaces, found 0
     118 | ERROR   | [x] Line indented incorrectly; expected 2 spaces, found 0
     118 | ERROR   | [ ] Comment indentation error, expected only 4 spaces
     119 | ERROR   | [x] Line indented incorrectly; expected 2 spaces, found 0
     120 | WARNING | [x] There must be no blank line following an inline comment
     120 | WARNING | [ ] There must be no blank line following an inline comment
     120 | ERROR   | [x] Line indented incorrectly; expected 2 spaces, found 0
    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 29 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /Users/interns/Demo-site/drupal-org-issues/web/modules/contrib/most_viewed/most_viewed.info.yml
    -----------------------------------------------------------------------------------------------------------
    FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
    -----------------------------------------------------------------------------------------------------------
     1 | WARNING | Remove "version" from the info file, it will be added by drupal.org packaging automatically
    -----------------------------------------------------------------------------------------------------------
    
    
    FILE: /Users/interns/Demo-site/drupal-org-issues/web/modules/contrib/most_viewed/views/most_viewedD7.default_viewsD7.inc
    ------------------------------------------------------------------------------------------------------------------------
    FOUND 40 ERRORS AFFECTING 40 LINES
    ------------------------------------------------------------------------------------------------------------------------
       4 | ERROR | [x] The second line in the file doc comment must be "@file"
       9 | ERROR | [x] Expected 1 blank line before function; 0 found
      10 | ERROR | [x] Short array syntax must be used to define arrays
      58 | ERROR | [x] Short array syntax must be used to define arrays
      65 | ERROR | [x] Short array syntax must be used to define arrays
      66 | ERROR | [x] Short array syntax must be used to define arrays
      73 | ERROR | [x] Short array syntax must be used to define arrays
      80 | ERROR | [x] Short array syntax must be used to define arrays
      87 | ERROR | [x] Short array syntax must be used to define arrays
     130 | ERROR | [x] Short array syntax must be used to define arrays
     145 | ERROR | [x] Short array syntax must be used to define arrays
     153 | ERROR | [x] Short array syntax must be used to define arrays
     154 | ERROR | [x] Short array syntax must be used to define arrays
     159 | ERROR | [x] Short array syntax must be used to define arrays
     164 | ERROR | [x] Short array syntax must be used to define arrays
     180 | ERROR | [x] Short array syntax must be used to define arrays
     214 | ERROR | [x] Short array syntax must be used to define arrays
     221 | ERROR | [x] Short array syntax must be used to define arrays
     222 | ERROR | [x] Short array syntax must be used to define arrays
     229 | ERROR | [x] Short array syntax must be used to define arrays
     236 | ERROR | [x] Short array syntax must be used to define arrays
     243 | ERROR | [x] Short array syntax must be used to define arrays
     293 | ERROR | [x] Short array syntax must be used to define arrays
     308 | ERROR | [x] Short array syntax must be used to define arrays
     316 | ERROR | [x] Short array syntax must be used to define arrays
     317 | ERROR | [x] Short array syntax must be used to define arrays
     322 | ERROR | [x] Short array syntax must be used to define arrays
     327 | ERROR | [x] Short array syntax must be used to define arrays
     343 | ERROR | [x] Short array syntax must be used to define arrays
     380 | ERROR | [x] Short array syntax must be used to define arrays
     386 | ERROR | [x] Short array syntax must be used to define arrays
     387 | ERROR | [x] Short array syntax must be used to define arrays
     394 | ERROR | [x] Short array syntax must be used to define arrays
     401 | ERROR | [x] Short array syntax must be used to define arrays
     440 | ERROR | [x] Short array syntax must be used to define arrays
     455 | ERROR | [x] Short array syntax must be used to define arrays
     463 | ERROR | [x] Short array syntax must be used to define arrays
     464 | ERROR | [x] Short array syntax must be used to define arrays
     469 | ERROR | [x] Short array syntax must be used to define arrays
     474 | ERROR | [x] Short array syntax must be used to define arrays
    ------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 40 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /Users/interns/Demo-site/drupal-org-issues/web/modules/contrib/most_viewed/views/most_viewedD7.viewsD7.inc
    ----------------------------------------------------------------------------------------------------------------
    FOUND 31 ERRORS AFFECTING 31 LINES
    ----------------------------------------------------------------------------------------------------------------
       1 | ERROR | [x] The PHP open tag must be followed by exactly one blank line
      11 | ERROR | [x] Short array syntax must be used to define arrays
      14 | ERROR | [x] Short array syntax must be used to define arrays
      21 | ERROR | [x] Short array syntax must be used to define arrays
      22 | ERROR | [x] Short array syntax must be used to define arrays
      28 | ERROR | [x] Short array syntax must be used to define arrays
      31 | ERROR | [x] Short array syntax must be used to define arrays
      35 | ERROR | [x] Short array syntax must be used to define arrays
      38 | ERROR | [x] Short array syntax must be used to define arrays
      43 | ERROR | [x] Short array syntax must be used to define arrays
      46 | ERROR | [x] Short array syntax must be used to define arrays
      50 | ERROR | [x] Short array syntax must be used to define arrays
      53 | ERROR | [x] Short array syntax must be used to define arrays
      58 | ERROR | [x] Short array syntax must be used to define arrays
      61 | ERROR | [x] Short array syntax must be used to define arrays
      65 | ERROR | [x] Short array syntax must be used to define arrays
      68 | ERROR | [x] Short array syntax must be used to define arrays
      73 | ERROR | [x] Short array syntax must be used to define arrays
      76 | ERROR | [x] Short array syntax must be used to define arrays
      80 | ERROR | [x] Short array syntax must be used to define arrays
      83 | ERROR | [x] Short array syntax must be used to define arrays
      88 | ERROR | [x] Short array syntax must be used to define arrays
      91 | ERROR | [x] Short array syntax must be used to define arrays
      95 | ERROR | [x] Short array syntax must be used to define arrays
      98 | ERROR | [x] Short array syntax must be used to define arrays
     103 | ERROR | [x] Short array syntax must be used to define arrays
     106 | ERROR | [x] Short array syntax must be used to define arrays
     118 | ERROR | [x] Short array syntax must be used to define arrays
     119 | ERROR | [x] Short array syntax must be used to define arrays
     122 | ERROR | [x] Short array syntax must be used to define arrays
     123 | ERROR | [x] Short array syntax must be used to define arrays
    ----------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 31 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ----------------------------------------------------------------------------------------------------------------
    
    
    FILE: /Users/interns/Demo-site/drupal-org-issues/web/modules/contrib/most_viewed/views/handlers/most_viewed_handler_sort_most_viewed.inc
    ---------------------------------------------------------------------------------------------------------------------------------------------
    FOUND 19 ERRORS AND 2 WARNINGS AFFECTING 16 LINES
    ---------------------------------------------------------------------------------------------------------------------------------------------
      1 | ERROR   | [x] The PHP open tag must be followed by exactly one blank line
      7 | ERROR   | [ ] Class name must begin with a capital letter
      7 | ERROR   | [ ] Class name must use UpperCamel naming without underscores
      7 | ERROR   | [x] Missing class doc comment
      9 | ERROR   | [x] Visibility must be declared on method "option_definition"
      9 | ERROR   | [ ] Method name "most_viewed_handler_sort_most_viewed::option_definition" is not in lowerCamel format
      9 | ERROR   | [x] Missing function doc comment
     12 | ERROR   | [x] Short array syntax must be used to define arrays
     19 | ERROR   | [x] Visibility must be declared on method "options_form"
     19 | ERROR   | [ ] Method name "most_viewed_handler_sort_most_viewed::options_form" is not in lowerCamel format
     21 | ERROR   | [x] Short array syntax must be used to define arrays
     23 | WARNING | [ ] t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
     24 | WARNING | [ ] t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
     32 | ERROR   | [x] Visibility must be declared on method "query"
     34 | ERROR   | [x] Short array syntax must be used to define arrays
     41 | ERROR   | [x] Inline comments must start with a capital letter
     45 | ERROR   | [x] Functions must not contain multiple empty lines in a row; found 2 empty lines
     47 | ERROR   | [x] Inline comments must end in full-stops, exclamation marks, question marks, colons, or closing parentheses
     48 | ERROR   | [x] Short array syntax must be used to define arrays
     51 | ERROR   | [x] Expected 1 blank line after function; 0 found
     52 | ERROR   | [x] The closing brace for the class must have an empty line before it
    ---------------------------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 15 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ---------------------------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /Users/interns/Demo-site/drupal-org-issues/web/modules/contrib/most_viewed/src/Controller/MVController.php
    ---------------------------------------------------------------------------------------------------------------------------
    FOUND 5 ERRORS AND 1 WARNING AFFECTING 6 LINES
    ---------------------------------------------------------------------------------------------------------------------------
      7 | ERROR   | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\Component\Datetime\Time.
      8 | WARNING | [x] Unused use statement
     63 | ERROR   | [x] There must be exactly one blank line before the tags in a doc comment
     74 | ERROR   | [x] Short array syntax must be used to define arrays
     83 | ERROR   | [x] Expected 1 blank line after function; 0 found
     84 | ERROR   | [x] The closing brace for the class must have an empty line before it
    ---------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 6 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ---------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /Users/interns/Demo-site/drupal-org-issues/web/modules/contrib/most_viewed/src/MVSettingsForm.php
    ---------------------------------------------------------------------------------------------------------------------------------------------
    FOUND 2 ERRORS AND 11 WARNINGS AFFECTING 13 LINES
    ---------------------------------------------------------------------------------------------------------------------------------------------
     39 | WARNING | [ ] NodeType::loadMultiple calls should be avoided in classes, use dependency injection instead
     46 | WARNING | [ ] t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
     47 | WARNING | [ ] t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
     49 | ERROR   | [x] Expected 1 space after "=>"; 2 found
     53 | ERROR   | [x] Short array syntax must be used to define arrays
     54 | WARNING | [ ] Vocabulary::loadMultiple calls should be avoided in classes, use dependency injection instead
     61 | WARNING | [ ] t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
     62 | WARNING | [ ] t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
     69 | WARNING | [ ] t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
     70 | WARNING | [ ] t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
     71 | WARNING | [ ] t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
     81 | WARNING | [ ] t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
     82 | WARNING | [ ] t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
    ---------------------------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ---------------------------------------------------------------------------------------------------------------------------------------------
    
    Time: 456ms; Memory: 14MB
    
    โžœ  contrib

    I will move the status to RTBC since this issue focuses on that issue only.

    Thank you.

Production build 0.71.5 2024