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 | 13 | 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 | 10 | 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 | 10 | 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 | 11 | public function registerService(string $identifier, $serviceClass) : ServiceInterface |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Gets the set up data for the service registration. |
||
| 159 | * |
||
| 160 | * @param string $identifier |
||
| 161 | * @param string $serviceClass |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | 11 | private function getServiceSetupData(string $identifier, string $serviceClass) : array |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Resolves inheritance. Could solve in within the getServiceSetupData method but it would dramatically increase the |
||
| 189 | * code complexity index. |
||
| 190 | * |
||
| 191 | * @param array $setUpData |
||
| 192 | * @return array |
||
| 193 | */ |
||
| 194 | private function resolveInheritance(array $setUpData) : array |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Adds a method call for the service. It will be triggered as soon as the service had been initialized. |
||
| 216 | * |
||
| 217 | * @param Definition $service |
||
| 218 | * @param string $method |
||
| 219 | * @param array $parameterList |
||
| 220 | * @return void |
||
| 221 | */ |
||
| 222 | 2 | private function addMethodCall(Definition $service, string $method, array $parameterList = []) : void |
|
| 231 | |||
| 232 | /** |
||
| 233 | * If possible create register the parameter as a service and give it back as a reference. |
||
| 234 | * |
||
| 235 | * @param mixed $classOrServiceName |
||
| 236 | * @return mixed|Reference |
||
| 237 | */ |
||
| 238 | 10 | private function getReferenceServiceIfAvailable($classOrServiceName) |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Creates a safe normalized name. |
||
| 269 | * |
||
| 270 | * @param string $className |
||
| 271 | * @param string $argumentName |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | 10 | private function getNormalizedName(string $className, string $argumentName) : string |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Gets a service. It also tries to register the one without arguments which not yet registered. |
||
| 284 | * |
||
| 285 | * @param string $identifier |
||
| 286 | * @return object |
||
| 287 | */ |
||
| 288 | 8 | public function get(string $identifier) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Returns true if the given service is defined. |
||
| 306 | * |
||
| 307 | * @param string $identifier |
||
| 308 | * @return bool |
||
| 309 | */ |
||
| 310 | 11 | public function has(string $identifier) : bool |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Register module specific services. |
||
| 317 | * If a service is already registered, it will be skipped. |
||
| 318 | * |
||
| 319 | * @param string $moduleName |
||
| 320 | * @return ServiceInterface |
||
| 321 | */ |
||
| 322 | 11 | public function registerModuleServices(string $moduleName) : ServiceInterface |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Sets service argument. |
||
| 334 | * |
||
| 335 | * @param string|Definition $service |
||
| 336 | * @param mixed $parameter |
||
| 337 | * @throws RuntimeException |
||
| 338 | * @return ServiceInterface |
||
| 339 | */ |
||
| 340 | 10 | public function setServiceArgument($service, $parameter) : ServiceInterface |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Gets the real service instance. |
||
| 368 | * |
||
| 369 | * @param mixed $service |
||
| 370 | * @return Definition |
||
| 371 | */ |
||
| 372 | 10 | private function getRealService($service) : Definition |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Gets the real parameter name. |
||
| 383 | * |
||
| 384 | * @param mixed $parameterName |
||
| 385 | * @return string |
||
| 386 | */ |
||
| 387 | 10 | private function getRealParameterName($parameterName) : string |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Checks whether the service is shared and initialized |
||
| 398 | * |
||
| 399 | * @param string $serviceClass |
||
| 400 | * @throws RuntimeException |
||
| 401 | * @return void |
||
| 402 | */ |
||
| 403 | 10 | private function checkSharedServiceClassState(string $serviceClass) : void |
|
| 411 | } |
||
| 412 |