Classes should use dependency injection, when possible

Created on 20 June 2023, over 1 year ago
Updated 18 September 2024, about 2 months ago

Classes should use dependency injection when possible. Using PHP_CodeSniffer, I could find the following lines that uses either \Drupal methods or User::load(). These lines need to be changed to use dependencies from the dependency container.

I am quoting here the relevant parts of the PHP_CodeSniffer report.

FILE: ./context/modules/context_ui/src/Plugin/Block/ContextInspector.php
-------------------------------------------------------------------------
 30 | \Drupal calls should be avoided in classes, use dependency injection instead
 32 | \Drupal calls should be avoided in classes, use dependency injection instead
 35 | \Drupal calls should be avoided in classes, use dependency injection instead
-------------------------------------------------------------------------

FILE: ./context/src/Plugin/Condition/UserProfilePage.php
 184 | User::load calls should be avoided in classes, use dependency injection instead
-------------------------------------------------------------------------

FILE: ./context/src/Theme/ThemeSwitcherNegotiator.php
-------------------------------------------------------------------------
 76 | \Drupal calls should be avoided in classes, use dependency injection instead
 80 | \Drupal calls should be avoided in classes, use dependency injection instead
-------------------------------------------------------------------------
📌 Task
Status

RTBC

Version

5.0

Component

Code

Created by

🇮🇳India arti_parmar

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

Comments & Activities

  • Issue created by @arti_parmar
  • Issue was unassigned.
  • 🇮🇹Italy apaderno Brescia, 🇮🇹
  • First commit to issue fork.
  • Assigned to roshni27
  • Status changed to Needs work over 1 year ago
  • 🇮🇳India roshni27

    Execute the command: phpcs --standard=Drupal,DrupalPractice --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml,twig context-3367947/

    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/context.module
    -------------------------------------------------------------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    -------------------------------------------------------------------------------------------------------------------------------
     10 | ERROR | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\Component\Utility\NestedArray.
    -------------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    -------------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/modules/context_ui/src/ContextListBuilder.php
    ------------------------------------------------------------------------------------------------------------------------------------------
    FOUND 2 ERRORS AFFECTING 2 LINES
    ------------------------------------------------------------------------------------------------------------------------------------------
       7 | ERROR | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\Component\Utility\Html.
     213 | ERROR | [ ] The array declaration extends to column 82 (the limit is 80). The array content should be split up over multiple lines
    ------------------------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ------------------------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/modules/context_ui/src/Plugin/Block/ContextInspector.php
    ---------------------------------------------------------------------------------------------------------------------
    FOUND 0 ERRORS AND 3 WARNINGS AFFECTING 3 LINES
    ---------------------------------------------------------------------------------------------------------------------
     30 | WARNING | \Drupal calls should be avoided in classes, use dependency injection instead
     32 | WARNING | \Drupal calls should be avoided in classes, use dependency injection instead
     35 | WARNING | \Drupal calls should be avoided in classes, use dependency injection instead
    ---------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/modules/context_ui/src/Form/ReactionDeleteForm.php
    -----------------------------------------------------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    -----------------------------------------------------------------------------------------------------------------------
     6 | ERROR | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\context\ContextManager.
    -----------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    -----------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/modules/context_ui/src/Form/ContextDuplicateForm.php
    -----------------------------------------------------------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    -----------------------------------------------------------------------------------------------------------------------------
     8 | ERROR | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\Core\Form\FormStateInterface.
    -----------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    -----------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/modules/context_ui/src/Form/ConditionDeleteForm.php
    -------------------------------------------------------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    -------------------------------------------------------------------------------------------------------------------------
     7 | ERROR | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\context\ContextInterface.
    -------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    -------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/modules/context_ui/src/Form/ContextEditForm.php
    ----------------------------------------------------------------------------------------------------------------------------------------------
    FOUND 3 ERRORS AND 2 WARNINGS AFFECTING 5 LINES
    ----------------------------------------------------------------------------------------------------------------------------------------------
       6 | ERROR   | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\Core\Form\FormState.
     126 | ERROR   | [ ] The array declaration extends to column 99 (the limit is 80). The array content should be split up over multiple lines
     204 | ERROR   | [ ] The array declaration extends to column 95 (the limit is 80). The array content should be split up over multiple lines
     245 | WARNING | [ ] t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
     251 | WARNING | [ ] t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
    ----------------------------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ----------------------------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/modules/context_ui/src/Form/ContextFormBase.php
    -----------------------------------------------------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    -----------------------------------------------------------------------------------------------------------------------
     6 | ERROR | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\context\ContextManager.
    -----------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    -----------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/modules/context_ui/src/Form/ContextDeleteForm.php
    -----------------------------------------------------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    -----------------------------------------------------------------------------------------------------------------------
     6 | ERROR | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\context\ContextManager.
    -----------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    -----------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/modules/context_ui/src/Form/ContextDisableForm.php
    -----------------------------------------------------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    -----------------------------------------------------------------------------------------------------------------------
     6 | ERROR | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\context\ContextManager.
    -----------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    -----------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/modules/context_ui/src/Controller/ContextUIController.php
    -----------------------------------------------------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    -----------------------------------------------------------------------------------------------------------------------
     7 | ERROR | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\Component\Utility\Html.
    -----------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    -----------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/modules/context_ui/context_ui.install
    --------------------------------------------------------------------------------------------------
    FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
    --------------------------------------------------------------------------------------------------
     14 | WARNING | Unused variable $key.
    --------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/src/ContextReactionInterface.php
    -------------------------------------------------------------------------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    -------------------------------------------------------------------------------------------------------------------------------------------
     6 | ERROR | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\Component\Plugin\PluginInspectionInterface.
    -------------------------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    -------------------------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/src/Plugin/ContextReaction/Blocks.php
    --------------------------------------------------------------------------------------------------------------------------------------------
    FOUND 7 ERRORS AND 2 WARNINGS AFFECTING 9 LINES
    --------------------------------------------------------------------------------------------------------------------------------------------
      11 | ERROR   | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\Core\Cache\Cache.
     198 | WARNING | [x] The variable name should be defined after the type
     300 | ERROR   | [ ] The array declaration extends to column 91 (the limit is 80). The array content should be split up over multiple lines
     302 | WARNING | [ ] Line exceeds 80 characters; contains 82 characters
     303 | ERROR   | [ ] The array declaration extends to column 93 (the limit is 80). The array content should be split up over multiple lines
     554 | ERROR   | [x] Use null coalesce operator instead of ternary operator.
     706 | ERROR   | [x] Use null coalesce operator instead of ternary operator.
     747 | ERROR   | [ ] The array declaration extends to column 98 (the limit is 80). The array content should be split up over multiple lines
     776 | ERROR   | [x] Use null coalesce operator instead of ternary operator.
    --------------------------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 5 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    --------------------------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/src/Plugin/ContextReaction/Regions.php
    ------------------------------------------------------------------------------------------------------------
    FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
    ------------------------------------------------------------------------------------------------------------
     83 | WARNING | [x] 'TODO: Implement execute() method.' should match the format '@todo Fix problem X here.'
    ------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/src/Plugin/ContextReaction/PageTitle.php
    -----------------------------------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    -----------------------------------------------------------------------------------------------------
     41 | ERROR | [x] Use null coalesce operator instead of ternary operator.
    -----------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    -----------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/src/Plugin/ContextReaction/Menu.php
    ------------------------------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    ------------------------------------------------------------------------------------------------
     74 | ERROR | [x] Use null coalesce operator instead of ternary operator.
    ------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/src/Plugin/ContextReaction/PageTemplateSuggestions.php
    -------------------------------------------------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    -------------------------------------------------------------------------------------------------------------------
     42 | ERROR | [x] Use null coalesce operator instead of ternary operator.
    -------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    -------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/src/Plugin/ContextReaction/Theme.php
    ----------------------------------------------------------------------------------------------------------------------------------------------
    FOUND 1 ERROR AND 3 WARNINGS AFFECTING 4 LINES
    ----------------------------------------------------------------------------------------------------------------------------------------------
      75 | WARNING | [x] 'TODO: Implement execute() method.' should match the format '@todo Fix problem X here.'
      88 | WARNING | [ ] t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
      89 | WARNING | [ ] t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
     101 | ERROR   | [x] Use null coalesce operator instead of ternary operator.
    ----------------------------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ----------------------------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/src/Plugin/Condition/RequestDomain.php
    -----------------------------------------------------------------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    -----------------------------------------------------------------------------------------------------------------------------------
     7 | ERROR | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\Core\Condition\ConditionPluginBase.
    -----------------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    -----------------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/src/Plugin/Condition/ViewInclusion.php
    -----------------------------------------------------------------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    -----------------------------------------------------------------------------------------------------------------------------------
     7 | ERROR | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\Core\Condition\ConditionPluginBase.
    -----------------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    -----------------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/src/Plugin/Condition/UserProfilePage.php
    ----------------------------------------------------------------------------------------------------------------------------------------------
    FOUND 5 ERRORS AND 3 WARNINGS AFFECTING 8 LINES
    ----------------------------------------------------------------------------------------------------------------------------------------------
       6 | ERROR   | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\Core\Condition\ConditionPluginBase.
      90 | WARNING | [ ] #description values usually have to run through t() for translation
     100 | ERROR   | [x] Use null coalesce operator instead of ternary operator.
     142 | WARNING | [ ] t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
     177 | ERROR   | [x] Use "elseif" in place of "else if"
     180 | ERROR   | [x] Use "elseif" in place of "else if"
     183 | ERROR   | [x] Use "elseif" in place of "else if"
     184 | WARNING | [ ] User::load calls should be avoided in classes, use dependency injection instead
    ----------------------------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 5 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ----------------------------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/src/Plugin/ContextReactionPluginCollection.php
    -----------------------------------------------------------------------------------------------------------
    FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
    -----------------------------------------------------------------------------------------------------------
     15 | WARNING | Possible useless method overriding detected
    -----------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/src/Plugin/DisplayVariant/ContextBlockPageVariant.php
    ----------------------------------------------------------------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    ----------------------------------------------------------------------------------------------------------------------------------
     7 | ERROR | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\Core\Display\PageVariantInterface.
    ----------------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ----------------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/src/Entity/ContextAccess.php
    -----------------------------------------------------------------------------------------
    FOUND 0 ERRORS AND 2 WARNINGS AFFECTING 2 LINES
    -----------------------------------------------------------------------------------------
     23 | WARNING | Possible useless method overriding detected
     37 | WARNING | Possible useless method overriding detected
    -----------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/src/Entity/Context.php
    -------------------------------------------------------------------------------------------------------------------------
    FOUND 3 ERRORS AFFECTING 3 LINES
    -------------------------------------------------------------------------------------------------------------------------
     5 | ERROR | [x] Non-namespaced classes/interfaces/traits should not be referenced with use statements
     6 | ERROR | [x] Non-namespaced classes/interfaces/traits should not be referenced with use statements
     7 | ERROR | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\context\ContextInterface.
    -------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 3 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    -------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/src/ContextReactionManager.php
    ---------------------------------------------------------------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    ---------------------------------------------------------------------------------------------------------------------------------
     6 | ERROR | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\Core\Cache\CacheBackendInterface.
    ---------------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ---------------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/src/EventSubscriber/BlockPageDisplayVariantSubscriber.php
    --------------------------------------------------------------------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    --------------------------------------------------------------------------------------------------------------------------------------
     7 | ERROR | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\context\Plugin\ContextReaction\Blocks.
    --------------------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    --------------------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/src/Reaction/Blocks/BlockCollection.php
    ----------------------------------------------------------------------------------------------------
    FOUND 3 ERRORS AND 1 WARNING AFFECTING 4 LINES
    ----------------------------------------------------------------------------------------------------
     16 | WARNING | [ ] Possible useless method overriding detected
     44 | ERROR   | [x] Use null coalesce operator instead of ternary operator.
     54 | ERROR   | [x] Use null coalesce operator instead of ternary operator.
     57 | ERROR   | [x] Use null coalesce operator instead of ternary operator.
    ----------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 3 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ----------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/src/Reaction/Blocks/Form/BlockFormBase.php
    ------------------------------------------------------------------------------------------------------------------------------------------
    FOUND 6 ERRORS AFFECTING 6 LINES
    ------------------------------------------------------------------------------------------------------------------------------------------
      18 | ERROR | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\context\ContextInterface.
     238 | ERROR | [x] Use null coalesce operator instead of ternary operator.
     250 | ERROR | [x] Use null coalesce operator instead of ternary operator.
     257 | ERROR | [x] Use null coalesce operator instead of ternary operator.
     268 | ERROR | [x] Use null coalesce operator instead of ternary operator.
     337 | ERROR | [ ] The array declaration extends to column 91 (the limit is 80). The array content should be split up over multiple lines
    ------------------------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 5 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ------------------------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/src/Reaction/Blocks/Controller/ContextReactionBlocksController.php
    -------------------------------------------------------------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    -------------------------------------------------------------------------------------------------------------------------------
     10 | ERROR | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\context\ContextInterface.
    -------------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    -------------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/src/Theme/ThemeSwitcherNegotiator.php
    --------------------------------------------------------------------------------------------------
    FOUND 0 ERRORS AND 2 WARNINGS AFFECTING 2 LINES
    --------------------------------------------------------------------------------------------------
     76 | WARNING | \Drupal calls should be avoided in classes, use dependency injection instead
     80 | WARNING | \Drupal calls should be avoided in classes, use dependency injection instead
    --------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/src/ContextManager.php
    ------------------------------------------------------------------------------------------------------------------------------------------
    FOUND 4 ERRORS AFFECTING 3 LINES
    ------------------------------------------------------------------------------------------------------------------------------------------
     10 | ERROR | [x] Use statements should be sorted alphabetically. The first wrong one is Drupal\Core\Condition\ConditionPluginCollection.
     88 | ERROR | [x] The open comment tag must be the only content on the line
     88 | ERROR | [x] Doc comment short description must be on the first line
     94 | ERROR | [ ] Parameter $currentRouteMatch is not described in comment
    ------------------------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 3 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ------------------------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/help_topics/context.overview.html.twig
    ---------------------------------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    ---------------------------------------------------------------------------------------------------
     20 | ERROR | [x] Expected 1 newline at end of file; 0 found
    ---------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ---------------------------------------------------------------------------------------------------
    
    
    FILE: /home/lenovo/d7to9/web/modules/contrib/context-3367947/tests/modules/context_all_any_test/context_all_any_test.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
    -----------------------------------------------------------------------------------------------------------------------------
    
    Time: 794ms; Memory: 14MB

    I am working on it.

  • Issue was unassigned.
  • Status changed to Needs review over 1 year ago
  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    Given the purpose of this issue (\Drupal calls should be avoided in classes, use dependency injection instead), only replacing calls to \Drupal methods is an on-topic change. This issue is not about fixing all the warnings/errors reported by PHP_CodeSniffer.

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.0.7 + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    Composer require failure
  • @roshni27 opened merge request.
  • 🇮🇳India roshni27

    All dependency injection issues resolved. Please review the MR .

  • 🇮🇹Italy apaderno Brescia, 🇮🇹
  • Status changed to Needs work over 1 year ago
  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    Please see the issue summary. This issue is not about fixing all the warnings/errors reported by PHP_CodeSniffer, but changing the code to use dependency injection (and its container) where possible.

  • 🇮🇹Italy apaderno Brescia, 🇮🇹
  • Status changed to Needs review about 1 year ago
  • 🇮🇳India chaitanyadessai Goa

    Please review patch.

  • Status changed to Needs work about 1 year ago
  • 🇮🇹Italy apaderno Brescia, 🇮🇹
     use Drupal\Core\Block\BlockBase;
    +use Symfony\Component\DependencyInjection\ContainerInterface;
    +use Drupal\Core\Extension\ModuleHandlerInterface;
    +use Drupal\Core\Session\AccountInterface;
    +use Drupal\context\ContextManager;

    use statements must be ordered alphabetically per class namespace.

    +  /**
    +   * The module handler service.
    +   *
    +   * @var \Drupal\Core\Extension\ModuleHandlerInterface
    +   */
    +  protected $moduleHandler;

    The module handler. is sufficient.

    +  /**
    +   * ContextManager.
    +   *
    +   * @var \Drupal\context\ContextManager
    +   */
    +
    +  private $contextManager;

    The description is not sufficient, since it is merely repeating the property class.

    +  /**
    +   * @var AccountInterface $account
    +   */
    +  protected $account;

    The description is missing.

    +  /**
    +   * Constructs a MyController object
    +   *
    +   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
    +   *   The module handler service.
    +   * @param \Drupal\context\ContextManager $contextManager
    +   *   ContextManager.
    +   */

    For constructors, the description is not anymore necessary. If it is added, the description should be Constructs a new [class name] object. where [class name] is the class name, namespace included.
    For the parameter descriptions see what I reported for the properties.

    -    $moduleHandler = \Drupal::service('module_handler');
    +    $moduleHandler = $this->moduleHandler ('module_handler');

    There is a space between the method name and the parenthesis that needs to be removed.

       /**
        * UserProfilePage constructor.
        */
    -  public function __construct(array $configuration, $plugin_id, $plugin_definition, CurrentRouteMatch $currentRouteMatch, EntityFieldManager $entityFieldManager, AccountProxyInterface $currentUser) {
    +  public function __construct(array $configuration, $plugin_id, $plugin_definition, CurrentRouteMatch $currentRouteMatch, EntityFieldManager $entityFieldManager, AccountProxyInterface $currentUser, UserStorageInterface $user_storage) {

    Since the constructor definition has been changed, also the documentation comment must be corrected.

    +  private $configFactory;
       /**
        * ContextManager.
        *

    There is a missing empty line after the property definition.

        * @param \Drupal\context\ContextManager $contextManager
        *   ContextManager.
    +   * 
    +   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
    +   *   The config factory.

    Since that documentation comment is edited, also the first parameter description must be corrected.

  • Status changed to Needs review about 1 year ago
  • 🇮🇳India chaitanyadessai Goa

    Please review patch, changes addressed of #13.

  • Status changed to Needs work about 1 year ago
  • 🇮🇹Italy apaderno Brescia, 🇮🇹
    +  /**
    +   * Variable declaration ContextManager.
    +   *
    +   * @var \Drupal\context\ContextManager
    +   */
    +
    +  private $contextManager;

    That is still not describing the property.
    There is an empty line between the documentation comment and the property declaration that should not be there.

    +  /**
    +   * Variable declaration account.
    +   * @var AccountInterface $account
    +   */
    +  protected $account;

    That is not describing the property.
    Between the short description and the @var line there must be an empty line.

    +  /**
    +   * Constructor definition.
    +   *
    +   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
    +   *   The module handler.
    +   * @param \Drupal\context\ContextManager $contextManager
    +   *   ContextManager paramater.

    For constructors, the documentation comment is not anymore necessary. If it is added, the description should be

    Constructs a new [class name] object.</code where [class name] is the class name, namespace included.
    
    <code>+   * @param \Drupal\context\ContextManager $contextManager
    +   *   ContextManager paramater.

    [class name] parameter. is not a description we use for parameters.

       private $currentUser;
     
    +  private $userStorage;
    +

    Class properties must have a documentation comment.

       /**
    -   * UserProfilePage constructor.
    +   * Constructor for UserProfilePage class.
        */
    -  public function __construct(array $configuration, $plugin_id, $plugin_definition, CurrentRouteMatch $currentRouteMatch, EntityFieldManager $entityFieldManager, AccountProxyInterface $currentUser) {
    +  public function __construct(array $configuration, $plugin_id, $plugin_definition, CurrentRouteMatch $currentRouteMatch, EntityFieldManager $entityFieldManager, AccountProxyInterface $currentUser, UserStorageInterface $user_storage) {
         parent::__construct($configuration, $plugin_id, $plugin_definition);
         $this->currentRouteMatch = $currentRouteMatch;

    For constructors, the documentation comment is not anymore necessary. If it is added, the description should be Constructs a new [class name] object.</code where <code>[class name] is the class name, namespace included.
    Furthermore, if the documentation comment is added to a constructor, its parameters must be documented too.

    +   * @var \Drupal\Core\Config\ConfigFactoryInterface
    +   */
    +
    +  private $configFactory;
    +  /**
    +   * ContextManager.
        * @var \Drupal\context\ContextManager
        */

    ContextManager is the property class, not a property description.
    The empty line before private $configFactory; must be removed.
    An empty line is missing between the property description and the @var line.

  • First commit to issue fork.
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.0.7 + Environment: PHP 7.4 & MySQL 5.7
    last update about 1 year ago
    Composer require failure
  • I applied the fixes to the issue fork, but now I see more changes than this issue wants to be fixed. I will extract the fix and create a patch file.

  • @admirlju opened merge request.
  • Status changed to Needs review about 1 year ago
  • Made a separate branch on the issue fork from the 5.x branch, so the code is clean. Applied patch #14, cleaned up how the DI is added, and removed some useless local variables when using DI services. Fixed the CS problems mentioned in #15. Please review.

  • Status changed to RTBC about 2 months ago
  • 🇸🇮Slovenia agile-mark-l

    I have checked the merge request !39 and everything discussed seems to be accounted for.

Production build 0.71.5 2024