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