Complex classes like DoctrineOrmServiceProvider 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 DoctrineOrmServiceProvider, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | final class DoctrineOrmServiceProvider implements ServiceProviderInterface |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * Register ORM service. |
||
| 42 | * |
||
| 43 | * @param Container $container |
||
| 44 | */ |
||
| 45 | 2 | public function register(Container $container) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * @return array |
||
| 91 | */ |
||
| 92 | 2 | private function getOrmEmDefaultOptions(): array |
|
| 100 | |||
| 101 | /** |
||
| 102 | * @param Container $container |
||
| 103 | * |
||
| 104 | * @return \Closure |
||
| 105 | */ |
||
| 106 | private function getOrmEmsOptionsInitializerDefinition(Container $container): \Closure |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param Container $container |
||
| 138 | * |
||
| 139 | * @return \Closure |
||
| 140 | */ |
||
| 141 | private function getOrmEmsDefinition(Container $container): \Closure |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param Container $container |
||
| 170 | * |
||
| 171 | * @return \Closure |
||
| 172 | */ |
||
| 173 | private function getOrmEmsConfigServiceProvider(Container $container): \Closure |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param Container $container |
||
| 189 | * @param string $name |
||
| 190 | * @param array $options |
||
| 191 | * |
||
| 192 | * @return \Closure |
||
| 193 | */ |
||
| 194 | private function getOrmEmConfigByNameAndOptionsDefinition( |
||
| 195 | Container $container, |
||
| 196 | string $name, |
||
| 197 | array $options |
||
| 198 | ): \Closure { |
||
| 199 | 2 | return function () use ($container, $name, $options) { |
|
| 200 | 2 | $config = new Configuration(); |
|
| 201 | |||
| 202 | 2 | $config->setProxyDir($container['orm.proxies_dir']); |
|
| 203 | 2 | $config->setAutoGenerateProxyClasses($container['orm.auto_generate_proxies']); |
|
| 204 | 2 | $config->setProxyNamespace($container['orm.proxies_namespace']); |
|
| 205 | $config->setMetadataDriverImpl( |
||
| 206 | $container['orm.mapping_driver_chain']($name, $config, (array) $options['mappings']) |
||
| 207 | 2 | ); |
|
| 208 | 2 | $config->setQueryCacheImpl($container['orm.cache.locator']($name, 'query', $options)); |
|
| 209 | 1 | $config->setHydrationCacheImpl($container['orm.cache.locator']($name, 'hydration', $options)); |
|
| 210 | $config->setMetadataCacheImpl($container['orm.cache.locator']($name, 'metadata', $options)); |
||
| 211 | $config->setResultCacheImpl($container['orm.cache.locator']($name, 'result', $options)); |
||
| 212 | |||
| 213 | foreach ((array) $options['types'] as $typeName => $typeClass) { |
||
| 214 | if (Type::hasType($typeName)) { |
||
| 215 | 1 | Type::overrideType($typeName, $typeClass); |
|
| 216 | } else { |
||
| 217 | Type::addType($typeName, $typeClass); |
||
| 218 | } |
||
| 219 | 1 | } |
|
| 220 | 1 | ||
| 221 | $config->setCustomStringFunctions($container['orm.custom.functions.string']); |
||
| 222 | $config->setCustomNumericFunctions($container['orm.custom.functions.numeric']); |
||
| 223 | $config->setCustomDatetimeFunctions($container['orm.custom.functions.datetime']); |
||
| 224 | $config->setCustomHydrationModes($container['orm.custom.hydration_modes']); |
||
| 225 | |||
| 226 | 1 | $config->setClassMetadataFactoryName($container['orm.class_metadata_factory_name']); |
|
| 227 | $config->setDefaultRepositoryClassName($container['orm.default_repository_class']); |
||
| 228 | |||
| 229 | 2 | $config->setNamingStrategy($container['orm.strategy.naming']); |
|
| 230 | $config->setQuoteStrategy($container['orm.strategy.quote']); |
||
| 231 | 2 | ||
| 232 | 2 | $config->setEntityListenerResolver($container['orm.entity_listener_resolver']); |
|
| 233 | 2 | $config->setRepositoryFactory($container['orm.repository_factory']); |
|
| 234 | 2 | ||
| 235 | $config->setSecondLevelCacheEnabled($container['orm.second_level_cache.enabled']); |
||
| 236 | 2 | $config->setSecondLevelCacheConfiguration($container['orm.second_level_cache.configuration']); |
|
| 237 | |||
| 238 | $config->setDefaultQueryHints($container['orm.default.query_hints']); |
||
| 239 | |||
| 240 | return $config; |
||
| 241 | }; |
||
| 242 | } |
||
| 243 | |||
| 244 | 2 | /** |
|
| 245 | 2 | * @param Container $container |
|
| 246 | 2 | * |
|
| 247 | 2 | * @return \Closure |
|
| 248 | */ |
||
| 249 | 2 | private function getOrmMappingDriverChainDefinition(Container $container): \Closure |
|
| 250 | 2 | { |
|
| 251 | return $container->protect(function (string $name, Configuration $config, array $mappings) use ($container) { |
||
| 252 | 2 | $container['orm.ems.options.initializer'](); |
|
| 253 | 2 | ||
| 254 | if (null === $name) { |
||
| 255 | 2 | $name = $container['orm.ems.default']; |
|
| 256 | 2 | } |
|
| 257 | |||
| 258 | 2 | $cacheInstanceKey = 'orm.mapping_driver_chain.instances.'.$name; |
|
| 259 | 2 | if (isset($container[$cacheInstanceKey])) { |
|
| 260 | return $container[$cacheInstanceKey]; |
||
| 261 | 2 | } |
|
| 262 | |||
| 263 | 2 | /** @var MappingDriverChain $chain */ |
|
| 264 | 2 | $chain = $container['orm.mapping_driver_chain.factory'](); |
|
| 265 | foreach ($mappings as $entity) { |
||
| 266 | if (!is_array($entity)) { |
||
| 267 | throw new \InvalidArgumentException( |
||
| 268 | "The 'orm.em.options' option 'mappings' should be an array of arrays." |
||
| 269 | ); |
||
| 270 | } |
||
| 271 | |||
| 272 | if (isset($entity['alias'])) { |
||
| 273 | $config->addEntityNamespace($entity['alias'], $entity['namespace']); |
||
| 274 | 2 | } |
|
| 275 | 2 | ||
| 276 | $factoryKey = sprintf('orm.mapping_driver.factory.%s', $entity['type']); |
||
| 277 | 2 | if (!isset($container[$factoryKey])) { |
|
| 278 | throw new \InvalidArgumentException( |
||
| 279 | sprintf('There is no driver factory for type "%s"', $entity['type']) |
||
| 280 | ); |
||
| 281 | 2 | } |
|
| 282 | 2 | ||
| 283 | $chain->addDriver($container[$factoryKey]($entity, $config), $entity['namespace']); |
||
| 284 | } |
||
| 285 | |||
| 286 | 2 | return $container[$cacheInstanceKey] = $chain; |
|
| 287 | 2 | }); |
|
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param Container $container |
||
| 292 | * |
||
| 293 | * @return \Closure |
||
| 294 | */ |
||
| 295 | private function getOrmMappingDriverChainFactoryDefinition(Container $container): \Closure |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param Container $container |
||
| 304 | * |
||
| 305 | * @return \Closure |
||
| 306 | */ |
||
| 307 | private function getOrmMappingDriverFactoryAnnotation(Container $container): \Closure |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param Container $container |
||
| 321 | * |
||
| 322 | * @return \Closure |
||
| 323 | */ |
||
| 324 | private function getOrmMappingDriverFactoryYaml(Container $container): \Closure |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param Container $container |
||
| 333 | * |
||
| 334 | * @return \Closure |
||
| 335 | */ |
||
| 336 | private function getOrmMappingDriverFactorySimpleYaml(Container $container): \Closure |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @param Container $container |
||
| 345 | * |
||
| 346 | * @return \Closure |
||
| 347 | */ |
||
| 348 | private function getOrmMappingDriverFactoryXml(Container $container): \Closure |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param Container $container |
||
| 357 | * |
||
| 358 | * @return \Closure |
||
| 359 | */ |
||
| 360 | private function getOrmMappingDriverFactorySimpleXml(Container $container): \Closure |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param Container $container |
||
| 369 | * |
||
| 370 | * @return \Closure |
||
| 371 | */ |
||
| 372 | private function getOrmMappingDriverFactoryPhp(Container $container): \Closure |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @param Container $container |
||
| 381 | * |
||
| 382 | * @return \Closure |
||
| 383 | */ |
||
| 384 | private function getOrmCacheLocatorDefinition(Container $container): \Closure |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @param Container $container |
||
| 422 | * |
||
| 423 | * @return \Closure |
||
| 424 | */ |
||
| 425 | private function getOrmCacheFactoryDefinition(Container $container): \Closure |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @param Container $container |
||
| 441 | * |
||
| 442 | * @return \Closure |
||
| 443 | */ |
||
| 444 | private function getOrmCacheFactoryApcuDefinition(Container $container): \Closure |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @param Container $container |
||
| 453 | * |
||
| 454 | * @return \Closure |
||
| 455 | */ |
||
| 456 | private function getOrmCacheFactoryArrayDefinition(Container $container): \Closure |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @param Container $container |
||
| 465 | * |
||
| 466 | * @return \Closure |
||
| 467 | */ |
||
| 468 | private function getOrmCacheFactoryFilesystemDefinition(Container $container): \Closure |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @param Container $container |
||
| 486 | * |
||
| 487 | * @return \Closure |
||
| 488 | */ |
||
| 489 | private function getOrmCacheFactoryMemcacheDefinition(Container $container): \Closure |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @param Container $container |
||
| 508 | * |
||
| 509 | * @return \Closure |
||
| 510 | */ |
||
| 511 | private function getOrmCacheFactoryMemcachedDefinition(Container $container): \Closure |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @param Container $container |
||
| 530 | * |
||
| 531 | * @return \Closure |
||
| 532 | */ |
||
| 533 | private function getOrmCacheFactoryRedisDefinition(Container $container): \Closure |
||
| 553 | |||
| 554 | /** |
||
| 555 | * @param Container $container |
||
| 556 | * |
||
| 557 | * @return \Closure |
||
| 558 | */ |
||
| 559 | private function getOrmCacheFactoryXCacheDefinition(Container $container): \Closure |
||
| 565 | |||
| 566 | /** |
||
| 567 | * @param Container $container |
||
| 568 | * |
||
| 569 | * @return \Closure |
||
| 570 | */ |
||
| 571 | private function getOrmNamingStrategyDefinition(Container $container): \Closure |
||
| 577 | |||
| 578 | /** |
||
| 579 | * @param Container $container |
||
| 580 | * |
||
| 581 | * @return \Closure |
||
| 582 | */ |
||
| 583 | private function getOrmQuoteStrategyDefinition(Container $container): \Closure |
||
| 589 | |||
| 590 | /** |
||
| 591 | * @param Container $container |
||
| 592 | * |
||
| 593 | * @return \Closure |
||
| 594 | */ |
||
| 595 | private function getOrmEntityListenerResolverDefinition(Container $container): \Closure |
||
| 601 | |||
| 602 | /** |
||
| 603 | * @param Container $container |
||
| 604 | * |
||
| 605 | * @return \Closure |
||
| 606 | */ |
||
| 607 | private function getOrmRepositoryFactoryDefinition(Container $container): \Closure |
||
| 613 | |||
| 614 | /** |
||
| 615 | * @param Container $container |
||
| 616 | * |
||
| 617 | * @return \Closure |
||
| 618 | */ |
||
| 619 | private function getOrmSecondLevelCacheConfigurationDefinition(Container $container): \Closure |
||
| 625 | |||
| 626 | /** |
||
| 627 | * @param Container $container |
||
| 628 | * |
||
| 629 | * @return \Closure |
||
| 630 | */ |
||
| 631 | private function getOrmEmDefinition(Container $container): \Closure |
||
| 639 | |||
| 640 | /** |
||
| 641 | * @param Container $container |
||
| 642 | * |
||
| 643 | * @return \Closure |
||
| 644 | */ |
||
| 645 | private function getOrmEmConfigDefinition(Container $container): \Closure |
||
| 653 | } |
||
| 654 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.