Problem/Motivation

https://mglaman.dev/blog/dependency-injection-anti-patterns-drupal

Assigning a configuration object to a property instead of the configuration factory

This anti-pattern is the most common, as everyone works with configuration objects. The configuration factory gets injected into the service, retrieving the configuration object during service construction. This involves creating objects from a factory during construction instead of at time of need. Loading configuration objects should be delegated until the configuration object is needed.

$this->config = $config_factory->get('mymodule.settings');

I have also seen developers interact with the configuration in the object to set up other properties as flags inside a service. If you need these properties to act as flags from configuration values, consider how or why those properties are used. It has a code smell for a refactoring target.

// __construct
$this->configFactory = $config_factory;

// method
$config = $this->configFactory->get('mymodule.settings');

Steps to reproduce

Proposed resolution

Move the config get to the methods.

Issue fork piwik_pro-3589922

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

joshahubbers created an issue. See original summary.

joshahubbers’s picture

Status: Active » Needs review