Complex classes like ServiceAdapter 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 ServiceAdapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class ServiceAdapter implements ServiceInterface |
||
| 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 | private const SERVICE_INHERIT = 'inherits'; |
||
| 33 | |||
| 34 | /** @var ContainerBuilder */ |
||
| 35 | private $container; |
||
| 36 | /** @var array */ |
||
| 37 | private $configuration; |
||
| 38 | /** @var string */ |
||
| 39 | private $moduleNamespace; |
||
| 40 | /** @var array */ |
||
| 41 | private $servicesToDefine = []; |
||
| 42 | /** @var array */ |
||
| 43 | private $instantiatedSharedServices = []; |
||
| 44 | /** @var array */ |
||
| 45 | private $defaultSetUpData = [ |
||
| 46 | self::SERVICE_CLASS => '', |
||
| 47 | self::SERVICE_ARGUMENTS => [], |
||
| 48 | self::SERVICE_METHOD_CALL => [], |
||
| 49 | // By default the Symfony DI shares all services. In WebHemi by default nothing is shared. |
||
| 50 | self::SERVICE_SHARE => false, |
||
| 51 | ]; |
||
| 52 | /** @var int */ |
||
| 53 | private static $parameterIndex = 0; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * ServiceAdapter constructor. |
||
| 57 | * |
||
| 58 | * @param ConfigurationInterface $configuration |
||
| 59 | */ |
||
| 60 | 14 | public function __construct(ConfigurationInterface $configuration) |
|
| 65 | |||
| 66 | /** |
||
| 67 | * Initializes the DI container from the config. |
||
| 68 | * |
||
| 69 | * @param array $dependencies |
||
| 70 | * @return ServiceAdapter |
||
| 71 | */ |
||
| 72 | 11 | private function registerServices(array $dependencies) : ServiceAdapter |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Gets real service class name. |
||
| 88 | * |
||
| 89 | * @param array $setupData |
||
| 90 | * @param string $alias |
||
| 91 | * @return string |
||
| 92 | */ |
||
| 93 | 11 | private function getRealServiceClass(array $setupData, string $alias) : string |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Registers the service. |
||
| 106 | * |
||
| 107 | * @param string $identifier |
||
| 108 | * @param string|object $serviceClass |
||
| 109 | * @return ServiceInterface |
||
| 110 | */ |
||
| 111 | 12 | public function registerService(string $identifier, $serviceClass) : ServiceInterface |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Gets the set up data for the service registration. |
||
| 161 | * |
||
| 162 | * @param string $identifier |
||
| 163 | * @param string $serviceClass |
||
| 164 | * @return array |
||
| 165 | */ |
||
| 166 | 12 | private function getServiceSetupData(string $identifier, string $serviceClass) : array |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Resolves inheritance. Could solve in within the getServiceSetupData method but it would dramatically increase the |
||
| 194 | * code complexity index. |
||
| 195 | * |
||
| 196 | * @param array $setUpData |
||
| 197 | * @return array |
||
| 198 | */ |
||
| 199 | 1 | private function resolveInheritance(array $setUpData) : array |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Adds a method call for the service. It will be triggered as soon as the service had been initialized. |
||
| 222 | * |
||
| 223 | * @param Definition $service |
||
| 224 | * @param string $method |
||
| 225 | * @param array $parameterList |
||
| 226 | * @return void |
||
| 227 | */ |
||
| 228 | 2 | private function addMethodCall(Definition $service, string $method, array $parameterList = []) : void |
|
| 237 | |||
| 238 | /** |
||
| 239 | * If possible create register the parameter as a service and give it back as a reference. |
||
| 240 | * |
||
| 241 | * @param mixed $classOrServiceName |
||
| 242 | * @return mixed|Reference |
||
| 243 | */ |
||
| 244 | 10 | private function getReferenceServiceIfAvailable($classOrServiceName) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Creates a safe normalized name. |
||
| 275 | * |
||
| 276 | * @param string $className |
||
| 277 | * @param string $argumentName |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | 10 | private function getNormalizedName(string $className, string $argumentName) : string |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Gets a service. It also tries to register the one without arguments which not yet registered. |
||
| 290 | * |
||
| 291 | * @param string $identifier |
||
| 292 | * @return object |
||
| 293 | */ |
||
| 294 | 8 | public function get(string $identifier) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Returns true if the given service is defined. |
||
| 312 | * |
||
| 313 | * @param string $identifier |
||
| 314 | * @return bool |
||
| 315 | */ |
||
| 316 | 12 | public function has(string $identifier) : bool |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Register module specific services. |
||
| 323 | * If a service is already registered, it will be skipped. |
||
| 324 | * |
||
| 325 | * @param string $moduleName |
||
| 326 | * @return ServiceInterface |
||
| 327 | */ |
||
| 328 | 12 | public function registerModuleServices(string $moduleName) : ServiceInterface |
|
| 337 | |||
| 338 | /** |
||
| 339 | * Sets service argument. |
||
| 340 | * |
||
| 341 | * @param string|Definition $service |
||
| 342 | * @param mixed $parameter |
||
| 343 | * @throws RuntimeException |
||
| 344 | * @return ServiceInterface |
||
| 345 | */ |
||
| 346 | 10 | public function setServiceArgument($service, $parameter) : ServiceInterface |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Gets the real service instance. |
||
| 374 | * |
||
| 375 | * @param mixed $service |
||
| 376 | * @return Definition |
||
| 377 | */ |
||
| 378 | 10 | private function getRealService($service) : Definition |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Gets the real parameter name. |
||
| 389 | * |
||
| 390 | * @param mixed $parameterName |
||
| 391 | * @return string |
||
| 392 | */ |
||
| 393 | 10 | private function getRealParameterName($parameterName) : string |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Checks whether the service is shared and initialized |
||
| 404 | * |
||
| 405 | * @param string $serviceClass |
||
| 406 | * @throws RuntimeException |
||
| 407 | * @return void |
||
| 408 | */ |
||
| 409 | 10 | private function checkSharedServiceClassState(string $serviceClass) : void |
|
| 417 | } |
||
| 418 |