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 | 25 | public function __construct($debug) |
|
| 29 | |||
| 30 | /** |
||
| 31 | * Generates the configuration tree. |
||
| 32 | * |
||
| 33 | * @return TreeBuilder |
||
| 34 | */ |
||
| 35 | 25 | public function getConfigTreeBuilder() |
|
| 64 | |||
| 65 | /** |
||
| 66 | * Adds the configuration for the "clients" key. |
||
| 67 | */ |
||
| 68 | 25 | private function addClientsSection(ArrayNodeDefinition $rootNode) |
|
| 69 | { |
||
| 70 | $rootNode |
||
| 71 | 25 | ->fixXmlConfig('client') |
|
| 72 | 25 | ->children() |
|
| 73 | 25 | ->arrayNode('clients') |
|
| 74 | 25 | ->useAttributeAsKey('id') |
|
| 75 | 25 | ->prototype('array') |
|
| 76 | 25 | ->performNoDeepMerging() |
|
| 77 | // BC - Renaming 'servers' node to 'connections' |
||
| 78 | 25 | ->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 | 25 | }) |
|
| 86 | 25 | ->end() |
|
| 87 | // Elastica names its properties with camel case, support both |
||
| 88 | 25 | ->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 | 25 | }) |
|
| 96 | 25 | ->end() |
|
| 97 | // If there is no connections array key defined, assume a single connection. |
||
| 98 | 25 | ->beforeNormalization() |
|
| 99 | ->ifTrue(function ($v) { return is_array($v) && !array_key_exists('connections', $v); }) |
||
| 100 | ->then(function ($v) { |
||
| 101 | return array( |
||
| 102 | 24 | 'connections' => array($v), |
|
| 103 | 24 | ); |
|
| 104 | 25 | }) |
|
| 105 | 25 | ->end() |
|
| 106 | 25 | ->children() |
|
| 107 | 25 | ->arrayNode('connections') |
|
| 108 | 25 | ->requiresAtLeastOneElement() |
|
| 109 | 25 | ->prototype('array') |
|
| 110 | 25 | ->fixXmlConfig('header') |
|
| 111 | 25 | ->children() |
|
| 112 | 25 | ->scalarNode('url') |
|
| 113 | 25 | ->validate() |
|
| 114 | ->ifTrue(function ($url) { return $url && substr($url, -1) !== '/'; }) |
||
| 115 | ->then(function ($url) { return $url.'/'; }) |
||
| 116 | 25 | ->end() |
|
| 117 | 25 | ->end() |
|
| 118 | 25 | ->scalarNode('host')->end() |
|
| 119 | 25 | ->scalarNode('port')->end() |
|
| 120 | 25 | ->scalarNode('proxy')->end() |
|
| 121 | 25 | ->scalarNode('logger') |
|
| 122 | 25 | ->defaultValue($this->debug ? 'fos_elastica.logger' : false) |
|
| 123 | 25 | ->treatNullLike('fos_elastica.logger') |
|
| 124 | 25 | ->treatTrueLike('fos_elastica.logger') |
|
| 125 | 25 | ->end() |
|
| 126 | 25 | ->booleanNode('compression')->defaultValue(false)->end() |
|
| 127 | 25 | ->arrayNode('headers') |
|
| 128 | 25 | ->useAttributeAsKey('name') |
|
| 129 | 25 | ->prototype('scalar')->end() |
|
| 130 | 25 | ->end() |
|
| 131 | 25 | ->scalarNode('transport')->end() |
|
| 132 | 25 | ->scalarNode('timeout')->end() |
|
| 133 | 25 | ->scalarNode('retryOnConflict') |
|
| 134 | 25 | ->defaultValue(0) |
|
| 135 | 25 | ->end() |
|
| 136 | 25 | ->end() |
|
| 137 | 25 | ->end() |
|
| 138 | 25 | ->end() |
|
| 139 | 25 | ->scalarNode('timeout')->end() |
|
| 140 | 25 | ->scalarNode('headers')->end() |
|
| 141 | 25 | ->scalarNode('connectionStrategy')->defaultValue('Simple')->end() |
|
| 142 | 25 | ->end() |
|
| 143 | 25 | ->end() |
|
| 144 | 25 | ->end() |
|
| 145 | 25 | ->end() |
|
| 146 | ; |
||
| 147 | 25 | } |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Adds the configuration for the "indexes" key. |
||
| 151 | */ |
||
| 152 | 25 | private function addIndexesSection(ArrayNodeDefinition $rootNode) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Returns the array node used for "types". |
||
| 189 | */ |
||
| 190 | 25 | protected function getTypesNode() |
|
| 191 | { |
||
| 192 | 25 | $builder = new TreeBuilder(); |
|
| 193 | 25 | $node = $builder->root('types'); |
|
| 194 | |||
| 195 | $node |
||
| 196 | 25 | ->useAttributeAsKey('name') |
|
| 197 | 25 | ->prototype('array') |
|
| 198 | 25 | ->treatNullLike(array()) |
|
| 199 | 25 | ->beforeNormalization() |
|
| 200 | 25 | ->ifNull() |
|
| 201 | 25 | ->thenEmptyArray() |
|
| 202 | 25 | ->end() |
|
| 203 | // BC - Renaming 'mappings' node to 'properties' |
||
| 204 | 25 | ->beforeNormalization() |
|
| 205 | ->ifTrue(function ($v) { return array_key_exists('mappings', $v); }) |
||
| 206 | ->then(function ($v) { |
||
| 207 | 13 | $v['properties'] = $v['mappings']; |
|
| 208 | 13 | unset($v['mappings']); |
|
| 209 | |||
| 210 | 13 | return $v; |
|
| 211 | 25 | }) |
|
| 212 | 25 | ->end() |
|
| 213 | // BC - Support the old is_indexable_callback property |
||
| 214 | 25 | ->beforeNormalization() |
|
| 215 | ->ifTrue(function ($v) { |
||
| 216 | 18 | return isset($v['persistence']) && |
|
| 217 | 18 | isset($v['persistence']['listener']) && |
|
| 218 | 18 | isset($v['persistence']['listener']['is_indexable_callback']); |
|
| 219 | 25 | }) |
|
| 220 | ->then(function ($v) { |
||
| 221 | 5 | $callback = $v['persistence']['listener']['is_indexable_callback']; |
|
| 222 | |||
| 223 | 5 | if (is_array($callback)) { |
|
| 224 | 5 | list($class) = $callback + array(null); |
|
| 225 | |||
| 226 | 5 | if ($class[0] !== '@' && is_string($class) && !class_exists($class)) { |
|
| 227 | $callback[0] = '@'.$class; |
||
| 228 | } |
||
| 229 | 5 | } |
|
| 230 | |||
| 231 | 5 | $v['indexable_callback'] = $callback; |
|
| 232 | 5 | unset($v['persistence']['listener']['is_indexable_callback']); |
|
| 233 | |||
| 234 | 5 | return $v; |
|
| 235 | 25 | }) |
|
| 236 | 25 | ->end() |
|
| 237 | // Support multiple dynamic_template formats to match the old bundle style |
||
| 238 | // and the way ElasticSearch expects them |
||
| 239 | 25 | ->beforeNormalization() |
|
| 240 | ->ifTrue(function ($v) { return isset($v['dynamic_templates']); }) |
||
| 241 | ->then(function ($v) { |
||
| 242 | 4 | $dt = array(); |
|
| 243 | 4 | foreach ($v['dynamic_templates'] as $key => $type) { |
|
| 244 | 4 | if (is_int($key)) { |
|
| 245 | 4 | $dt[] = $type; |
|
| 246 | 4 | } else { |
|
| 247 | 4 | $dt[][$key] = $type; |
|
| 248 | } |
||
| 249 | 4 | } |
|
| 250 | |||
| 251 | 4 | $v['dynamic_templates'] = $dt; |
|
| 252 | |||
| 253 | 4 | return $v; |
|
| 254 | 25 | }) |
|
| 255 | 25 | ->end() |
|
| 256 | 25 | ->children() |
|
| 257 | 25 | ->booleanNode('date_detection')->end() |
|
| 258 | 25 | ->arrayNode('dynamic_date_formats')->prototype('scalar')->end()->end() |
|
| 259 | 25 | ->scalarNode('index_analyzer')->end() |
|
| 260 | 25 | ->booleanNode('numeric_detection')->end() |
|
| 261 | 25 | ->scalarNode('search_analyzer')->end() |
|
| 262 | 25 | ->scalarNode('dynamic')->end() |
|
| 263 | 25 | ->variableNode('indexable_callback')->end() |
|
| 264 | 25 | ->append($this->getPersistenceNode()) |
|
| 265 | 25 | ->append($this->getSerializerNode()) |
|
| 266 | 25 | ->end() |
|
| 267 | 25 | ->append($this->getIdNode()) |
|
| 268 | 25 | ->append($this->getPropertiesNode()) |
|
| 269 | 25 | ->append($this->getDynamicTemplateNode()) |
|
| 270 | 25 | ->append($this->getSourceNode()) |
|
| 271 | 25 | ->append($this->getBoostNode()) |
|
| 272 | 25 | ->append($this->getRoutingNode()) |
|
| 273 | 25 | ->append($this->getParentNode()) |
|
| 274 | 25 | ->append($this->getAllNode()) |
|
| 275 | 25 | ->append($this->getTimestampNode()) |
|
| 276 | 25 | ->append($this->getTtlNode()) |
|
| 277 | 25 | ->end() |
|
| 278 | ; |
||
| 279 | |||
| 280 | 25 | return $node; |
|
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Returns the array node used for "properties". |
||
| 285 | */ |
||
| 286 | 25 | protected function getPropertiesNode() |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Returns the array node used for "dynamic_templates". |
||
| 301 | */ |
||
| 302 | 25 | public function getDynamicTemplateNode() |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Returns the array node used for "_id". |
||
| 332 | */ |
||
| 333 | 25 | View Code Duplication | protected function getIdNode() |
| 346 | |||
| 347 | /** |
||
| 348 | * Returns the array node used for "_source". |
||
| 349 | */ |
||
| 350 | 25 | protected function getSourceNode() |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Returns the array node used for "_boost". |
||
| 376 | */ |
||
| 377 | 25 | protected function getBoostNode() |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Returns the array node used for "_routing". |
||
| 394 | */ |
||
| 395 | 25 | View Code Duplication | protected function getRoutingNode() |
| 409 | |||
| 410 | /** |
||
| 411 | * Returns the array node used for "_parent". |
||
| 412 | */ |
||
| 413 | 25 | protected function getParentNode() |
|
| 428 | |||
| 429 | /** |
||
| 430 | * Returns the array node used for "_all". |
||
| 431 | */ |
||
| 432 | 25 | protected function getAllNode() |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Returns the array node used for "_timestamp". |
||
| 450 | */ |
||
| 451 | 25 | View Code Duplication | protected function getTimestampNode() |
| 468 | |||
| 469 | /** |
||
| 470 | * Returns the array node used for "_ttl". |
||
| 471 | */ |
||
| 472 | 25 | View Code Duplication | protected function getTtlNode() |
| 488 | |||
| 489 | /** |
||
| 490 | * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition |
||
| 491 | */ |
||
| 492 | 25 | protected function getPersistenceNode() |
|
| 584 | |||
| 585 | /** |
||
| 586 | * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition |
||
| 587 | */ |
||
| 588 | 25 | protected function getSerializerNode() |
|
| 608 | } |
||
| 609 |
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: