Complex classes like Container often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Container, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
101 | class Container extends Component |
||
102 | { |
||
103 | /** |
||
104 | * @var array singleton objects indexed by their types |
||
105 | */ |
||
106 | private $_singletons = []; |
||
107 | /** |
||
108 | * @var array object definitions indexed by their types |
||
109 | */ |
||
110 | private $_definitions = []; |
||
111 | /** |
||
112 | * @var array constructor parameters indexed by object types |
||
113 | */ |
||
114 | private $_params = []; |
||
115 | /** |
||
116 | * @var array cached ReflectionClass objects indexed by class/interface names |
||
117 | */ |
||
118 | private $_reflections = []; |
||
119 | /** |
||
120 | * @var array cached dependencies indexed by class/interface names. Each class name |
||
121 | * is associated with a list of constructor parameter types or default values. |
||
122 | */ |
||
123 | private $_dependencies = []; |
||
124 | /** |
||
125 | * @var array used to collect classes instantiated during get to detect circular references |
||
126 | */ |
||
127 | private $_getting = []; |
||
128 | |||
129 | /** |
||
130 | * Returns an instance of the requested class. |
||
131 | * |
||
132 | * You may provide constructor parameters (`$params`) and object configurations (`$config`) |
||
133 | * that will be used during the creation of the instance. |
||
134 | * |
||
135 | * If the class implements [[\yii\base\Configurable]], the `$config` parameter will be passed as the last |
||
136 | * parameter to the class constructor; Otherwise, the configuration will be applied *after* the object is |
||
137 | * instantiated. |
||
138 | * |
||
139 | * Note that if the class is declared to be singleton by calling [[setSingleton()]], |
||
140 | * the same instance of the class will be returned each time this method is called. |
||
141 | * In this case, the constructor parameters and object configurations will be used |
||
142 | * only if the class is instantiated the first time. |
||
143 | * |
||
144 | * @param string $class the class name or an alias name (e.g. `foo`) that was previously registered via [[set()]] |
||
145 | * or [[setSingleton()]]. |
||
146 | * @param array $params a list of constructor parameter values. The parameters should be provided in the order |
||
147 | * they appear in the constructor declaration. If you want to skip some parameters, you should index the remaining |
||
148 | * ones with the integers that represent their positions in the constructor parameter list. |
||
149 | * @param array $config a list of name-value pairs that will be used to initialize the object properties. |
||
150 | * @return object an instance of the requested class. |
||
151 | * @throws InvalidConfigException if the class cannot be recognized or correspond to an invalid definition |
||
152 | * @throws NotInstantiableException If resolved to an abstract class or an interface (since 2.0.9) |
||
153 | */ |
||
154 | 3256 | public function get($class, $params = [], $config = []) |
|
199 | |||
200 | /** |
||
201 | * Registers a class definition with this container. |
||
202 | * |
||
203 | * For example, |
||
204 | * |
||
205 | * ```php |
||
206 | * // register a class name as is. This can be skipped. |
||
207 | * $container->set(\yii\db\Connection::class); |
||
208 | * |
||
209 | * // register an interface |
||
210 | * // When a class depends on the interface, the corresponding class |
||
211 | * // will be instantiated as the dependent object |
||
212 | * $container->set(\yii\mail\MailInterface::class, \yii\swiftmailer\Mailer::class); |
||
213 | * |
||
214 | * // register an alias name. You can use $container->get('foo') |
||
215 | * // to create an instance of Connection |
||
216 | * $container->set('foo', \yii\db\Connection::class); |
||
217 | * |
||
218 | * // register a class with configuration. The configuration |
||
219 | * // will be applied when the class is instantiated by get() |
||
220 | * $container->set(\yii\db\Connection::class, [ |
||
221 | * 'dsn' => 'mysql:host=127.0.0.1;dbname=demo', |
||
222 | * 'username' => 'root', |
||
223 | * 'password' => '', |
||
224 | * 'charset' => 'utf8', |
||
225 | * ]); |
||
226 | * |
||
227 | * // register an alias name with class configuration |
||
228 | * // In this case, a "class" element is required to specify the class |
||
229 | * $container->set('db', [ |
||
230 | * '__class' => \yii\db\Connection::class, |
||
231 | * 'dsn' => 'mysql:host=127.0.0.1;dbname=demo', |
||
232 | * 'username' => 'root', |
||
233 | * 'password' => '', |
||
234 | * 'charset' => 'utf8', |
||
235 | * ]); |
||
236 | * |
||
237 | * // register a PHP callable |
||
238 | * // The callable will be executed when $container->get('db') is called |
||
239 | * $container->set('db', function ($container, $params, $config) { |
||
240 | * return new \yii\db\Connection($config); |
||
241 | * }); |
||
242 | * ``` |
||
243 | * |
||
244 | * If a class definition with the same name already exists, it will be overwritten with the new one. |
||
245 | * You may use [[has()]] to check if a class definition already exists. |
||
246 | * |
||
247 | * @param string $class class name, interface name or alias name |
||
248 | * @param mixed $definition the definition associated with `$class`. It can be one of the following: |
||
249 | * |
||
250 | * - a PHP callable: The callable will be executed when [[get()]] is invoked. The signature of the callable |
||
251 | * should be `function ($container, $params, $config)`, where `$params` stands for the list of constructor |
||
252 | * parameters, `$config` the object configuration, and `$container` the container object. The return value |
||
253 | * of the callable will be returned by [[get()]] as the object instance requested. |
||
254 | * - a configuration array: the array contains name-value pairs that will be used to initialize the property |
||
255 | * values of the newly created object when [[get()]] is called. The `class` element stands for the |
||
256 | * the class of the object to be created. If `class` is not specified, `$class` will be used as the class name. |
||
257 | * - a string: a class name, an interface name or an alias name. |
||
258 | * @param array $params the list of constructor parameters. The parameters will be passed to the class |
||
259 | * constructor when [[get()]] is called. |
||
260 | * @return $this the container itself |
||
261 | */ |
||
262 | 534 | public function set($class, $definition = [], array $params = []) |
|
269 | /** |
||
270 | * Registers a class definition with this container and marks the class as a singleton class. |
||
271 | * |
||
272 | * This method is similar to [[set()]] except that classes registered via this method will only have one |
||
273 | * instance. Each time [[get()]] is called, the same instance of the specified class will be returned. |
||
274 | * |
||
275 | * @param string $class class name, interface name or alias name |
||
276 | * @param mixed $definition the definition associated with `$class`. See [[set()]] for more details. |
||
277 | * @param array $params the list of constructor parameters. The parameters will be passed to the class |
||
278 | * constructor when [[get()]] is called. |
||
279 | * @return $this the container itself |
||
280 | * @see set() |
||
281 | */ |
||
282 | 1 | public function setSingleton($class, $definition = [], array $params = []) |
|
289 | |||
290 | /** |
||
291 | * Returns a value indicating whether the container has the definition of the specified name. |
||
292 | * @param string $class class name, interface name or alias name |
||
293 | * @return bool whether the container has the definition of the specified name.. |
||
294 | * @see set() |
||
295 | */ |
||
296 | public function has($class) |
||
300 | |||
301 | /** |
||
302 | * Returns a value indicating whether the given name corresponds to a registered singleton. |
||
303 | * @param string $class class name, interface name or alias name |
||
304 | * @param bool $checkInstance whether to check if the singleton has been instantiated. |
||
305 | * @return bool whether the given name corresponds to a registered singleton. If `$checkInstance` is true, |
||
306 | * the method should return a value indicating whether the singleton has been instantiated. |
||
307 | */ |
||
308 | public function hasSingleton($class, $checkInstance = false) |
||
312 | |||
313 | /** |
||
314 | * Removes the definition for the specified name. |
||
315 | * @param string $class class name, interface name or alias name |
||
316 | */ |
||
317 | public function clear($class) |
||
321 | |||
322 | /** |
||
323 | * Normalizes the class definition. |
||
324 | * @param string $class class name |
||
325 | * @param string|array|callable $definition the class definition |
||
326 | * @return array the normalized class definition |
||
327 | * @throws InvalidConfigException if the definition is invalid. |
||
328 | */ |
||
329 | 535 | protected function normalizeDefinition($class, $definition) |
|
351 | |||
352 | /** |
||
353 | * Returns the list of the object definitions or the loaded shared objects. |
||
354 | * @return array the list of the object definitions or the loaded shared objects (type or ID => definition or instance). |
||
355 | */ |
||
356 | public function getDefinitions() |
||
360 | |||
361 | /** |
||
362 | * Creates an instance of the specified class. |
||
363 | * This method will resolve dependencies of the specified class, instantiate them, and inject |
||
364 | * them into the new instance of the specified class. |
||
365 | * @param string $class the class name |
||
366 | * @param array $params constructor parameters |
||
367 | * @param array $config configurations to be applied to the new instance |
||
368 | * @return object the newly created instance of the specified class |
||
369 | * @throws NotInstantiableException If resolved to an abstract class or an interface (since 2.0.9) |
||
370 | */ |
||
371 | 3255 | protected function build($class, $params, $config) |
|
410 | |||
411 | /** |
||
412 | * Merges the user-specified constructor parameters with the ones registered via [[set()]]. |
||
413 | * @param string $class class name, interface name or alias name |
||
414 | * @param array $params the constructor parameters |
||
415 | * @return array the merged parameters |
||
416 | */ |
||
417 | 24 | protected function mergeParams($class, $params) |
|
432 | |||
433 | /** |
||
434 | * Returns the dependencies of the specified class. |
||
435 | * @param string $class class name, interface name or alias name |
||
436 | * @return array the dependencies of the specified class. |
||
437 | */ |
||
438 | 3255 | protected function getDependencies($class) |
|
466 | |||
467 | /** |
||
468 | * Resolves dependencies by replacing them with the actual object instances. |
||
469 | * @param array $dependencies the dependencies |
||
470 | * @param ReflectionClass $reflection the class reflection associated with the dependencies |
||
471 | * @return array the resolved dependencies |
||
472 | * @throws InvalidConfigException if a dependency cannot be resolved or if a dependency cannot be fulfilled. |
||
473 | */ |
||
474 | 3253 | protected function resolveDependencies($dependencies, $reflection = null) |
|
490 | |||
491 | /** |
||
492 | * Invoke a callback with resolving dependencies in parameters. |
||
493 | * |
||
494 | * This methods allows invoking a callback and let type hinted parameter names to be |
||
495 | * resolved as objects of the Container. It additionally allow calling function using named parameters. |
||
496 | * |
||
497 | * For example, the following callback may be invoked using the Container to resolve the formatter dependency: |
||
498 | * |
||
499 | * ```php |
||
500 | * $formatString = function($string, \yii\i18n\Formatter $formatter) { |
||
501 | * // ... |
||
502 | * } |
||
503 | * Yii::$container->invoke($formatString, ['string' => 'Hello World!']); |
||
504 | * ``` |
||
505 | * |
||
506 | * This will pass the string `'Hello World!'` as the first param, and a formatter instance created |
||
507 | * by the DI container as the second param to the callable. |
||
508 | * |
||
509 | * @param callable $callback callable to be invoked. |
||
510 | * @param array $params The array of parameters for the function. |
||
511 | * This can be either a list of parameters, or an associative array representing named function parameters. |
||
512 | * @return mixed the callback return value. |
||
513 | * @throws InvalidConfigException if a dependency cannot be resolved or if a dependency cannot be fulfilled. |
||
514 | * @throws NotInstantiableException If resolved to an abstract class or an interface (since 2.0.9) |
||
515 | * @since 2.0.7 |
||
516 | */ |
||
517 | 10 | public function invoke(callable $callback, $params = []) |
|
525 | |||
526 | /** |
||
527 | * Resolve dependencies for a function. |
||
528 | * |
||
529 | * This method can be used to implement similar functionality as provided by [[invoke()]] in other |
||
530 | * components. |
||
531 | * |
||
532 | * @param callable $callback callable to be invoked. |
||
533 | * @param array $params The array of parameters for the function, can be either numeric or associative. |
||
534 | * @return array The resolved dependencies. |
||
535 | * @throws InvalidConfigException if a dependency cannot be resolved or if a dependency cannot be fulfilled. |
||
536 | * @throws NotInstantiableException If resolved to an abstract class or an interface (since 2.0.9) |
||
537 | * @since 2.0.7 |
||
538 | */ |
||
539 | 11 | public function resolveCallableDependencies(callable $callback, $params = []) |
|
596 | |||
597 | /** |
||
598 | * Registers class definitions within this container. |
||
599 | * |
||
600 | * @param array $definitions array of definitions. There are two allowed formats of array. |
||
601 | * The first format: |
||
602 | * - key: class name, interface name or alias name. The key will be passed to the [[set()]] method |
||
603 | * as a first argument `$class`. |
||
604 | * - value: the definition associated with `$class`. Possible values are described in |
||
605 | * [[set()]] documentation for the `$definition` parameter. Will be passed to the [[set()]] method |
||
606 | * as the second argument `$definition`. |
||
607 | * |
||
608 | * Example: |
||
609 | * ```php |
||
610 | * $container->setDefinitions([ |
||
611 | * 'yii\web\Request' => 'app\components\Request', |
||
612 | * 'yii\web\Response' => [ |
||
613 | * '__class' => 'app\components\Response', |
||
614 | * 'format' => 'json' |
||
615 | * ], |
||
616 | * 'foo\Bar' => function () { |
||
617 | * $qux = new Qux; |
||
618 | * $foo = new Foo($qux); |
||
619 | * return new Bar($foo); |
||
620 | * } |
||
621 | * ]); |
||
622 | * ``` |
||
623 | * |
||
624 | * The second format: |
||
625 | * - key: class name, interface name or alias name. The key will be passed to the [[set()]] method |
||
626 | * as a first argument `$class`. |
||
627 | * - value: array of two elements. The first element will be passed the [[set()]] method as the |
||
628 | * second argument `$definition`, the second one — as `$params`. |
||
629 | * |
||
630 | * Example: |
||
631 | * ```php |
||
632 | * $container->setDefinitions([ |
||
633 | * 'foo\Bar' => [ |
||
634 | * ['__class' => 'app\Bar'], |
||
635 | * [Instance::of('baz')] |
||
636 | * ] |
||
637 | * ]); |
||
638 | * ``` |
||
639 | * |
||
640 | * @see set() to know more about possible values of definitions |
||
641 | * @since 2.0.11 |
||
642 | */ |
||
643 | 2 | public function setDefinitions(array $definitions) |
|
654 | |||
655 | /** |
||
656 | * Registers class definitions as singletons within this container by calling [[setSingleton()]]. |
||
657 | * |
||
658 | * @param array $singletons array of singleton definitions. See [[setDefinitions()]] |
||
659 | * for allowed formats of array. |
||
660 | * |
||
661 | * @see setDefinitions() for allowed formats of $singletons parameter |
||
662 | * @see setSingleton() to know more about possible values of definitions |
||
663 | * @since 2.0.11 |
||
664 | */ |
||
665 | 1 | public function setSingletons(array $singletons) |
|
676 | } |
||
677 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: