Complex classes like FOSElasticaExtension 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 FOSElasticaExtension, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class FOSElasticaExtension extends Extension |
||
14 | { |
||
15 | /** |
||
16 | * Definition of elastica clients as configured by this extension. |
||
17 | * |
||
18 | * @var array |
||
19 | */ |
||
20 | private $clients = array(); |
||
21 | |||
22 | /** |
||
23 | * An array of indexes as configured by the extension. |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | private $indexConfigs = array(); |
||
28 | |||
29 | /** |
||
30 | * If we've encountered a type mapped to a specific persistence driver, it will be loaded |
||
31 | * here. |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | private $loadedDrivers = array(); |
||
36 | |||
37 | 14 | public function load(array $configs, ContainerBuilder $container) |
|
38 | { |
||
39 | 14 | $configuration = $this->getConfiguration($configs, $container); |
|
40 | 14 | $config = $this->processConfiguration($configuration, $configs); |
|
41 | |||
42 | 14 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
43 | |||
44 | 14 | if (empty($config['clients']) || empty($config['indexes'])) { |
|
45 | // No Clients or indexes are defined |
||
46 | return; |
||
47 | } |
||
48 | |||
49 | 14 | foreach (array('config', 'index', 'persister', 'provider', 'source', 'transformer') as $basename) { |
|
50 | 14 | $loader->load(sprintf('%s.xml', $basename)); |
|
51 | } |
||
52 | |||
53 | 14 | if (empty($config['default_client'])) { |
|
54 | 14 | $keys = array_keys($config['clients']); |
|
55 | 14 | $config['default_client'] = reset($keys); |
|
56 | } |
||
57 | |||
58 | 14 | if (empty($config['default_index'])) { |
|
59 | 14 | $keys = array_keys($config['indexes']); |
|
60 | 14 | $config['default_index'] = reset($keys); |
|
61 | } |
||
62 | |||
63 | 14 | if (isset($config['serializer'])) { |
|
64 | 3 | $loader->load('serializer.xml'); |
|
65 | |||
66 | 3 | $this->loadSerializer($config['serializer'], $container); |
|
67 | } |
||
68 | |||
69 | 14 | $this->loadClients($config['clients'], $container); |
|
70 | 14 | $container->setAlias('fos_elastica.client', sprintf('fos_elastica.client.%s', $config['default_client'])); |
|
71 | |||
72 | 14 | $this->loadIndexes($config['indexes'], $container); |
|
73 | 14 | $container->setAlias('fos_elastica.index', sprintf('fos_elastica.index.%s', $config['default_index'])); |
|
74 | |||
75 | 14 | $container->getDefinition('fos_elastica.config_source.container')->replaceArgument(0, $this->indexConfigs); |
|
76 | |||
77 | 14 | $this->loadIndexManager($container); |
|
78 | |||
79 | 14 | $this->createDefaultManagerAlias($config['default_manager'], $container); |
|
80 | 14 | } |
|
81 | |||
82 | /** |
||
83 | * @param array $config |
||
84 | * @param ContainerBuilder $container |
||
85 | * |
||
86 | * @return Configuration |
||
87 | */ |
||
88 | 14 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
89 | { |
||
90 | 14 | return new Configuration($container->getParameter('kernel.debug')); |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * Loads the configured clients. |
||
95 | * |
||
96 | * @param array $clients An array of clients configurations |
||
97 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
98 | * |
||
99 | * @return array |
||
100 | */ |
||
101 | 14 | private function loadClients(array $clients, ContainerBuilder $container) |
|
124 | |||
125 | /** |
||
126 | * Loads the configured indexes. |
||
127 | * |
||
128 | * @param array $indexes An array of indexes configurations |
||
129 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
130 | * |
||
131 | * @throws \InvalidArgumentException |
||
132 | * |
||
133 | * @return array |
||
134 | */ |
||
135 | 14 | private function loadIndexes(array $indexes, ContainerBuilder $container) |
|
191 | |||
192 | /** |
||
193 | * Loads the configured index finders. |
||
194 | * |
||
195 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
||
196 | * @param string $name The index name |
||
197 | * @param Reference $index Reference to the related index |
||
198 | * |
||
199 | * @return string |
||
200 | */ |
||
201 | private function loadIndexFinder(ContainerBuilder $container, $name, Reference $index) |
||
202 | { |
||
203 | /* Note: transformer services may conflict with "collection.index", if |
||
204 | * an index and type names were "collection" and an index, respectively. |
||
205 | */ |
||
206 | $transformerId = sprintf('fos_elastica.elastica_to_model_transformer.collection.%s', $name); |
||
207 | $transformerDef = new DefinitionDecorator('fos_elastica.elastica_to_model_transformer.collection'); |
||
208 | $container->setDefinition($transformerId, $transformerDef); |
||
209 | |||
210 | $finderId = sprintf('fos_elastica.finder.%s', $name); |
||
211 | $finderDef = new DefinitionDecorator('fos_elastica.finder'); |
||
212 | $finderDef->replaceArgument(0, $index); |
||
213 | $finderDef->replaceArgument(1, new Reference($transformerId)); |
||
214 | |||
215 | $container->setDefinition($finderId, $finderDef); |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * Loads the configured types. |
||
220 | * |
||
221 | * @param array $types |
||
222 | * @param ContainerBuilder $container |
||
223 | * @param array $indexConfig |
||
224 | * @param array $indexableCallbacks |
||
225 | */ |
||
226 | 14 | private function loadTypes(array $types, ContainerBuilder $container, array $indexConfig, array &$indexableCallbacks) |
|
316 | |||
317 | /** |
||
318 | * Loads the optional provider and finder for a type. |
||
319 | * |
||
320 | * @param array $typeConfig |
||
321 | * @param ContainerBuilder $container |
||
322 | * @param Reference $typeRef |
||
323 | * @param string $indexName |
||
324 | * @param string $typeName |
||
325 | */ |
||
326 | 10 | private function loadTypePersistenceIntegration(array $typeConfig, ContainerBuilder $container, Reference $typeRef, $indexName, $typeName) |
|
346 | |||
347 | /** |
||
348 | * Creates and loads an ElasticaToModelTransformer. |
||
349 | * |
||
350 | * @param array $typeConfig |
||
351 | * @param ContainerBuilder $container |
||
352 | * @param string $indexName |
||
353 | * @param string $typeName |
||
354 | * |
||
355 | * @return string |
||
356 | */ |
||
357 | 10 | private function loadElasticaToModelTransformer(array $typeConfig, ContainerBuilder $container, $indexName, $typeName) |
|
358 | { |
||
359 | 10 | if (isset($typeConfig['elastica_to_model_transformer']['service'])) { |
|
360 | 1 | return $typeConfig['elastica_to_model_transformer']['service']; |
|
361 | } |
||
362 | |||
363 | /* Note: transformer services may conflict with "prototype.driver", if |
||
364 | * the index and type names were "prototype" and a driver, respectively. |
||
365 | */ |
||
366 | 9 | $abstractId = sprintf('fos_elastica.elastica_to_model_transformer.prototype.%s', $typeConfig['driver']); |
|
367 | 9 | $serviceId = sprintf('fos_elastica.elastica_to_model_transformer.%s.%s', $indexName, $typeName); |
|
368 | 9 | $serviceDef = new DefinitionDecorator($abstractId); |
|
369 | 9 | $serviceDef->addTag('fos_elastica.elastica_to_model_transformer', array('type' => $typeName, 'index' => $indexName)); |
|
370 | |||
371 | // Doctrine has a mandatory service as first argument |
||
372 | 9 | $argPos = ('propel' === $typeConfig['driver']) ? 0 : 1; |
|
373 | |||
374 | 9 | $serviceDef->replaceArgument($argPos, $typeConfig['model']); |
|
375 | 9 | $serviceDef->replaceArgument($argPos + 1, array_merge($typeConfig['elastica_to_model_transformer'], array( |
|
376 | 9 | 'identifier' => $typeConfig['identifier'], |
|
377 | ))); |
||
378 | 9 | $container->setDefinition($serviceId, $serviceDef); |
|
379 | |||
380 | 9 | return $serviceId; |
|
381 | } |
||
382 | |||
383 | /** |
||
384 | * Creates and loads a ModelToElasticaTransformer for an index/type. |
||
385 | * |
||
386 | * @param array $typeConfig |
||
387 | * @param ContainerBuilder $container |
||
388 | * @param string $indexName |
||
389 | * @param string $typeName |
||
390 | * |
||
391 | * @return string |
||
392 | */ |
||
393 | 10 | private function loadModelToElasticaTransformer(array $typeConfig, ContainerBuilder $container, $indexName, $typeName) |
|
394 | { |
||
395 | 10 | if (isset($typeConfig['model_to_elastica_transformer']['service'])) { |
|
396 | return $typeConfig['model_to_elastica_transformer']['service']; |
||
397 | } |
||
398 | |||
399 | 10 | $abstractId = $container->hasDefinition('fos_elastica.serializer_callback_prototype') ? |
|
400 | 3 | 'fos_elastica.model_to_elastica_identifier_transformer' : |
|
401 | 10 | 'fos_elastica.model_to_elastica_transformer'; |
|
402 | |||
403 | 10 | $serviceId = sprintf('fos_elastica.model_to_elastica_transformer.%s.%s', $indexName, $typeName); |
|
404 | 10 | $serviceDef = new DefinitionDecorator($abstractId); |
|
405 | 10 | $serviceDef->replaceArgument(0, array( |
|
406 | 10 | 'identifier' => $typeConfig['identifier'], |
|
407 | )); |
||
408 | 10 | $container->setDefinition($serviceId, $serviceDef); |
|
409 | |||
410 | 10 | return $serviceId; |
|
411 | } |
||
412 | |||
413 | /** |
||
414 | * Creates and loads an object persister for a type. |
||
415 | * |
||
416 | * @param array $typeConfig |
||
417 | * @param Reference $typeRef |
||
418 | * @param ContainerBuilder $container |
||
419 | * @param string $indexName |
||
420 | * @param string $typeName |
||
421 | * @param string $transformerId |
||
422 | * |
||
423 | * @return string |
||
424 | */ |
||
425 | 10 | private function loadObjectPersister(array $typeConfig, Reference $typeRef, ContainerBuilder $container, $indexName, $typeName, $transformerId) |
|
461 | |||
462 | /** |
||
463 | * Loads a provider for a type. |
||
464 | * |
||
465 | * @param array $typeConfig |
||
466 | * @param ContainerBuilder $container |
||
467 | * @param string $objectPersisterId |
||
468 | * @param string $indexName |
||
469 | * @param string $typeName |
||
470 | * |
||
471 | * @return string |
||
472 | */ |
||
473 | 5 | private function loadTypeProvider(array $typeConfig, ContainerBuilder $container, $objectPersisterId, $indexName, $typeName) |
|
474 | { |
||
475 | 5 | if (isset($typeConfig['provider']['service'])) { |
|
476 | return $typeConfig['provider']['service']; |
||
477 | } |
||
478 | |||
479 | /* Note: provider services may conflict with "prototype.driver", if the |
||
480 | * index and type names were "prototype" and a driver, respectively. |
||
481 | */ |
||
482 | 5 | $providerId = sprintf('fos_elastica.provider.%s.%s', $indexName, $typeName); |
|
483 | 5 | $providerDef = new DefinitionDecorator('fos_elastica.provider.prototype.'.$typeConfig['driver']); |
|
484 | 5 | $providerDef->addTag('fos_elastica.provider', array('index' => $indexName, 'type' => $typeName)); |
|
485 | 5 | $providerDef->replaceArgument(0, new Reference($objectPersisterId)); |
|
486 | 5 | $providerDef->replaceArgument(2, $typeConfig['model']); |
|
487 | // Propel provider can simply ignore Doctrine-specific options |
||
488 | 5 | $providerDef->replaceArgument(3, array_merge(array_diff_key($typeConfig['provider'], array('service' => 1)), array( |
|
489 | 5 | 'indexName' => $indexName, |
|
490 | 5 | 'typeName' => $typeName, |
|
491 | ))); |
||
492 | 5 | $container->setDefinition($providerId, $providerDef); |
|
493 | |||
494 | 5 | return $providerId; |
|
495 | } |
||
496 | |||
497 | /** |
||
498 | * Loads doctrine listeners to handle indexing of new or updated objects. |
||
499 | * |
||
500 | * @param array $typeConfig |
||
501 | * @param ContainerBuilder $container |
||
502 | * @param string $objectPersisterId |
||
503 | * @param string $indexName |
||
504 | * @param string $typeName |
||
505 | * |
||
506 | * @return string |
||
507 | */ |
||
508 | 5 | private function loadTypeListener(array $typeConfig, ContainerBuilder $container, $objectPersisterId, $indexName, $typeName) |
|
554 | |||
555 | /** |
||
556 | * Map Elastica to Doctrine events for the current driver. |
||
557 | */ |
||
558 | 5 | private function getDoctrineEvents(array $typeConfig) |
|
590 | |||
591 | /** |
||
592 | * Loads a Type specific Finder. |
||
593 | * |
||
594 | * @param array $typeConfig |
||
595 | * @param ContainerBuilder $container |
||
596 | * @param string $elasticaToModelId |
||
597 | * @param Reference $typeRef |
||
598 | * @param string $indexName |
||
599 | * @param string $typeName |
||
600 | * |
||
601 | * @return string |
||
602 | */ |
||
603 | 5 | private function loadTypeFinder(array $typeConfig, ContainerBuilder $container, $elasticaToModelId, Reference $typeRef, $indexName, $typeName) |
|
604 | { |
||
605 | 5 | if (isset($typeConfig['finder']['service'])) { |
|
606 | $finderId = $typeConfig['finder']['service']; |
||
607 | } else { |
||
608 | 5 | $finderId = sprintf('fos_elastica.finder.%s.%s', $indexName, $typeName); |
|
609 | 5 | $finderDef = new DefinitionDecorator('fos_elastica.finder'); |
|
610 | 5 | $finderDef->replaceArgument(0, $typeRef); |
|
611 | 5 | $finderDef->replaceArgument(1, new Reference($elasticaToModelId)); |
|
612 | 5 | $container->setDefinition($finderId, $finderDef); |
|
613 | } |
||
614 | |||
615 | 5 | $managerId = sprintf('fos_elastica.manager.%s', $typeConfig['driver']); |
|
616 | 5 | $managerDef = $container->getDefinition($managerId); |
|
617 | 5 | $arguments = array( $typeConfig['model'], new Reference($finderId)); |
|
618 | 5 | if (isset($typeConfig['repository'])) { |
|
619 | $arguments[] = $typeConfig['repository']; |
||
620 | } |
||
621 | 5 | $managerDef->addMethodCall('addEntity', $arguments); |
|
622 | |||
623 | 5 | return $finderId; |
|
624 | } |
||
625 | |||
626 | /** |
||
627 | * Loads the index manager. |
||
628 | * |
||
629 | * @param ContainerBuilder $container |
||
630 | **/ |
||
631 | private function loadIndexManager(ContainerBuilder $container) |
||
640 | |||
641 | /** |
||
642 | * Makes sure a specific driver has been loaded. |
||
643 | * |
||
644 | * @param ContainerBuilder $container |
||
645 | * @param string $driver |
||
646 | */ |
||
647 | 9 | private function loadDriver(ContainerBuilder $container, $driver) |
|
657 | |||
658 | /** |
||
659 | * Loads and configures the serializer prototype. |
||
660 | * |
||
661 | * @param array $config |
||
662 | * @param ContainerBuilder $container |
||
663 | */ |
||
664 | 3 | private function loadSerializer($config, ContainerBuilder $container) |
|
665 | { |
||
666 | 3 | $container->setAlias('fos_elastica.serializer', $config['serializer']); |
|
667 | |||
668 | 3 | $serializer = $container->getDefinition('fos_elastica.serializer_callback_prototype'); |
|
669 | 3 | $serializer->setClass($config['callback_class']); |
|
670 | |||
671 | 3 | $callbackClassImplementedInterfaces = class_implements($config['callback_class']); |
|
672 | 3 | if (isset($callbackClassImplementedInterfaces['Symfony\Component\DependencyInjection\ContainerAwareInterface'])) { |
|
673 | $serializer->addMethodCall('setContainer', array(new Reference('service_container'))); |
||
674 | } |
||
675 | 3 | } |
|
676 | |||
677 | /** |
||
678 | * Creates a default manager alias for defined default manager or the first loaded driver. |
||
679 | * |
||
680 | * @param string $defaultManager |
||
681 | * @param ContainerBuilder $container |
||
682 | */ |
||
683 | 14 | private function createDefaultManagerAlias($defaultManager, ContainerBuilder $container) |
|
684 | { |
||
685 | 14 | if (0 == count($this->loadedDrivers)) { |
|
686 | 5 | return; |
|
687 | } |
||
688 | |||
689 | 9 | if (count($this->loadedDrivers) > 1 |
|
690 | 9 | && in_array($defaultManager, $this->loadedDrivers) |
|
691 | ) { |
||
692 | $defaultManagerService = $defaultManager; |
||
693 | } else { |
||
694 | 9 | $defaultManagerService = $this->loadedDrivers[0]; |
|
695 | } |
||
696 | |||
697 | 9 | $container->setAlias('fos_elastica.manager', sprintf('fos_elastica.manager.%s', $defaultManagerService)); |
|
698 | 9 | } |
|
699 | |||
700 | /** |
||
701 | * Returns a reference to a client given its configured name. |
||
702 | * |
||
703 | * @param string $clientName |
||
704 | * |
||
705 | * @return Reference |
||
706 | * |
||
707 | * @throws \InvalidArgumentException |
||
708 | */ |
||
709 | 2 | private function getClient($clientName) |
|
717 | } |
||
718 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.