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 |
||
13 | class Container implements ContainerInterface |
||
14 | { |
||
15 | /** |
||
16 | * Array of singletons |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $shares = []; |
||
20 | |||
21 | /** |
||
22 | * Array pf definitions, support instance,callable,Definition, class |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $definitions = []; |
||
26 | |||
27 | /** |
||
28 | * Array of interface bindings |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $contextBindings = []; |
||
32 | |||
33 | /** |
||
34 | * Array of parameters |
||
35 | * @var ParameterStore |
||
36 | */ |
||
37 | protected $parameters; |
||
38 | |||
39 | /** |
||
40 | * @var ClassDefinitionResolver |
||
41 | */ |
||
42 | protected $classDefinitionResolver; |
||
43 | |||
44 | public function __construct() |
||
49 | |||
50 | /** |
||
51 | * Add a Definition class |
||
52 | * @param string $name |
||
53 | * @param string $class |
||
54 | * @return ClassDefinition |
||
55 | */ |
||
56 | public function define($name, $class) |
||
62 | |||
63 | /** |
||
64 | * Bind an callable to the container with its name |
||
65 | * @param string $name |
||
66 | * @param mixed $creation A invalid callable |
||
67 | * @throws ConfigException |
||
68 | * @return $this |
||
69 | */ |
||
70 | public function call($name, $creation) |
||
78 | |||
79 | /** |
||
80 | * Bind an instance to the container with its name |
||
81 | * ``` |
||
82 | * $container->instance('user', new User()); |
||
83 | * //Or just give instance |
||
84 | * $container->instance(new User()); |
||
85 | * |
||
86 | * ``` |
||
87 | * @param string $name |
||
88 | * @param object $instance |
||
89 | * @throws ConfigException |
||
90 | * @return $this |
||
91 | */ |
||
92 | public function instance($name, $instance = null) |
||
105 | |||
106 | /** |
||
107 | * Binds an interface or abstract class to its implementation; |
||
108 | * It's also be used to bind a service name to an existing class |
||
109 | * @param string $name |
||
110 | * @param string $implementation |
||
111 | * @param string|array $context the specified context to bind |
||
112 | * @throws ConfigException |
||
113 | * @return $this |
||
114 | */ |
||
115 | public function bind($name, $implementation, $context = null) |
||
132 | |||
133 | /** |
||
134 | * Share the service by given name |
||
135 | * @param string $name |
||
136 | * @return $this |
||
137 | */ |
||
138 | public function share($name) |
||
143 | |||
144 | /** |
||
145 | * Add a definition to the container |
||
146 | * ``` |
||
147 | * //Add an instance like "instance" method |
||
148 | * $container->set('student', new Student()); |
||
149 | * |
||
150 | * //Add a callable definition |
||
151 | * $container->set('student', 'StudentFactory::create'); |
||
152 | * $container->set('student', function(){ |
||
153 | * return new Student(); |
||
154 | * }); |
||
155 | * |
||
156 | * //Add an instance of "Slince\Di\Definition" |
||
157 | * $container->set('student', new Definition('Foo\Bar\StudentClass', [ |
||
158 | * 'gender' => 'boy', |
||
159 | * 'school' => new Reference('school') |
||
160 | * ], [ |
||
161 | * 'setAge' => [18] |
||
162 | * ], [ |
||
163 | * 'father' => 'James', |
||
164 | * 'mather' => 'Sophie' |
||
165 | * ])); |
||
166 | * |
||
167 | * //Add a class definition |
||
168 | * $container->set('student', Foo\Bar\StudentClass); |
||
169 | * ``` |
||
170 | * @param string $name |
||
171 | * @param mixed $definition |
||
172 | * @throws ConfigException |
||
173 | * @return $this |
||
174 | */ |
||
175 | public function set($name, $definition) |
||
188 | |||
189 | /** |
||
190 | * Get a service instance by specified name |
||
191 | * @param string $name |
||
192 | * @param array $arguments |
||
193 | * @return object |
||
194 | */ |
||
195 | public function get($name, $arguments = []) |
||
216 | |||
217 | /** |
||
218 | * {@inheritdoc} |
||
219 | */ |
||
220 | public function has($name) |
||
230 | |||
231 | /** |
||
232 | * Gets all global parameters |
||
233 | * @return array |
||
234 | */ |
||
235 | public function getParameters() |
||
239 | |||
240 | /** |
||
241 | * Sets array of parameters |
||
242 | * @param array $parameterStore |
||
243 | */ |
||
244 | public function setParameters(array $parameterStore) |
||
248 | |||
249 | /** |
||
250 | * Add some parameters |
||
251 | * @param array $parameters |
||
252 | */ |
||
253 | public function addParameters(array $parameters) |
||
257 | |||
258 | /** |
||
259 | * Sets a parameter with its name and value |
||
260 | * @param $name |
||
261 | * @param mixed $value |
||
262 | */ |
||
263 | public function setParameter($name, $value) |
||
267 | |||
268 | /** |
||
269 | * Gets a parameter by given name |
||
270 | * @param $name |
||
271 | * @param mixed $default |
||
272 | * @return mixed |
||
273 | */ |
||
274 | public function getParameter($name, $default = null) |
||
278 | |||
279 | /** |
||
280 | * Resolves all arguments for the function or method. |
||
281 | * @param \ReflectionFunctionAbstract $method |
||
282 | * @param array $arguments |
||
283 | * @param array $contextBindings The context bindings for the function |
||
284 | * @throws DependencyInjectionException |
||
285 | * @return array |
||
286 | */ |
||
287 | public function resolveFunctionArguments(\ReflectionFunctionAbstract $method, array $arguments, array $contextBindings = []) |
||
323 | |||
324 | /** |
||
325 | * Gets all context bindings for the class and method |
||
326 | * [ |
||
327 | * 'User' => [ |
||
328 | * 'original' => 'SchoolInterface' |
||
329 | * 'bind' => 'MagicSchool', |
||
330 | * ] |
||
331 | * ] |
||
332 | * @param string $contextClass |
||
333 | * @param string $contextMethod |
||
334 | * @return array |
||
335 | */ |
||
336 | public function getContextBindings($contextClass, $contextMethod) |
||
348 | |||
349 | protected function createInstanceFromDefinition($definition, array $arguments) |
||
367 | |||
368 | protected function getClassDefinitionResolver() |
||
375 | |||
376 | /** |
||
377 | * Resolves array of parameters |
||
378 | * @param array $parameters |
||
379 | * @return array |
||
380 | */ |
||
381 | protected function resolveParameters($parameters) |
||
394 | |||
395 | /** |
||
396 | * Formats parameter value |
||
397 | * @param string $value |
||
398 | * @return string |
||
399 | * @throws DependencyInjectionException |
||
400 | */ |
||
401 | protected function formatParameter($value) |
||
420 | } |
||
421 |
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: