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 |
||
20 | class Container implements ContainerInterface |
||
21 | { |
||
22 | /** |
||
23 | * Config |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $config = []; |
||
28 | |||
29 | |||
30 | /** |
||
31 | * Service registry |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $service = []; |
||
36 | |||
37 | |||
38 | /** |
||
39 | * Registered but not initialized service registry |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $registry = []; |
||
44 | |||
45 | |||
46 | /** |
||
47 | * Create container |
||
48 | * |
||
49 | * @param array $config |
||
50 | */ |
||
51 | public function __construct(Iterable $config=[]) |
||
59 | |||
60 | |||
61 | /** |
||
62 | * Get service |
||
63 | * |
||
64 | * @param string $name |
||
65 | * @return mixed |
||
66 | */ |
||
67 | public function get($name) |
||
81 | |||
82 | |||
83 | /** |
||
84 | * Get new instance (Do not store in container) |
||
85 | * |
||
86 | * @param string $name |
||
87 | * @return mixed |
||
88 | */ |
||
89 | public function getNew(string $name) |
||
97 | |||
98 | |||
99 | /** |
||
100 | * Add service |
||
101 | * |
||
102 | * @param string $name |
||
103 | * @param Closure $service |
||
104 | * @return Container |
||
105 | */ |
||
106 | public function add(string $name, Closure $service): Container |
||
115 | |||
116 | |||
117 | /** |
||
118 | * Check if service is registered |
||
119 | * |
||
120 | * @return bool |
||
121 | */ |
||
122 | public function has($name): bool |
||
126 | |||
127 | |||
128 | /** |
||
129 | * Auto wire |
||
130 | * |
||
131 | * @param string $name |
||
132 | * @param Iterable $config |
||
133 | * @param array $parents |
||
134 | * @return mixed |
||
135 | */ |
||
136 | protected function autoWire(string $name, $config=null, array $parents=[]) |
||
200 | |||
201 | |||
202 | /** |
||
203 | * Traverse services with parents and find correct service to use |
||
204 | * |
||
205 | * @param string $name |
||
206 | * @param string $class |
||
207 | * @return mixed |
||
208 | */ |
||
209 | protected function findParentService(string $name, string $class, $config, $parents) |
||
238 | |||
239 | |||
240 | /** |
||
241 | * Create instance |
||
242 | * |
||
243 | * @param string $name |
||
244 | * @param ReflectionClass $class |
||
245 | * @param array $args |
||
246 | * @return mixed |
||
247 | */ |
||
248 | protected function createInstance(string $name, ReflectionClass $class, array $args, Iterable $config, $parents=[]) |
||
295 | |||
296 | |||
297 | /** |
||
298 | * Parse param value |
||
299 | * |
||
300 | * @param mixed $param |
||
301 | * @return mixed |
||
302 | */ |
||
303 | protected function parseParam($param) |
||
332 | |||
333 | |||
334 | |||
335 | |||
336 | /** |
||
337 | * Get config value |
||
338 | * |
||
339 | * @param string $name |
||
340 | * @param string $param |
||
341 | * @return mixed |
||
342 | */ |
||
343 | public function getParam(string $name, string $param, ?Iterable $config=null) |
||
355 | } |
||
356 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: