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 |
||
34 | class DoctrineExtension extends AbstractDoctrineExtension |
||
35 | { |
||
36 | /** @var string */ |
||
37 | private $defaultConnection; |
||
38 | |||
39 | /** @var SymfonyBridgeAdapter */ |
||
40 | private $adapter; |
||
41 | |||
42 | public function __construct(SymfonyBridgeAdapter $adapter = null) |
||
46 | |||
47 | /** |
||
48 | * {@inheritDoc} |
||
49 | */ |
||
50 | public function load(array $configs, ContainerBuilder $container) |
||
75 | |||
76 | /** |
||
77 | * Loads the DBAL configuration. |
||
78 | * |
||
79 | * Usage example: |
||
80 | * |
||
81 | * <doctrine:dbal id="myconn" dbname="sfweb" user="root" /> |
||
82 | * |
||
83 | * @param array $config An array of configuration settings |
||
84 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
85 | */ |
||
86 | protected function dbalLoad(array $config, ContainerBuilder $container) |
||
117 | |||
118 | /** |
||
119 | * Loads a configured DBAL connection. |
||
120 | * |
||
121 | * @param string $name The name of the connection |
||
122 | * @param array $connection A dbal connection configuration. |
||
123 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
124 | */ |
||
125 | protected function loadDbalConnection($name, array $connection, ContainerBuilder $container) |
||
201 | |||
202 | protected function getConnectionOptions($connection) |
||
203 | { |
||
204 | $options = $connection; |
||
205 | |||
206 | if (isset($options['platform_service'])) { |
||
207 | $options['platform'] = new Reference($options['platform_service']); |
||
208 | unset($options['platform_service']); |
||
209 | } |
||
210 | unset($options['mapping_types']); |
||
211 | |||
212 | if (isset($options['shard_choser_service'])) { |
||
213 | $options['shard_choser'] = new Reference($options['shard_choser_service']); |
||
214 | unset($options['shard_choser_service']); |
||
215 | } |
||
216 | |||
217 | foreach ([ |
||
218 | 'options' => 'driverOptions', |
||
219 | 'driver_class' => 'driverClass', |
||
220 | 'wrapper_class' => 'wrapperClass', |
||
221 | 'keep_slave' => 'keepSlave', |
||
222 | 'shard_choser' => 'shardChoser', |
||
223 | 'shard_manager_class' => 'shardManagerClass', |
||
224 | 'server_version' => 'serverVersion', |
||
225 | 'default_table_options' => 'defaultTableOptions', |
||
226 | ] as $old => $new) { |
||
227 | if (! isset($options[$old])) { |
||
228 | continue; |
||
229 | } |
||
230 | |||
231 | $options[$new] = $options[$old]; |
||
232 | unset($options[$old]); |
||
233 | } |
||
234 | |||
235 | if (! empty($options['slaves']) && ! empty($options['shards'])) { |
||
236 | throw new InvalidArgumentException('Sharding and master-slave connection cannot be used together'); |
||
237 | } |
||
238 | |||
239 | if (! empty($options['slaves'])) { |
||
240 | $nonRewrittenKeys = [ |
||
241 | 'driver' => true, |
||
242 | 'driverOptions' => true, |
||
243 | 'driverClass' => true, |
||
244 | 'wrapperClass' => true, |
||
245 | 'keepSlave' => true, |
||
246 | 'shardChoser' => true, |
||
247 | 'platform' => true, |
||
248 | 'slaves' => true, |
||
249 | 'master' => true, |
||
250 | 'shards' => true, |
||
251 | 'serverVersion' => true, |
||
252 | 'defaultTableOptions' => true, |
||
253 | // included by safety but should have been unset already |
||
254 | 'logging' => true, |
||
255 | 'profiling' => true, |
||
256 | 'mapping_types' => true, |
||
257 | 'platform_service' => true, |
||
258 | ]; |
||
259 | View Code Duplication | foreach ($options as $key => $value) { |
|
260 | if (isset($nonRewrittenKeys[$key])) { |
||
261 | continue; |
||
262 | } |
||
263 | $options['master'][$key] = $value; |
||
264 | unset($options[$key]); |
||
265 | } |
||
266 | if (empty($options['wrapperClass'])) { |
||
267 | // Change the wrapper class only if the user does not already forced using a custom one. |
||
268 | $options['wrapperClass'] = 'Doctrine\\DBAL\\Connections\\MasterSlaveConnection'; |
||
269 | } |
||
270 | } else { |
||
271 | unset($options['slaves']); |
||
272 | } |
||
273 | |||
274 | if (! empty($options['shards'])) { |
||
275 | $nonRewrittenKeys = [ |
||
276 | 'driver' => true, |
||
277 | 'driverOptions' => true, |
||
278 | 'driverClass' => true, |
||
279 | 'wrapperClass' => true, |
||
280 | 'keepSlave' => true, |
||
281 | 'shardChoser' => true, |
||
282 | 'platform' => true, |
||
283 | 'slaves' => true, |
||
284 | 'global' => true, |
||
285 | 'shards' => true, |
||
286 | 'serverVersion' => true, |
||
287 | 'defaultTableOptions' => true, |
||
288 | // included by safety but should have been unset already |
||
289 | 'logging' => true, |
||
290 | 'profiling' => true, |
||
291 | 'mapping_types' => true, |
||
292 | 'platform_service' => true, |
||
293 | ]; |
||
294 | View Code Duplication | foreach ($options as $key => $value) { |
|
295 | if (isset($nonRewrittenKeys[$key])) { |
||
296 | continue; |
||
297 | } |
||
298 | $options['global'][$key] = $value; |
||
299 | unset($options[$key]); |
||
300 | } |
||
301 | if (empty($options['wrapperClass'])) { |
||
302 | // Change the wrapper class only if the user does not already forced using a custom one. |
||
303 | $options['wrapperClass'] = 'Doctrine\\DBAL\\Sharding\\PoolingShardConnection'; |
||
304 | } |
||
305 | if (empty($options['shardManagerClass'])) { |
||
306 | // Change the shard manager class only if the user does not already forced using a custom one. |
||
307 | $options['shardManagerClass'] = 'Doctrine\\DBAL\\Sharding\\PoolingShardManager'; |
||
308 | } |
||
309 | } else { |
||
310 | unset($options['shards']); |
||
311 | } |
||
312 | |||
313 | return $options; |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * Loads the Doctrine ORM configuration. |
||
318 | * |
||
319 | * Usage example: |
||
320 | * |
||
321 | * <doctrine:orm id="mydm" connection="myconn" /> |
||
322 | * |
||
323 | * @param array $config An array of configuration settings |
||
324 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
325 | */ |
||
326 | protected function ormLoad(array $config, ContainerBuilder $container) |
||
327 | { |
||
328 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
||
329 | $loader->load('orm.xml'); |
||
330 | |||
331 | $entityManagers = []; |
||
332 | foreach (array_keys($config['entity_managers']) as $name) { |
||
333 | $entityManagers[$name] = sprintf('doctrine.orm.%s_entity_manager', $name); |
||
334 | } |
||
335 | $container->setParameter('doctrine.entity_managers', $entityManagers); |
||
336 | |||
337 | if (empty($config['default_entity_manager'])) { |
||
338 | $tmp = array_keys($entityManagers); |
||
339 | $config['default_entity_manager'] = reset($tmp); |
||
340 | } |
||
341 | $container->setParameter('doctrine.default_entity_manager', $config['default_entity_manager']); |
||
342 | |||
343 | $options = ['auto_generate_proxy_classes', 'proxy_dir', 'proxy_namespace']; |
||
344 | foreach ($options as $key) { |
||
345 | $container->setParameter('doctrine.orm.' . $key, $config[$key]); |
||
346 | } |
||
347 | |||
348 | $container->setAlias('doctrine.orm.entity_manager', sprintf('doctrine.orm.%s_entity_manager', $config['default_entity_manager'])); |
||
349 | $container->getAlias('doctrine.orm.entity_manager')->setPublic(true); |
||
350 | |||
351 | $config['entity_managers'] = $this->fixManagersAutoMappings($config['entity_managers'], $container->getParameter('kernel.bundles')); |
||
352 | |||
353 | foreach ($config['entity_managers'] as $name => $entityManager) { |
||
354 | $entityManager['name'] = $name; |
||
355 | $this->loadOrmEntityManager($entityManager, $container); |
||
356 | |||
357 | $this->loadPropertyInfoExtractor($name, $container); |
||
358 | $this->loadValidatorLoader($name, $container); |
||
359 | } |
||
360 | |||
361 | if ($config['resolve_target_entities']) { |
||
362 | $def = $container->findDefinition('doctrine.orm.listeners.resolve_target_entity'); |
||
363 | foreach ($config['resolve_target_entities'] as $name => $implementation) { |
||
364 | $def->addMethodCall('addResolveTargetEntity', [ |
||
365 | $name, |
||
366 | $implementation, |
||
367 | [], |
||
368 | ]); |
||
369 | } |
||
370 | |||
371 | $def->addTag('doctrine.event_subscriber'); |
||
372 | } |
||
373 | |||
374 | $container->registerForAutoconfiguration(ServiceEntityRepositoryInterface::class) |
||
375 | ->addTag(ServiceRepositoryCompilerPass::REPOSITORY_SERVICE_TAG); |
||
376 | |||
377 | $this->loadMessengerServices($container); |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * Loads a configured ORM entity manager. |
||
382 | * |
||
383 | * @param array $entityManager A configured ORM entity manager. |
||
384 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
385 | */ |
||
386 | protected function loadOrmEntityManager(array $entityManager, ContainerBuilder $container) |
||
516 | |||
517 | /** |
||
518 | * Loads an ORM entity managers bundle mapping information. |
||
519 | * |
||
520 | * There are two distinct configuration possibilities for mapping information: |
||
521 | * |
||
522 | * 1. Specify a bundle and optionally details where the entity and mapping information reside. |
||
523 | * 2. Specify an arbitrary mapping location. |
||
524 | * |
||
525 | * @param array $entityManager A configured ORM entity manager |
||
526 | * @param Definition $ormConfigDef A Definition instance |
||
527 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
528 | * |
||
529 | * @example |
||
530 | * |
||
531 | * doctrine.orm: |
||
532 | * mappings: |
||
533 | * MyBundle1: ~ |
||
534 | * MyBundle2: yml |
||
535 | * MyBundle3: { type: annotation, dir: Entities/ } |
||
536 | * MyBundle4: { type: xml, dir: Resources/config/doctrine/mapping } |
||
537 | * MyBundle5: |
||
538 | * type: yml |
||
539 | * dir: bundle-mappings/ |
||
540 | * alias: BundleAlias |
||
541 | * arbitrary_key: |
||
542 | * type: xml |
||
543 | * dir: %kernel.root_dir%/../src/vendor/DoctrineExtensions/lib/DoctrineExtensions/Entities |
||
544 | * prefix: DoctrineExtensions\Entities\ |
||
545 | * alias: DExt |
||
546 | * |
||
547 | * In the case of bundles everything is really optional (which leads to autodetection for this bundle) but |
||
548 | * in the mappings key everything except alias is a required argument. |
||
549 | */ |
||
550 | protected function loadOrmEntityManagerMappingInformation(array $entityManager, Definition $ormConfigDef, ContainerBuilder $container) |
||
561 | |||
562 | /** |
||
563 | * Loads an ORM second level cache bundle mapping information. |
||
564 | * |
||
565 | * @param array $entityManager A configured ORM entity manager |
||
566 | * @param Definition $ormConfigDef A Definition instance |
||
567 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
568 | * |
||
569 | * @example |
||
570 | * entity_managers: |
||
571 | * default: |
||
572 | * second_level_cache: |
||
573 | * region_cache_driver: apc |
||
574 | * log_enabled: true |
||
575 | * regions: |
||
576 | * my_service_region: |
||
577 | * type: service |
||
578 | * service : "my_service_region" |
||
579 | * |
||
580 | * my_query_region: |
||
581 | * lifetime: 300 |
||
582 | * cache_driver: array |
||
583 | * type: filelock |
||
584 | * |
||
585 | * my_entity_region: |
||
586 | * lifetime: 600 |
||
587 | * cache_driver: |
||
588 | * type: apc |
||
589 | */ |
||
590 | protected function loadOrmSecondLevelCache(array $entityManager, Definition $ormConfigDef, ContainerBuilder $container) |
||
678 | |||
679 | /** |
||
680 | * {@inheritDoc} |
||
681 | */ |
||
682 | protected function getObjectManagerElementName($name) |
||
686 | |||
687 | protected function getMappingObjectDefaultName() |
||
691 | |||
692 | /** |
||
693 | * {@inheritDoc} |
||
694 | */ |
||
695 | protected function getMappingResourceConfigDirectory() |
||
699 | |||
700 | /** |
||
701 | * {@inheritDoc} |
||
702 | */ |
||
703 | protected function getMappingResourceExtension() |
||
707 | |||
708 | /** |
||
709 | * {@inheritDoc} |
||
710 | */ |
||
711 | protected function loadCacheDriver($driverName, $entityManagerName, array $driverMap, ContainerBuilder $container) |
||
738 | |||
739 | /** |
||
740 | * Loads a configured entity managers cache drivers. |
||
741 | * |
||
742 | * @param array $entityManager A configured ORM entity manager. |
||
743 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
744 | */ |
||
745 | protected function loadOrmCacheDrivers(array $entityManager, ContainerBuilder $container) |
||
751 | |||
752 | /** |
||
753 | * Loads a property info extractor for each defined entity manager. |
||
754 | * |
||
755 | * @param string $entityManagerName |
||
756 | */ |
||
757 | private function loadPropertyInfoExtractor($entityManagerName, ContainerBuilder $container) |
||
778 | |||
779 | /** |
||
780 | * Loads a validator loader for each defined entity manager. |
||
781 | */ |
||
782 | private function loadValidatorLoader(string $entityManagerName, ContainerBuilder $container) : void |
||
793 | |||
794 | /** |
||
795 | * @param array $objectManager |
||
796 | * @param string $cacheName |
||
797 | */ |
||
798 | public function loadObjectManagerCacheDriver(array $objectManager, ContainerBuilder $container, $cacheName) |
||
802 | |||
803 | /** |
||
804 | * {@inheritDoc} |
||
805 | */ |
||
806 | public function getXsdValidationBasePath() |
||
810 | |||
811 | /** |
||
812 | * {@inheritDoc} |
||
813 | */ |
||
814 | public function getNamespace() |
||
818 | |||
819 | /** |
||
820 | * {@inheritDoc} |
||
821 | */ |
||
822 | public function getConfiguration(array $config, ContainerBuilder $container) |
||
826 | |||
827 | private function loadMessengerServices(ContainerBuilder $container) : void |
||
844 | |||
845 | private function createPoolCacheDefinition(ContainerBuilder $container, string $aliasId, string $poolName) : string |
||
859 | } |
||
860 |
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: