Dependency injection anti-patterns

Created on 21 November 2023, 10 months ago
Updated 28 March 2024, 6 months ago

Problem/Motivation

According to @mglaman having a get method in constructor is an anti pattern since the object is not lazy loaded anymore.

That is the case with ohdear_integration.settings service:

https://git.drupalcode.org/project/ohdear_integration/-/blob/2.x/ohdear_...

Proposed resolution

Should be replaced with config.factory service.

📌 Task
Status

Fixed

Version

2.0

Component

Code

Created by

🇸🇮Slovenia useernamee Ljubljana

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

Merge Requests

Comments & Activities

  • Issue created by @useernamee
  • Status changed to Needs review 8 months ago
  • 🇳🇱Netherlands casey

    In development environments the api key is typically not provided. When the api key is missing currently every drush call triggers at least one " [error] OhDear site id is missing! Fix it ..."

  • 🇸🇮Slovenia useernamee Ljubljana

    @casey - good point - should we add some checkbox to tell the module that the site is not online? Or maybe we should just handle missing API key differently (e.g. we could move the functionality into submodule).

    Are there any good examples from other modules, how they handle similar scenarios?

  • Status changed to Needs work 7 months ago
  • 🇸🇮Slovenia useernamee Ljubljana

    I've checked the patch.
    ohdear_integration.settings service should be removed as well from services.yml file.
    Then from:
    - https://git.drupalcode.org/project/ohdear_integration/-/blob/2.x/src/OhD...

    Otherwise the patch looks good. It just need a few more things to take care of.

  • First commit to issue fork.
  • Merge request !19Fix dependency injection antipatterns. → (Merged) created by roderik
  • Status changed to Needs review 7 months ago
  • 🇳🇱Netherlands roderik Amsterdam,NL / Budapest,HU

    I did the requested, and/but:

    • I didn't remove ohdear_integration.settings from the service file, because this is a published stable module and its users might be using the service. Instead, there's a deprecation message now. Maybe you can do a followup task for removing them in v3.
    • I also removed the logger service in the same way. If using a settings object/service is an antipattern, then a logger is an antipattern too. This made me remove the $logger class variable from various classes where it wasn't used.
    • fixed phpcs errors because there were few besides code I was already touching anyway.

    I hope you don't mind long lines because I just replaced ohDearIntegrationSettings with configFactory->get('ohdear_integration.settings') on the same single line.

    Tested, after a 'drush cr', to make sure I didn't do anything dumb:

    • executed getLogger() in OhDearIntegrationController and OhDearSdkService::getSiteId()

    Did not test:

    • casey's change in OhDearSdkService::getSite() - because I have no Ohdear API key/settings handy. Reviewed the change, and it looks right / functionally-unchanged to me.
  • 🇸🇮Slovenia useernamee Ljubljana

    Reviewed and it looks good.

    Thank you @casey & @roderik for you contributions.

  • Pipeline finished with Skipped
    7 months ago
    #119274
  • Status changed to Fixed 7 months ago
  • 🇸🇮Slovenia useernamee Ljubljana
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024