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 |
||
| 26 | class SymfonyAdapter implements DependencyInjectionAdapterInterface |
||
| 27 | { |
||
| 28 | private const SERVICE_CLASS = 'class'; |
||
| 29 | private const SERVICE_ARGUMENTS = 'arguments'; |
||
| 30 | private const SERVICE_METHOD_CALL = 'calls'; |
||
| 31 | private const SERVICE_SHARE = 'shared'; |
||
| 32 | |||
| 33 | /** @var ContainerBuilder */ |
||
| 34 | private $container; |
||
| 35 | /** @var array */ |
||
| 36 | private $configuration; |
||
| 37 | /** @var string */ |
||
| 38 | private $moduleNamespace; |
||
| 39 | /** @var array */ |
||
| 40 | private $servicesToDefine = []; |
||
| 41 | /** @var array */ |
||
| 42 | private $instantiatedSharedServices = []; |
||
| 43 | /** @var array */ |
||
| 44 | private $defaultSetUpData = [ |
||
| 45 | self::SERVICE_CLASS => '', |
||
| 46 | self::SERVICE_ARGUMENTS => [], |
||
| 47 | self::SERVICE_METHOD_CALL => [], |
||
| 48 | // By default the Symfony DI shares all services. In WebHemi by default nothing is shared. |
||
| 49 | self::SERVICE_SHARE => false, |
||
| 50 | ]; |
||
| 51 | /** @var int */ |
||
| 52 | private static $parameterIndex = 0; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * DependencyInjectionAdapterInterface constructor. |
||
| 56 | * |
||
| 57 | * @param ConfigInterface $configuration |
||
| 58 | */ |
||
| 59 | 12 | public function __construct(ConfigInterface $configuration) |
|
| 64 | |||
| 65 | /** |
||
| 66 | * Initializes the DI container from the config. |
||
| 67 | * |
||
| 68 | * @param array $dependencies |
||
| 69 | * @return DependencyInjectionAdapterInterface |
||
| 70 | */ |
||
| 71 | 9 | private function registerServices(array $dependencies) : DependencyInjectionAdapterInterface |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Gets real service class name. |
||
| 87 | * |
||
| 88 | * @param array $setupData |
||
| 89 | * @param string $alias |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | 9 | private function getRealServiceClass(array $setupData, string $alias) : string |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Registers the service. |
||
| 105 | * |
||
| 106 | * @param string $identifier |
||
| 107 | * @param string|object $serviceClass |
||
| 108 | * @return DependencyInjectionAdapterInterface |
||
| 109 | */ |
||
| 110 | 10 | public function registerService(string $identifier, $serviceClass) : DependencyInjectionAdapterInterface |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Gets the set up data for the service registration. |
||
| 158 | * |
||
| 159 | * @param string $identifier |
||
| 160 | * @param string $serviceClass |
||
| 161 | * @return array |
||
| 162 | */ |
||
| 163 | 10 | private function getServiceSetupData(string $identifier, string $serviceClass) : array |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Adds a method call for the service. It will be triggered as soon as the service had been initialized. |
||
| 181 | * |
||
| 182 | * @param Definition $service |
||
| 183 | * @param string $method |
||
| 184 | * @param array $parameterList |
||
| 185 | * @return void |
||
| 186 | */ |
||
| 187 | 2 | private function addMethodCall(Definition $service, string $method, array $parameterList = []) : void |
|
| 196 | |||
| 197 | /** |
||
| 198 | * If possible create register the parameter as a service and give it back as a reference. |
||
| 199 | * |
||
| 200 | * @param mixed $classOrServiceName |
||
| 201 | * @return mixed|Reference |
||
| 202 | */ |
||
| 203 | 9 | private function getReferenceServiceIfAvailable($classOrServiceName) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Creates a safe normalized name. |
||
| 234 | * |
||
| 235 | * @param string $className |
||
| 236 | * @param string $argumentName |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | 9 | private function getNormalizedName(string $className, string $argumentName) : string |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Gets a service. It also tries to register the one without arguments which not yet registered. |
||
| 249 | * |
||
| 250 | * @param string $identifier |
||
| 251 | * @return object |
||
| 252 | */ |
||
| 253 | 8 | public function get(string $identifier) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Returns true if the given service is defined. |
||
| 271 | * |
||
| 272 | * @param string $identifier |
||
| 273 | * @return bool |
||
| 274 | */ |
||
| 275 | 10 | public function has(string $identifier) : bool |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Register module specific services. |
||
| 282 | * If a service is already registered, it will be skipped. |
||
| 283 | * |
||
| 284 | * @param string $moduleName |
||
| 285 | * @return DependencyInjectionAdapterInterface |
||
| 286 | */ |
||
| 287 | 10 | public function registerModuleServices(string $moduleName) : DependencyInjectionAdapterInterface |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Sets service argument. |
||
| 299 | * |
||
| 300 | * @param string|Definition $service |
||
| 301 | * @param mixed $parameter |
||
| 302 | * @throws RuntimeException |
||
| 303 | * @return DependencyInjectionAdapterInterface |
||
| 304 | */ |
||
| 305 | 9 | public function setServiceArgument($service, $parameter) : DependencyInjectionAdapterInterface |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Gets the real service instance. |
||
| 333 | * |
||
| 334 | * @param mixed $service |
||
| 335 | * @return Definition |
||
| 336 | */ |
||
| 337 | 9 | private function getRealService($service) : Definition |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Gets the real parameter name. |
||
| 348 | * |
||
| 349 | * @param mixed $parameterName |
||
| 350 | * @return string |
||
| 351 | */ |
||
| 352 | 9 | private function getRealParameterName($parameterName) : string |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Checks whether the service is shared and initialized |
||
| 363 | * |
||
| 364 | * @param string $serviceClass |
||
| 365 | * @throws RuntimeException |
||
| 366 | * @return void |
||
| 367 | */ |
||
| 368 | 9 | private function checkSharedServiceClassState(string $serviceClass) : void |
|
| 376 | } |
||
| 377 |