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 | 17 | public function __construct($debug) |
|
| 38 | |||
| 39 | /** |
||
| 40 | * Generates the configuration tree. |
||
| 41 | * |
||
| 42 | * @return TreeBuilder |
||
| 43 | */ |
||
| 44 | 17 | public function getConfigTreeBuilder() |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Adds the configuration for the "clients" key. |
||
| 76 | */ |
||
| 77 | 17 | private function addClientsSection(ArrayNodeDefinition $rootNode) |
|
| 78 | { |
||
| 79 | $rootNode |
||
| 80 | 17 | ->fixXmlConfig('client') |
|
| 81 | 17 | ->children() |
|
| 82 | 17 | ->arrayNode('clients') |
|
| 83 | 17 | ->useAttributeAsKey('id') |
|
| 84 | 17 | ->prototype('array') |
|
| 85 | 17 | ->performNoDeepMerging() |
|
| 86 | // Elastica names its properties with camel case, support both |
||
| 87 | 17 | ->beforeNormalization() |
|
| 88 | 17 | ->ifTrue(function ($v) { |
|
| 89 | 16 | return isset($v['connection_strategy']); |
|
| 90 | 17 | }) |
|
| 91 | 17 | ->then(function ($v) { |
|
| 92 | 1 | $v['connectionStrategy'] = $v['connection_strategy']; |
|
| 93 | 1 | unset($v['connection_strategy']); |
|
| 94 | |||
| 95 | 1 | return $v; |
|
| 96 | 17 | }) |
|
| 97 | 17 | ->end() |
|
| 98 | // If there is no connections array key defined, assume a single connection. |
||
| 99 | 17 | ->beforeNormalization() |
|
| 100 | 17 | ->ifTrue(function ($v) { |
|
| 101 | 16 | return is_array($v) && !array_key_exists('connections', $v); |
|
| 102 | 17 | }) |
|
| 103 | 17 | ->then(function ($v) { |
|
| 104 | return [ |
||
| 105 | 16 | 'connections' => [$v], |
|
| 106 | ]; |
||
| 107 | 17 | }) |
|
| 108 | 17 | ->end() |
|
| 109 | 17 | ->children() |
|
| 110 | 17 | ->arrayNode('connections') |
|
| 111 | 17 | ->requiresAtLeastOneElement() |
|
| 112 | 17 | ->prototype('array') |
|
| 113 | 17 | ->fixXmlConfig('header') |
|
| 114 | 17 | ->children() |
|
| 115 | 17 | ->scalarNode('url') |
|
| 116 | 17 | ->validate() |
|
| 117 | 17 | ->ifTrue(function ($url) { |
|
| 118 | 12 | return $url && substr($url, -1) !== '/'; |
|
| 119 | 17 | }) |
|
| 120 | 17 | ->then(function ($url) { |
|
| 121 | 12 | return $url.'/'; |
|
| 122 | 17 | }) |
|
| 123 | 17 | ->end() |
|
| 124 | 17 | ->end() |
|
| 125 | 17 | ->scalarNode('username')->end() |
|
| 126 | 17 | ->scalarNode('password')->end() |
|
| 127 | 17 | ->scalarNode('host')->end() |
|
| 128 | 17 | ->scalarNode('port')->end() |
|
| 129 | 17 | ->scalarNode('proxy')->end() |
|
| 130 | 17 | ->scalarNode('aws_access_key_id')->end() |
|
| 131 | 17 | ->scalarNode('aws_secret_access_key')->end() |
|
| 132 | 17 | ->scalarNode('aws_region')->end() |
|
| 133 | 17 | ->scalarNode('aws_session_token')->end() |
|
| 134 | 17 | ->scalarNode('logger') |
|
| 135 | 17 | ->defaultValue($this->debug ? 'fos_elastica.logger' : false) |
|
| 136 | 17 | ->treatNullLike('fos_elastica.logger') |
|
| 137 | 17 | ->treatTrueLike('fos_elastica.logger') |
|
| 138 | 17 | ->end() |
|
| 139 | 17 | ->booleanNode('compression')->defaultValue(false)->end() |
|
| 140 | 17 | ->arrayNode('headers') |
|
| 141 | 17 | ->normalizeKeys(false) |
|
| 142 | 17 | ->useAttributeAsKey('name') |
|
| 143 | 17 | ->prototype('scalar')->end() |
|
| 144 | 17 | ->end() |
|
| 145 | 17 | ->arrayNode('curl') |
|
| 146 | 17 | ->useAttributeAsKey(CURLOPT_SSL_VERIFYPEER) |
|
| 147 | 17 | ->prototype('boolean')->end() |
|
| 148 | 17 | ->end() |
|
| 149 | 17 | ->scalarNode('transport')->end() |
|
| 150 | 17 | ->scalarNode('timeout')->end() |
|
| 151 | 17 | ->scalarNode('connectTimeout')->end() |
|
| 152 | 17 | ->scalarNode('retryOnConflict') |
|
| 153 | 17 | ->defaultValue(0) |
|
| 154 | 17 | ->end() |
|
| 155 | 17 | ->end() |
|
| 156 | 17 | ->end() |
|
| 157 | 17 | ->end() |
|
| 158 | 17 | ->scalarNode('timeout')->end() |
|
| 159 | 17 | ->scalarNode('connectTimeout')->end() |
|
| 160 | 17 | ->scalarNode('headers')->end() |
|
| 161 | 17 | ->scalarNode('connectionStrategy')->defaultValue('Simple')->end() |
|
| 162 | 17 | ->end() |
|
| 163 | 17 | ->end() |
|
| 164 | 17 | ->end() |
|
| 165 | 17 | ->end() |
|
| 166 | ; |
||
| 167 | 17 | } |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Adds the configuration for the "indexes" key. |
||
| 171 | */ |
||
| 172 | 17 | private function addIndexesSection(ArrayNodeDefinition $rootNode) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Returns the array node used for "types". |
||
| 208 | */ |
||
| 209 | 17 | protected function getTypesNode() |
|
| 210 | { |
||
| 211 | 17 | $builder = new TreeBuilder(); |
|
| 212 | 17 | $node = $builder->root('types'); |
|
| 213 | |||
| 214 | $node |
||
| 215 | 17 | ->useAttributeAsKey('name') |
|
| 216 | 17 | ->prototype('array') |
|
| 217 | 17 | ->treatNullLike([]) |
|
| 218 | 17 | ->beforeNormalization() |
|
| 219 | 17 | ->ifNull() |
|
| 220 | 17 | ->thenEmptyArray() |
|
| 221 | 17 | ->end() |
|
| 222 | // Support multiple dynamic_template formats to match the old bundle style |
||
| 223 | // and the way ElasticSearch expects them |
||
| 224 | 17 | ->beforeNormalization() |
|
| 225 | 17 | ->ifTrue(function ($v) { |
|
| 226 | 8 | return isset($v['dynamic_templates']); |
|
| 227 | 17 | }) |
|
| 228 | 17 | ->then(function ($v) { |
|
| 229 | 1 | $dt = []; |
|
| 230 | 1 | foreach ($v['dynamic_templates'] as $key => $type) { |
|
| 231 | 1 | if (is_int($key)) { |
|
| 232 | 1 | $dt[] = $type; |
|
| 233 | } else { |
||
| 234 | 1 | $dt[][$key] = $type; |
|
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | 1 | $v['dynamic_templates'] = $dt; |
|
| 239 | |||
| 240 | 1 | return $v; |
|
| 241 | 17 | }) |
|
| 242 | 17 | ->end() |
|
| 243 | 17 | ->children() |
|
| 244 | 17 | ->booleanNode('date_detection')->end() |
|
| 245 | 17 | ->arrayNode('dynamic_date_formats')->prototype('scalar')->end()->end() |
|
| 246 | 17 | ->scalarNode('analyzer')->end() |
|
| 247 | 17 | ->booleanNode('numeric_detection')->end() |
|
| 248 | 17 | ->scalarNode('dynamic')->end() |
|
| 249 | 17 | ->variableNode('indexable_callback')->end() |
|
| 250 | 17 | ->append($this->getPersistenceNode()) |
|
| 251 | 17 | ->append($this->getSerializerNode()) |
|
| 252 | 17 | ->end() |
|
| 253 | 17 | ->append($this->getIdNode()) |
|
| 254 | 17 | ->append($this->getPropertiesNode()) |
|
| 255 | 17 | ->append($this->getDynamicTemplateNode()) |
|
| 256 | 17 | ->append($this->getSourceNode()) |
|
| 257 | 17 | ->append($this->getRoutingNode()) |
|
| 258 | 17 | ->append($this->getParentNode()) |
|
| 259 | 17 | ->append($this->getAllNode()) |
|
| 260 | 17 | ->end() |
|
| 261 | ; |
||
| 262 | |||
| 263 | 17 | return $node; |
|
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Returns the array node used for "properties". |
||
| 268 | */ |
||
| 269 | 17 | protected function getPropertiesNode() |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Returns the array node used for "dynamic_templates". |
||
| 284 | */ |
||
| 285 | 17 | public function getDynamicTemplateNode() |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Returns the array node used for "_id". |
||
| 315 | */ |
||
| 316 | 17 | View Code Duplication | protected function getIdNode() |
| 329 | |||
| 330 | /** |
||
| 331 | * Returns the array node used for "_source". |
||
| 332 | */ |
||
| 333 | 17 | protected function getSourceNode() |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Returns the array node used for "_routing". |
||
| 359 | */ |
||
| 360 | 17 | View Code Duplication | protected function getRoutingNode() |
| 374 | |||
| 375 | /** |
||
| 376 | * Returns the array node used for "_parent". |
||
| 377 | */ |
||
| 378 | 17 | protected function getParentNode() |
|
| 393 | |||
| 394 | /** |
||
| 395 | * Returns the array node used for "_all". |
||
| 396 | */ |
||
| 397 | 17 | protected function getAllNode() |
|
| 411 | |||
| 412 | /** |
||
| 413 | * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition |
||
| 414 | */ |
||
| 415 | 17 | protected function getPersistenceNode() |
|
| 416 | { |
||
| 417 | 17 | $builder = new TreeBuilder(); |
|
| 418 | 17 | $node = $builder->root('persistence'); |
|
| 419 | |||
| 420 | $node |
||
| 421 | 17 | ->validate() |
|
| 422 | 17 | View Code Duplication | ->ifTrue(function ($v) { |
| 423 | return isset($v['driver']) && 'propel' === $v['driver'] && isset($v['listener']); |
||
| 424 | 17 | }) |
|
| 425 | 17 | ->thenInvalid('Propel doesn\'t support listeners') |
|
| 426 | 17 | View Code Duplication | ->ifTrue(function ($v) { |
| 427 | return isset($v['driver']) && 'propel' === $v['driver'] && isset($v['repository']); |
||
| 428 | 17 | }) |
|
| 429 | 17 | ->thenInvalid('Propel doesn\'t support the "repository" parameter') |
|
| 430 | 17 | ->ifTrue(function ($v) { |
|
| 431 | 6 | return isset($v['driver']) && 'orm' !== $v['driver'] && !empty($v['elastica_to_model_transformer']['hints']); |
|
| 432 | 17 | }) |
|
| 433 | 17 | ->thenInvalid('Hints are only supported by the "orm" driver') |
|
| 434 | 17 | ->end() |
|
| 435 | 17 | ->children() |
|
| 436 | 17 | ->scalarNode('driver') |
|
| 437 | 17 | ->defaultValue('orm') |
|
| 438 | 17 | ->validate() |
|
| 439 | 17 | ->ifNotInArray($this->supportedDrivers) |
|
| 440 | 17 | ->thenInvalid('The driver %s is not supported. Please choose one of '.json_encode($this->supportedDrivers)) |
|
| 441 | 17 | ->end() |
|
| 442 | 17 | ->end() |
|
| 443 | 17 | ->scalarNode('model')->defaultValue(null)->end() |
|
| 444 | 17 | ->scalarNode('repository')->end() |
|
| 445 | 17 | ->scalarNode('identifier')->defaultValue('id')->end() |
|
| 446 | 17 | ->arrayNode('provider') |
|
| 447 | 17 | ->addDefaultsIfNotSet() |
|
| 448 | 17 | ->children() |
|
| 449 | 17 | ->scalarNode('batch_size')->defaultValue(100)->end() |
|
| 450 | 17 | ->scalarNode('clear_object_manager')->defaultTrue()->end() |
|
| 451 | 17 | ->scalarNode('debug_logging') |
|
| 452 | 17 | ->defaultValue($this->debug) |
|
| 453 | 17 | ->treatNullLike(true) |
|
| 454 | 17 | ->end() |
|
| 455 | 17 | ->scalarNode('query_builder_method')->defaultValue('createQueryBuilder')->end() |
|
| 456 | 17 | ->scalarNode('service')->end() |
|
| 457 | 17 | ->end() |
|
| 458 | 17 | ->end() |
|
| 459 | 17 | ->arrayNode('listener') |
|
| 460 | 17 | ->addDefaultsIfNotSet() |
|
| 461 | 17 | ->children() |
|
| 462 | 17 | ->scalarNode('insert')->defaultTrue()->end() |
|
| 463 | 17 | ->scalarNode('update')->defaultTrue()->end() |
|
| 464 | 17 | ->scalarNode('delete')->defaultTrue()->end() |
|
| 465 | 17 | ->scalarNode('flush')->defaultTrue()->end() |
|
| 466 | 17 | ->booleanNode('defer')->defaultFalse()->end() |
|
| 467 | 17 | ->scalarNode('logger') |
|
| 468 | 17 | ->defaultFalse() |
|
| 469 | 17 | ->treatNullLike('fos_elastica.logger') |
|
| 470 | 17 | ->treatTrueLike('fos_elastica.logger') |
|
| 471 | 17 | ->end() |
|
| 472 | 17 | ->scalarNode('service')->end() |
|
| 473 | 17 | ->end() |
|
| 474 | 17 | ->end() |
|
| 475 | 17 | ->arrayNode('finder') |
|
| 476 | 17 | ->addDefaultsIfNotSet() |
|
| 477 | 17 | ->children() |
|
| 478 | 17 | ->scalarNode('service')->end() |
|
| 479 | 17 | ->end() |
|
| 480 | 17 | ->end() |
|
| 481 | 17 | ->arrayNode('elastica_to_model_transformer') |
|
| 482 | 17 | ->addDefaultsIfNotSet() |
|
| 483 | 17 | ->children() |
|
| 484 | 17 | ->arrayNode('hints') |
|
| 485 | 17 | ->prototype('array') |
|
| 486 | 17 | ->children() |
|
| 487 | 17 | ->scalarNode('name')->end() |
|
| 488 | 17 | ->scalarNode('value')->end() |
|
| 489 | 17 | ->end() |
|
| 490 | 17 | ->end() |
|
| 491 | 17 | ->end() |
|
| 492 | 17 | ->booleanNode('hydrate')->defaultTrue()->end() |
|
| 493 | 17 | ->booleanNode('ignore_missing') |
|
| 494 | 17 | ->defaultFalse() |
|
| 495 | 17 | ->info('Silently ignore results returned from Elasticsearch without corresponding persistent object.') |
|
| 496 | 17 | ->end() |
|
| 497 | 17 | ->scalarNode('query_builder_method')->defaultValue('createQueryBuilder')->end() |
|
| 498 | 17 | ->scalarNode('service')->end() |
|
| 499 | 17 | ->end() |
|
| 500 | 17 | ->end() |
|
| 501 | 17 | ->arrayNode('model_to_elastica_transformer') |
|
| 502 | 17 | ->addDefaultsIfNotSet() |
|
| 503 | 17 | ->children() |
|
| 504 | 17 | ->scalarNode('service')->end() |
|
| 505 | 17 | ->end() |
|
| 506 | 17 | ->end() |
|
| 507 | 17 | ->arrayNode('persister') |
|
| 508 | 17 | ->addDefaultsIfNotSet() |
|
| 509 | 17 | ->children() |
|
| 510 | 17 | ->scalarNode('service')->end() |
|
| 511 | 17 | ->end() |
|
| 512 | 17 | ->end() |
|
| 513 | 17 | ->end(); |
|
| 514 | |||
| 515 | 17 | return $node; |
|
| 516 | } |
||
| 517 | |||
| 518 | /** |
||
| 519 | * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition |
||
| 520 | */ |
||
| 521 | 17 | protected function getSerializerNode() |
|
| 541 | } |
||
| 542 |
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.