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:
1 | <?php |
||
18 | class Configuration implements ConfigurationInterface |
||
19 | { |
||
20 | /** |
||
21 | * Stores supported database drivers. |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | private $supportedDrivers = ['orm', 'mongodb', 'propel', 'phpcr']; |
||
26 | |||
27 | /** |
||
28 | * If the kernel is running in debug mode. |
||
29 | * |
||
30 | * @var bool |
||
31 | */ |
||
32 | private $debug; |
||
33 | |||
34 | 25 | public function __construct($debug) |
|
38 | |||
39 | /** |
||
40 | * Generates the configuration tree. |
||
41 | * |
||
42 | * @return TreeBuilder |
||
43 | */ |
||
44 | 25 | public function getConfigTreeBuilder() |
|
73 | |||
74 | /** |
||
75 | * Returns the array node used for "dynamic_templates". |
||
76 | */ |
||
77 | 25 | public function getDynamicTemplateNode() |
|
104 | |||
105 | /** |
||
106 | * Returns the array node used for "types". |
||
107 | */ |
||
108 | 25 | protected function getTypesNode() |
|
109 | { |
||
110 | 25 | $builder = new TreeBuilder(); |
|
111 | 25 | $node = $builder->root('types'); |
|
112 | |||
113 | $node |
||
114 | 25 | ->useAttributeAsKey('name') |
|
115 | 25 | ->prototype('array') |
|
116 | 25 | ->treatNullLike([]) |
|
117 | 25 | ->beforeNormalization() |
|
118 | 25 | ->ifNull() |
|
119 | 25 | ->thenEmptyArray() |
|
120 | 25 | ->end() |
|
121 | // Support multiple dynamic_template formats to match the old bundle style |
||
122 | // and the way ElasticSearch expects them |
||
123 | 25 | ->beforeNormalization() |
|
124 | ->ifTrue(function ($v) { |
||
125 | 16 | return isset($v['dynamic_templates']); |
|
126 | 25 | }) |
|
127 | ->then(function ($v) { |
||
128 | 1 | $dt = []; |
|
129 | 1 | foreach ($v['dynamic_templates'] as $key => $type) { |
|
130 | 1 | if (is_int($key)) { |
|
131 | 1 | $dt[] = $type; |
|
132 | 1 | } else { |
|
133 | 1 | $dt[][$key] = $type; |
|
134 | } |
||
135 | 1 | } |
|
136 | |||
137 | 1 | $v['dynamic_templates'] = $dt; |
|
138 | |||
139 | 1 | return $v; |
|
140 | 25 | }) |
|
141 | 25 | ->end() |
|
142 | 25 | ->children() |
|
143 | 25 | ->booleanNode('date_detection')->end() |
|
144 | 25 | ->arrayNode('dynamic_date_formats')->prototype('scalar')->end()->end() |
|
145 | 25 | ->scalarNode('analyzer')->end() |
|
146 | 25 | ->booleanNode('numeric_detection')->end() |
|
147 | 25 | ->scalarNode('dynamic')->end() |
|
148 | 25 | ->variableNode('indexable_callback')->end() |
|
149 | 25 | ->append($this->getPersistenceNode()) |
|
150 | 25 | ->append($this->getSerializerNode()) |
|
151 | 25 | ->end() |
|
152 | 25 | ->append($this->getIdNode()) |
|
153 | 25 | ->append($this->getPropertiesNode()) |
|
154 | 25 | ->append($this->getDynamicTemplateNode()) |
|
155 | 25 | ->append($this->getSourceNode()) |
|
156 | 25 | ->append($this->getRoutingNode()) |
|
157 | 25 | ->append($this->getParentNode()) |
|
158 | 25 | ->append($this->getAllNode()) |
|
159 | 25 | ->end() |
|
160 | ; |
||
161 | |||
162 | 25 | return $node; |
|
163 | } |
||
164 | |||
165 | /** |
||
166 | * Returns the array node used for "properties". |
||
167 | */ |
||
168 | 25 | protected function getPropertiesNode() |
|
180 | |||
181 | /** |
||
182 | * Returns the array node used for "_id". |
||
183 | */ |
||
184 | 25 | View Code Duplication | protected function getIdNode() |
197 | |||
198 | /** |
||
199 | * Returns the array node used for "_source". |
||
200 | */ |
||
201 | 25 | protected function getSourceNode() |
|
224 | |||
225 | /** |
||
226 | * Returns the array node used for "_routing". |
||
227 | */ |
||
228 | 25 | View Code Duplication | protected function getRoutingNode() |
229 | { |
||
230 | 25 | $builder = new TreeBuilder(); |
|
231 | 25 | $node = $builder->root('_routing'); |
|
232 | |||
233 | $node |
||
234 | 25 | ->children() |
|
235 | 25 | ->scalarNode('required')->end() |
|
236 | 25 | ->scalarNode('path')->end() |
|
237 | 25 | ->end() |
|
238 | ; |
||
239 | |||
240 | 25 | return $node; |
|
241 | } |
||
242 | |||
243 | /** |
||
244 | * Returns the array node used for "_parent". |
||
245 | */ |
||
246 | 25 | protected function getParentNode() |
|
261 | |||
262 | /** |
||
263 | * Returns the array node used for "_all". |
||
264 | */ |
||
265 | 25 | protected function getAllNode() |
|
279 | |||
280 | /** |
||
281 | * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition |
||
282 | */ |
||
283 | 25 | protected function getPersistenceNode() |
|
284 | { |
||
285 | 25 | $builder = new TreeBuilder(); |
|
286 | 25 | $node = $builder->root('persistence'); |
|
287 | |||
288 | $node |
||
289 | 25 | ->validate() |
|
290 | View Code Duplication | ->ifTrue(function ($v) { |
|
291 | return isset($v['driver']) && 'propel' === $v['driver'] && isset($v['listener']); |
||
292 | 25 | }) |
|
293 | 25 | ->thenInvalid('Propel doesn\'t support listeners') |
|
294 | View Code Duplication | ->ifTrue(function ($v) { |
|
295 | return isset($v['driver']) && 'propel' === $v['driver'] && isset($v['repository']); |
||
296 | 25 | }) |
|
297 | 25 | ->thenInvalid('Propel doesn\'t support the "repository" parameter') |
|
298 | ->ifTrue(function ($v) { |
||
299 | 14 | return isset($v['driver']) && 'orm' !== $v['driver'] && !empty($v['elastica_to_model_transformer']['hints']); |
|
300 | 25 | }) |
|
301 | 25 | ->thenInvalid('Hints are only supported by the "orm" driver') |
|
302 | 25 | ->end() |
|
303 | 25 | ->children() |
|
304 | 25 | ->scalarNode('driver') |
|
305 | 25 | ->defaultValue('orm') |
|
306 | 25 | ->validate() |
|
307 | 25 | ->ifNotInArray($this->supportedDrivers) |
|
308 | 25 | ->thenInvalid('The driver %s is not supported. Please choose one of '.json_encode($this->supportedDrivers)) |
|
309 | 25 | ->end() |
|
310 | 25 | ->end() |
|
311 | 25 | ->scalarNode('model')->defaultValue(null)->end() |
|
312 | 25 | ->scalarNode('repository')->end() |
|
313 | 25 | ->scalarNode('identifier')->defaultValue('id')->end() |
|
314 | 25 | ->arrayNode('provider') |
|
315 | 25 | ->addDefaultsIfNotSet() |
|
316 | 25 | ->children() |
|
317 | 25 | ->scalarNode('batch_size')->defaultValue(100)->end() |
|
318 | 25 | ->scalarNode('clear_object_manager')->defaultTrue()->end() |
|
319 | 25 | ->scalarNode('debug_logging') |
|
320 | 25 | ->defaultValue($this->debug) |
|
321 | 25 | ->treatNullLike(true) |
|
322 | 25 | ->end() |
|
323 | 25 | ->scalarNode('query_builder_method')->defaultValue('createQueryBuilder')->end() |
|
324 | 25 | ->scalarNode('service')->end() |
|
325 | 25 | ->end() |
|
326 | 25 | ->end() |
|
327 | 25 | ->arrayNode('listener') |
|
328 | 25 | ->addDefaultsIfNotSet() |
|
329 | 25 | ->children() |
|
330 | 25 | ->scalarNode('insert')->defaultTrue()->end() |
|
331 | 25 | ->scalarNode('update')->defaultTrue()->end() |
|
332 | 25 | ->scalarNode('delete')->defaultTrue()->end() |
|
333 | 25 | ->scalarNode('flush')->defaultTrue()->end() |
|
334 | 25 | ->booleanNode('defer')->defaultFalse()->end() |
|
335 | 25 | ->scalarNode('logger') |
|
336 | 25 | ->defaultFalse() |
|
337 | 25 | ->treatNullLike('fos_elastica.logger') |
|
338 | 25 | ->treatTrueLike('fos_elastica.logger') |
|
339 | 25 | ->end() |
|
340 | 25 | ->scalarNode('service')->end() |
|
341 | 25 | ->end() |
|
342 | 25 | ->end() |
|
343 | 25 | ->arrayNode('finder') |
|
344 | 25 | ->addDefaultsIfNotSet() |
|
345 | 25 | ->children() |
|
346 | 25 | ->scalarNode('service')->end() |
|
347 | 25 | ->end() |
|
348 | 25 | ->end() |
|
349 | 25 | ->arrayNode('elastica_to_model_transformer') |
|
350 | 25 | ->addDefaultsIfNotSet() |
|
351 | 25 | ->children() |
|
352 | 25 | ->arrayNode('hints') |
|
353 | 25 | ->prototype('array') |
|
354 | 25 | ->children() |
|
355 | 25 | ->scalarNode('name')->end() |
|
356 | 25 | ->scalarNode('value')->end() |
|
357 | 25 | ->end() |
|
358 | 25 | ->end() |
|
359 | 25 | ->end() |
|
360 | 25 | ->booleanNode('hydrate')->defaultTrue()->end() |
|
361 | 25 | ->booleanNode('ignore_missing') |
|
362 | 25 | ->defaultFalse() |
|
363 | 25 | ->info('Silently ignore results returned from Elasticsearch without corresponding persistent object.') |
|
364 | 25 | ->end() |
|
365 | 25 | ->scalarNode('query_builder_method')->defaultValue('createQueryBuilder')->end() |
|
366 | 25 | ->scalarNode('service')->end() |
|
367 | 25 | ->end() |
|
368 | 25 | ->end() |
|
369 | 25 | ->arrayNode('model_to_elastica_transformer') |
|
370 | 25 | ->addDefaultsIfNotSet() |
|
371 | 25 | ->children() |
|
372 | 25 | ->scalarNode('service')->end() |
|
373 | 25 | ->end() |
|
374 | 25 | ->end() |
|
375 | 25 | ->arrayNode('persister') |
|
376 | 25 | ->addDefaultsIfNotSet() |
|
377 | 25 | ->children() |
|
378 | 25 | ->scalarNode('service')->end() |
|
379 | 25 | ->end() |
|
380 | 25 | ->end() |
|
381 | 25 | ->end(); |
|
382 | |||
383 | 25 | return $node; |
|
384 | } |
||
385 | |||
386 | /** |
||
387 | * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition |
||
388 | */ |
||
389 | 25 | protected function getSerializerNode() |
|
409 | |||
410 | /** |
||
411 | * Adds the configuration for the "clients" key. |
||
412 | */ |
||
413 | 25 | private function addClientsSection(ArrayNodeDefinition $rootNode) |
|
414 | { |
||
415 | $rootNode |
||
416 | 25 | ->fixXmlConfig('client') |
|
417 | 25 | ->children() |
|
418 | 25 | ->arrayNode('clients') |
|
419 | 25 | ->useAttributeAsKey('id') |
|
420 | 25 | ->prototype('array') |
|
421 | 25 | ->performNoDeepMerging() |
|
422 | // Elastica names its properties with camel case, support both |
||
423 | 25 | ->beforeNormalization() |
|
424 | ->ifTrue(function ($v) { |
||
425 | 24 | return isset($v['connection_strategy']); |
|
426 | 25 | }) |
|
427 | ->then(function ($v) { |
||
428 | 1 | $v['connectionStrategy'] = $v['connection_strategy']; |
|
429 | 1 | unset($v['connection_strategy']); |
|
430 | |||
431 | 1 | return $v; |
|
432 | 25 | }) |
|
433 | 25 | ->end() |
|
434 | // If there is no connections array key defined, assume a single connection. |
||
435 | 25 | ->beforeNormalization() |
|
436 | ->ifTrue(function ($v) { |
||
437 | 24 | return is_array($v) && !array_key_exists('connections', $v); |
|
438 | 25 | }) |
|
439 | ->then(function ($v) { |
||
440 | return [ |
||
441 | 24 | 'connections' => [$v], |
|
442 | 24 | ]; |
|
443 | 25 | }) |
|
444 | 25 | ->end() |
|
445 | 25 | ->children() |
|
446 | 25 | ->arrayNode('connections') |
|
447 | 25 | ->requiresAtLeastOneElement() |
|
448 | 25 | ->prototype('array') |
|
449 | 25 | ->fixXmlConfig('header') |
|
450 | 25 | ->children() |
|
451 | 25 | ->scalarNode('url') |
|
452 | 25 | ->validate() |
|
453 | ->ifTrue(function ($url) { |
||
454 | 12 | return $url && '/' !== substr($url, -1); |
|
455 | 25 | }) |
|
456 | 25 | ->then(function ($url) { |
|
457 | 12 | return $url.'/'; |
|
458 | 25 | }) |
|
459 | 25 | ->end() |
|
460 | 25 | ->end() |
|
461 | 25 | ->scalarNode('username')->end() |
|
462 | 25 | ->scalarNode('password')->end() |
|
463 | 25 | ->scalarNode('host')->end() |
|
464 | 25 | ->scalarNode('port')->end() |
|
465 | 25 | ->scalarNode('proxy')->end() |
|
466 | 25 | ->scalarNode('aws_access_key_id')->end() |
|
467 | 25 | ->scalarNode('aws_secret_access_key')->end() |
|
468 | 25 | ->scalarNode('aws_region')->end() |
|
469 | 25 | ->scalarNode('aws_session_token')->end() |
|
470 | 25 | ->booleanNode('ssl')->defaultValue(false)->end() |
|
471 | 25 | ->scalarNode('logger') |
|
472 | 25 | ->defaultValue($this->debug ? 'fos_elastica.logger' : false) |
|
473 | 25 | ->treatNullLike('fos_elastica.logger') |
|
474 | 25 | ->treatTrueLike('fos_elastica.logger') |
|
475 | 25 | ->end() |
|
476 | 25 | ->booleanNode('compression')->defaultValue(false)->end() |
|
477 | 25 | ->arrayNode('headers') |
|
478 | 25 | ->normalizeKeys(false) |
|
479 | 25 | ->useAttributeAsKey('name') |
|
480 | 25 | ->prototype('scalar')->end() |
|
481 | 25 | ->end() |
|
482 | 25 | ->arrayNode('curl') |
|
483 | 25 | ->useAttributeAsKey(CURLOPT_SSL_VERIFYPEER) |
|
484 | 25 | ->prototype('boolean')->end() |
|
485 | 25 | ->end() |
|
486 | 25 | ->scalarNode('transport')->end() |
|
487 | 25 | ->scalarNode('timeout')->end() |
|
488 | 25 | ->scalarNode('connectTimeout')->end() |
|
489 | 25 | ->scalarNode('retryOnConflict') |
|
490 | 25 | ->defaultValue(0) |
|
491 | 25 | ->end() |
|
492 | 25 | ->end() |
|
493 | 25 | ->end() |
|
494 | 25 | ->end() |
|
495 | 25 | ->scalarNode('timeout')->end() |
|
496 | 25 | ->scalarNode('connectTimeout')->end() |
|
497 | 25 | ->scalarNode('headers')->end() |
|
498 | 25 | ->scalarNode('connectionStrategy')->defaultValue('Simple')->end() |
|
499 | 25 | ->end() |
|
500 | 25 | ->end() |
|
501 | 25 | ->end() |
|
502 | 25 | ->end() |
|
503 | ; |
||
504 | 25 | } |
|
505 | |||
506 | /** |
||
507 | * Adds the configuration for the "indexes" key. |
||
508 | */ |
||
509 | 25 | private function addIndexesSection(ArrayNodeDefinition $rootNode) |
|
542 | } |
||
543 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.