Complex classes like SymfonyAdapter 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 SymfonyAdapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class SymfonyAdapter implements DependencyInjectionAdapterInterface |
||
25 | { |
||
26 | /** @var ContainerBuilder */ |
||
27 | private $container; |
||
28 | /** @var array */ |
||
29 | private $configuration; |
||
30 | /** @var string */ |
||
31 | private $moduleNamespace; |
||
32 | /** @var array */ |
||
33 | private $servicesToDefine = []; |
||
34 | /** @var array */ |
||
35 | private $instantiatedSharedServices = []; |
||
36 | /** @var int */ |
||
37 | private static $parameterIndex = 0; |
||
38 | |||
39 | /** |
||
40 | * DependencyInjectionAdapterInterface constructor. |
||
41 | * |
||
42 | * @param ConfigInterface $configuration |
||
43 | */ |
||
44 | 10 | public function __construct(ConfigInterface $configuration) |
|
53 | |||
54 | /** |
||
55 | * Initializes the DI container from the config. |
||
56 | * |
||
57 | * @param array $dependencies |
||
58 | */ |
||
59 | 9 | private function registerServices(array $dependencies) |
|
70 | |||
71 | /** |
||
72 | * Gets real service class name. |
||
73 | * |
||
74 | * @param array $setupData |
||
75 | * @param string $alias |
||
76 | * @return mixed |
||
77 | */ |
||
78 | 9 | private function getRealServiceClass(array $setupData, $alias) |
|
88 | |||
89 | /** |
||
90 | * Register the service. |
||
91 | * |
||
92 | * @param string $identifier |
||
93 | * @param string $serviceClass |
||
94 | */ |
||
95 | 9 | public function registerService($identifier, $serviceClass) |
|
141 | |||
142 | /** |
||
143 | * Adds a method call for the service. It will be triggered as soon as the service had been initialized. |
||
144 | * |
||
145 | * @param Definition $service |
||
146 | * @param string $method |
||
147 | * @param array $parameterList |
||
148 | */ |
||
149 | 2 | private function addMethodCall(Definition $service, $method, $parameterList = []) |
|
158 | |||
159 | /** |
||
160 | * If possible create register the parameter as a service and give it back as a reference. |
||
161 | * |
||
162 | * @param mixed $classOrServiceName |
||
163 | * |
||
164 | * @return mixed|Reference |
||
165 | */ |
||
166 | 8 | private function getReferenceServiceIfAvailable($classOrServiceName) |
|
194 | |||
195 | /** |
||
196 | * Creates a safe normalized name. |
||
197 | * |
||
198 | * @param $className |
||
199 | * @param $argumentName |
||
200 | * |
||
201 | * @return string |
||
202 | */ |
||
203 | 8 | private function getNormalizedName($className, $argumentName) |
|
210 | |||
211 | /** |
||
212 | * Gets a service. It also tries to register the one without arguments which not yet registered. |
||
213 | * |
||
214 | * @param string $identifier |
||
215 | * |
||
216 | * @return object |
||
217 | */ |
||
218 | 6 | public function get($identifier) |
|
233 | |||
234 | /** |
||
235 | * Returns true if the given service is defined. |
||
236 | * |
||
237 | * @param string $identifier |
||
238 | * |
||
239 | * @return bool |
||
240 | */ |
||
241 | 9 | public function has($identifier) |
|
245 | |||
246 | /** |
||
247 | * Register module specific services. |
||
248 | * If a service is already registered in the Global namespace, it will be skipped. |
||
249 | * |
||
250 | * @param string $moduleName |
||
251 | * @return DependencyInjectionAdapterInterface |
||
252 | */ |
||
253 | 6 | public function registerModuleServices($moduleName) |
|
263 | |||
264 | /** |
||
265 | * Sets service argument. |
||
266 | * |
||
267 | * @param string|Definition $service |
||
268 | * @param mixed $parameter |
||
269 | * |
||
270 | * @throws RuntimeException |
||
271 | * |
||
272 | * @return DependencyInjectionAdapterInterface |
||
273 | */ |
||
274 | 8 | public function setServiceArgument($service, $parameter) |
|
299 | |||
300 | /** |
||
301 | * Gets the real service instance. |
||
302 | * |
||
303 | * @param mixed $service |
||
304 | * @return Definition |
||
305 | */ |
||
306 | 8 | private function getRealService($service) |
|
314 | |||
315 | /** |
||
316 | * Gets the real parameter name. |
||
317 | * |
||
318 | * @param $parameterName |
||
319 | * @return mixed |
||
320 | */ |
||
321 | 8 | private function getRealParameterName($parameterName) |
|
329 | |||
330 | /** |
||
331 | * Checks whether the service is shared and initialized |
||
332 | * |
||
333 | * @param $serviceClass |
||
334 | * @throws RuntimeException |
||
335 | */ |
||
336 | 8 | private function checkSharedServiceClassState($serviceClass) |
|
344 | } |
||
345 |