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 |
||
| 23 | class FOSElasticaExtension extends Extension |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Definition of elastica clients as configured by this extension. |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | private $clients = []; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * An array of indexes as configured by the extension. |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private $indexConfigs = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * If we've encountered a type mapped to a specific persistence driver, it will be loaded |
||
| 41 | * here. |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | private $loadedDrivers = []; |
||
| 46 | |||
| 47 | 9 | public function load(array $configs, ContainerBuilder $container) |
|
| 48 | { |
||
| 49 | 9 | $configuration = $this->getConfiguration($configs, $container); |
|
| 50 | 9 | $config = $this->processConfiguration($configuration, $configs); |
|
| 51 | |||
| 52 | 9 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
| 53 | |||
| 54 | 9 | if (empty($config['clients']) || empty($config['indexes'])) { |
|
| 55 | // No Clients or indexes are defined |
||
| 56 | return; |
||
| 57 | } |
||
| 58 | |||
| 59 | 9 | foreach (['config', 'index', 'persister', 'provider', 'source', 'transformer', 'event_listener'] as $basename) { |
|
| 60 | 9 | $loader->load(sprintf('%s.xml', $basename)); |
|
| 61 | } |
||
| 62 | |||
| 63 | 9 | if (empty($config['default_client'])) { |
|
| 64 | 9 | $keys = array_keys($config['clients']); |
|
| 65 | 9 | $config['default_client'] = reset($keys); |
|
| 66 | } |
||
| 67 | |||
| 68 | 9 | if (empty($config['default_index'])) { |
|
| 69 | 9 | $keys = array_keys($config['indexes']); |
|
| 70 | 9 | $config['default_index'] = reset($keys); |
|
| 71 | } |
||
| 72 | |||
| 73 | 9 | if (isset($config['serializer'])) { |
|
| 74 | $loader->load('serializer.xml'); |
||
| 75 | |||
| 76 | $this->loadSerializer($config['serializer'], $container); |
||
| 77 | } |
||
| 78 | |||
| 79 | 9 | $this->loadClients($config['clients'], $container); |
|
| 80 | 9 | $container->setAlias('fos_elastica.client', sprintf('fos_elastica.client.%s', $config['default_client'])); |
|
| 81 | |||
| 82 | 9 | $this->loadIndexes($config['indexes'], $container); |
|
| 83 | 9 | $container->setAlias('fos_elastica.index', sprintf('fos_elastica.index.%s', $config['default_index'])); |
|
| 84 | 9 | $container->setParameter('fos_elastica.default_index', $config['default_index']); |
|
| 85 | |||
| 86 | 9 | $container->getDefinition('fos_elastica.config_source.container')->replaceArgument(0, $this->indexConfigs); |
|
| 87 | |||
| 88 | 9 | $this->loadIndexManager($container); |
|
| 89 | |||
| 90 | 9 | $this->createDefaultManagerAlias($config['default_manager'], $container); |
|
| 91 | 9 | } |
|
| 92 | |||
| 93 | /** |
||
| 94 | * @param array $config |
||
| 95 | * @param ContainerBuilder $container |
||
| 96 | * |
||
| 97 | * @return Configuration |
||
| 98 | */ |
||
| 99 | 9 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Loads the configured clients. |
||
| 106 | * |
||
| 107 | * @param array $clients An array of clients configurations |
||
| 108 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
| 109 | * |
||
| 110 | * @return array |
||
| 111 | */ |
||
| 112 | 9 | private function loadClients(array $clients, ContainerBuilder $container) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Loads the configured indexes. |
||
| 138 | * |
||
| 139 | * @param array $indexes An array of indexes configurations |
||
| 140 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
| 141 | * |
||
| 142 | * @throws \InvalidArgumentException |
||
| 143 | * |
||
| 144 | * @return array |
||
| 145 | */ |
||
| 146 | 9 | private function loadIndexes(array $indexes, ContainerBuilder $container) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Loads the configured index finders. |
||
| 192 | * |
||
| 193 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
||
| 194 | * @param string $name The index name |
||
| 195 | * @param Reference $index Reference to the related index |
||
| 196 | * |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | private function loadIndexFinder(ContainerBuilder $container, $name, Reference $index) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Loads the configured types. |
||
| 218 | * |
||
| 219 | * @param array $types |
||
| 220 | * @param ContainerBuilder $container |
||
| 221 | * @param array $indexConfig |
||
| 222 | * @param array $indexableCallbacks |
||
| 223 | */ |
||
| 224 | 9 | private function loadTypes(array $types, ContainerBuilder $container, array $indexConfig, array &$indexableCallbacks) |
|
| 225 | { |
||
| 226 | 9 | foreach ($types as $name => $type) { |
|
| 227 | 9 | $indexName = $indexConfig['name']; |
|
| 228 | |||
| 229 | 9 | $typeId = sprintf('%s.%s', $indexConfig['reference'], $name); |
|
| 230 | 9 | $typeDef = new DefinitionDecorator('fos_elastica.type_prototype'); |
|
| 231 | 9 | $typeDef->setFactory([$indexConfig['reference'], 'getType']); |
|
| 232 | 9 | $typeDef->replaceArgument(0, $name); |
|
| 233 | |||
| 234 | 9 | $container->setDefinition($typeId, $typeDef); |
|
| 235 | |||
| 236 | $typeConfig = [ |
||
| 237 | 9 | 'name' => $name, |
|
| 238 | 'mapping' => [], // An array containing anything that gets sent directly to ElasticSearch |
||
| 239 | 'config' => [], |
||
| 240 | ]; |
||
| 241 | |||
| 242 | foreach ([ |
||
| 243 | 9 | 'dynamic_templates', |
|
| 244 | 'properties', |
||
| 245 | '_all', |
||
| 246 | '_id', |
||
| 247 | '_parent', |
||
| 248 | '_routing', |
||
| 249 | '_source', |
||
| 250 | ] as $field) { |
||
| 251 | 9 | if (isset($type[$field])) { |
|
| 252 | 9 | $typeConfig['mapping'][$field] = $type[$field]; |
|
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 256 | foreach ([ |
||
| 257 | 9 | 'persistence', |
|
| 258 | 'serializer', |
||
| 259 | 'analyzer', |
||
| 260 | 'search_analyzer', |
||
| 261 | 'dynamic', |
||
| 262 | 'date_detection', |
||
| 263 | 'dynamic_date_formats', |
||
| 264 | 'numeric_detection', |
||
| 265 | ] as $field) { |
||
| 266 | 9 | $typeConfig['config'][$field] = array_key_exists($field, $type) ? |
|
| 267 | 9 | $type[$field] : |
|
| 268 | 9 | null; |
|
| 269 | } |
||
| 270 | |||
| 271 | 9 | $this->indexConfigs[$indexName]['types'][$name] = $typeConfig; |
|
| 272 | |||
| 273 | 9 | if (isset($type['persistence'])) { |
|
| 274 | 9 | $this->loadTypePersistenceIntegration($type['persistence'], $container, new Reference($typeId), $indexName, $name); |
|
| 275 | |||
| 276 | 9 | $typeConfig['persistence'] = $type['persistence']; |
|
| 277 | } |
||
| 278 | |||
| 279 | 9 | if (isset($type['_parent'])) { |
|
| 280 | // _parent mapping cannot contain `property` and `identifier`, so removing them after building `persistence` |
||
| 281 | 1 | unset($indexConfig['types'][$name]['mapping']['_parent']['property'], $indexConfig['types'][$name]['mapping']['_parent']['identifier']); |
|
| 282 | } |
||
| 283 | |||
| 284 | 9 | if (isset($type['indexable_callback'])) { |
|
| 285 | $indexableCallbacks[sprintf('%s/%s', $indexName, $name)] = $type['indexable_callback']; |
||
| 286 | } |
||
| 287 | |||
| 288 | 9 | if ($container->hasDefinition('fos_elastica.serializer_callback_prototype')) { |
|
| 289 | $typeSerializerId = sprintf('%s.serializer.callback', $typeId); |
||
| 290 | $typeSerializerDef = new DefinitionDecorator('fos_elastica.serializer_callback_prototype'); |
||
| 291 | |||
| 292 | if (isset($type['serializer']['groups'])) { |
||
| 293 | $typeSerializerDef->addMethodCall('setGroups', [$type['serializer']['groups']]); |
||
| 294 | } |
||
| 295 | |||
| 296 | if (isset($type['serializer']['serialize_null'])) { |
||
| 297 | $typeSerializerDef->addMethodCall('setSerializeNull', [$type['serializer']['serialize_null']]); |
||
| 298 | } |
||
| 299 | |||
| 300 | if (isset($type['serializer']['version'])) { |
||
| 301 | $typeSerializerDef->addMethodCall('setVersion', [$type['serializer']['version']]); |
||
| 302 | } |
||
| 303 | |||
| 304 | $typeDef->addMethodCall('setSerializer', [[new Reference($typeSerializerId), 'serialize']]); |
||
| 305 | 9 | $container->setDefinition($typeSerializerId, $typeSerializerDef); |
|
| 306 | } |
||
| 307 | } |
||
| 308 | 9 | } |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Loads the optional provider and finder for a type. |
||
| 312 | * |
||
| 313 | * @param array $typeConfig |
||
| 314 | * @param ContainerBuilder $container |
||
| 315 | * @param Reference $typeRef |
||
| 316 | * @param string $indexName |
||
| 317 | * @param string $typeName |
||
| 318 | */ |
||
| 319 | 9 | private function loadTypePersistenceIntegration(array $typeConfig, ContainerBuilder $container, Reference $typeRef, $indexName, $typeName) |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Creates and loads an ElasticaToModelTransformer. |
||
| 350 | * |
||
| 351 | * @param array $typeConfig |
||
| 352 | * @param ContainerBuilder $container |
||
| 353 | * @param string $indexName |
||
| 354 | * @param string $typeName |
||
| 355 | * |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | 9 | private function loadElasticaToModelTransformer(array $typeConfig, ContainerBuilder $container, $indexName, $typeName) |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Creates and loads a ModelToElasticaTransformer for an index/type. |
||
| 386 | * |
||
| 387 | * @param array $typeConfig |
||
| 388 | * @param ContainerBuilder $container |
||
| 389 | * @param string $indexName |
||
| 390 | * @param string $typeName |
||
| 391 | * |
||
| 392 | * @return string |
||
| 393 | */ |
||
| 394 | 9 | private function loadModelToElasticaTransformer(array $typeConfig, ContainerBuilder $container, $indexName, $typeName) |
|
| 395 | { |
||
| 396 | 9 | if (isset($typeConfig['model_to_elastica_transformer']['service'])) { |
|
| 397 | return $typeConfig['model_to_elastica_transformer']['service']; |
||
| 398 | } |
||
| 399 | |||
| 400 | 9 | $abstractId = $container->hasDefinition('fos_elastica.serializer_callback_prototype') ? |
|
| 401 | 'fos_elastica.model_to_elastica_identifier_transformer' : |
||
| 402 | 9 | 'fos_elastica.model_to_elastica_transformer'; |
|
| 403 | |||
| 404 | 9 | $serviceId = sprintf('fos_elastica.model_to_elastica_transformer.%s.%s', $indexName, $typeName); |
|
| 405 | 9 | $serviceDef = new DefinitionDecorator($abstractId); |
|
| 406 | 9 | $serviceDef->replaceArgument(0, [ |
|
| 407 | 9 | 'identifier' => $typeConfig['identifier'], |
|
| 408 | ]); |
||
| 409 | 9 | $container->setDefinition($serviceId, $serviceDef); |
|
| 410 | |||
| 411 | 9 | return $serviceId; |
|
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Creates and loads an object persister for a type. |
||
| 416 | * |
||
| 417 | * @param array $typeConfig |
||
| 418 | * @param Reference $typeRef |
||
| 419 | * @param ContainerBuilder $container |
||
| 420 | * @param string $indexName |
||
| 421 | * @param string $typeName |
||
| 422 | * @param string $transformerId |
||
| 423 | * |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | 9 | private function loadObjectPersister(array $typeConfig, Reference $typeRef, ContainerBuilder $container, $indexName, $typeName, $transformerId) |
|
| 427 | { |
||
| 428 | 9 | if (isset($typeConfig['persister']['service'])) { |
|
| 429 | 1 | return $typeConfig['persister']['service']; |
|
| 430 | } |
||
| 431 | |||
| 432 | $arguments = [ |
||
| 433 | 8 | $typeRef, |
|
| 434 | 8 | new Reference($transformerId), |
|
| 435 | 8 | $typeConfig['model'], |
|
| 436 | ]; |
||
| 437 | |||
| 438 | 8 | if ($container->hasDefinition('fos_elastica.serializer_callback_prototype')) { |
|
| 439 | $abstractId = 'fos_elastica.object_serializer_persister'; |
||
| 440 | $callbackId = sprintf('%s.%s.serializer.callback', $this->indexConfigs[$indexName]['reference'], $typeName); |
||
| 441 | $arguments[] = [new Reference($callbackId), 'serialize']; |
||
| 442 | } else { |
||
| 443 | 8 | $abstractId = 'fos_elastica.object_persister'; |
|
| 444 | 8 | $mapping = $this->indexConfigs[$indexName]['types'][$typeName]['mapping']; |
|
| 445 | 8 | $argument = $mapping['properties']; |
|
| 446 | 8 | if (isset($mapping['_parent'])) { |
|
| 447 | 1 | $argument['_parent'] = $mapping['_parent']; |
|
| 448 | } |
||
| 449 | 8 | $arguments[] = $argument; |
|
| 450 | } |
||
| 451 | |||
| 452 | 8 | $serviceId = sprintf('fos_elastica.object_persister.%s.%s', $indexName, $typeName); |
|
| 453 | 8 | $serviceDef = new DefinitionDecorator($abstractId); |
|
| 454 | 8 | foreach ($arguments as $i => $argument) { |
|
| 455 | 8 | $serviceDef->replaceArgument($i, $argument); |
|
| 456 | } |
||
| 457 | |||
| 458 | 8 | $serviceDef->addTag('fos_elastica.persister', ['index' => $indexName, 'type' => $typeName]); |
|
| 459 | |||
| 460 | 8 | $container->setDefinition($serviceId, $serviceDef); |
|
| 461 | |||
| 462 | 8 | return $serviceId; |
|
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Loads a provider for a type. |
||
| 467 | * |
||
| 468 | * @param array $typeConfig |
||
| 469 | * @param ContainerBuilder $container |
||
| 470 | * @param string $objectPersisterId |
||
| 471 | * @param string $indexName |
||
| 472 | * @param string $typpeName |
||
|
|
|||
| 473 | * |
||
| 474 | * @return string |
||
| 475 | */ |
||
| 476 | 9 | private function loadTypeProvider(array $typeConfig, ContainerBuilder $container, $objectPersisterId, $indexName, $typeName) |
|
| 499 | |||
| 500 | /** |
||
| 501 | * Loads a pager provider for a type. |
||
| 502 | * |
||
| 503 | * @param array $typeConfig |
||
| 504 | * @param ContainerBuilder $container |
||
| 505 | * @param string $indexName |
||
| 506 | * @param string $typeName |
||
| 507 | * |
||
| 508 | * @return string |
||
| 509 | */ |
||
| 510 | 6 | private function loadTypePagerProvider(array $typeConfig, ContainerBuilder $container, $indexName, $typeName) |
|
| 561 | |||
| 562 | /** |
||
| 563 | * Loads doctrine listeners to handle indexing of new or updated objects. |
||
| 564 | * |
||
| 565 | * @param array $typeConfig |
||
| 566 | * @param ContainerBuilder $container |
||
| 567 | * @param string $objectPersisterId |
||
| 568 | * @param string $indexName |
||
| 569 | * @param string $typeName |
||
| 570 | * |
||
| 571 | * @return string |
||
| 572 | */ |
||
| 573 | 9 | private function loadTypeListener(array $typeConfig, ContainerBuilder $container, $objectPersisterId, $indexName, $typeName) |
|
| 634 | |||
| 635 | /** |
||
| 636 | * Map Elastica to Doctrine events for the current driver. |
||
| 637 | */ |
||
| 638 | 6 | private function getDoctrineEvents(array $typeConfig) |
|
| 670 | |||
| 671 | /** |
||
| 672 | * Loads a Type specific Finder. |
||
| 673 | * |
||
| 674 | * @param array $typeConfig |
||
| 675 | * @param ContainerBuilder $container |
||
| 676 | * @param string $elasticaToModelId |
||
| 677 | * @param Reference $typeRef |
||
| 678 | * @param string $indexName |
||
| 679 | * @param string $typeName |
||
| 680 | * |
||
| 681 | * @return string |
||
| 682 | */ |
||
| 683 | 9 | private function loadTypeFinder(array $typeConfig, ContainerBuilder $container, $elasticaToModelId, Reference $typeRef, $indexName, $typeName) |
|
| 684 | { |
||
| 685 | 9 | if (isset($typeConfig['finder']['service'])) { |
|
| 686 | $finderId = $typeConfig['finder']['service']; |
||
| 687 | } else { |
||
| 688 | 9 | $finderId = sprintf('fos_elastica.finder.%s.%s', $indexName, $typeName); |
|
| 689 | 9 | $finderDef = new DefinitionDecorator('fos_elastica.finder'); |
|
| 690 | 9 | $finderDef->replaceArgument(0, $typeRef); |
|
| 691 | 9 | $finderDef->replaceArgument(1, new Reference($elasticaToModelId)); |
|
| 692 | 9 | $container->setDefinition($finderId, $finderDef); |
|
| 693 | } |
||
| 694 | |||
| 695 | 9 | $indexTypeName = "$indexName/$typeName"; |
|
| 696 | 9 | $arguments = [$indexTypeName, new Reference($finderId)]; |
|
| 697 | 9 | if (isset($typeConfig['repository'])) { |
|
| 698 | $arguments[] = $typeConfig['repository']; |
||
| 699 | } |
||
| 700 | |||
| 701 | 9 | $container->getDefinition('fos_elastica.repository_manager') |
|
| 702 | 9 | ->addMethodCall('addType', $arguments); |
|
| 703 | |||
| 704 | 9 | $managerId = sprintf('fos_elastica.manager.%s', $typeConfig['driver']); |
|
| 705 | 9 | $container->getDefinition($managerId) |
|
| 706 | 9 | ->addMethodCall('addEntity', [$typeConfig['model'], $indexTypeName]); |
|
| 707 | |||
| 708 | 9 | return $finderId; |
|
| 709 | } |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Loads the index manager. |
||
| 713 | * |
||
| 714 | * @param ContainerBuilder $container |
||
| 715 | **/ |
||
| 716 | private function loadIndexManager(ContainerBuilder $container) |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Makes sure a specific driver has been loaded. |
||
| 728 | * |
||
| 729 | * @param ContainerBuilder $container |
||
| 730 | * @param string $driver |
||
| 731 | */ |
||
| 732 | 9 | private function loadDriver(ContainerBuilder $container, $driver) |
|
| 742 | |||
| 743 | /** |
||
| 744 | * Loads and configures the serializer prototype. |
||
| 745 | * |
||
| 746 | * @param array $config |
||
| 747 | * @param ContainerBuilder $container |
||
| 748 | */ |
||
| 749 | private function loadSerializer($config, ContainerBuilder $container) |
||
| 750 | { |
||
| 751 | $container->setAlias('fos_elastica.serializer', $config['serializer']); |
||
| 752 | |||
| 753 | $serializer = $container->getDefinition('fos_elastica.serializer_callback_prototype'); |
||
| 754 | $serializer->setClass($config['callback_class']); |
||
| 755 | |||
| 756 | if (is_subclass_of($config['callback_class'], ContainerAwareInterface::class)) { |
||
| 757 | $serializer->addMethodCall('setContainer', [new Reference('service_container')]); |
||
| 758 | } |
||
| 759 | } |
||
| 760 | |||
| 761 | /** |
||
| 762 | * Creates a default manager alias for defined default manager or the first loaded driver. |
||
| 763 | * |
||
| 764 | * @param string $defaultManager |
||
| 765 | * @param ContainerBuilder $container |
||
| 766 | */ |
||
| 767 | 9 | private function createDefaultManagerAlias($defaultManager, ContainerBuilder $container) |
|
| 768 | { |
||
| 769 | 9 | if (0 == count($this->loadedDrivers)) { |
|
| 770 | return; |
||
| 771 | } |
||
| 772 | |||
| 773 | 9 | if (count($this->loadedDrivers) > 1 |
|
| 774 | 9 | && in_array($defaultManager, $this->loadedDrivers) |
|
| 775 | ) { |
||
| 776 | $defaultManagerService = $defaultManager; |
||
| 777 | } else { |
||
| 778 | 9 | $defaultManagerService = $this->loadedDrivers[0]; |
|
| 779 | } |
||
| 780 | |||
| 781 | 9 | $container->setAlias('fos_elastica.manager', sprintf('fos_elastica.manager.%s', $defaultManagerService)); |
|
| 782 | 9 | } |
|
| 783 | |||
| 784 | /** |
||
| 785 | * Returns a reference to a client given its configured name. |
||
| 786 | * |
||
| 787 | * @param string $clientName |
||
| 788 | * |
||
| 789 | * @return Reference |
||
| 790 | * |
||
| 791 | * @throws \InvalidArgumentException |
||
| 792 | */ |
||
| 793 | 2 | private function getClient($clientName) |
|
| 801 | } |
||
| 802 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.