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 |
||
98 | class Container extends Component |
||
99 | { |
||
100 | /** |
||
101 | * @var array singleton objects indexed by their types |
||
102 | */ |
||
103 | private $_singletons = []; |
||
104 | /** |
||
105 | * @var array object definitions indexed by their types |
||
106 | */ |
||
107 | private $_definitions = []; |
||
108 | /** |
||
109 | * @var array constructor parameters indexed by object types |
||
110 | */ |
||
111 | private $_params = []; |
||
112 | /** |
||
113 | * @var array cached ReflectionClass objects indexed by class/interface names |
||
114 | */ |
||
115 | private $_reflections = []; |
||
116 | /** |
||
117 | * @var array cached dependencies indexed by class/interface names. Each class name |
||
118 | * is associated with a list of constructor parameter types or default values. |
||
119 | */ |
||
120 | private $_dependencies = []; |
||
121 | |||
122 | |||
123 | /** |
||
124 | * Returns an instance of the requested class. |
||
125 | * |
||
126 | * You may provide constructor parameters (`$params`) and object configurations (`$config`) |
||
127 | * that will be used during the creation of the instance. |
||
128 | * |
||
129 | * If the class implements [[\yii\base\Configurable]], the `$config` parameter will be passed as the last |
||
130 | * parameter to the class constructor; Otherwise, the configuration will be applied *after* the object is |
||
131 | * instantiated. |
||
132 | * |
||
133 | * Note that if the class is declared to be singleton by calling [[setSingleton()]], |
||
134 | * the same instance of the class will be returned each time this method is called. |
||
135 | * In this case, the constructor parameters and object configurations will be used |
||
136 | * only if the class is instantiated the first time. |
||
137 | * |
||
138 | * @param string $class the class name or an alias name (e.g. `foo`) that was previously registered via [[set()]] |
||
139 | * or [[setSingleton()]]. |
||
140 | * @param array $params a list of constructor parameter values. The parameters should be provided in the order |
||
141 | * they appear in the constructor declaration. If you want to skip some parameters, you should index the remaining |
||
142 | * ones with the integers that represent their positions in the constructor parameter list. |
||
143 | * @param array $config a list of name-value pairs that will be used to initialize the object properties. |
||
144 | * @return object an instance of the requested class. |
||
145 | * @throws InvalidConfigException if the class cannot be recognized or correspond to an invalid definition |
||
146 | */ |
||
147 | 1535 | public function get($class, $params = [], $config = []) |
|
186 | |||
187 | /** |
||
188 | * Registers a class definition with this container. |
||
189 | * |
||
190 | * For example, |
||
191 | * |
||
192 | * ```php |
||
193 | * // register a class name as is. This can be skipped. |
||
194 | * $container->set('yii\db\Connection'); |
||
195 | * |
||
196 | * // register an interface |
||
197 | * // When a class depends on the interface, the corresponding class |
||
198 | * // will be instantiated as the dependent object |
||
199 | * $container->set('yii\mail\MailInterface', 'yii\swiftmailer\Mailer'); |
||
200 | * |
||
201 | * // register an alias name. You can use $container->get('foo') |
||
202 | * // to create an instance of Connection |
||
203 | * $container->set('foo', 'yii\db\Connection'); |
||
204 | * |
||
205 | * // register a class with configuration. The configuration |
||
206 | * // will be applied when the class is instantiated by get() |
||
207 | * $container->set('yii\db\Connection', [ |
||
208 | * 'dsn' => 'mysql:host=127.0.0.1;dbname=demo', |
||
209 | * 'username' => 'root', |
||
210 | * 'password' => '', |
||
211 | * 'charset' => 'utf8', |
||
212 | * ]); |
||
213 | * |
||
214 | * // register an alias name with class configuration |
||
215 | * // In this case, a "class" element is required to specify the class |
||
216 | * $container->set('db', [ |
||
217 | * 'class' => 'yii\db\Connection', |
||
218 | * 'dsn' => 'mysql:host=127.0.0.1;dbname=demo', |
||
219 | * 'username' => 'root', |
||
220 | * 'password' => '', |
||
221 | * 'charset' => 'utf8', |
||
222 | * ]); |
||
223 | * |
||
224 | * // register a PHP callable |
||
225 | * // The callable will be executed when $container->get('db') is called |
||
226 | * $container->set('db', function ($container, $params, $config) { |
||
227 | * return new \yii\db\Connection($config); |
||
228 | * }); |
||
229 | * ``` |
||
230 | * |
||
231 | * If a class definition with the same name already exists, it will be overwritten with the new one. |
||
232 | * You may use [[has()]] to check if a class definition already exists. |
||
233 | * |
||
234 | * @param string $class class name, interface name or alias name |
||
235 | * @param mixed $definition the definition associated with `$class`. It can be one of the following: |
||
236 | * |
||
237 | * - a PHP callable: The callable will be executed when [[get()]] is invoked. The signature of the callable |
||
238 | * should be `function ($container, $params, $config)`, where `$params` stands for the list of constructor |
||
239 | * parameters, `$config` the object configuration, and `$container` the container object. The return value |
||
240 | * of the callable will be returned by [[get()]] as the object instance requested. |
||
241 | * - a configuration array: the array contains name-value pairs that will be used to initialize the property |
||
242 | * values of the newly created object when [[get()]] is called. The `class` element stands for the |
||
243 | * the class of the object to be created. If `class` is not specified, `$class` will be used as the class name. |
||
244 | * - a string: a class name, an interface name or an alias name. |
||
245 | * @param array $params the list of constructor parameters. The parameters will be passed to the class |
||
246 | * constructor when [[get()]] is called. |
||
247 | * @return $this the container itself |
||
248 | */ |
||
249 | 326 | public function set($class, $definition = [], array $params = []) |
|
256 | |||
257 | /** |
||
258 | * Registers a class definition with this container and marks the class as a singleton class. |
||
259 | * |
||
260 | * This method is similar to [[set()]] except that classes registered via this method will only have one |
||
261 | * instance. Each time [[get()]] is called, the same instance of the specified class will be returned. |
||
262 | * |
||
263 | * @param string $class class name, interface name or alias name |
||
264 | * @param mixed $definition the definition associated with `$class`. See [[set()]] for more details. |
||
265 | * @param array $params the list of constructor parameters. The parameters will be passed to the class |
||
266 | * constructor when [[get()]] is called. |
||
267 | * @return $this the container itself |
||
268 | * @see set() |
||
269 | */ |
||
270 | public function setSingleton($class, $definition = [], array $params = []) |
||
277 | |||
278 | /** |
||
279 | * Returns a value indicating whether the container has the definition of the specified name. |
||
280 | * @param string $class class name, interface name or alias name |
||
281 | * @return boolean whether the container has the definition of the specified name.. |
||
282 | * @see set() |
||
283 | */ |
||
284 | public function has($class) |
||
288 | |||
289 | /** |
||
290 | * Returns a value indicating whether the given name corresponds to a registered singleton. |
||
291 | * @param string $class class name, interface name or alias name |
||
292 | * @param boolean $checkInstance whether to check if the singleton has been instantiated. |
||
293 | * @return boolean whether the given name corresponds to a registered singleton. If `$checkInstance` is true, |
||
294 | * the method should return a value indicating whether the singleton has been instantiated. |
||
295 | */ |
||
296 | public function hasSingleton($class, $checkInstance = false) |
||
300 | |||
301 | /** |
||
302 | * Removes the definition for the specified name. |
||
303 | * @param string $class class name, interface name or alias name |
||
304 | */ |
||
305 | public function clear($class) |
||
309 | |||
310 | /** |
||
311 | * Normalizes the class definition. |
||
312 | * @param string $class class name |
||
313 | * @param string|array|callable $definition the class definition |
||
314 | * @return array the normalized class definition |
||
315 | * @throws InvalidConfigException if the definition is invalid. |
||
316 | */ |
||
317 | 326 | protected function normalizeDefinition($class, $definition) |
|
338 | |||
339 | /** |
||
340 | * Returns the list of the object definitions or the loaded shared objects. |
||
341 | * @return array the list of the object definitions or the loaded shared objects (type or ID => definition or instance). |
||
342 | */ |
||
343 | public function getDefinitions() |
||
347 | |||
348 | /** |
||
349 | * Creates an instance of the specified class. |
||
350 | * This method will resolve dependencies of the specified class, instantiate them, and inject |
||
351 | * them into the new instance of the specified class. |
||
352 | * @param string $class the class name |
||
353 | * @param array $params constructor parameters |
||
354 | * @param array $config configurations to be applied to the new instance |
||
355 | * @return object the newly created instance of the specified class |
||
356 | */ |
||
357 | 1535 | protected function build($class, $params, $config) |
|
358 | { |
||
359 | /* @var $reflection ReflectionClass */ |
||
360 | 1535 | list ($reflection, $dependencies) = $this->getDependencies($class); |
|
361 | |||
362 | 1534 | foreach ($params as $index => $param) { |
|
363 | 233 | $dependencies[$index] = $param; |
|
364 | 1534 | } |
|
365 | |||
366 | 1534 | $dependencies = $this->resolveDependencies($dependencies, $reflection); |
|
367 | 1534 | if (empty($config)) { |
|
368 | 1005 | return $reflection->newInstanceArgs($dependencies); |
|
369 | } |
||
370 | |||
371 | 1484 | if (!empty($dependencies) && $reflection->implementsInterface('yii\base\Configurable')) { |
|
372 | // set $config as the last parameter (existing one will be overwritten) |
||
373 | 1483 | $dependencies[count($dependencies) - 1] = $config; |
|
374 | 1483 | return $reflection->newInstanceArgs($dependencies); |
|
375 | } else { |
||
376 | 1 | $object = $reflection->newInstanceArgs($dependencies); |
|
377 | 1 | foreach ($config as $name => $value) { |
|
378 | 1 | $object->$name = $value; |
|
379 | 1 | } |
|
380 | 1 | return $object; |
|
381 | } |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * Merges the user-specified constructor parameters with the ones registered via [[set()]]. |
||
386 | * @param string $class class name, interface name or alias name |
||
387 | * @param array $params the constructor parameters |
||
388 | * @return array the merged parameters |
||
389 | */ |
||
390 | 11 | protected function mergeParams($class, $params) |
|
404 | |||
405 | /** |
||
406 | * Returns the dependencies of the specified class. |
||
407 | * @param string $class class name, interface name or alias name |
||
408 | * @return array the dependencies of the specified class. |
||
409 | */ |
||
410 | 1535 | protected function getDependencies($class) |
|
436 | |||
437 | /** |
||
438 | * Resolves dependencies by replacing them with the actual object instances. |
||
439 | * @param array $dependencies the dependencies |
||
440 | * @param ReflectionClass $reflection the class reflection associated with the dependencies |
||
441 | * @return array the resolved dependencies |
||
442 | * @throws InvalidConfigException if a dependency cannot be resolved or if a dependency cannot be fulfilled. |
||
443 | */ |
||
444 | 1534 | protected function resolveDependencies($dependencies, $reflection = null) |
|
459 | |||
460 | /** |
||
461 | * Invoke a callback with resolving dependencies in parameters. |
||
462 | * |
||
463 | * This methods allows invoking a callback and let type hinted parameter names to be |
||
464 | * resolved as objects of the Container. It additionally allow calling function using named parameters. |
||
465 | * |
||
466 | * For example, the following callback may be invoked using the Container to resolve the formatter dependency: |
||
467 | * |
||
468 | * ```php |
||
469 | * $formatString = function($string, \yii\i18n\Formatter $formatter) { |
||
470 | * // ... |
||
471 | * } |
||
472 | * Yii::$container->invoke($formatString, ['string' => 'Hello World!']); |
||
473 | * ``` |
||
474 | * |
||
475 | * This will pass the string `'Hello World!'` as the first param, and a formatter instance created |
||
476 | * by the DI container as the second param to the callable. |
||
477 | * |
||
478 | * @param callable $callback callable to be invoked. |
||
479 | * @param array $params The array of parameters for the function. |
||
480 | * This can be either a list of parameters, or an associative array representing named function parameters. |
||
481 | * @return mixed the callback return value. |
||
482 | * @throws InvalidConfigException if a dependency cannot be resolved or if a dependency cannot be fulfilled. |
||
483 | * @since 2.0.7 |
||
484 | */ |
||
485 | 4 | public function invoke(callable $callback, $params = []) |
|
493 | |||
494 | /** |
||
495 | * Resolve dependencies for a function. |
||
496 | * |
||
497 | * This method can be used to implement similar functionality as provided by [[invoke()]] in other |
||
498 | * components. |
||
499 | * |
||
500 | * @param callable $callback callable to be invoked. |
||
501 | * @param array $params The array of parameters for the function, can be either numeric or associative. |
||
502 | * @return array The resolved dependencies. |
||
503 | * @throws InvalidConfigException if a dependency cannot be resolved or if a dependency cannot be fulfilled. |
||
504 | * @since 2.0.7 |
||
505 | */ |
||
506 | 5 | public function resolveCallableDependencies(callable $callback, $params = []) |
|
550 | } |
||
551 |
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: