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, FactoryInterface |
||
18 | { |
||
19 | /** |
||
20 | * @type string pattern for parsing an argument type from a ReflectionParameter string |
||
21 | * @see getArgumentType() |
||
22 | */ |
||
23 | const ARG_PATTERN = '/(?:\<required\>|\<optional\>)\\s+([\\w\\\\]+)/'; |
||
24 | |||
25 | /** |
||
26 | * @var mixed[] map where component name => value |
||
27 | */ |
||
28 | protected $values = array(); |
||
29 | |||
30 | /** |
||
31 | * @var callable[] map where component name => factory function |
||
32 | */ |
||
33 | protected $factory = array(); |
||
34 | |||
35 | /** |
||
36 | * @var array map where component name => mixed list/map of parameter names |
||
37 | */ |
||
38 | protected $factory_map = array(); |
||
39 | |||
40 | /** |
||
41 | * @var bool[] map where component name => true (if the component is immutable) |
||
42 | */ |
||
43 | protected $immutable = array(); |
||
44 | |||
45 | /** |
||
46 | * @var (callable[])[] map where component name => list of configuration functions |
||
47 | */ |
||
48 | protected $config = array(); |
||
49 | |||
50 | /** |
||
51 | * @var array map where component name => mixed list/map of parameter names |
||
52 | */ |
||
53 | protected $config_map = array(); |
||
54 | |||
55 | /** |
||
56 | * Self-register this container for dependency injection |
||
57 | */ |
||
58 | 1 | public function __construct() |
|
59 | { |
||
60 | 1 | $this->values[get_class($this)] = |
|
61 | 1 | $this->values[__CLASS__] = |
|
62 | 1 | $this->values[ContainerInterface::class] = |
|
63 | 1 | $this->values[FactoryInterface::class] = |
|
64 | $this; |
||
65 | 1 | } |
|
66 | |||
67 | /** |
||
68 | * Resolve the registered component with the given name. |
||
69 | * |
||
70 | * @param string $name component name |
||
71 | * |
||
72 | * @return mixed |
||
73 | * |
||
74 | * @throws ContainerException |
||
75 | * @throws NotFoundException |
||
76 | */ |
||
77 | 1 | public function get($name) |
|
99 | |||
100 | /** |
||
101 | * Directly inject a component into the container - use this to register components that |
||
102 | * have already been created for some reason; for example, the Composer ClassLoader. |
||
103 | * |
||
104 | * @param string $name component name |
||
105 | * @param mixed $value |
||
106 | * |
||
107 | * @return void |
||
108 | * |
||
109 | * @throws ContainerException |
||
110 | */ |
||
111 | 1 | public function set($name, $value) |
|
121 | |||
122 | /** |
||
123 | * Register a component for dependency injection. |
||
124 | * |
||
125 | * There are numerous valid ways to register components. |
||
126 | * |
||
127 | * * `register(Foo::class)` registers a component by it's class-name, and will try to |
||
128 | * automatically resolve all of it's constructor arguments. |
||
129 | * |
||
130 | * * `register(Foo::class, ['bar'])` registers a component by it's class-name, and will |
||
131 | * use `'bar'` as the first constructor argument, and try to resolve the rest. |
||
132 | * |
||
133 | * * `register(Foo::class, [$container->ref(Bar::class)])` creates a boxed reference to |
||
134 | * a registered component `Bar` and provides that as the first argument. |
||
135 | * |
||
136 | * * `register(Foo::class, ['bat' => 'zap'])` registers a component by it's class-name |
||
137 | * and will use `'zap'` for the constructor argument named `$bat`, and try to resolve |
||
138 | * any other arguments. |
||
139 | * |
||
140 | * * `register(Bar::class, Foo::class)` registers a component `Foo` under another name |
||
141 | * `Bar`, which might be an interface or an abstract class. |
||
142 | * |
||
143 | * * `register(Bar::class, Foo::class, ['bar'])` same as above, but uses `'bar'` as the |
||
144 | * first argument. |
||
145 | * |
||
146 | * * `register(Bar::class, Foo::class, ['bat' => 'zap'])` same as above, but, well, guess. |
||
147 | * |
||
148 | * * `register(Bar::class, function (Foo $foo) { return new Bar(...); })` registers a |
||
149 | * component with a custom creation function. |
||
150 | * |
||
151 | * * `register(Bar::class, function ($name) { ... }, [$container->ref('db.name')]);` |
||
152 | * registers a component creation function with a reference to a component "db.name" |
||
153 | * as the first argument. |
||
154 | * |
||
155 | * In effect, you can think of `$func` as being an optional argument. |
||
156 | * |
||
157 | * The provided parameter values may include any `BoxedValueInterface`, such as the boxed |
||
158 | * component referenced created by {@see Container::ref()} - these will be unboxed as late |
||
159 | * as possible. |
||
160 | * |
||
161 | * @param string $name component name |
||
162 | * @param callable|mixed|mixed[]|null $func_or_map_or_type creation function or class-name, or, if the first |
||
163 | * argument is a class-name, a map of constructor arguments |
||
164 | * @param mixed|mixed[] $map mixed list/map of parameter values (and/or boxed values) |
||
165 | * |
||
166 | * @return void |
||
167 | * |
||
168 | * @throws ContainerException |
||
169 | */ |
||
170 | 1 | public function register($name, $func_or_map_or_type = null, $map = array()) |
|
204 | |||
205 | /** |
||
206 | * Register a component as an alias of another registered component. |
||
207 | * |
||
208 | * @param string $name new component name |
||
209 | * @param string $ref_name existing component name |
||
210 | */ |
||
211 | public function alias($name, $ref_name) |
||
217 | |||
218 | /** |
||
219 | * Register a configuration function, which will be applied as late as possible, e.g. |
||
220 | * on first use of the component. For example: |
||
221 | * |
||
222 | * $container->configure('stack', function (MiddlewareStack $stack) { |
||
223 | * $stack->push(new MoreAwesomeMiddleware()); |
||
224 | * }); |
||
225 | * |
||
226 | * The given configuration function should include the configured component as the |
||
227 | * first parameter to the closure, but may include any number of parameters, which |
||
228 | * will be resolved and injected. |
||
229 | * |
||
230 | * The first argument (component name) is optional - that is, the name can be inferred |
||
231 | * from the first parameter of the closure; the following will work: |
||
232 | * |
||
233 | * $container->configure(function (PageLayout $layout) { |
||
234 | * $layout->title = "Welcome"; |
||
235 | * }); |
||
236 | * |
||
237 | * In some cases, such as using component names like "cache.path" (which because of the |
||
238 | * dot in the name cannot be resolved by parameter name), you can use a boxed reference |
||
239 | * in the optional `$map` argument, e.g.: |
||
240 | * |
||
241 | * $container->configure( |
||
242 | * function (FileCache $cache, $path) { |
||
243 | * $cache->setPath($path); |
||
244 | * }, |
||
245 | * ['path' => $container->ref('cache.path')] |
||
246 | * ); |
||
247 | * |
||
248 | * You may optionally provide a list/map of parameter values, similar to the one |
||
249 | * accepted by {@see Container::register()} - the typical reason to use this, is if |
||
250 | * you need to inject another component by name, e.g. using {@see Container::ref()}. |
||
251 | * |
||
252 | * You can also use `configure()` to decorate objects, or manipulate (or replace) values: |
||
253 | * |
||
254 | * $container->configure('num_kittens', function ($num_kittens) { |
||
255 | * return $num_kittens + 6; // add another litter |
||
256 | * }); |
||
257 | * |
||
258 | * In other words, if your closure returns something, the component will be replaced. |
||
259 | * |
||
260 | * @param string|callable $name_or_func component name |
||
261 | * (or callable, if name is left out) |
||
262 | * @param callable|mixed|mixed[] $func_or_map `function (Type $component, ...) : void` |
||
263 | * (or parameter values, if name is left out) |
||
264 | * @param mixed|mixed[] $map mixed list/map of parameter values and/or boxed values |
||
265 | * (or unused, if name is left out) |
||
266 | * |
||
267 | * @return void |
||
268 | * |
||
269 | * @throws NotFoundException |
||
270 | */ |
||
271 | 1 | public function configure($name_or_func, $func_or_map = null, $map = []) |
|
312 | |||
313 | /** |
||
314 | * Check for the existence of a component with a given name. |
||
315 | * |
||
316 | * @param string $name component name |
||
317 | * |
||
318 | * @return bool true, if a component with the given name has been defined |
||
319 | */ |
||
320 | 1 | public function has($name) |
|
325 | |||
326 | /** |
||
327 | * Check if a component has been unboxed and is currently active. |
||
328 | * |
||
329 | * @param string $name component name |
||
330 | * |
||
331 | * @return bool |
||
332 | */ |
||
333 | 1 | public function isActive($name) |
|
337 | |||
338 | /** |
||
339 | * Call any given callable, using dependency injection to satisfy it's arguments, and/or |
||
340 | * manually specifying some of those arguments - then return the value from the call. |
||
341 | * |
||
342 | * This will work for any callable: |
||
343 | * |
||
344 | * $container->call('foo'); // function foo() |
||
345 | * $container->call($foo, 'baz'); // instance method $foo->baz() |
||
346 | * $container->call([Foo::class, 'bar']); // static method Foo::bar() |
||
347 | * $container->call($foo); // closure (or class implementing __invoke) |
||
348 | * |
||
349 | * In any of those examples, you can also supply custom arguments, either named or |
||
350 | * positional, or mixed, as per the `$map` argument in `register()`, `configure()`, etc. |
||
351 | * |
||
352 | * @param callable|object $callback any arbitrary closure or callable, or object implementing __invoke() |
||
353 | * @param mixed|mixed[] $map mixed list/map of parameter values (and/or boxed values) |
||
354 | * |
||
355 | * @return mixed return value from the given callable |
||
356 | */ |
||
357 | 1 | public function call($callback, $map = array()) |
|
363 | |||
364 | /** |
||
365 | * Create an instance of a given class. |
||
366 | * |
||
367 | * The container will internally resolve and inject any constructor arguments |
||
368 | * not explicitly provided in the (optional) second parameter. |
||
369 | * |
||
370 | * @param string $class_name fully-qualified class-name |
||
371 | * @param mixed|mixed[] $map mixed list/map of parameter values (and/or boxed values) |
||
372 | * |
||
373 | * @return mixed |
||
374 | */ |
||
375 | 1 | public function create($class_name, $map = array()) |
|
395 | |||
396 | /** |
||
397 | * Creates a boxed reference to a component in the container. |
||
398 | * |
||
399 | * You can use this in conjunction with `register()` to provide a component reference |
||
400 | * without expanding that reference until first use - for example: |
||
401 | * |
||
402 | * $container->register(UserRepo::class, [$container->ref('cache')]); |
||
403 | * |
||
404 | * This will reference the "cache" component and provide it as the first argument to the |
||
405 | * constructor of `UserRepo` - compared with using `$container->get('cache')`, this has |
||
406 | * the advantage of not actually activating the "cache" component until `UserRepo` is |
||
407 | * used for the first time. |
||
408 | * |
||
409 | * Another reason (besides performance) to use references, is to defer the reference: |
||
410 | * |
||
411 | * $container->register(FileCache::class, ['root_path' => $container->ref('cache.path')]); |
||
412 | * |
||
413 | * In this example, the component "cache.path" will be fetched from the container on |
||
414 | * first use of `FileCache`, giving you a chance to configure "cache.path" later. |
||
415 | * |
||
416 | * @param string $name component name |
||
417 | * |
||
418 | * @return BoxedValueInterface boxed component reference |
||
419 | */ |
||
420 | 1 | public function ref($name) |
|
424 | |||
425 | /** |
||
426 | * Add a packaged configuration (a "provider") to this container. |
||
427 | * |
||
428 | * @see ProviderInterface |
||
429 | * |
||
430 | * @param ProviderInterface $provider |
||
431 | * |
||
432 | * @return void |
||
433 | */ |
||
434 | 1 | public function add(ProviderInterface $provider) |
|
438 | |||
439 | /** |
||
440 | * Internally reflect on any type of callable |
||
441 | * |
||
442 | * @param callable $callback |
||
443 | * |
||
444 | * @return ReflectionFunctionAbstract |
||
445 | */ |
||
446 | 1 | protected function reflect($callback) |
|
468 | |||
469 | /** |
||
470 | * Internally resolves parameters to functions or constructors. |
||
471 | * |
||
472 | * This is the heart of the beast. |
||
473 | * |
||
474 | * @param ReflectionParameter[] $params parameter reflections |
||
475 | * @param mixed|mixed[] $map mixed list/map of parameter values (and/or boxed values) |
||
476 | * |
||
477 | * @return array parameters |
||
478 | * |
||
479 | * @throws ContainerException |
||
480 | * @throws NotFoundException |
||
481 | */ |
||
482 | 1 | protected function resolve(array $params, $map) |
|
531 | |||
532 | /** |
||
533 | * Internally initialize an active component. |
||
534 | * |
||
535 | * @param string $name component name |
||
536 | * |
||
537 | * @return void |
||
538 | * |
||
539 | * @throws ContainerException on attempt to initialize an already-initialized component |
||
540 | */ |
||
541 | 1 | protected function initialize($name) |
|
561 | } |
||
562 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: