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 | * Registers the service. |
||
| 91 | * |
||
| 92 | * @param string $identifier |
||
| 93 | * @param string $serviceClass |
||
| 94 | */ |
||
| 95 | 9 | public function registerService($identifier, $serviceClass) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Gets the set up data for the service registration. |
||
| 130 | * |
||
| 131 | * @param string $identifier |
||
| 132 | * @param string $serviceClass |
||
| 133 | * @return array |
||
| 134 | */ |
||
| 135 | 9 | private function getServiceSetupData($identifier, $serviceClass) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Adds a method call for the service. It will be triggered as soon as the service had been initialized. |
||
| 158 | * |
||
| 159 | * @param Definition $service |
||
| 160 | * @param string $method |
||
| 161 | * @param array $parameterList |
||
| 162 | */ |
||
| 163 | 2 | private function addMethodCall(Definition $service, $method, $parameterList = []) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * If possible create register the parameter as a service and give it back as a reference. |
||
| 175 | * |
||
| 176 | * @param mixed $classOrServiceName |
||
| 177 | * |
||
| 178 | * @return mixed|Reference |
||
| 179 | */ |
||
| 180 | 8 | private function getReferenceServiceIfAvailable($classOrServiceName) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Creates a safe normalized name. |
||
| 211 | * |
||
| 212 | * @param $className |
||
| 213 | * @param $argumentName |
||
| 214 | * |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | 8 | private function getNormalizedName($className, $argumentName) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Gets a service. It also tries to register the one without arguments which not yet registered. |
||
| 227 | * |
||
| 228 | * @param string $identifier |
||
| 229 | * |
||
| 230 | * @return object |
||
| 231 | */ |
||
| 232 | 6 | public function get($identifier) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Returns true if the given service is defined. |
||
| 250 | * |
||
| 251 | * @param string $identifier |
||
| 252 | * |
||
| 253 | * @return bool |
||
| 254 | */ |
||
| 255 | 9 | public function has($identifier) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Register module specific services. |
||
| 262 | * If a service is already registered in the Global namespace, it will be skipped. |
||
| 263 | * |
||
| 264 | * @param string $moduleName |
||
| 265 | * @return DependencyInjectionAdapterInterface |
||
| 266 | */ |
||
| 267 | 5 | public function registerModuleServices($moduleName) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Sets service argument. |
||
| 280 | * |
||
| 281 | * @param string|Definition $service |
||
| 282 | * @param mixed $parameter |
||
| 283 | * |
||
| 284 | * @throws RuntimeException |
||
| 285 | * |
||
| 286 | * @return DependencyInjectionAdapterInterface |
||
| 287 | */ |
||
| 288 | 8 | public function setServiceArgument($service, $parameter) |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Gets the real service instance. |
||
| 316 | * |
||
| 317 | * @param mixed $service |
||
| 318 | * @return Definition |
||
| 319 | */ |
||
| 320 | 8 | private function getRealService($service) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Gets the real parameter name. |
||
| 331 | * |
||
| 332 | * @param $parameterName |
||
| 333 | * @return mixed |
||
| 334 | */ |
||
| 335 | 8 | private function getRealParameterName($parameterName) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Checks whether the service is shared and initialized |
||
| 346 | * |
||
| 347 | * @param $serviceClass |
||
| 348 | * @throws RuntimeException |
||
| 349 | */ |
||
| 350 | 8 | private function checkSharedServiceClassState($serviceClass) |
|
| 358 | } |
||
| 359 |