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 |
||
16 | class Container implements ArrayAccess, ContainerContract |
||
17 | { |
||
18 | /** |
||
19 | * The current globally available container (if any). |
||
20 | * |
||
21 | * @var static |
||
22 | */ |
||
23 | protected static $instance; |
||
24 | |||
25 | /** |
||
26 | * The container's bindings. |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $bindings = []; |
||
31 | |||
32 | /** |
||
33 | * The container's shared instances. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $instances = []; |
||
38 | |||
39 | /** |
||
40 | * The stack of concretions currently being built. |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $buildStack = []; |
||
45 | |||
46 | /** |
||
47 | * Register a binding with the container. |
||
48 | * |
||
49 | * @param string $abstract |
||
50 | * @param \Closure|string|null $concrete |
||
51 | * @param bool $shared |
||
52 | * @return void |
||
53 | */ |
||
54 | public function bind(string $abstract, $concrete = null, bool $shared = false) |
||
78 | |||
79 | /** |
||
80 | * Register a shared binding in the container. |
||
81 | * |
||
82 | * @param string|array $abstract |
||
83 | * @param \Closure|string|null $concrete |
||
84 | * @return void |
||
85 | */ |
||
86 | public function singleton(string $abstract, $concrete = null) |
||
90 | |||
91 | /** |
||
92 | * Register an existing instance as shared in the container. |
||
93 | * |
||
94 | * @param string $abstract |
||
95 | * @param mixed $instance |
||
96 | * @return void |
||
97 | */ |
||
98 | public function instance(string $abstract, $instance) |
||
104 | |||
105 | /** |
||
106 | * Determine if the given abstract type has been bound. |
||
107 | * |
||
108 | * @param string $abstract |
||
109 | * @return bool |
||
110 | */ |
||
111 | public function bound(string $abstract) |
||
117 | |||
118 | /** |
||
119 | * Resolve the given type from the container. |
||
120 | * |
||
121 | * @param string $abstract |
||
122 | * @param array $parameters |
||
123 | * @return mixed |
||
124 | */ |
||
125 | public function make(string $abstract, array $parameters = []) |
||
156 | |||
157 | /** |
||
158 | * Instantiate a concrete instance of the given type. |
||
159 | * |
||
160 | * @param \Closure|string $concrete |
||
161 | * @param array $parameters |
||
162 | * @return mixed |
||
163 | * |
||
164 | * @throws \Spires\Contracts\Container\BindingResolutionException |
||
165 | */ |
||
166 | public function build($concrete, array $parameters = []) |
||
222 | |||
223 | /** |
||
224 | * Call the given Closure / [object, method] and inject its dependencies. |
||
225 | * |
||
226 | * @param callable|array $callable |
||
227 | * @param array $parameters |
||
228 | * @return mixed |
||
229 | */ |
||
230 | public function call($callable, array $parameters = []) |
||
236 | |||
237 | /** |
||
238 | * Set the globally available instance of the container. |
||
239 | * |
||
240 | * @return static |
||
241 | */ |
||
242 | public static function getInstance() |
||
246 | |||
247 | /** |
||
248 | * Set the shared instance of the container. |
||
249 | * |
||
250 | * @param \Spires\Contracts\Container\Container $container |
||
251 | * @return void |
||
252 | */ |
||
253 | public static function setInstance(ContainerContract $container) |
||
257 | |||
258 | /** |
||
259 | * Normalize the given class name by removing leading slashes. |
||
260 | * |
||
261 | * @param mixed $service |
||
262 | * @return mixed |
||
263 | */ |
||
264 | protected function normalize($service) |
||
268 | |||
269 | /** |
||
270 | * Drop all of the stale instances and aliases. |
||
271 | * |
||
272 | * @param string $abstract |
||
273 | * @return void |
||
274 | */ |
||
275 | protected function dropStaleInstances(string $abstract) |
||
279 | |||
280 | /** |
||
281 | * Get the Closure to be used when building a type. |
||
282 | * |
||
283 | * @param string $abstract |
||
284 | * @param string $concrete |
||
285 | * @return \Closure |
||
286 | */ |
||
287 | protected function getClosure(string $abstract, string $concrete) |
||
295 | |||
296 | /** |
||
297 | * Get the concrete type for a given abstract. |
||
298 | * |
||
299 | * @param string $abstract |
||
300 | * @return mixed $concrete |
||
301 | */ |
||
302 | protected function getConcrete(string $abstract) |
||
313 | |||
314 | /** |
||
315 | * Determine if the given concrete is buildable. |
||
316 | * |
||
317 | * @param mixed $concrete |
||
318 | * @param string $abstract |
||
319 | * @return bool |
||
320 | */ |
||
321 | protected function isBuildable($concrete, string $abstract) |
||
325 | |||
326 | /** |
||
327 | * Determine if a given type is shared. |
||
328 | * |
||
329 | * @param string $abstract |
||
330 | * @return bool |
||
331 | */ |
||
332 | protected function isShared(string $abstract) |
||
346 | |||
347 | /** |
||
348 | * If extra parameters are passed by numeric ID, rekey them by argument name. |
||
349 | * |
||
350 | * @param array $dependencies |
||
351 | * @param array $parameters |
||
352 | * @return array |
||
353 | */ |
||
354 | protected function keyParametersByArgument(array $dependencies, array $parameters) |
||
366 | |||
367 | /** |
||
368 | * Resolve all of the dependencies from the ReflectionParameters. |
||
369 | * |
||
370 | * @param array $parameters |
||
371 | * @param array $primitives |
||
372 | * @return array |
||
373 | */ |
||
374 | protected function getDependencies(array $parameters, array $primitives = []) |
||
395 | |||
396 | /** |
||
397 | * Resolve a non-class hinted dependency. |
||
398 | * |
||
399 | * @param \ReflectionParameter $parameter |
||
400 | * @return mixed |
||
401 | * |
||
402 | * @throws \Spires\Contracts\Container\BindingResolutionException |
||
403 | */ |
||
404 | protected function resolveNonClass(ReflectionParameter $parameter) |
||
414 | |||
415 | /** |
||
416 | * Resolve a class based dependency from the container. |
||
417 | * |
||
418 | * @param \ReflectionParameter $parameter |
||
419 | * @return mixed |
||
420 | * |
||
421 | * @throws \Spires\Contracts\Container\BindingResolutionException |
||
422 | */ |
||
423 | protected function resolveClass(ReflectionParameter $parameter) |
||
440 | |||
441 | /** |
||
442 | * Get all dependencies for a given method. |
||
443 | * |
||
444 | * @param callable|array $callable |
||
445 | * @param array $parameters |
||
446 | * @return array |
||
447 | */ |
||
448 | protected function getInjectedMethodParameters($callable, array $parameters = []) |
||
458 | |||
459 | /** |
||
460 | * Get the proper reflection instance for the given callback. |
||
461 | * |
||
462 | * @param callable|array $callable |
||
463 | * @return \ReflectionFunctionAbstract |
||
464 | */ |
||
465 | protected function getCallReflector($callable) |
||
474 | |||
475 | /** |
||
476 | * Get the dependency for the given call parameter. |
||
477 | * |
||
478 | * @param \ReflectionParameter $parameter |
||
479 | * @param array $parameters |
||
480 | * @return mixed |
||
481 | */ |
||
482 | protected function addDependencyForCallParameter(ReflectionParameter $parameter, array &$parameters) |
||
496 | |||
497 | /** |
||
498 | * Determine if a given offset exists. |
||
499 | * |
||
500 | * @param string $key |
||
501 | * @return bool |
||
502 | */ |
||
503 | public function offsetExists($key) |
||
507 | |||
508 | /** |
||
509 | * Get the value at a given offset. |
||
510 | * |
||
511 | * @param string $key |
||
512 | * @return mixed |
||
513 | */ |
||
514 | public function offsetGet($key) |
||
518 | |||
519 | /** |
||
520 | * Set the value at a given offset. |
||
521 | * |
||
522 | * @param string $key |
||
523 | * @param mixed $value |
||
524 | * @return void |
||
525 | */ |
||
526 | public function offsetSet($key, $value) |
||
539 | |||
540 | /** |
||
541 | * Unset the value at a given offset. |
||
542 | * |
||
543 | * @param string $key |
||
544 | * @return void |
||
545 | */ |
||
546 | public function offsetUnset($key) |
||
552 | |||
553 | /** |
||
554 | * Dynamically access container services. |
||
555 | * |
||
556 | * @param string $key |
||
557 | * @return mixed |
||
558 | */ |
||
559 | public function __get($key) |
||
563 | |||
564 | /** |
||
565 | * Dynamically set container services. |
||
566 | * |
||
567 | * @param string $key |
||
568 | * @param mixed $value |
||
569 | * @return void |
||
570 | */ |
||
571 | public function __set($key, $value) |
||
575 | } |
||
576 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.