Using the dependency injection rather than invoking global services

Created on 13 July 2023, 12 months ago
Updated 15 July 2023, 12 months ago

Problem/Motivation

Codesniffer reports the dependency injection error for two files: MetaClient.php and MetaApiTwig.php with global calls to the 'request' service and the 'meta_conversions_api.meta_client' service respectively.

Steps to reproduce

Run the phpcs command on the module:

phpcs --standard=Drupal,DrupalPractice --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml,twig meta_conversions_api

Proposed resolution

Use a dependency injection to inject the services for the classes.

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Fixed

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

Comments & Activities

  • Issue created by @sidharth_soman
  • Issue was unassigned.
  • Status changed to Needs review 12 months ago
  • 🇮🇳India sidharth_soman Bangalore

    I have added the DI for MetaClient.php. I'm holding off on doing it for MetaApiTwig.php for now since it seems like a manual method to access the MetaClient service was implemented instead. I'll change it as per your request if it's really required.

    In the meantime, please review the patch.

  • 🇬🇧United Kingdom Rob230

    The advice I've seen is not to have the request stack as a required in the constructor because it's only available for HTTP requests, so may not be available in tests. The pattern of having getters and setters which optionally set it on creation if it's available is is how it's done elsewhere in Drupal (Forms, Views).

    On the other hand, we are currently using the Request object without checking it exists, and the code isn't going to work without it, so it probably does make sense to inject it, and get the current request at time of use only, not at time of construction.

    • Rob230 committed 0e139447 on 1.x
      Issue #3374316 by sidharth_soman, Rob230: Use dependency injection for...
  • Status changed to Fixed 12 months ago
  • 🇬🇧United Kingdom Rob230
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.69.0 2024