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 |
||
38 | class Container extends Component implements ContainerInterface, FactoryInterface, ResolverInterface |
||
39 | { |
||
40 | /** |
||
41 | * IoC bindings. |
||
42 | * |
||
43 | * @what-if private |
||
44 | * @invisible |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $bindings = [ |
||
49 | ContainerInterface::class => self::class, |
||
50 | FactoryInterface::class => self::class, |
||
51 | ResolverInterface::class => self::class |
||
52 | ]; |
||
53 | |||
54 | /** |
||
55 | * Registered injectors. |
||
56 | * |
||
57 | * @what-if private |
||
58 | * @invisible |
||
59 | * |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $injectors = []; |
||
63 | |||
64 | /** |
||
65 | * Container constructor. |
||
66 | */ |
||
67 | public function __construct() |
||
72 | |||
73 | /** |
||
74 | * Container can not be cloned. |
||
75 | */ |
||
76 | public function __clone() |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | public function has($alias) |
||
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | * |
||
92 | * Context parameter will be passed to class injectors, which makes possible to use this method |
||
93 | * as: |
||
94 | * $this->container->get(DatabaseInterface::class, 'default'); |
||
95 | * |
||
96 | * @param string|null $context Call context. |
||
97 | * |
||
98 | * @throws ContainerException |
||
99 | */ |
||
100 | public function get($alias, $context = null) |
||
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | * |
||
109 | * @param string|null $context Related to parameter caused injection if any. |
||
110 | */ |
||
111 | final public function make(string $class, $parameters = [], string $context = null) |
||
169 | |||
170 | /** |
||
171 | * {@inheritdoc} |
||
172 | * |
||
173 | * @param string $context |
||
174 | */ |
||
175 | final public function resolveArguments( |
||
236 | |||
237 | /** |
||
238 | * Bind value resolver to container alias. Resolver can be class name (will be constructed |
||
239 | * for each method call), function array or Closure (executed every call). Only object resolvers |
||
240 | * supported by this method. |
||
241 | * |
||
242 | * @param string $alias |
||
243 | * @param string|array|callable $resolver |
||
244 | * |
||
245 | * @return self |
||
246 | */ |
||
247 | final public function bind(string $alias, $resolver): Container |
||
260 | |||
261 | /** |
||
262 | * Bind value resolver to container alias to be executed as cached. Resolver can be class name |
||
263 | * (will be constructed only once), function array or Closure (executed only once call). |
||
264 | * |
||
265 | * @param string $alias |
||
266 | * @param string|array|callable $resolver |
||
267 | * |
||
268 | * @return self |
||
269 | */ |
||
270 | final public function bindSingleton(string $alias, $resolver): Container |
||
283 | |||
284 | /** |
||
285 | * Specify binding which has to be used for class injection. |
||
286 | * |
||
287 | * @param string $class |
||
288 | * @param string|object $injector |
||
289 | * |
||
290 | * @return self |
||
291 | */ |
||
292 | public function bindInjector(string $class, $injector): Container |
||
302 | |||
303 | /** |
||
304 | * Check if given class has associated injector. |
||
305 | * |
||
306 | * @param \ReflectionClass $reflection |
||
307 | * |
||
308 | * @return bool |
||
309 | */ |
||
310 | public function hasInjector(\ReflectionClass $reflection): bool |
||
319 | |||
320 | /** |
||
321 | * Check if alias points to constructed instance (singleton). |
||
322 | * |
||
323 | * @param string $alias |
||
324 | * |
||
325 | * @return bool |
||
326 | */ |
||
327 | final public function hasInstance(string $alias): bool |
||
340 | |||
341 | /** |
||
342 | * @param string $alias |
||
343 | */ |
||
344 | final public function removeBinding(string $alias) |
||
348 | |||
349 | /** |
||
350 | * @param string $class |
||
351 | */ |
||
352 | final public function removeInjector(string $class) |
||
356 | |||
357 | /** |
||
358 | * Every declared Container binding. Must not be used in production code due container format is |
||
359 | * vary. |
||
360 | * |
||
361 | * @return array |
||
362 | */ |
||
363 | final public function getBindings(): array |
||
367 | |||
368 | /** |
||
369 | * Every binded injector. |
||
370 | * |
||
371 | * @return array |
||
372 | */ |
||
373 | final public function getInjectors(): array |
||
377 | |||
378 | /** |
||
379 | * Automatically create class. |
||
380 | * |
||
381 | * @param string $class |
||
382 | * @param array $parameters |
||
383 | * @param string $context |
||
384 | * |
||
385 | * @return object |
||
386 | * |
||
387 | * @throws AutowireException |
||
388 | */ |
||
389 | final protected function autowire(string $class, array $parameters, string $context = null) |
||
406 | |||
407 | /** |
||
408 | * Register instance in container, might perform methods like auto-singletons, log populations |
||
409 | * and etc. Can be extended. |
||
410 | * |
||
411 | * @param object $instance Created object. |
||
412 | * @param array $parameters Parameters which been passed with created instance. |
||
413 | * |
||
414 | * @return object |
||
415 | */ |
||
416 | protected function registerInstance($instance, array $parameters) |
||
431 | |||
432 | /** |
||
433 | * Create instance of desired class. |
||
434 | * |
||
435 | * @param string $class |
||
436 | * @param array $parameters Constructor parameters. |
||
437 | * @param string|null $context |
||
438 | * |
||
439 | * @return object |
||
440 | * |
||
441 | * @throws ContainerException |
||
442 | */ |
||
443 | private function createInstance(string $class, array $parameters, string $context = null) |
||
475 | |||
476 | /** |
||
477 | * Get injector associated with given class. |
||
478 | * |
||
479 | * @param \ReflectionClass $reflection |
||
480 | * |
||
481 | * @return InjectorInterface |
||
482 | */ |
||
483 | private function getInjector(\ReflectionClass $reflection): InjectorInterface |
||
501 | |||
502 | /** |
||
503 | * Assert that given value are matched parameter type. |
||
504 | * |
||
505 | * @param \ReflectionParameter $parameter |
||
506 | * @param \ReflectionFunctionAbstract $context |
||
507 | * @param mixed $value |
||
508 | * |
||
509 | * @throws ArgumentException |
||
510 | */ |
||
511 | private function assertType( |
||
538 | } |