Total Complexity | 59 |
Total Lines | 401 |
Duplicated Lines | 0 % |
Changes | 0 |
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.
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 |
||
19 | class Container implements ContainerInterface |
||
20 | { |
||
21 | /** |
||
22 | * Config. |
||
23 | * |
||
24 | * @var iterable |
||
25 | */ |
||
26 | protected $config = []; |
||
27 | |||
28 | /** |
||
29 | * Service registry. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $service = []; |
||
34 | |||
35 | /** |
||
36 | * Registered but not initialized service registry. |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $registry = []; |
||
41 | |||
42 | /** |
||
43 | * Parent container. |
||
44 | * |
||
45 | * @var ContainerInterface |
||
46 | */ |
||
47 | protected $parent; |
||
48 | |||
49 | /** |
||
50 | * Children container. |
||
51 | * |
||
52 | * @var ContainerInterface[] |
||
53 | */ |
||
54 | protected $children = []; |
||
55 | |||
56 | /** |
||
57 | * Create container. |
||
58 | * |
||
59 | * @param iterable $config |
||
60 | */ |
||
61 | public function __construct(Iterable $config = [], ?ContainerInterface $parent = null) |
||
62 | { |
||
63 | $this->config = $config; |
||
64 | $this->parent = $parent; |
||
65 | $this->add(ContainerInterface::class, $this); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Get service. |
||
70 | * |
||
71 | * @param string $name |
||
72 | * |
||
73 | * @return mixed |
||
74 | */ |
||
75 | public function get($name) |
||
76 | { |
||
77 | if ($this->has($name)) { |
||
78 | return $this->service[$name]; |
||
79 | } |
||
80 | |||
81 | if (isset($this->registry[$name])) { |
||
82 | return $this->addStaticService($name); |
||
83 | } |
||
84 | |||
85 | if (isset($this->config[$name])) { |
||
86 | return $this->autoWireClass($name); |
||
87 | } |
||
88 | |||
89 | try { |
||
90 | return $this->lookupService($name); |
||
91 | } catch (Exception\ServiceNotFound $e) { |
||
92 | return $this->autoWireClass($name); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Traverse tree up and look for service. |
||
98 | * |
||
99 | * @param string $name |
||
100 | * |
||
101 | * @return mixed |
||
102 | */ |
||
103 | public function lookupService(string $name) |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Add service. |
||
126 | * |
||
127 | * @param string $name |
||
128 | * @param mixed $service |
||
129 | * |
||
130 | * @return Container |
||
131 | */ |
||
132 | public function add(string $name, $service): self |
||
133 | { |
||
134 | if ($this->has($name)) { |
||
135 | throw new Exception\ServiceAlreadyExists('service '.$name.' is already registered'); |
||
136 | } |
||
137 | |||
138 | $this->registry[$name] = $service; |
||
139 | |||
140 | return $this; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Check if service is registered. |
||
145 | * |
||
146 | * @param mixed $name |
||
147 | * |
||
148 | * @return bool |
||
149 | */ |
||
150 | public function has($name): bool |
||
151 | { |
||
152 | return isset($this->service[$name]); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Check for static injections. |
||
157 | * |
||
158 | * @param string $name |
||
159 | * |
||
160 | * @return mixed |
||
161 | */ |
||
162 | protected function addStaticService(string $name) |
||
163 | { |
||
164 | if ($this->registry[$name] instanceof Closure) { |
||
165 | $this->service[$name] = $this->registry[$name]->call($this); |
||
166 | } else { |
||
167 | $this->service[$name] = $this->registry[$name]; |
||
168 | } |
||
169 | |||
170 | unset($this->registry[$name]); |
||
171 | |||
172 | return $this->service[$name]; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Create service config. |
||
177 | * |
||
178 | * @param string $name |
||
179 | * |
||
180 | * @return array |
||
181 | */ |
||
182 | protected function createServiceConfig(string $name): array |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Auto wire. |
||
201 | * |
||
202 | * @param string $name |
||
203 | * @param array $config |
||
204 | * @param array $parents |
||
205 | * |
||
206 | * @return mixed |
||
207 | */ |
||
208 | protected function autoWireClass(string $name) |
||
209 | { |
||
210 | $class = $name; |
||
211 | $config = []; |
||
212 | |||
213 | if (isset($this->config[$name])) { |
||
214 | $config = $this->config[$name]; |
||
215 | } |
||
216 | |||
217 | if (isset($config['use'])) { |
||
218 | if (!is_string($config['use'])) { |
||
219 | throw new Exception\Configuration('use must be a string for service '.$name); |
||
220 | } |
||
221 | |||
222 | $class = $config['use']; |
||
223 | } else { |
||
224 | $config = $this->createServiceConfig($name); |
||
225 | } |
||
226 | |||
227 | if (preg_match('#^\{(.*)\}$#', $class, $match)) { |
||
228 | $service = $this->get($match[1]); |
||
229 | |||
230 | if (isset($this->config[$name]['selects'])) { |
||
231 | $reflection = new ReflectionClass(get_class($service)); |
||
232 | |||
233 | foreach ($this->config[$name]['selects'] as $select) { |
||
234 | $args = $this->autoWireMethod($name, $reflection->getMethod($select['method']), $select); |
||
235 | $service = call_user_func_array([&$service, $select['method']], $args); |
||
236 | } |
||
237 | } |
||
238 | |||
239 | return $this->storeService($name, $config, $service); |
||
240 | } |
||
241 | |||
242 | try { |
||
243 | $reflection = new ReflectionClass($class); |
||
244 | } catch (\Exception $e) { |
||
245 | throw new Exception\ServiceNotFound($class.' can not be resolved to an existing class for service '.$name); |
||
246 | } |
||
247 | |||
248 | $constructor = $reflection->getConstructor(); |
||
249 | |||
250 | if (null === $constructor) { |
||
251 | return new $class(); |
||
252 | } |
||
253 | |||
254 | $args = $this->autoWireMethod($name, $constructor, $config); |
||
255 | |||
256 | return $this->createInstance($name, $reflection, $args, $config); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Store service. |
||
261 | * |
||
262 | * @param param string $name |
||
263 | * @param array $config |
||
264 | * @param mixed $service |
||
265 | * |
||
266 | * @return mixed |
||
267 | */ |
||
268 | protected function storeService(string $name, array $config, $service) |
||
269 | { |
||
270 | if (isset($config['singleton']) && true === $config['singleton']) { |
||
271 | return $service; |
||
272 | } |
||
273 | |||
274 | $this->service[$name] = $service; |
||
275 | |||
276 | return $service; |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * Create instance. |
||
281 | * |
||
282 | * @param string $name |
||
283 | * @param ReflectionClass $class |
||
284 | * @param array $arguments |
||
285 | * @param array $config |
||
286 | * |
||
287 | * @return mixed |
||
288 | */ |
||
289 | protected function createInstance(string $name, ReflectionClass $class, array $arguments, array $config) |
||
290 | { |
||
291 | $instance = $class->newInstanceArgs($arguments); |
||
292 | $this->storeService($name, $config, $instance); |
||
293 | |||
294 | if (isset($this->config[$name]['calls'])) { |
||
295 | foreach ($this->config[$name]['calls'] as $call) { |
||
296 | if (!isset($call['method'])) { |
||
297 | throw new Exception\Configuration('method is required for setter injection in service '.$name); |
||
298 | } |
||
299 | |||
300 | $arguments = []; |
||
|
|||
301 | |||
302 | try { |
||
303 | $method = $class->getMethod($call['method']); |
||
304 | } catch (\ReflectionException $e) { |
||
305 | throw new Exception\Configuration('method '.$call['method'].' is not callable in class '.$class->getName().' for service '.$name); |
||
306 | } |
||
307 | |||
308 | $arguments = $this->autoWireMethod($name, $method, $call); |
||
309 | call_user_func_array([&$instance, $call['method']], $arguments); |
||
310 | } |
||
311 | } |
||
312 | |||
313 | return $instance; |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * Autowire method. |
||
318 | * |
||
319 | * @param string $name |
||
320 | * @param ReflectionMethod $method |
||
321 | * @param array $config |
||
322 | * |
||
323 | * @return array |
||
324 | */ |
||
325 | protected function autoWireMethod(string $name, ReflectionMethod $method, array $config): array |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * Parse param value. |
||
358 | * |
||
359 | * @param mixed $param |
||
360 | * @param string $name |
||
361 | * |
||
362 | * @return mixed |
||
363 | */ |
||
364 | protected function parseParam($param, string $name) |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * Locate service. |
||
403 | * |
||
404 | * @param string $current_service |
||
405 | * @param string $service |
||
406 | */ |
||
407 | protected function findService(string $current_service, string $service) |
||
420 | } |
||
421 | } |
||
422 |