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 | 28 | public function __construct($debug) |
|
| 29 | |||
| 30 | /** |
||
| 31 | * Generates the configuration tree. |
||
| 32 | * |
||
| 33 | * @return TreeBuilder |
||
| 34 | */ |
||
| 35 | 28 | public function getConfigTreeBuilder() |
|
| 64 | |||
| 65 | /** |
||
| 66 | * Adds the configuration for the "clients" key. |
||
| 67 | */ |
||
| 68 | 28 | private function addClientsSection(ArrayNodeDefinition $rootNode) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Adds the configuration for the "indexes" key. |
||
| 151 | */ |
||
| 152 | 28 | private function addIndexesSection(ArrayNodeDefinition $rootNode) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Returns the array node used for "types". |
||
| 188 | */ |
||
| 189 | 28 | protected function getTypesNode() |
|
| 190 | { |
||
| 191 | 28 | $builder = new TreeBuilder(); |
|
| 192 | 28 | $node = $builder->root('types'); |
|
| 193 | |||
| 194 | $node |
||
| 195 | 28 | ->useAttributeAsKey('name') |
|
| 196 | 28 | ->prototype('array') |
|
| 197 | 28 | ->treatNullLike(array()) |
|
| 198 | 28 | ->beforeNormalization() |
|
| 199 | 28 | ->ifNull() |
|
| 200 | 28 | ->thenEmptyArray() |
|
| 201 | 28 | ->end() |
|
| 202 | // Support multiple dynamic_template formats to match the old bundle style |
||
| 203 | // and the way ElasticSearch expects them |
||
| 204 | 28 | ->beforeNormalization() |
|
| 205 | ->ifTrue(function ($v) { return isset($v['dynamic_templates']); }) |
||
| 206 | ->then(function ($v) { |
||
| 207 | 4 | $dt = array(); |
|
| 208 | 4 | foreach ($v['dynamic_templates'] as $key => $type) { |
|
| 209 | 4 | if (is_int($key)) { |
|
| 210 | $dt[] = $type; |
||
| 211 | } else { |
||
| 212 | 4 | $dt[][$key] = $type; |
|
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | 4 | $v['dynamic_templates'] = $dt; |
|
| 217 | |||
| 218 | 4 | return $v; |
|
| 219 | 28 | }) |
|
| 220 | 28 | ->end() |
|
| 221 | 28 | ->children() |
|
| 222 | 28 | ->booleanNode('date_detection')->end() |
|
| 223 | 28 | ->arrayNode('dynamic_date_formats')->prototype('scalar')->end()->end() |
|
| 224 | 28 | ->scalarNode('analyzer')->end() |
|
| 225 | 28 | ->booleanNode('numeric_detection')->end() |
|
| 226 | 28 | ->scalarNode('dynamic')->end() |
|
| 227 | 28 | ->variableNode('indexable_callback')->end() |
|
| 228 | 28 | ->append($this->getPersistenceNode()) |
|
| 229 | 28 | ->append($this->getSerializerNode()) |
|
| 230 | 28 | ->end() |
|
| 231 | 28 | ->append($this->getIdNode()) |
|
| 232 | 28 | ->append($this->getPropertiesNode()) |
|
| 233 | 28 | ->append($this->getDynamicTemplateNode()) |
|
| 234 | 28 | ->append($this->getSourceNode()) |
|
| 235 | 28 | ->append($this->getRoutingNode()) |
|
| 236 | 28 | ->append($this->getParentNode()) |
|
| 237 | 28 | ->append($this->getAllNode()) |
|
| 238 | 28 | ->end() |
|
| 239 | ; |
||
| 240 | |||
| 241 | 28 | return $node; |
|
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Returns the array node used for "properties". |
||
| 246 | */ |
||
| 247 | 28 | protected function getPropertiesNode() |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Returns the array node used for "dynamic_templates". |
||
| 262 | */ |
||
| 263 | 28 | public function getDynamicTemplateNode() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Returns the array node used for "_id". |
||
| 293 | */ |
||
| 294 | 28 | View Code Duplication | protected function getIdNode() |
| 307 | |||
| 308 | /** |
||
| 309 | * Returns the array node used for "_source". |
||
| 310 | */ |
||
| 311 | 28 | protected function getSourceNode() |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Returns the array node used for "_routing". |
||
| 337 | */ |
||
| 338 | 28 | View Code Duplication | protected function getRoutingNode() |
| 339 | { |
||
| 340 | 28 | $builder = new TreeBuilder(); |
|
| 341 | 28 | $node = $builder->root('_routing'); |
|
| 342 | |||
| 343 | $node |
||
| 344 | 28 | ->children() |
|
| 345 | 28 | ->scalarNode('required')->end() |
|
| 346 | 28 | ->scalarNode('path')->end() |
|
| 347 | 28 | ->end() |
|
| 348 | ; |
||
| 349 | |||
| 350 | 28 | return $node; |
|
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Returns the array node used for "_parent". |
||
| 355 | */ |
||
| 356 | 28 | protected function getParentNode() |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Returns the array node used for "_all". |
||
| 374 | */ |
||
| 375 | 28 | protected function getAllNode() |
|
| 389 | |||
| 390 | /** |
||
| 391 | * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition |
||
| 392 | */ |
||
| 393 | 28 | protected function getPersistenceNode() |
|
| 394 | { |
||
| 395 | 28 | $builder = new TreeBuilder(); |
|
| 396 | 28 | $node = $builder->root('persistence'); |
|
| 397 | |||
| 398 | $node |
||
| 399 | 28 | ->beforeNormalization() |
|
| 400 | ->ifTrue(function($v) { return isset($v['immediate']); }) |
||
| 401 | ->then(function($v) { |
||
| 402 | @trigger_error('The immediate configuration key is deprecated since version 3.2 and will be removed in 4.0.', E_USER_DEPRECATED); |
||
| 403 | |||
| 404 | return $v; |
||
| 405 | 28 | }) |
|
| 406 | 28 | ->end() |
|
| 407 | 28 | ->validate() |
|
| 408 | View Code Duplication | ->ifTrue(function ($v) { return isset($v['driver']) && 'propel' === $v['driver'] && isset($v['listener']); }) |
|
| 409 | 28 | ->thenInvalid('Propel doesn\'t support listeners') |
|
| 410 | View Code Duplication | ->ifTrue(function ($v) { return isset($v['driver']) && 'propel' === $v['driver'] && isset($v['repository']); }) |
|
| 411 | 28 | ->thenInvalid('Propel doesn\'t support the "repository" parameter') |
|
| 412 | ->ifTrue(function($v) { return isset($v['driver']) && 'orm' !== $v['driver'] && !empty($v['elastica_to_model_transformer']['hints']); }) |
||
| 413 | 28 | ->thenInvalid('Hints are only supported by the "orm" driver') |
|
| 414 | 28 | ->end() |
|
| 415 | 28 | ->children() |
|
| 416 | 28 | ->scalarNode('driver') |
|
| 417 | 28 | ->defaultValue('orm') |
|
| 418 | 28 | ->validate() |
|
| 419 | 28 | ->ifNotInArray($this->supportedDrivers) |
|
| 420 | 28 | ->thenInvalid('The driver %s is not supported. Please choose one of '.json_encode($this->supportedDrivers)) |
|
| 421 | 28 | ->end() |
|
| 422 | 28 | ->end() |
|
| 423 | 28 | ->scalarNode('model')->defaultValue(null)->end() |
|
| 424 | 28 | ->scalarNode('repository')->end() |
|
| 425 | 28 | ->scalarNode('identifier')->defaultValue('id')->end() |
|
| 426 | 28 | ->arrayNode('provider') |
|
| 427 | 28 | ->addDefaultsIfNotSet() |
|
| 428 | 28 | ->children() |
|
| 429 | 28 | ->scalarNode('batch_size')->defaultValue(100)->end() |
|
| 430 | 28 | ->scalarNode('clear_object_manager')->defaultTrue()->end() |
|
| 431 | 28 | ->scalarNode('debug_logging') |
|
| 432 | 28 | ->defaultValue($this->debug) |
|
| 433 | 28 | ->treatNullLike(true) |
|
| 434 | 28 | ->end() |
|
| 435 | 28 | ->scalarNode('query_builder_method')->defaultValue('createQueryBuilder')->end() |
|
| 436 | 28 | ->scalarNode('service')->end() |
|
| 437 | 28 | ->end() |
|
| 438 | 28 | ->end() |
|
| 439 | 28 | ->arrayNode('listener') |
|
| 440 | 28 | ->addDefaultsIfNotSet() |
|
| 441 | 28 | ->children() |
|
| 442 | 28 | ->scalarNode('insert')->defaultTrue()->end() |
|
| 443 | 28 | ->scalarNode('update')->defaultTrue()->end() |
|
| 444 | 28 | ->scalarNode('delete')->defaultTrue()->end() |
|
| 445 | 28 | ->scalarNode('flush')->defaultTrue()->end() |
|
| 446 | 28 | ->booleanNode('immediate')->defaultFalse()->end() |
|
| 447 | 28 | ->booleanNode('defer')->defaultFalse()->end() |
|
| 448 | 28 | ->scalarNode('logger') |
|
| 449 | 28 | ->defaultFalse() |
|
| 450 | 28 | ->treatNullLike('fos_elastica.logger') |
|
| 451 | 28 | ->treatTrueLike('fos_elastica.logger') |
|
| 452 | 28 | ->end() |
|
| 453 | 28 | ->scalarNode('service')->end() |
|
| 454 | 28 | ->end() |
|
| 455 | 28 | ->end() |
|
| 456 | 28 | ->arrayNode('finder') |
|
| 457 | 28 | ->addDefaultsIfNotSet() |
|
| 458 | 28 | ->children() |
|
| 459 | 28 | ->scalarNode('service')->end() |
|
| 460 | 28 | ->end() |
|
| 461 | 28 | ->end() |
|
| 462 | 28 | ->arrayNode('elastica_to_model_transformer') |
|
| 463 | 28 | ->addDefaultsIfNotSet() |
|
| 464 | 28 | ->children() |
|
| 465 | 28 | ->arrayNode('hints') |
|
| 466 | 28 | ->prototype('array') |
|
| 467 | 28 | ->children() |
|
| 468 | 28 | ->scalarNode('name')->end() |
|
| 469 | 28 | ->scalarNode('value')->end() |
|
| 470 | 28 | ->end() |
|
| 471 | 28 | ->end() |
|
| 472 | 28 | ->end() |
|
| 473 | 28 | ->booleanNode('hydrate')->defaultTrue()->end() |
|
| 474 | 28 | ->booleanNode('ignore_missing') |
|
| 475 | 28 | ->defaultFalse() |
|
| 476 | 28 | ->info('Silently ignore results returned from Elasticsearch without corresponding persistent object.') |
|
| 477 | 28 | ->end() |
|
| 478 | 28 | ->scalarNode('query_builder_method')->defaultValue('createQueryBuilder')->end() |
|
| 479 | 28 | ->scalarNode('service')->end() |
|
| 480 | 28 | ->end() |
|
| 481 | 28 | ->end() |
|
| 482 | 28 | ->arrayNode('model_to_elastica_transformer') |
|
| 483 | 28 | ->addDefaultsIfNotSet() |
|
| 484 | 28 | ->children() |
|
| 485 | 28 | ->scalarNode('service')->end() |
|
| 486 | 28 | ->end() |
|
| 487 | 28 | ->end() |
|
| 488 | 28 | ->arrayNode('persister') |
|
| 489 | 28 | ->addDefaultsIfNotSet() |
|
| 490 | 28 | ->children() |
|
| 491 | 28 | ->scalarNode('service')->end() |
|
| 492 | 28 | ->end() |
|
| 493 | 28 | ->end() |
|
| 494 | 28 | ->end(); |
|
| 495 | |||
| 496 | 28 | return $node; |
|
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition |
||
| 501 | */ |
||
| 502 | 28 | protected function getSerializerNode() |
|
| 522 | } |
||
| 523 |
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: