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 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  | 
            ||
| 26 | class FOSElasticaExtension extends Extension  | 
            ||
| 27 | { | 
            ||
| 28 | /**  | 
            ||
| 29 | * Definition of elastica clients as configured by this extension.  | 
            ||
| 30 | *  | 
            ||
| 31 | * @var array  | 
            ||
| 32 | */  | 
            ||
| 33 | private $clients = [];  | 
            ||
| 34 | |||
| 35 | /**  | 
            ||
| 36 | * An array of indexes as configured by the extension.  | 
            ||
| 37 | *  | 
            ||
| 38 | * @var array  | 
            ||
| 39 | */  | 
            ||
| 40 | private $indexConfigs = [];  | 
            ||
| 41 | |||
| 42 | /**  | 
            ||
| 43 | * An array of index templates as configured by the extension.  | 
            ||
| 44 | *  | 
            ||
| 45 | * @var array  | 
            ||
| 46 | */  | 
            ||
| 47 | private $indexTemplateConfigs = array();  | 
            ||
| 48 | |||
| 49 | /**  | 
            ||
| 50 | 18 | * If we've encountered a type mapped to a specific persistence driver, it will be loaded  | 
            |
| 51 | * here.  | 
            ||
| 52 | 18 | *  | 
            |
| 53 | 18 | * @var array  | 
            |
| 54 | */  | 
            ||
| 55 | 18 | private $loadedDrivers = [];  | 
            |
| 56 | |||
| 57 | 18 | public function load(array $configs, ContainerBuilder $container)  | 
            |
| 121 | |||
| 122 | 18 | /**  | 
            |
| 123 | 18 | * @param array $config  | 
            |
| 124 | * @param ContainerBuilder $container  | 
            ||
| 125 | 18 | *  | 
            |
| 126 | 18 | * @return Configuration  | 
            |
| 127 | */  | 
            ||
| 128 | 18 | public function getConfiguration(array $config, ContainerBuilder $container)  | 
            |
| 132 | |||
| 133 | 18 | /**  | 
            |
| 134 | * Loads the configured clients.  | 
            ||
| 135 | 18 | *  | 
            |
| 136 | * @param array $clients An array of clients configurations  | 
            ||
| 137 | 18 | * @param ContainerBuilder $container A ContainerBuilder instance  | 
            |
| 138 | 18 | *  | 
            |
| 139 | 18 | * @return array  | 
            |
| 140 | */  | 
            ||
| 141 | private function loadClients(array $clients, ContainerBuilder $container)  | 
            ||
| 164 | 18 | ||
| 165 | 18 | /**  | 
            |
| 166 | 18 | * Loads the configured indexes.  | 
            |
| 167 | *  | 
            ||
| 168 | * @param array $indexes An array of indexes configurations  | 
            ||
| 169 | 18 | * @param ContainerBuilder $container A ContainerBuilder instance  | 
            |
| 170 | 2 | *  | 
            |
| 171 | * @throws \InvalidArgumentException  | 
            ||
| 172 | 2 | *  | 
            |
| 173 | * @return array  | 
            ||
| 174 | */  | 
            ||
| 175 | 18 | private function loadIndexes(array $indexes, ContainerBuilder $container)  | 
            |
| 176 | 18 |     { | 
            |
| 177 | $indexableCallbacks = [];  | 
            ||
| 178 | 18 | ||
| 179 | 18 |         foreach ($indexes as $name => $index) { | 
            |
| 180 | 18 |             $indexId = sprintf('fos_elastica.index.%s', $name); | 
            |
| 181 | 18 | $indexName = isset($index['index_name']) ? $index['index_name'] : $name;  | 
            |
| 182 | 18 | ||
| 183 | 18 |             $indexDef = new ChildDefinition('fos_elastica.index_prototype'); | 
            |
| 184 | 18 |             $indexDef->setFactory([new Reference('fos_elastica.client'), 'getIndex']); | 
            |
| 185 | $indexDef->replaceArgument(0, $indexName);  | 
            ||
| 186 |             $indexDef->addTag('fos_elastica.index', [ | 
            ||
| 187 | 18 | 'name' => $name,  | 
            |
| 188 | ]);  | 
            ||
| 189 | |||
| 190 | View Code Duplication |             if (isset($index['client'])) { | 
            |
| 191 | 18 | $client = $this->getClient($index['client']);  | 
            |
| 192 | |||
| 193 | $indexDef->setFactory([$client, 'getIndex']);  | 
            ||
| 194 | 18 | }  | 
            |
| 195 | 18 | ||
| 196 | 18 | $container->setDefinition($indexId, $indexDef);  | 
            |
| 197 | $reference = new Reference($indexId);  | 
            ||
| 198 | |||
| 199 | $this->indexConfigs[$name] = [  | 
            ||
| 200 | 'elasticsearch_name' => $indexName,  | 
            ||
| 201 | 'reference' => $reference,  | 
            ||
| 202 | 'name' => $name,  | 
            ||
| 203 | 'settings' => $index['settings'],  | 
            ||
| 204 | 'type_prototype' => isset($index['type_prototype']) ? $index['type_prototype'] : [],  | 
            ||
| 205 | 'use_alias' => $index['use_alias'],  | 
            ||
| 206 | ];  | 
            ||
| 207 | |||
| 208 |             if ($index['finder']) { | 
            ||
| 209 | $this->loadIndexFinder($container, $name, $reference);  | 
            ||
| 210 | }  | 
            ||
| 211 | |||
| 212 | $this->loadTypes((array) $index['types'], $container, $this->indexConfigs[$name], $indexableCallbacks);  | 
            ||
| 213 | }  | 
            ||
| 214 | |||
| 215 |         $indexable = $container->getDefinition('fos_elastica.indexable'); | 
            ||
| 216 | $indexable->replaceArgument(0, $indexableCallbacks);  | 
            ||
| 217 | }  | 
            ||
| 218 | |||
| 219 | /**  | 
            ||
| 220 | * Loads the configured indexes.  | 
            ||
| 221 | *  | 
            ||
| 222 | * @param array $indexTemplates An array of indexes configurations  | 
            ||
| 223 | * @param ContainerBuilder $container A ContainerBuilder instance  | 
            ||
| 224 | *  | 
            ||
| 225 | * @throws \InvalidArgumentException  | 
            ||
| 226 | *  | 
            ||
| 227 | * @return void  | 
            ||
| 228 | */  | 
            ||
| 229 | private function loadIndexTemplates(array $indexTemplates, ContainerBuilder $container)  | 
            ||
| 230 |     { | 
            ||
| 231 | $indexableCallbacks = array();  | 
            ||
| 232 | 18 |         foreach ($indexTemplates as $name => $indexTemplate) { | 
            |
| 233 |             $indexId = sprintf('fos_elastica.index_template.%s', $name); | 
            ||
| 234 | 18 | $indexTemplateName = isset($indexTemplate['template_name']) ? $indexTemplate['template_name'] : $name;  | 
            |
| 235 | 18 | ||
| 236 |             $indexDef = new ChildDefinition('fos_elastica.index_template_prototype'); | 
            ||
| 237 | 18 |             $indexDef->setFactory([new Reference('fos_elastica.client'), 'getIndexTemplate']); | 
            |
| 238 | 18 | $indexDef->replaceArgument(0, $indexTemplateName);  | 
            |
| 239 | 18 |             $indexDef->addTag('fos_elastica.index_template', array( | 
            |
| 240 | 18 | 'name' => $name,  | 
            |
| 241 | ));  | 
            ||
| 242 | 18 | ||
| 243 | View Code Duplication |             if (isset($indexTemplate['client'])) { | 
            |
| 244 | $client = $this->getClient($indexTemplate['client']);  | 
            ||
| 245 | 18 | $indexDef->setFactory([$client, 'getIndexTemplate']);  | 
            |
| 246 | }  | 
            ||
| 247 | |||
| 248 | $container->setDefinition($indexId, $indexDef);  | 
            ||
| 249 | $reference = new Reference($indexId);  | 
            ||
| 250 | |||
| 251 | 18 | $this->indexTemplateConfigs[$name] = array(  | 
            |
| 252 | 'elasticsearch_name' => $indexTemplateName,  | 
            ||
| 253 | 'reference' => $reference,  | 
            ||
| 254 | 'name' => $name,  | 
            ||
| 255 | 'settings' => $indexTemplate['settings'],  | 
            ||
| 256 | 'template' => $indexTemplate['template'],  | 
            ||
| 257 | );  | 
            ||
| 258 | |||
| 259 | 18 | $this->loadTypes(  | 
            |
| 260 | 18 | (array) $indexTemplate['types'],  | 
            |
| 261 | $container,  | 
            ||
| 262 | $this->indexTemplateConfigs[$name],  | 
            ||
| 263 | $indexableCallbacks  | 
            ||
| 264 | );  | 
            ||
| 265 | 18 | }  | 
            |
| 266 | |||
| 267 |         if ($indexableCallbacks) { | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 268 |             throw new \RuntimeException('`indexable_callback` option is not supported by index templates'); | 
            ||
| 269 | }  | 
            ||
| 270 | }  | 
            ||
| 271 | |||
| 272 | /**  | 
            ||
| 273 | * Loads the configured index finders.  | 
            ||
| 274 | 18 | *  | 
            |
| 275 | 18 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container  | 
            |
| 276 | 18 | * @param string $name The index name  | 
            |
| 277 | * @param Reference $index Reference to the related index  | 
            ||
| 278 | *  | 
            ||
| 279 | 18 | * @return string  | 
            |
| 280 | */  | 
            ||
| 281 | 18 | private function loadIndexFinder(ContainerBuilder $container, $name, Reference $index)  | 
            |
| 297 | 1 | ||
| 298 | 1 | /**  | 
            |
| 299 | * Loads the configured types.  | 
            ||
| 300 | 1 | *  | 
            |
| 301 | 1 | * @param array $types  | 
            |
| 302 | * @param ContainerBuilder $container  | 
            ||
| 303 | * @param array $indexConfig  | 
            ||
| 304 | 1 | * @param array $indexableCallbacks  | 
            |
| 305 | 1 | */  | 
            |
| 306 | private function loadTypes(array $types, ContainerBuilder $container, array &$indexConfig, array &$indexableCallbacks)  | 
            ||
| 391 | 15 | ||
| 392 | 1 | private function buildCallback($indexCallback, $typeName)  | 
            |
| 417 | |||
| 418 | private function transformServiceReference($classOrService)  | 
            ||
| 422 | 15 | ||
| 423 | /**  | 
            ||
| 424 | 15 | * Loads the optional provider and finder for a type.  | 
            |
| 425 | *  | 
            ||
| 426 | * @param array $typeConfig  | 
            ||
| 427 | * @param ContainerBuilder $container  | 
            ||
| 428 | 15 | * @param Reference $typeRef  | 
            |
| 429 | 1 | * @param string $indexName  | 
            |
| 430 | 15 | * @param string $typeName  | 
            |
| 431 | */  | 
            ||
| 432 | 15 | private function loadTypePersistenceIntegration(array $typeConfig, ContainerBuilder $container, Reference $typeRef, $indexName, $typeName)  | 
            |
| 452 | |||
| 453 | /**  | 
            ||
| 454 | * Creates and loads an ElasticaToModelTransformer.  | 
            ||
| 455 | 15 | *  | 
            |
| 456 | * @param array $typeConfig  | 
            ||
| 457 | 15 | * @param ContainerBuilder $container  | 
            |
| 458 | 1 | * @param string $indexName  | 
            |
| 459 | * @param string $typeName  | 
            ||
| 460 | *  | 
            ||
| 461 | * @return string  | 
            ||
| 462 | 14 | */  | 
            |
| 463 | 14 | private function loadElasticaToModelTransformer(array $typeConfig, ContainerBuilder $container, $indexName, $typeName)  | 
            |
| 485 | 14 | ||
| 486 | 14 | /**  | 
            |
| 487 | * Creates and loads a ModelToElasticaTransformer for an index/type.  | 
            ||
| 488 | *  | 
            ||
| 489 | 14 | * @param array $typeConfig  | 
            |
| 490 | * @param ContainerBuilder $container  | 
            ||
| 491 | 14 | * @param string $indexName  | 
            |
| 492 | * @param string $typeName  | 
            ||
| 493 | 14 | *  | 
            |
| 494 | * @return string  | 
            ||
| 495 | */  | 
            ||
| 496 | private function loadModelToElasticaTransformer(array $typeConfig, ContainerBuilder $container, $indexName, $typeName)  | 
            ||
| 516 | |||
| 517 | 15 | /**  | 
            |
| 518 | 15 | * Creates and loads an object persister for a type.  | 
            |
| 519 | 14 | *  | 
            |
| 520 | 14 | * @param array $typeConfig  | 
            |
| 521 | 14 | * @param Reference $typeRef  | 
            |
| 522 | * @param ContainerBuilder $container  | 
            ||
| 523 | 14 | * @param string $indexName  | 
            |
| 524 | 1 | * @param string $typeName  | 
            |
| 525 | * @param string $transformerId  | 
            ||
| 526 | *  | 
            ||
| 527 | * @return string  | 
            ||
| 528 | */  | 
            ||
| 529 | private function loadObjectPersister(array $typeConfig, Reference $typeRef, ContainerBuilder $container, $indexName, $typeName, $transformerId)  | 
            ||
| 569 | |||
| 570 | /**  | 
            ||
| 571 | 14 | * Loads a pager provider for a type.  | 
            |
| 572 | 14 | *  | 
            |
| 573 | 14 | * @param array $typeConfig  | 
            |
| 574 | 14 | * @param ContainerBuilder $container  | 
            |
| 575 | 14 | * @param string $indexName  | 
            |
| 576 | * @param string $typeName  | 
            ||
| 577 | 14 | *  | 
            |
| 578 | * @return string  | 
            ||
| 579 | */  | 
            ||
| 580 | 14 | private function loadTypePagerProvider(array $typeConfig, ContainerBuilder $container, $indexName, $typeName)  | 
            |
| 624 | |||
| 625 | /**  | 
            ||
| 626 | * Loads doctrine listeners to handle indexing of new or updated objects.  | 
            ||
| 627 | 14 | *  | 
            |
| 628 | * @param array $typeConfig  | 
            ||
| 629 | 14 | * @param ContainerBuilder $container  | 
            |
| 630 | 14 | * @param string $objectPersisterId  | 
            |
| 631 | 13 | * @param string $indexName  | 
            |
| 632 | 13 | * @param string $typeName  | 
            |
| 633 | 1 | *  | 
            |
| 634 | 1 | * @return string  | 
            |
| 635 | 1 | */  | 
            |
| 636 | private function loadTypeListener(array $typeConfig, ContainerBuilder $container, $objectPersisterId, $indexName, $typeName)  | 
            ||
| 697 | 15 | ||
| 698 | /**  | 
            ||
| 699 | * Map Elastica to Doctrine events for the current driver.  | 
            ||
| 700 | */  | 
            ||
| 701 | private function getDoctrineEvents(array $typeConfig)  | 
            ||
| 733 | |||
| 734 | /**  | 
            ||
| 735 | * Loads a Type specific Finder.  | 
            ||
| 736 | *  | 
            ||
| 737 | * @param array $typeConfig  | 
            ||
| 738 | 1 | * @param ContainerBuilder $container  | 
            |
| 739 | * @param string $elasticaToModelId  | 
            ||
| 740 | 1 | * @param Reference $typeRef  | 
            |
| 741 | * @param string $indexName  | 
            ||
| 742 | 1 | * @param string $typeName  | 
            |
| 743 | 1 | *  | 
            |
| 744 | * @return string  | 
            ||
| 745 | 1 | */  | 
            |
| 746 | private function loadTypeFinder(array $typeConfig, ContainerBuilder $container, $elasticaToModelId, Reference $typeRef, $indexName, $typeName)  | 
            ||
| 773 | 15 | ||
| 774 | 15 | /**  | 
            |
| 775 | * Loads the index manager.  | 
            ||
| 776 | *  | 
            ||
| 777 | * @param ContainerBuilder $container  | 
            ||
| 778 | **/  | 
            ||
| 779 | View Code Duplication | private function loadIndexManager(ContainerBuilder $container)  | 
            |
| 788 | |||
| 789 | /**  | 
            ||
| 790 | * Load index template manager  | 
            ||
| 791 | 2 | *  | 
            |
| 792 | * @param ContainerBuilder $container  | 
            ||
| 793 | *  | 
            ||
| 794 | * @return void  | 
            ||
| 795 | */  | 
            ||
| 796 | View Code Duplication | private function loadIndexTemplateManager(ContainerBuilder $container)  | 
            |
| 805 | |||
| 806 | /**  | 
            ||
| 807 | * Makes sure a specific driver has been loaded.  | 
            ||
| 808 | *  | 
            ||
| 809 | * @param ContainerBuilder $container  | 
            ||
| 810 | * @param string $driver  | 
            ||
| 811 | */  | 
            ||
| 812 | private function loadDriver(ContainerBuilder $container, $driver)  | 
            ||
| 822 | |||
| 823 | /**  | 
            ||
| 824 | * Loads and configures the serializer prototype.  | 
            ||
| 825 | *  | 
            ||
| 826 | * @param array $config  | 
            ||
| 827 | * @param ContainerBuilder $container  | 
            ||
| 828 | */  | 
            ||
| 829 | private function loadSerializer($config, ContainerBuilder $container)  | 
            ||
| 840 | |||
| 841 | /**  | 
            ||
| 842 | * Creates a default manager alias for defined default manager or the first loaded driver.  | 
            ||
| 843 | *  | 
            ||
| 844 | * @param string $defaultManager  | 
            ||
| 845 | * @param ContainerBuilder $container  | 
            ||
| 846 | */  | 
            ||
| 847 | private function createDefaultManagerAlias($defaultManager, ContainerBuilder $container)  | 
            ||
| 866 | |||
| 867 | /**  | 
            ||
| 868 | * Returns a reference to a client given its configured name.  | 
            ||
| 869 | *  | 
            ||
| 870 | * @param string $clientName  | 
            ||
| 871 | *  | 
            ||
| 872 | * @return Reference  | 
            ||
| 873 | *  | 
            ||
| 874 | * @throws \InvalidArgumentException  | 
            ||
| 875 | */  | 
            ||
| 876 | private function getClient($clientName)  | 
            ||
| 884 | }  | 
            ||
| 885 | 
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.