Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like DoctrineExtension 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 DoctrineExtension, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class DoctrineExtension extends AbstractDoctrineExtension |
||
| 36 | { |
||
| 37 | /** @var string */ |
||
| 38 | private $defaultConnection; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * {@inheritDoc} |
||
| 42 | */ |
||
| 43 | public function load(array $configs, ContainerBuilder $container) |
||
| 44 | { |
||
| 45 | $configuration = $this->getConfiguration($configs, $container); |
||
| 46 | $config = $this->processConfiguration($configuration, $configs); |
||
|
|
|||
| 47 | |||
| 48 | if (! empty($config['dbal'])) { |
||
| 49 | $this->dbalLoad($config['dbal'], $container); |
||
| 50 | |||
| 51 | $this->loadMessengerServices($container); |
||
| 52 | } |
||
| 53 | |||
| 54 | if (empty($config['orm'])) { |
||
| 55 | return; |
||
| 56 | } |
||
| 57 | |||
| 58 | if (empty($config['dbal'])) { |
||
| 59 | throw new LogicException('Configuring the ORM layer requires to configure the DBAL layer as well.'); |
||
| 60 | } |
||
| 61 | |||
| 62 | $this->ormLoad($config['orm'], $container); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Loads the DBAL configuration. |
||
| 67 | * |
||
| 68 | * Usage example: |
||
| 69 | * |
||
| 70 | * <doctrine:dbal id="myconn" dbname="sfweb" user="root" /> |
||
| 71 | * |
||
| 72 | * @param array $config An array of configuration settings |
||
| 73 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
| 74 | */ |
||
| 75 | protected function dbalLoad(array $config, ContainerBuilder $container) |
||
| 76 | { |
||
| 77 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
||
| 78 | $loader->load('dbal.xml'); |
||
| 79 | |||
| 80 | if (empty($config['default_connection'])) { |
||
| 81 | $keys = array_keys($config['connections']); |
||
| 82 | $config['default_connection'] = reset($keys); |
||
| 83 | } |
||
| 84 | |||
| 85 | $this->defaultConnection = $config['default_connection']; |
||
| 86 | |||
| 87 | $container->setAlias('database_connection', sprintf('doctrine.dbal.%s_connection', $this->defaultConnection)); |
||
| 88 | $container->getAlias('database_connection')->setPublic(true); |
||
| 89 | $container->setAlias('doctrine.dbal.event_manager', new Alias(sprintf('doctrine.dbal.%s_connection.event_manager', $this->defaultConnection), false)); |
||
| 90 | |||
| 91 | $container->setParameter('doctrine.dbal.connection_factory.types', $config['types']); |
||
| 92 | |||
| 93 | $connections = []; |
||
| 94 | |||
| 95 | foreach (array_keys($config['connections']) as $name) { |
||
| 96 | $connections[$name] = sprintf('doctrine.dbal.%s_connection', $name); |
||
| 97 | } |
||
| 98 | |||
| 99 | $container->setParameter('doctrine.connections', $connections); |
||
| 100 | $container->setParameter('doctrine.default_connection', $this->defaultConnection); |
||
| 101 | |||
| 102 | foreach ($config['connections'] as $name => $connection) { |
||
| 103 | $this->loadDbalConnection($name, $connection, $container); |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Loads a configured DBAL connection. |
||
| 109 | * |
||
| 110 | * @param string $name The name of the connection |
||
| 111 | * @param array $connection A dbal connection configuration. |
||
| 112 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
| 113 | */ |
||
| 114 | protected function loadDbalConnection($name, array $connection, ContainerBuilder $container) |
||
| 115 | { |
||
| 116 | $configuration = $container->setDefinition(sprintf('doctrine.dbal.%s_connection.configuration', $name), new ChildDefinition('doctrine.dbal.connection.configuration')); |
||
| 117 | $logger = null; |
||
| 118 | if ($connection['logging']) { |
||
| 119 | $logger = new Reference('doctrine.dbal.logger'); |
||
| 120 | } |
||
| 121 | unset($connection['logging']); |
||
| 122 | |||
| 123 | if ($connection['profiling']) { |
||
| 124 | $profilingAbstractId = $connection['profiling_collect_backtrace'] ? |
||
| 125 | 'doctrine.dbal.logger.backtrace' : |
||
| 126 | 'doctrine.dbal.logger.profiling'; |
||
| 127 | |||
| 128 | $profilingLoggerId = $profilingAbstractId . '.' . $name; |
||
| 129 | $container->setDefinition($profilingLoggerId, new ChildDefinition($profilingAbstractId)); |
||
| 130 | $profilingLogger = new Reference($profilingLoggerId); |
||
| 131 | $container->getDefinition('data_collector.doctrine')->addMethodCall('addLogger', [$name, $profilingLogger]); |
||
| 132 | |||
| 133 | if ($logger !== null) { |
||
| 134 | $chainLogger = new ChildDefinition('doctrine.dbal.logger.chain'); |
||
| 135 | $chainLogger->addMethodCall('addLogger', [$profilingLogger]); |
||
| 136 | |||
| 137 | $loggerId = 'doctrine.dbal.logger.chain.' . $name; |
||
| 138 | $container->setDefinition($loggerId, $chainLogger); |
||
| 139 | $logger = new Reference($loggerId); |
||
| 140 | } else { |
||
| 141 | $logger = $profilingLogger; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | unset($connection['profiling'], $connection['profiling_collect_backtrace']); |
||
| 145 | |||
| 146 | if (isset($connection['auto_commit'])) { |
||
| 147 | $configuration->addMethodCall('setAutoCommit', [$connection['auto_commit']]); |
||
| 148 | } |
||
| 149 | |||
| 150 | unset($connection['auto_commit']); |
||
| 151 | |||
| 152 | if (isset($connection['schema_filter']) && $connection['schema_filter']) { |
||
| 153 | $definition = new Definition(RegexSchemaAssetFilter::class, [$connection['schema_filter']]); |
||
| 154 | $definition->addTag('doctrine.dbal.schema_filter', ['connection' => $name]); |
||
| 155 | $container->setDefinition(sprintf('doctrine.dbal.%s_regex_schema_filter', $name), $definition); |
||
| 156 | } |
||
| 157 | |||
| 158 | unset($connection['schema_filter']); |
||
| 159 | |||
| 160 | if ($logger) { |
||
| 161 | $configuration->addMethodCall('setSQLLogger', [$logger]); |
||
| 162 | } |
||
| 163 | |||
| 164 | // event manager |
||
| 165 | $container->setDefinition(sprintf('doctrine.dbal.%s_connection.event_manager', $name), new ChildDefinition('doctrine.dbal.connection.event_manager')); |
||
| 166 | |||
| 167 | // connection |
||
| 168 | $options = $this->getConnectionOptions($connection); |
||
| 169 | |||
| 170 | $def = $container |
||
| 171 | ->setDefinition(sprintf('doctrine.dbal.%s_connection', $name), new ChildDefinition('doctrine.dbal.connection')) |
||
| 172 | ->setPublic(true) |
||
| 173 | ->setArguments([ |
||
| 174 | $options, |
||
| 175 | new Reference(sprintf('doctrine.dbal.%s_connection.configuration', $name)), |
||
| 176 | new Reference(sprintf('doctrine.dbal.%s_connection.event_manager', $name)), |
||
| 177 | $connection['mapping_types'], |
||
| 178 | ]); |
||
| 179 | |||
| 180 | // Set class in case "wrapper_class" option was used to assist IDEs |
||
| 181 | if (isset($options['wrapperClass'])) { |
||
| 182 | $def->setClass($options['wrapperClass']); |
||
| 183 | } |
||
| 184 | |||
| 185 | if (! empty($connection['use_savepoints'])) { |
||
| 186 | $def->addMethodCall('setNestTransactionsWithSavepoints', [$connection['use_savepoints']]); |
||
| 187 | } |
||
| 188 | |||
| 189 | // Create a shard_manager for this connection |
||
| 190 | if (! isset($options['shards'])) { |
||
| 191 | return; |
||
| 192 | } |
||
| 193 | |||
| 194 | $shardManagerDefinition = new Definition($options['shardManagerClass'], [new Reference(sprintf('doctrine.dbal.%s_connection', $name))]); |
||
| 195 | $container->setDefinition(sprintf('doctrine.dbal.%s_shard_manager', $name), $shardManagerDefinition); |
||
| 196 | } |
||
| 197 | |||
| 198 | protected function getConnectionOptions($connection) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Loads the Doctrine ORM configuration. |
||
| 314 | * |
||
| 315 | * Usage example: |
||
| 316 | * |
||
| 317 | * <doctrine:orm id="mydm" connection="myconn" /> |
||
| 318 | * |
||
| 319 | * @param array $config An array of configuration settings |
||
| 320 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
| 321 | */ |
||
| 322 | protected function ormLoad(array $config, ContainerBuilder $container) |
||
| 323 | { |
||
| 324 | if (! class_exists(Version::class)) { |
||
| 325 | throw new LogicException('To configure the ORM layer, you must first install the doctrine/orm package.'); |
||
| 326 | } |
||
| 327 | |||
| 328 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
||
| 329 | $loader->load('orm.xml'); |
||
| 330 | |||
| 331 | if (class_exists(AbstractType::class)) { |
||
| 332 | $container->getDefinition('form.type.entity')->addTag('kernel.reset', ['method' => 'reset']); |
||
| 333 | } |
||
| 334 | |||
| 335 | $entityManagers = []; |
||
| 336 | foreach (array_keys($config['entity_managers']) as $name) { |
||
| 337 | $entityManagers[$name] = sprintf('doctrine.orm.%s_entity_manager', $name); |
||
| 338 | } |
||
| 339 | $container->setParameter('doctrine.entity_managers', $entityManagers); |
||
| 340 | |||
| 341 | if (empty($config['default_entity_manager'])) { |
||
| 342 | $tmp = array_keys($entityManagers); |
||
| 343 | $config['default_entity_manager'] = reset($tmp); |
||
| 344 | } |
||
| 345 | $container->setParameter('doctrine.default_entity_manager', $config['default_entity_manager']); |
||
| 346 | |||
| 347 | $options = ['auto_generate_proxy_classes', 'proxy_dir', 'proxy_namespace']; |
||
| 348 | foreach ($options as $key) { |
||
| 349 | $container->setParameter('doctrine.orm.' . $key, $config[$key]); |
||
| 350 | } |
||
| 351 | |||
| 352 | $container->setAlias('doctrine.orm.entity_manager', sprintf('doctrine.orm.%s_entity_manager', $config['default_entity_manager'])); |
||
| 353 | $container->getAlias('doctrine.orm.entity_manager')->setPublic(true); |
||
| 354 | |||
| 355 | $config['entity_managers'] = $this->fixManagersAutoMappings($config['entity_managers'], $container->getParameter('kernel.bundles')); |
||
| 356 | |||
| 357 | foreach ($config['entity_managers'] as $name => $entityManager) { |
||
| 358 | $entityManager['name'] = $name; |
||
| 359 | $this->loadOrmEntityManager($entityManager, $container); |
||
| 360 | |||
| 361 | if (interface_exists(PropertyInfoExtractorInterface::class)) { |
||
| 362 | $this->loadPropertyInfoExtractor($name, $container); |
||
| 363 | } |
||
| 364 | |||
| 365 | $this->loadValidatorLoader($name, $container); |
||
| 366 | } |
||
| 367 | |||
| 368 | if ($config['resolve_target_entities']) { |
||
| 369 | $def = $container->findDefinition('doctrine.orm.listeners.resolve_target_entity'); |
||
| 370 | foreach ($config['resolve_target_entities'] as $name => $implementation) { |
||
| 371 | $def->addMethodCall('addResolveTargetEntity', [ |
||
| 372 | $name, |
||
| 373 | $implementation, |
||
| 374 | [], |
||
| 375 | ]); |
||
| 376 | } |
||
| 377 | |||
| 378 | $def->addTag('doctrine.event_subscriber'); |
||
| 379 | } |
||
| 380 | |||
| 381 | $container->registerForAutoconfiguration(ServiceEntityRepositoryInterface::class) |
||
| 382 | ->addTag(ServiceRepositoryCompilerPass::REPOSITORY_SERVICE_TAG); |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Loads a configured ORM entity manager. |
||
| 387 | * |
||
| 388 | * @param array $entityManager A configured ORM entity manager. |
||
| 389 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
| 390 | */ |
||
| 391 | protected function loadOrmEntityManager(array $entityManager, ContainerBuilder $container) |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Loads an ORM entity managers bundle mapping information. |
||
| 524 | * |
||
| 525 | * There are two distinct configuration possibilities for mapping information: |
||
| 526 | * |
||
| 527 | * 1. Specify a bundle and optionally details where the entity and mapping information reside. |
||
| 528 | * 2. Specify an arbitrary mapping location. |
||
| 529 | * |
||
| 530 | * @param array $entityManager A configured ORM entity manager |
||
| 531 | * @param Definition $ormConfigDef A Definition instance |
||
| 532 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
| 533 | * |
||
| 534 | * @example |
||
| 535 | * |
||
| 536 | * doctrine.orm: |
||
| 537 | * mappings: |
||
| 538 | * MyBundle1: ~ |
||
| 539 | * MyBundle2: yml |
||
| 540 | * MyBundle3: { type: annotation, dir: Entities/ } |
||
| 541 | * MyBundle4: { type: xml, dir: Resources/config/doctrine/mapping } |
||
| 542 | * MyBundle5: |
||
| 543 | * type: yml |
||
| 544 | * dir: bundle-mappings/ |
||
| 545 | * alias: BundleAlias |
||
| 546 | * arbitrary_key: |
||
| 547 | * type: xml |
||
| 548 | * dir: %kernel.project_dir%/src/vendor/DoctrineExtensions/lib/DoctrineExtensions/Entities |
||
| 549 | * prefix: DoctrineExtensions\Entities\ |
||
| 550 | * alias: DExt |
||
| 551 | * |
||
| 552 | * In the case of bundles everything is really optional (which leads to autodetection for this bundle) but |
||
| 553 | * in the mappings key everything except alias is a required argument. |
||
| 554 | */ |
||
| 555 | protected function loadOrmEntityManagerMappingInformation(array $entityManager, Definition $ormConfigDef, ContainerBuilder $container) |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Loads an ORM second level cache bundle mapping information. |
||
| 569 | * |
||
| 570 | * @param array $entityManager A configured ORM entity manager |
||
| 571 | * @param Definition $ormConfigDef A Definition instance |
||
| 572 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
| 573 | * |
||
| 574 | * @example |
||
| 575 | * entity_managers: |
||
| 576 | * default: |
||
| 577 | * second_level_cache: |
||
| 578 | * region_cache_driver: apc |
||
| 579 | * log_enabled: true |
||
| 580 | * regions: |
||
| 581 | * my_service_region: |
||
| 582 | * type: service |
||
| 583 | * service : "my_service_region" |
||
| 584 | * |
||
| 585 | * my_query_region: |
||
| 586 | * lifetime: 300 |
||
| 587 | * cache_driver: array |
||
| 588 | * type: filelock |
||
| 589 | * |
||
| 590 | * my_entity_region: |
||
| 591 | * lifetime: 600 |
||
| 592 | * cache_driver: |
||
| 593 | * type: apc |
||
| 594 | */ |
||
| 595 | protected function loadOrmSecondLevelCache(array $entityManager, Definition $ormConfigDef, ContainerBuilder $container) |
||
| 683 | |||
| 684 | /** |
||
| 685 | * {@inheritDoc} |
||
| 686 | */ |
||
| 687 | protected function getObjectManagerElementName($name) : string |
||
| 691 | |||
| 692 | protected function getMappingObjectDefaultName() : string |
||
| 696 | |||
| 697 | /** |
||
| 698 | * {@inheritDoc} |
||
| 699 | */ |
||
| 700 | protected function getMappingResourceConfigDirectory() : string |
||
| 704 | |||
| 705 | /** |
||
| 706 | * {@inheritDoc} |
||
| 707 | */ |
||
| 708 | protected function getMappingResourceExtension() : string |
||
| 712 | |||
| 713 | /** |
||
| 714 | * {@inheritDoc} |
||
| 715 | */ |
||
| 716 | protected function loadCacheDriver($cacheName, $objectManagerName, array $cacheDriver, ContainerBuilder $container) : string |
||
| 747 | |||
| 748 | /** |
||
| 749 | * Loads a configured entity managers cache drivers. |
||
| 750 | * |
||
| 751 | * @param array $entityManager A configured ORM entity manager. |
||
| 752 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
| 753 | */ |
||
| 754 | protected function loadOrmCacheDrivers(array $entityManager, ContainerBuilder $container) |
||
| 760 | |||
| 761 | /** |
||
| 762 | * Loads a property info extractor for each defined entity manager. |
||
| 763 | * |
||
| 764 | * @param string $entityManagerName |
||
| 765 | */ |
||
| 766 | private function loadPropertyInfoExtractor($entityManagerName, ContainerBuilder $container) |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Loads a validator loader for each defined entity manager. |
||
| 780 | */ |
||
| 781 | private function loadValidatorLoader(string $entityManagerName, ContainerBuilder $container) : void |
||
| 788 | |||
| 789 | /** |
||
| 790 | * @param array $objectManager |
||
| 791 | * @param string $cacheName |
||
| 792 | */ |
||
| 793 | public function loadObjectManagerCacheDriver(array $objectManager, ContainerBuilder $container, $cacheName) |
||
| 797 | |||
| 798 | /** |
||
| 799 | * {@inheritDoc} |
||
| 800 | */ |
||
| 801 | public function getXsdValidationBasePath() : string |
||
| 805 | |||
| 806 | /** |
||
| 807 | * {@inheritDoc} |
||
| 808 | */ |
||
| 809 | public function getNamespace() : string |
||
| 813 | |||
| 814 | /** |
||
| 815 | * {@inheritDoc} |
||
| 816 | */ |
||
| 817 | public function getConfiguration(array $config, ContainerBuilder $container) : Configuration |
||
| 821 | |||
| 822 | protected function getMetadataDriverClass(string $driverType) : string |
||
| 826 | |||
| 827 | private function loadMessengerServices(ContainerBuilder $container) : void |
||
| 848 | |||
| 849 | private function createPoolCacheDefinition(ContainerBuilder $container, string $poolName) : string |
||
| 859 | |||
| 860 | private function createArrayAdapterCachePool(ContainerBuilder $container, string $objectManagerName, string $cacheName) : string |
||
| 870 | } |
||
| 871 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: