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 |
||
25 | class Container implements ContainerInterface |
||
26 | { |
||
27 | const LOGIC_FUNCTION_NAME = 'diContainer'; |
||
28 | |||
29 | /** @var array[string] Collection of alias => class name for alias resolving */ |
||
30 | protected $aliases = array(); |
||
31 | |||
32 | /** @var array[string] Collection of entity name resolving */ |
||
33 | protected $resolver = array(); |
||
34 | |||
35 | /** @var array[string] Collection of alias => closure for alias resolving */ |
||
36 | protected $callbacks = array(); |
||
37 | |||
38 | /** @var array[string] Collection of loaded services */ |
||
39 | protected $services = array(); |
||
40 | |||
41 | /** @var array[string] Collection of class name dependencies trees */ |
||
42 | protected $dependencies = array(); |
||
43 | |||
44 | /** @var Generator */ |
||
45 | protected $generator; |
||
46 | |||
47 | /** |
||
48 | * Container constructor. |
||
49 | * |
||
50 | * @param Generator $generator |
||
51 | */ |
||
52 | 4 | public function __construct(Generator $generator) |
|
56 | |||
57 | /** |
||
58 | * Help container resolving interfaces and abstract classes or any entities to |
||
59 | * different one. |
||
60 | * |
||
61 | * @param string $source Source entity name |
||
62 | * @param string $destination Destination entity name |
||
63 | * |
||
64 | * @return self Chaining |
||
65 | */ |
||
66 | public function resolve($source, $destination) |
||
72 | |||
73 | /** |
||
74 | * Internal logic handler. Calls generated logic function |
||
75 | * for performing entity creation or search. This is encapsulated |
||
76 | * method for further overriding. |
||
77 | * |
||
78 | * @param string $alias Entity alias |
||
79 | * |
||
80 | * @return mixed Created instance or null |
||
81 | */ |
||
82 | 3 | protected function logic($alias) |
|
86 | |||
87 | /** |
||
88 | * Get reflection paramater class name type hint if present without |
||
89 | * auto loading and throwing exceptions. |
||
90 | * |
||
91 | * @param \ReflectionParameter $param Parameter for parsing |
||
92 | * |
||
93 | * @return string|null Class name typehint or null |
||
94 | */ |
||
95 | 4 | protected function getClassName(\ReflectionParameter $param) |
|
102 | |||
103 | /** |
||
104 | * Recursively build class constructor dependencies tree. |
||
105 | * TODO: Analyze recurrent dependencies and throw an exception |
||
106 | * |
||
107 | * @param string $className Current class name for analyzing |
||
108 | * @param array $dependencies Reference to tree for filling up |
||
109 | * |
||
110 | * @return array [string] Multidimensional array as dependency tree |
||
111 | * @throws ClassNotFoundException |
||
112 | */ |
||
113 | 4 | protected function buildDependenciesTree($className, array &$dependencies) |
|
149 | |||
150 | /** |
||
151 | * Recursive object creation with dependencies. |
||
152 | * |
||
153 | * @param array $dependencies Collection of current class dependenices |
||
154 | * @param string $class Current class name |
||
155 | * |
||
156 | * @throws ConstructorParameterNotSetException |
||
157 | */ |
||
158 | |||
159 | 1 | public function generateLogicConditions(array &$dependencies, $class) |
|
204 | |||
205 | /** |
||
206 | * Generate callback logic implementation as plain code. |
||
207 | * |
||
208 | * @param string $className Callback alias |
||
209 | */ |
||
210 | 1 | protected function generateCallback($className) |
|
238 | |||
239 | /** |
||
240 | * @param string $functionName |
||
241 | * |
||
242 | * @return string |
||
243 | * @throws ConstructorParameterNotSetException |
||
244 | */ |
||
245 | 1 | public function generateLogicFunction($functionName = self::LOGIC_FUNCTION_NAME) |
|
281 | |||
282 | /** |
||
283 | * Finds an entry of the container by its identifier and returns it. |
||
284 | * |
||
285 | * @param string $alias Identifier of the entry to look for. |
||
286 | * |
||
287 | * @throws NotFoundException No entry was found for this identifier. |
||
288 | * @throws ContainerException Error while retrieving the entry. |
||
289 | * |
||
290 | * @return mixed Entry. |
||
291 | */ |
||
292 | 3 | public function get($alias) |
|
307 | |||
308 | /** |
||
309 | * Returns true if the container can return an entry for the given identifier. |
||
310 | * Returns false otherwise. |
||
311 | * |
||
312 | * @param string $alias Identifier of the entry to look for. |
||
313 | * |
||
314 | * @return boolean |
||
315 | */ |
||
316 | 1 | public function has($alias) |
|
322 | |||
323 | /** |
||
324 | * Set dependency alias with callback function. |
||
325 | * |
||
326 | * @param callable $callable Callable to return dependency |
||
327 | * @param string $alias Dependency name |
||
328 | * |
||
329 | * @return self Chaining |
||
330 | */ |
||
331 | 4 | public function callback($callable, $alias = null) |
|
342 | |||
343 | /** |
||
344 | * Set service dependency. Upon first creation of this class instance |
||
345 | * it would be used everywhere where this dependency is needed. |
||
346 | * |
||
347 | * @param string $className Fully qualified class name |
||
348 | * @param string $alias Dependency name |
||
349 | * @param array $parameters Collection of parameters needed for dependency creation |
||
350 | * |
||
351 | * @return self Chaining |
||
352 | */ |
||
353 | 4 | public function service($className, $alias = null, array $parameters = array()) |
|
359 | |||
360 | /** |
||
361 | * Set service dependency by passing object instance. |
||
362 | * |
||
363 | * @param mixed $instance Instance that needs to be return by this dependency |
||
364 | * @param string $alias Dependency name |
||
365 | * @param array $parameters Collection of parameters needed for dependency creation |
||
366 | * |
||
367 | * @return self Chaining |
||
368 | */ |
||
369 | public function instance(&$instance, $alias = null, array $parameters = array()) |
||
374 | |||
375 | /** |
||
376 | * Set dependency. |
||
377 | * |
||
378 | * @param string $className Fully qualified class name |
||
379 | * @param string $alias Dependency name |
||
380 | * @param array $parameters Collection of parameters needed for dependency creation |
||
381 | * |
||
382 | * @return self Chaining |
||
383 | */ |
||
384 | 4 | public function set($className, $alias = null, array $parameters = array()) |
|
398 | } |
||
399 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.