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(array $config=[]) |
||
59 | |||
60 | |||
61 | /** |
||
62 | * Flatten config |
||
63 | * |
||
64 | * @param Iterable $config |
||
65 | * @param string $parent |
||
66 | * @return array |
||
67 | */ |
||
68 | protected function flattenConfig(Iterable $config, ?string $parent=null): array |
||
121 | |||
122 | |||
123 | /** |
||
124 | * Get all services |
||
125 | * |
||
126 | * @return array |
||
127 | */ |
||
128 | public function getAll(): array |
||
132 | |||
133 | |||
134 | /** |
||
135 | * Get service |
||
136 | * |
||
137 | * @param string $name |
||
138 | * @return mixed |
||
139 | */ |
||
140 | public function get($name) |
||
154 | |||
155 | |||
156 | /** |
||
157 | * Get new instance (Do not store in container) |
||
158 | * |
||
159 | * @param string $name |
||
160 | * @return mixed |
||
161 | */ |
||
162 | public function getNew(string $name) |
||
170 | |||
171 | |||
172 | /** |
||
173 | * Add service |
||
174 | * |
||
175 | * @param string $name |
||
176 | * @param Closure $service |
||
177 | * @return Container |
||
178 | */ |
||
179 | public function add(string $name, Closure $service): Container |
||
188 | |||
189 | |||
190 | /** |
||
191 | * Check if service is registered |
||
192 | * |
||
193 | * @return bool |
||
194 | */ |
||
195 | public function has($name): bool |
||
199 | |||
200 | |||
201 | /** |
||
202 | * Auto wire service |
||
203 | * |
||
204 | * @param string $name |
||
205 | * @return mixed |
||
206 | */ |
||
207 | protected function autoWire(string $name) |
||
254 | |||
255 | |||
256 | /** |
||
257 | * Traverse services with parents and find correct service to use |
||
258 | * |
||
259 | * @param string $name |
||
260 | * @param string $class |
||
261 | * @return mixed |
||
262 | */ |
||
263 | protected function findParentService(string $name, string $class) |
||
273 | |||
274 | |||
275 | /** |
||
276 | * Create instance |
||
277 | * |
||
278 | * @param string $name |
||
279 | * @param ReflectionClass $class |
||
280 | * @param array $args |
||
281 | * @return mixed |
||
282 | */ |
||
283 | protected function createInstance(string $name, ReflectionClass $class, array $args) |
||
305 | |||
306 | |||
307 | /** |
||
308 | * Get config value |
||
309 | * |
||
310 | * @param string $name |
||
311 | * @param string $param |
||
312 | * @return mixed |
||
313 | */ |
||
314 | public function getParam(string $name, string $param) |
||
326 | } |
||
327 |
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: