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 |
||
| 9 | class Configuration implements ConfigurationInterface |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Stores supported database drivers. |
||
| 13 | * |
||
| 14 | * @var array |
||
| 15 | */ |
||
| 16 | private $supportedDrivers = array('orm', 'mongodb', 'propel', 'phpcr'); |
||
| 17 | |||
| 18 | /** |
||
| 19 | * If the kernel is running in debug mode. |
||
| 20 | * |
||
| 21 | * @var bool |
||
| 22 | */ |
||
| 23 | private $debug; |
||
| 24 | |||
| 25 | 27 | public function __construct($debug) |
|
| 29 | |||
| 30 | /** |
||
| 31 | * Generates the configuration tree. |
||
| 32 | * |
||
| 33 | * @return TreeBuilder |
||
| 34 | */ |
||
| 35 | 27 | public function getConfigTreeBuilder() |
|
| 64 | |||
| 65 | /** |
||
| 66 | * Adds the configuration for the "clients" key. |
||
| 67 | */ |
||
| 68 | 27 | private function addClientsSection(ArrayNodeDefinition $rootNode) |
|
| 69 | { |
||
| 70 | $rootNode |
||
| 71 | 27 | ->fixXmlConfig('client') |
|
| 72 | 27 | ->children() |
|
| 73 | 27 | ->arrayNode('clients') |
|
| 74 | 27 | ->useAttributeAsKey('id') |
|
| 75 | 27 | ->prototype('array') |
|
| 76 | 27 | ->performNoDeepMerging() |
|
| 77 | // BC - Renaming 'servers' node to 'connections' |
||
| 78 | 27 | ->beforeNormalization() |
|
| 79 | ->ifTrue(function ($v) { return isset($v['servers']); }) |
||
| 80 | ->then(function ($v) { |
||
| 81 | $v['connections'] = $v['servers']; |
||
| 82 | unset($v['servers']); |
||
| 83 | |||
| 84 | return $v; |
||
| 85 | 27 | }) |
|
| 86 | 27 | ->end() |
|
| 87 | // Elastica names its properties with camel case, support both |
||
| 88 | 27 | ->beforeNormalization() |
|
| 89 | ->ifTrue(function ($v) { return isset($v['connection_strategy']); }) |
||
| 90 | ->then(function ($v) { |
||
| 91 | 4 | $v['connectionStrategy'] = $v['connection_strategy']; |
|
| 92 | 4 | unset($v['connection_strategy']); |
|
| 93 | |||
| 94 | 4 | return $v; |
|
| 95 | 27 | }) |
|
| 96 | 27 | ->end() |
|
| 97 | // If there is no connections array key defined, assume a single connection. |
||
| 98 | 27 | ->beforeNormalization() |
|
| 99 | ->ifTrue(function ($v) { return is_array($v) && !array_key_exists('connections', $v); }) |
||
| 100 | ->then(function ($v) { |
||
| 101 | return array( |
||
| 102 | 26 | 'connections' => array($v), |
|
| 103 | 26 | ); |
|
| 104 | 27 | }) |
|
| 105 | 27 | ->end() |
|
| 106 | 27 | ->children() |
|
| 107 | 27 | ->arrayNode('connections') |
|
| 108 | 27 | ->requiresAtLeastOneElement() |
|
| 109 | 27 | ->prototype('array') |
|
| 110 | 27 | ->fixXmlConfig('header') |
|
| 111 | 27 | ->children() |
|
| 112 | 27 | ->scalarNode('url') |
|
| 113 | 27 | ->validate() |
|
| 114 | ->ifTrue(function ($url) { return $url && substr($url, -1) !== '/'; }) |
||
| 115 | ->then(function ($url) { return $url.'/'; }) |
||
| 116 | 27 | ->end() |
|
| 117 | 27 | ->end() |
|
| 118 | 27 | ->scalarNode('host')->end() |
|
| 119 | 27 | ->scalarNode('port')->end() |
|
| 120 | 27 | ->scalarNode('proxy')->end() |
|
| 121 | 27 | ->scalarNode('aws_access_key_id')->end() |
|
| 122 | 27 | ->scalarNode('aws_secret_access_key')->end() |
|
| 123 | 27 | ->scalarNode('aws_region')->end() |
|
| 124 | 27 | ->scalarNode('aws_session_token')->end() |
|
| 125 | 27 | ->scalarNode('logger') |
|
| 126 | 27 | ->defaultValue($this->debug ? 'fos_elastica.logger' : false) |
|
| 127 | 27 | ->treatNullLike('fos_elastica.logger') |
|
| 128 | 27 | ->treatTrueLike('fos_elastica.logger') |
|
| 129 | 27 | ->end() |
|
| 130 | 27 | ->booleanNode('compression')->defaultValue(false)->end() |
|
| 131 | 27 | ->arrayNode('headers') |
|
| 132 | 27 | ->useAttributeAsKey('name') |
|
| 133 | 27 | ->prototype('scalar')->end() |
|
| 134 | 27 | ->end() |
|
| 135 | 27 | ->scalarNode('transport')->end() |
|
| 136 | 27 | ->scalarNode('timeout')->end() |
|
| 137 | 27 | ->scalarNode('connectTimeout')->end() |
|
| 138 | 27 | ->scalarNode('retryOnConflict') |
|
| 139 | 27 | ->defaultValue(0) |
|
| 140 | 27 | ->end() |
|
| 141 | 27 | ->end() |
|
| 142 | 27 | ->end() |
|
| 143 | 27 | ->end() |
|
| 144 | 27 | ->scalarNode('timeout')->end() |
|
| 145 | 27 | ->scalarNode('connectTimeout')->end() |
|
| 146 | 27 | ->scalarNode('headers')->end() |
|
| 147 | 27 | ->scalarNode('connectionStrategy')->defaultValue('Simple')->end() |
|
| 148 | 27 | ->end() |
|
| 149 | 27 | ->end() |
|
| 150 | 27 | ->end() |
|
| 151 | 27 | ->end() |
|
| 152 | ; |
||
| 153 | 27 | } |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Adds the configuration for the "indexes" key. |
||
| 157 | */ |
||
| 158 | 27 | private function addIndexesSection(ArrayNodeDefinition $rootNode) |
|
| 159 | { |
||
| 160 | $rootNode |
||
|
|
|||
| 161 | 27 | ->fixXmlConfig('index') |
|
| 162 | 27 | ->children() |
|
| 163 | 27 | ->arrayNode('indexes') |
|
| 164 | 27 | ->useAttributeAsKey('name') |
|
| 165 | 27 | ->prototype('array') |
|
| 166 | 27 | ->children() |
|
| 167 | 27 | ->scalarNode('index_name') |
|
| 168 | 27 | ->info('Defaults to the name of the index, but can be modified if the index name is different in ElasticSearch') |
|
| 169 | 27 | ->end() |
|
| 170 | 27 | ->booleanNode('use_alias')->defaultValue(false)->end() |
|
| 171 | 27 | ->scalarNode('client')->end() |
|
| 172 | 27 | ->scalarNode('finder') |
|
| 173 | 27 | ->treatNullLike(true) |
|
| 174 | 27 | ->defaultFalse() |
|
| 175 | 27 | ->end() |
|
| 176 | 27 | ->arrayNode('type_prototype') |
|
| 177 | 27 | ->children() |
|
| 178 | 27 | ->scalarNode('analyzer')->end() |
|
| 179 | 27 | ->append($this->getPersistenceNode()) |
|
| 180 | 27 | ->append($this->getSerializerNode()) |
|
| 181 | 27 | ->end() |
|
| 182 | 27 | ->end() |
|
| 183 | 27 | ->variableNode('settings')->defaultValue(array())->end() |
|
| 184 | 27 | ->end() |
|
| 185 | 27 | ->append($this->getTypesNode()) |
|
| 186 | 27 | ->end() |
|
| 187 | 27 | ->end() |
|
| 188 | 27 | ->end() |
|
| 189 | ; |
||
| 190 | 27 | } |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Returns the array node used for "types". |
||
| 194 | */ |
||
| 195 | 27 | protected function getTypesNode() |
|
| 196 | { |
||
| 197 | 27 | $builder = new TreeBuilder(); |
|
| 198 | 27 | $node = $builder->root('types'); |
|
| 199 | |||
| 200 | $node |
||
| 201 | 27 | ->useAttributeAsKey('name') |
|
| 202 | 27 | ->prototype('array') |
|
| 203 | 27 | ->treatNullLike(array()) |
|
| 204 | 27 | ->beforeNormalization() |
|
| 205 | 27 | ->ifNull() |
|
| 206 | 27 | ->thenEmptyArray() |
|
| 207 | 27 | ->end() |
|
| 208 | // BC - Renaming 'mappings' node to 'properties' |
||
| 209 | 27 | ->beforeNormalization() |
|
| 210 | ->ifTrue(function ($v) { return array_key_exists('mappings', $v); }) |
||
| 211 | ->then(function ($v) { |
||
| 212 | 13 | $v['properties'] = $v['mappings']; |
|
| 213 | 13 | unset($v['mappings']); |
|
| 214 | |||
| 215 | 13 | return $v; |
|
| 216 | 27 | }) |
|
| 217 | 27 | ->end() |
|
| 218 | // BC - Support the old is_indexable_callback property |
||
| 219 | 27 | ->beforeNormalization() |
|
| 220 | ->ifTrue(function ($v) { |
||
| 221 | 18 | return isset($v['persistence']) && |
|
| 222 | 18 | isset($v['persistence']['listener']) && |
|
| 223 | 18 | isset($v['persistence']['listener']['is_indexable_callback']); |
|
| 224 | 27 | }) |
|
| 225 | ->then(function ($v) { |
||
| 226 | 5 | $callback = $v['persistence']['listener']['is_indexable_callback']; |
|
| 227 | |||
| 228 | 5 | if (is_array($callback)) { |
|
| 229 | 5 | list($class) = $callback + array(null); |
|
| 230 | |||
| 231 | 5 | if ($class[0] !== '@' && is_string($class) && !class_exists($class)) { |
|
| 232 | $callback[0] = '@'.$class; |
||
| 233 | } |
||
| 234 | 5 | } |
|
| 235 | |||
| 236 | 5 | $v['indexable_callback'] = $callback; |
|
| 237 | 5 | unset($v['persistence']['listener']['is_indexable_callback']); |
|
| 238 | |||
| 239 | 5 | return $v; |
|
| 240 | 27 | }) |
|
| 241 | 27 | ->end() |
|
| 242 | // Support multiple dynamic_template formats to match the old bundle style |
||
| 243 | // and the way ElasticSearch expects them |
||
| 244 | 27 | ->beforeNormalization() |
|
| 245 | ->ifTrue(function ($v) { return isset($v['dynamic_templates']); }) |
||
| 246 | ->then(function ($v) { |
||
| 247 | 4 | $dt = array(); |
|
| 248 | 4 | foreach ($v['dynamic_templates'] as $key => $type) { |
|
| 249 | 4 | if (is_int($key)) { |
|
| 250 | 4 | $dt[] = $type; |
|
| 251 | 4 | } else { |
|
| 252 | 4 | $dt[][$key] = $type; |
|
| 253 | } |
||
| 254 | 4 | } |
|
| 255 | |||
| 256 | 4 | $v['dynamic_templates'] = $dt; |
|
| 257 | |||
| 258 | 4 | return $v; |
|
| 259 | 27 | }) |
|
| 260 | 27 | ->end() |
|
| 261 | 27 | ->children() |
|
| 262 | 27 | ->booleanNode('date_detection')->end() |
|
| 263 | 27 | ->arrayNode('dynamic_date_formats')->prototype('scalar')->end()->end() |
|
| 264 | 27 | ->scalarNode('analyzer')->end() |
|
| 265 | 27 | ->booleanNode('numeric_detection')->end() |
|
| 266 | 27 | ->scalarNode('dynamic')->end() |
|
| 267 | 27 | ->variableNode('indexable_callback')->end() |
|
| 268 | 27 | ->append($this->getPersistenceNode()) |
|
| 269 | 27 | ->append($this->getSerializerNode()) |
|
| 270 | 27 | ->end() |
|
| 271 | 27 | ->append($this->getIdNode()) |
|
| 272 | 27 | ->append($this->getPropertiesNode()) |
|
| 273 | 27 | ->append($this->getDynamicTemplateNode()) |
|
| 274 | 27 | ->append($this->getSourceNode()) |
|
| 275 | 27 | ->append($this->getBoostNode()) |
|
| 276 | 27 | ->append($this->getRoutingNode()) |
|
| 277 | 27 | ->append($this->getParentNode()) |
|
| 278 | 27 | ->append($this->getAllNode()) |
|
| 279 | 27 | ->end() |
|
| 280 | ; |
||
| 281 | |||
| 282 | 27 | return $node; |
|
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Returns the array node used for "properties". |
||
| 287 | */ |
||
| 288 | 27 | protected function getPropertiesNode() |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Returns the array node used for "dynamic_templates". |
||
| 303 | */ |
||
| 304 | 27 | public function getDynamicTemplateNode() |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Returns the array node used for "_id". |
||
| 334 | */ |
||
| 335 | 27 | View Code Duplication | protected function getIdNode() |
| 348 | |||
| 349 | /** |
||
| 350 | * Returns the array node used for "_source". |
||
| 351 | */ |
||
| 352 | 27 | protected function getSourceNode() |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Returns the array node used for "_boost". |
||
| 378 | */ |
||
| 379 | 27 | protected function getBoostNode() |
|
| 393 | |||
| 394 | /** |
||
| 395 | * Returns the array node used for "_routing". |
||
| 396 | */ |
||
| 397 | 27 | View Code Duplication | protected function getRoutingNode() |
| 411 | |||
| 412 | /** |
||
| 413 | * Returns the array node used for "_parent". |
||
| 414 | */ |
||
| 415 | 27 | protected function getParentNode() |
|
| 430 | |||
| 431 | /** |
||
| 432 | * Returns the array node used for "_all". |
||
| 433 | */ |
||
| 434 | 27 | protected function getAllNode() |
|
| 448 | |||
| 449 | /** |
||
| 450 | * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition |
||
| 451 | */ |
||
| 452 | 27 | protected function getPersistenceNode() |
|
| 453 | { |
||
| 454 | 27 | $builder = new TreeBuilder(); |
|
| 455 | 27 | $node = $builder->root('persistence'); |
|
| 456 | |||
| 457 | $node |
||
| 458 | 27 | ->beforeNormalization() |
|
| 459 | ->ifTrue(function($v) { return isset($v['immediate']); }) |
||
| 460 | ->then(function($v) { |
||
| 461 | @trigger_error('The immediate configuration key is deprecated since version 3.2 and will be removed in 4.0.', E_USER_DEPRECATED); |
||
| 462 | |||
| 463 | return $v; |
||
| 464 | 27 | }) |
|
| 465 | 27 | ->end() |
|
| 466 | 27 | ->validate() |
|
| 467 | View Code Duplication | ->ifTrue(function ($v) { return isset($v['driver']) && 'propel' === $v['driver'] && isset($v['listener']); }) |
|
| 468 | 27 | ->thenInvalid('Propel doesn\'t support listeners') |
|
| 469 | View Code Duplication | ->ifTrue(function ($v) { return isset($v['driver']) && 'propel' === $v['driver'] && isset($v['repository']); }) |
|
| 470 | 27 | ->thenInvalid('Propel doesn\'t support the "repository" parameter') |
|
| 471 | ->ifTrue(function($v) { return isset($v['driver']) && 'orm' !== $v['driver'] && !empty($v['elastica_to_model_transformer']['hints']); }) |
||
| 472 | 27 | ->thenInvalid('Hints are only supported by the "orm" driver') |
|
| 473 | 27 | ->end() |
|
| 474 | 27 | ->children() |
|
| 475 | 27 | ->scalarNode('driver') |
|
| 476 | 27 | ->defaultValue('orm') |
|
| 477 | 27 | ->validate() |
|
| 478 | 27 | ->ifNotInArray($this->supportedDrivers) |
|
| 479 | 27 | ->thenInvalid('The driver %s is not supported. Please choose one of '.json_encode($this->supportedDrivers)) |
|
| 480 | 27 | ->end() |
|
| 481 | 27 | ->end() |
|
| 482 | 27 | ->scalarNode('model')->defaultValue(null)->end() |
|
| 483 | 27 | ->scalarNode('repository')->end() |
|
| 484 | 27 | ->scalarNode('identifier')->defaultValue('id')->end() |
|
| 485 | 27 | ->arrayNode('provider') |
|
| 486 | 27 | ->addDefaultsIfNotSet() |
|
| 487 | 27 | ->children() |
|
| 488 | 27 | ->scalarNode('batch_size')->defaultValue(100)->end() |
|
| 489 | 27 | ->scalarNode('clear_object_manager')->defaultTrue()->end() |
|
| 490 | 27 | ->scalarNode('debug_logging') |
|
| 491 | 27 | ->defaultValue($this->debug) |
|
| 492 | 27 | ->treatNullLike(true) |
|
| 493 | 27 | ->end() |
|
| 494 | 27 | ->scalarNode('query_builder_method')->defaultValue('createQueryBuilder')->end() |
|
| 495 | 27 | ->scalarNode('service')->end() |
|
| 496 | 27 | ->end() |
|
| 497 | 27 | ->end() |
|
| 498 | 27 | ->arrayNode('listener') |
|
| 499 | 27 | ->addDefaultsIfNotSet() |
|
| 500 | 27 | ->children() |
|
| 501 | 27 | ->scalarNode('insert')->defaultTrue()->end() |
|
| 502 | 27 | ->scalarNode('update')->defaultTrue()->end() |
|
| 503 | 27 | ->scalarNode('delete')->defaultTrue()->end() |
|
| 504 | 27 | ->scalarNode('flush')->defaultTrue()->end() |
|
| 505 | 27 | ->booleanNode('immediate')->defaultFalse()->end() |
|
| 506 | 27 | ->scalarNode('logger') |
|
| 507 | 27 | ->defaultFalse() |
|
| 508 | 27 | ->treatNullLike('fos_elastica.logger') |
|
| 509 | 27 | ->treatTrueLike('fos_elastica.logger') |
|
| 510 | 27 | ->end() |
|
| 511 | 27 | ->scalarNode('service')->end() |
|
| 512 | 27 | ->end() |
|
| 513 | 27 | ->end() |
|
| 514 | 27 | ->arrayNode('finder') |
|
| 515 | 27 | ->addDefaultsIfNotSet() |
|
| 516 | 27 | ->children() |
|
| 517 | 27 | ->scalarNode('service')->end() |
|
| 518 | 27 | ->end() |
|
| 519 | 27 | ->end() |
|
| 520 | 27 | ->arrayNode('elastica_to_model_transformer') |
|
| 521 | 27 | ->addDefaultsIfNotSet() |
|
| 522 | 27 | ->children() |
|
| 523 | 27 | ->arrayNode('hints') |
|
| 524 | 27 | ->prototype('array') |
|
| 525 | 27 | ->children() |
|
| 526 | 27 | ->scalarNode('name')->end() |
|
| 527 | 27 | ->scalarNode('value')->end() |
|
| 528 | 27 | ->end() |
|
| 529 | 27 | ->end() |
|
| 530 | 27 | ->end() |
|
| 531 | 27 | ->booleanNode('hydrate')->defaultTrue()->end() |
|
| 532 | 27 | ->booleanNode('ignore_missing') |
|
| 533 | 27 | ->defaultFalse() |
|
| 534 | 27 | ->info('Silently ignore results returned from Elasticsearch without corresponding persistent object.') |
|
| 535 | 27 | ->end() |
|
| 536 | 27 | ->scalarNode('query_builder_method')->defaultValue('createQueryBuilder')->end() |
|
| 537 | 27 | ->scalarNode('service')->end() |
|
| 538 | 27 | ->end() |
|
| 539 | 27 | ->end() |
|
| 540 | 27 | ->arrayNode('model_to_elastica_transformer') |
|
| 541 | 27 | ->addDefaultsIfNotSet() |
|
| 542 | 27 | ->children() |
|
| 543 | 27 | ->scalarNode('service')->end() |
|
| 544 | 27 | ->end() |
|
| 545 | 27 | ->end() |
|
| 546 | 27 | ->arrayNode('persister') |
|
| 547 | 27 | ->addDefaultsIfNotSet() |
|
| 548 | 27 | ->children() |
|
| 549 | 27 | ->scalarNode('service')->end() |
|
| 550 | 27 | ->end() |
|
| 551 | 27 | ->end() |
|
| 552 | 27 | ->end(); |
|
| 553 | |||
| 554 | 27 | return $node; |
|
| 555 | } |
||
| 556 | |||
| 557 | /** |
||
| 558 | * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition |
||
| 559 | */ |
||
| 560 | 27 | protected function getSerializerNode() |
|
| 580 | } |
||
| 581 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: