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) |
|
136 | { |
||
137 | 14 | $indexableCallbacks = array(); |
|
138 | |||
139 | 14 | foreach ($indexes as $name => $index) { |
|
140 | 14 | $indexId = sprintf('fos_elastica.index.%s', $name); |
|
141 | 14 | $indexName = isset($index['index_name']) ? $index['index_name'] : $name; |
|
142 | |||
143 | 14 | $indexDef = new DefinitionDecorator('fos_elastica.index_prototype'); |
|
144 | 14 | $indexDef->replaceArgument(0, $indexName); |
|
145 | 14 | $indexDef->addTag('fos_elastica.index', array( |
|
146 | 14 | 'name' => $name, |
|
147 | )); |
||
148 | |||
149 | 14 | if (method_exists($indexDef, 'setFactory')) { |
|
150 | 14 | $indexDef->setFactory(array(new Reference('fos_elastica.client'), 'getIndex')); |
|
151 | } else { |
||
152 | // To be removed when dependency on Symfony DependencyInjection is bumped to 2.6 |
||
153 | $indexDef->setFactoryService('fos_elastica.client'); |
||
|
|||
154 | $indexDef->setFactoryMethod('getIndex'); |
||
155 | } |
||
156 | |||
157 | 14 | if (isset($index['client'])) { |
|
158 | 2 | $client = $this->getClient($index['client']); |
|
159 | |||
160 | 2 | if (method_exists($indexDef, 'setFactory')) { |
|
161 | 2 | $indexDef->setFactory(array($client, 'getIndex')); |
|
162 | } else { |
||
163 | // To be removed when dependency on Symfony DependencyInjection is bumped to 2.6 |
||
164 | $indexDef->setFactoryService('fos_elastica.client'); |
||
165 | $indexDef->setFactoryMethod('getIndex'); |
||
166 | } |
||
167 | } |
||
168 | |||
169 | 14 | $container->setDefinition($indexId, $indexDef); |
|
170 | 14 | $reference = new Reference($indexId); |
|
171 | |||
172 | 14 | $this->indexConfigs[$name] = array( |
|
173 | 14 | 'elasticsearch_name' => $indexName, |
|
174 | 14 | 'reference' => $reference, |
|
175 | 14 | 'name' => $name, |
|
176 | 14 | 'settings' => $index['settings'], |
|
177 | 14 | 'type_prototype' => isset($index['type_prototype']) ? $index['type_prototype'] : array(), |
|
178 | 14 | 'use_alias' => $index['use_alias'], |
|
179 | ); |
||
180 | |||
181 | 14 | if ($index['finder']) { |
|
182 | $this->loadIndexFinder($container, $name, $reference); |
||
183 | } |
||
184 | |||
185 | 14 | $this->loadTypes((array) $index['types'], $container, $this->indexConfigs[$name], $indexableCallbacks); |
|
186 | } |
||
187 | |||
188 | 14 | $indexable = $container->getDefinition('fos_elastica.indexable'); |
|
189 | 14 | $indexable->replaceArgument(0, $indexableCallbacks); |
|
190 | 14 | } |
|
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) |
||
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) |
|
227 | { |
||
228 | 14 | foreach ($types as $name => $type) { |
|
229 | 14 | $indexName = $indexConfig['name']; |
|
230 | |||
231 | 14 | $typeId = sprintf('%s.%s', $indexConfig['reference'], $name); |
|
232 | 14 | $typeDef = new DefinitionDecorator('fos_elastica.type_prototype'); |
|
233 | 14 | $typeDef->replaceArgument(0, $name); |
|
234 | |||
235 | 14 | if (method_exists($typeDef, 'setFactory')) { |
|
236 | 14 | $typeDef->setFactory(array($indexConfig['reference'], 'getType')); |
|
237 | } else { |
||
238 | // To be removed when dependency on Symfony DependencyInjection is bumped to 2.6 |
||
239 | $typeDef->setFactoryService((string) $indexConfig['reference']); |
||
240 | $typeDef->setFactoryMethod('getType'); |
||
241 | } |
||
242 | |||
243 | 14 | $container->setDefinition($typeId, $typeDef); |
|
244 | |||
245 | $typeConfig = array( |
||
246 | 14 | 'name' => $name, |
|
247 | 'mapping' => array(), // An array containing anything that gets sent directly to ElasticSearch |
||
248 | 'config' => array(), |
||
249 | ); |
||
250 | |||
251 | foreach (array( |
||
252 | 14 | 'dynamic_templates', |
|
253 | 'properties', |
||
254 | '_all', |
||
255 | '_boost', |
||
256 | '_id', |
||
257 | '_parent', |
||
258 | '_routing', |
||
259 | '_source', |
||
260 | '_timestamp', |
||
261 | '_ttl', |
||
262 | ) as $field) { |
||
263 | 14 | if (isset($type[$field])) { |
|
264 | 14 | $typeConfig['mapping'][$field] = $type[$field]; |
|
265 | } |
||
266 | } |
||
267 | |||
268 | foreach (array( |
||
269 | 14 | 'persistence', |
|
270 | 'serializer', |
||
271 | 'analyzer', |
||
272 | 'search_analyzer', |
||
273 | 'dynamic', |
||
274 | 'date_detection', |
||
275 | 'dynamic_date_formats', |
||
276 | 'numeric_detection', |
||
277 | ) as $field) { |
||
278 | 14 | $typeConfig['config'][$field] = array_key_exists($field, $type) ? |
|
279 | 14 | $type[$field] : |
|
280 | 14 | null; |
|
281 | } |
||
282 | |||
283 | 14 | $this->indexConfigs[$indexName]['types'][$name] = $typeConfig; |
|
284 | |||
285 | 14 | if (isset($type['persistence'])) { |
|
286 | 10 | $this->loadTypePersistenceIntegration($type['persistence'], $container, new Reference($typeId), $indexName, $name); |
|
287 | |||
288 | 10 | $typeConfig['persistence'] = $type['persistence']; |
|
289 | } |
||
290 | |||
291 | 14 | if (isset($type['_parent'])) { |
|
292 | // _parent mapping cannot contain `property` and `identifier`, so removing them after building `persistence` |
||
293 | 5 | unset($indexConfig['types'][$name]['mapping']['_parent']['property'], $indexConfig['types'][$name]['mapping']['_parent']['identifier']); |
|
294 | } |
||
295 | |||
296 | 14 | if (isset($type['indexable_callback'])) { |
|
297 | 5 | $indexableCallbacks[sprintf('%s/%s', $indexName, $name)] = $type['indexable_callback']; |
|
298 | } |
||
299 | |||
300 | 14 | if ($container->hasDefinition('fos_elastica.serializer_callback_prototype')) { |
|
301 | 3 | $typeSerializerId = sprintf('%s.serializer.callback', $typeId); |
|
302 | 3 | $typeSerializerDef = new DefinitionDecorator('fos_elastica.serializer_callback_prototype'); |
|
303 | |||
304 | 3 | if (isset($type['serializer']['groups'])) { |
|
305 | 3 | $typeSerializerDef->addMethodCall('setGroups', array($type['serializer']['groups'])); |
|
306 | } |
||
307 | |||
308 | 3 | if (isset($type['serializer']['serialize_null'])) { |
|
309 | 3 | $typeSerializerDef->addMethodCall('setSerializeNull', array($type['serializer']['serialize_null'])); |
|
310 | } |
||
311 | |||
312 | 3 | if (isset($type['serializer']['version'])) { |
|
313 | 3 | $typeSerializerDef->addMethodCall('setVersion', array($type['serializer']['version'])); |
|
314 | } |
||
315 | |||
316 | 3 | $typeDef->addMethodCall('setSerializer', array(array(new Reference($typeSerializerId), 'serialize'))); |
|
317 | 14 | $container->setDefinition($typeSerializerId, $typeSerializerDef); |
|
318 | } |
||
319 | } |
||
320 | 14 | } |
|
321 | |||
322 | /** |
||
323 | * Loads the optional provider and finder for a type. |
||
324 | * |
||
325 | * @param array $typeConfig |
||
326 | * @param ContainerBuilder $container |
||
327 | * @param Reference $typeRef |
||
328 | * @param string $indexName |
||
329 | * @param string $typeName |
||
330 | */ |
||
331 | 10 | private function loadTypePersistenceIntegration(array $typeConfig, ContainerBuilder $container, Reference $typeRef, $indexName, $typeName) |
|
351 | |||
352 | /** |
||
353 | * Creates and loads an ElasticaToModelTransformer. |
||
354 | * |
||
355 | * @param array $typeConfig |
||
356 | * @param ContainerBuilder $container |
||
357 | * @param string $indexName |
||
358 | * @param string $typeName |
||
359 | * |
||
360 | * @return string |
||
361 | */ |
||
362 | 10 | private function loadElasticaToModelTransformer(array $typeConfig, ContainerBuilder $container, $indexName, $typeName) |
|
387 | |||
388 | /** |
||
389 | * Creates and loads a ModelToElasticaTransformer for an index/type. |
||
390 | * |
||
391 | * @param array $typeConfig |
||
392 | * @param ContainerBuilder $container |
||
393 | * @param string $indexName |
||
394 | * @param string $typeName |
||
395 | * |
||
396 | * @return string |
||
397 | */ |
||
398 | 10 | private function loadModelToElasticaTransformer(array $typeConfig, ContainerBuilder $container, $indexName, $typeName) |
|
417 | |||
418 | /** |
||
419 | * Creates and loads an object persister for a type. |
||
420 | * |
||
421 | * @param array $typeConfig |
||
422 | * @param Reference $typeRef |
||
423 | * @param ContainerBuilder $container |
||
424 | * @param string $indexName |
||
425 | * @param string $typeName |
||
426 | * @param string $transformerId |
||
427 | * |
||
428 | * @return string |
||
429 | */ |
||
430 | 10 | private function loadObjectPersister(array $typeConfig, Reference $typeRef, ContainerBuilder $container, $indexName, $typeName, $transformerId) |
|
466 | |||
467 | /** |
||
468 | * Loads a provider for a type. |
||
469 | * |
||
470 | * @param array $typeConfig |
||
471 | * @param ContainerBuilder $container |
||
472 | * @param string $objectPersisterId |
||
473 | * @param string $indexName |
||
474 | * @param string $typeName |
||
475 | * |
||
476 | * @return string |
||
477 | */ |
||
478 | 10 | private function loadTypeProvider(array $typeConfig, ContainerBuilder $container, $objectPersisterId, $indexName, $typeName) |
|
501 | |||
502 | /** |
||
503 | * Loads doctrine listeners to handle indexing of new or updated objects. |
||
504 | * |
||
505 | * @param array $typeConfig |
||
506 | * @param ContainerBuilder $container |
||
507 | * @param string $objectPersisterId |
||
508 | * @param string $indexName |
||
509 | * @param string $typeName |
||
510 | * |
||
511 | * @return string |
||
512 | */ |
||
513 | 10 | private function loadTypeListener(array $typeConfig, ContainerBuilder $container, $objectPersisterId, $indexName, $typeName) |
|
559 | |||
560 | /** |
||
561 | * Map Elastica to Doctrine events for the current driver. |
||
562 | */ |
||
563 | 10 | private function getDoctrineEvents(array $typeConfig) |
|
595 | |||
596 | /** |
||
597 | * Loads a Type specific Finder. |
||
598 | * |
||
599 | * @param array $typeConfig |
||
600 | * @param ContainerBuilder $container |
||
601 | * @param string $elasticaToModelId |
||
602 | * @param Reference $typeRef |
||
603 | * @param string $indexName |
||
604 | * @param string $typeName |
||
605 | * |
||
606 | * @return string |
||
607 | */ |
||
608 | 10 | private function loadTypeFinder(array $typeConfig, ContainerBuilder $container, $elasticaToModelId, Reference $typeRef, $indexName, $typeName) |
|
630 | |||
631 | /** |
||
632 | * Loads the index manager. |
||
633 | * |
||
634 | * @param ContainerBuilder $container |
||
635 | **/ |
||
636 | private function loadIndexManager(ContainerBuilder $container) |
||
645 | |||
646 | /** |
||
647 | * Makes sure a specific driver has been loaded. |
||
648 | * |
||
649 | * @param ContainerBuilder $container |
||
650 | * @param string $driver |
||
651 | */ |
||
652 | 10 | private function loadDriver(ContainerBuilder $container, $driver) |
|
662 | |||
663 | /** |
||
664 | * Loads and configures the serializer prototype. |
||
665 | * |
||
666 | * @param array $config |
||
667 | * @param ContainerBuilder $container |
||
668 | */ |
||
669 | 3 | private function loadSerializer($config, ContainerBuilder $container) |
|
681 | |||
682 | /** |
||
683 | * Creates a default manager alias for defined default manager or the first loaded driver. |
||
684 | * |
||
685 | * @param string $defaultManager |
||
686 | * @param ContainerBuilder $container |
||
687 | */ |
||
688 | 14 | private function createDefaultManagerAlias($defaultManager, ContainerBuilder $container) |
|
704 | |||
705 | /** |
||
706 | * Returns a reference to a client given its configured name. |
||
707 | * |
||
708 | * @param string $clientName |
||
709 | * |
||
710 | * @return Reference |
||
711 | * |
||
712 | * @throws \InvalidArgumentException |
||
713 | */ |
||
714 | 2 | private function getClient($clientName) |
|
722 | } |
||
723 |
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.