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 |
||
17 | class Container implements ContainerInterface |
||
18 | { |
||
19 | /** |
||
20 | * @var \League\Container\Definition\DefinitionFactoryInterface |
||
21 | */ |
||
22 | protected $definitionFactory; |
||
23 | |||
24 | /** |
||
25 | * @var \League\Container\Definition\DefinitionInterface[] |
||
26 | */ |
||
27 | protected $definitions = []; |
||
28 | |||
29 | /** |
||
30 | * @var \League\Container\Definition\DefinitionInterface[] |
||
31 | */ |
||
32 | protected $sharedDefinitions = []; |
||
33 | |||
34 | /** |
||
35 | * @var \League\Container\Inflector\InflectorAggregateInterface |
||
36 | */ |
||
37 | protected $inflectors; |
||
38 | |||
39 | /** |
||
40 | * @var \League\Container\ServiceProvider\ServiceProviderAggregateInterface |
||
41 | */ |
||
42 | protected $providers; |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $shared = []; |
||
48 | |||
49 | /** |
||
50 | * @var \Interop\Container\ContainerInterface[] |
||
51 | */ |
||
52 | protected $delegates = []; |
||
53 | |||
54 | /** |
||
55 | * @var bool |
||
56 | */ |
||
57 | protected $isProtected = false; |
||
58 | |||
59 | 42 | /** |
|
60 | * Constructor. |
||
61 | * |
||
62 | * @param \League\Container\ServiceProvider\ServiceProviderAggregateInterface|null $providers |
||
63 | * @param \League\Container\Inflector\InflectorAggregateInterface|null $inflectors |
||
64 | * @param \League\Container\Definition\DefinitionFactoryInterface|null $definitionFactory |
||
65 | 42 | */ |
|
66 | 42 | public function __construct( |
|
84 | |||
85 | 30 | /** |
|
86 | 12 | * {@inheritdoc} |
|
87 | 12 | */ |
|
88 | 12 | public function get($alias, array $args = []) |
|
109 | 12 | ||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | 9 | */ |
|
113 | 3 | public function has($alias) |
|
125 | |||
126 | 36 | /** |
|
127 | * Returns a boolean to determine if the container has a shared instance of an alias. |
||
128 | 36 | * |
|
129 | * @param string $alias |
||
130 | 36 | * @param boolean $resolved |
|
131 | * @return boolean |
||
132 | */ |
||
133 | public function hasShared($alias, $resolved = false) |
||
139 | 3 | ||
140 | 3 | /** |
|
141 | * {@inheritdoc} |
||
142 | 30 | */ |
|
143 | public function add($alias, $concrete = null, $share = false) |
||
168 | |||
169 | 12 | /** |
|
170 | * {@inheritdoc} |
||
171 | 12 | */ |
|
172 | public function share($alias, $concrete = null) |
||
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | 6 | */ |
|
180 | public function addServiceProvider($provider) |
||
190 | 3 | ||
191 | /** |
||
192 | * {@inheritdoc} |
||
193 | 3 | */ |
|
194 | 3 | public function extend($alias) |
|
216 | |||
217 | /** |
||
218 | * {@inheritdoc} |
||
219 | */ |
||
220 | public function inflector($type, callable $callback = null) |
||
228 | |||
229 | 9 | /** |
|
230 | * {@inheritdoc} |
||
231 | */ |
||
232 | public function call(callable $callable, array $args = []) |
||
236 | |||
237 | /** |
||
238 | 9 | * Marks the container as protected. |
|
239 | * |
||
240 | 9 | * Prevents overriding services and delegating to other containers once the container is marked as protected. |
|
241 | 3 | */ |
|
242 | 3 | public function protect() |
|
262 | |||
263 | 3 | /** |
|
264 | 6 | * Marks the container as unprotected. |
|
265 | * |
||
266 | 6 | * Allows overriding services and delegating to other containers (this is the default). |
|
267 | */ |
||
268 | public function unprotect() |
||
272 | |||
273 | /** |
||
274 | * Returns true if the container is protected. |
||
275 | * |
||
276 | 30 | * @return bool |
|
277 | */ |
||
278 | 30 | public function isProtected() |
|
282 | 27 | ||
283 | 15 | /** |
|
284 | 15 | * Delegate a backup container to be checked for services if it |
|
285 | 15 | * cannot be resolved via this container. |
|
286 | * |
||
287 | * @param \Interop\Container\ContainerInterface $container |
||
288 | 24 | * @return $this |
|
289 | 6 | */ |
|
290 | 6 | public function delegate(InteropContainerInterface $container) |
|
304 | |||
305 | /** |
||
306 | * Returns true if service is registered in one of the delegated backup containers. |
||
307 | * |
||
308 | * @param string $alias |
||
309 | * @return boolean |
||
310 | */ |
||
311 | public function hasInDelegate($alias) |
||
321 | |||
322 | /** |
||
323 | * Attempt to get a service from the stack of delegated backup containers. |
||
324 | * |
||
325 | * @param string $alias |
||
326 | * @param array $args |
||
327 | * @return mixed |
||
328 | */ |
||
329 | protected function getFromDelegate($alias, array $args = []) |
||
341 | |||
342 | /** |
||
343 | * Get a service that has been registered in this container. |
||
344 | * |
||
345 | * @param string $alias |
||
346 | * @param array $args |
||
347 | * @return mixed |
||
348 | */ |
||
349 | protected function getFromThisContainer($alias, array $args = []) |
||
369 | } |
||
370 |