Fix \Drupal calls should be avoided in classes, use dependency injection

Created on 16 February 2025, 6 months ago

Problem/Motivation

PHPStan reports (and currently is forced to ignore) many \Drupal calls should be avoided in classes, use dependency injection instead warning. If possible these should be fixed and then removed from phpstan-baseline-to-fix.neon

Or if not possible to fix, then the ignoreError should be moved into the phpstan-baseline.neon file of permanently ignored warnings.

Remaining tasks

As at issue creation (16 Feb 2025) the phpstan custom summary script reports the following

---------------------------------------------------------------------------
Summary of PHPStan messages in phpstan-baseline-to-fix.neon
---------------------------------------------------------------------------
Drupal calls should be avoided in classes, use dependency injection instead
36 occurrence(s) in 12 file(s)
   scheduler_rules_integration/src/Plugin/Condition/PublishingIsEnabled.php
   scheduler_rules_integration/src/Plugin/Condition/UnpublishingIsEnabled.php
   scheduler_rules_integration/src/Plugin/RulesAction/RemovePublishingDate.php
   scheduler_rules_integration/src/Plugin/RulesAction/RemoveUnpublishingDate.php
   scheduler_rules_integration/src/Plugin/RulesAction/SchedulerRulesActionBase.php
   scheduler_rules_integration/src/Plugin/RulesAction/SetPublishingDate.php
   scheduler_rules_integration/src/Plugin/RulesAction/SetUnpublishingDate.php
   src/Plugin/Validation/Constraint/SchedulerPublishOnConstraintValidator.php
   src/Plugin/Validation/Constraint/SchedulerUnpublishOnConstraintValidator.php
   src/Routing/SchedulerRouteSubscriber.php
   src/SchedulerManager.php
   src/Theme/SchedulerThemeNegotiator.php
---------------------------------------------------------------------------
Number of messages by file
   17 in src/SchedulerManager.php
   4 in src/Plugin/Validation/Constraint/SchedulerUnpublishOnConstraintValidator.php
   3 in src/Plugin/Validation/Constraint/SchedulerPublishOnConstraintValidator.php
   3 in src/Theme/SchedulerThemeNegotiator.php
   2 in scheduler_rules_integration/src/Plugin/RulesAction/SchedulerRulesActionBase.php
   1 in scheduler_rules_integration/src/Plugin/Condition/PublishingIsEnabled.php
   1 in scheduler_rules_integration/src/Plugin/Condition/UnpublishingIsEnabled.php
   1 in scheduler_rules_integration/src/Plugin/RulesAction/RemovePublishingDate.php
   1 in scheduler_rules_integration/src/Plugin/RulesAction/RemoveUnpublishingDate.php
   1 in scheduler_rules_integration/src/Plugin/RulesAction/SetPublishingDate.php
   1 in scheduler_rules_integration/src/Plugin/RulesAction/SetUnpublishingDate.php
   1 in src/Routing/SchedulerRouteSubscriber.php
---------------------------------------------------------------------------
Different types of message: 1
Total number of messages: 36
---------------------------------------------------------------------------
📌 Task
Status

Active

Version

2.0

Component

Coding Standards

Created by

🇬🇧United Kingdom jonathan1055

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

Merge Requests

Comments & Activities

  • Issue created by @jonathan1055
  • Pipeline finished with Success
    6 months ago
    Total: 147s
    #429890
  • 🇬🇧United Kingdom jonathan1055

    Previously it was PHPStan version 1.12.27, used up to and including Core 11.1. Now we are running PHPStan version 2.1.27, in Core 11.2+ and it is more strict about ignorePaths which may not exist
    https://git.drupalcode.org/project/scheduler/-/jobs/5844600#L36
    Need to add (?) to indicate that the path is optional and may sometimes not exist.

  • 🇬🇧United Kingdom jonathan1055

    Nearly every call to \Drupal has been removed / replaced. The final ones are in SchedulerManager, which has 8 remaining.

    2 x \Drupal::service('workbench_moderation.moderation_information')

    /** @var \Drupal\workbench_moderation\ModerationInformationInterface $moderation_info */
    $moderation_info = \Drupal::service('workbench_moderation.moderation_information');
    

    line 426 and 644

    2 x \Drupal::service('extension.list.module')

    $optional_folder = \Drupal::service('extension.list.module')->getPath('scheduler') . '/config/optional';
    

    line 1235, 1238

    1 x \Drupal::service('config.storage')

    /** @var \Drupal\Core\Config\StorageInterface $config_storage */
    $config_storage = \Drupal::service('config.storage');

    line 1249

    2 x \Drupal::entityDefinitionUpdateManager()

    $entityUpdateManager = \Drupal::entityDefinitionUpdateManager();
    

    line 1181, 1297

    1 x \Drupal::service('entity_display.repository')

    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');

    line 1385

Production build 0.71.5 2024