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 | * Add service |
||
158 | * |
||
159 | * @param string $name |
||
160 | * @param Closure $service |
||
161 | * @return Container |
||
162 | */ |
||
163 | public function add(string $name, Closure $service): Container |
||
172 | |||
173 | |||
174 | /** |
||
175 | * Check if service is registered |
||
176 | * |
||
177 | * @return bool |
||
178 | */ |
||
179 | public function has($name): bool |
||
183 | |||
184 | |||
185 | /** |
||
186 | * Auto wire service |
||
187 | * |
||
188 | * @param string $name |
||
189 | * @return mixed |
||
190 | */ |
||
191 | protected function autoWire(string $name) |
||
238 | |||
239 | |||
240 | /** |
||
241 | * Traverse services with parents and find correct service to use |
||
242 | * |
||
243 | * @param string $name |
||
244 | * @param string $class |
||
245 | * @return mixed |
||
246 | */ |
||
247 | protected function findParentService(string $name, string $class) |
||
257 | |||
258 | |||
259 | /** |
||
260 | * Create instance |
||
261 | * |
||
262 | * @param string $name |
||
263 | * @param ReflectionClass $class |
||
264 | * @param array $args |
||
265 | * @return mixed |
||
266 | */ |
||
267 | protected function createInstance(string $name, ReflectionClass $class, array $args) |
||
289 | |||
290 | |||
291 | /** |
||
292 | * Get config value |
||
293 | * |
||
294 | * @param string $name |
||
295 | * @param string $param |
||
296 | * @return mixed |
||
297 | */ |
||
298 | public function getParam(string $name, string $param) |
||
310 | } |
||
311 |
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: