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 | 22 | public function __construct($debug) |
|
| 29 | |||
| 30 | /** |
||
| 31 | * Generates the configuration tree. |
||
| 32 | * |
||
| 33 | * @return TreeBuilder |
||
| 34 | */ |
||
| 35 | 22 | public function getConfigTreeBuilder() |
|
| 64 | |||
| 65 | /** |
||
| 66 | * Adds the configuration for the "clients" key. |
||
| 67 | */ |
||
| 68 | 22 | private function addClientsSection(ArrayNodeDefinition $rootNode) |
|
| 69 | { |
||
| 70 | $rootNode |
||
| 71 | 22 | ->fixXmlConfig('client') |
|
| 72 | 22 | ->children() |
|
| 73 | 22 | ->arrayNode('clients') |
|
| 74 | 22 | ->useAttributeAsKey('id') |
|
| 75 | 22 | ->prototype('array') |
|
| 76 | 22 | ->performNoDeepMerging() |
|
| 77 | // BC - Renaming 'servers' node to 'connections' |
||
| 78 | 22 | ->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 | 22 | }) |
|
| 86 | 22 | ->end() |
|
| 87 | // Elastica names its properties with camel case, support both |
||
| 88 | 22 | ->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 | 22 | }) |
|
| 96 | 22 | ->end() |
|
| 97 | // If there is no connections array key defined, assume a single connection. |
||
| 98 | 22 | ->beforeNormalization() |
|
| 99 | ->ifTrue(function ($v) { return is_array($v) && !array_key_exists('connections', $v); }) |
||
| 100 | ->then(function ($v) { |
||
| 101 | return array( |
||
| 102 | 21 | 'connections' => array($v), |
|
| 103 | ); |
||
| 104 | 22 | }) |
|
| 105 | 22 | ->end() |
|
| 106 | 22 | ->children() |
|
| 107 | 22 | ->arrayNode('connections') |
|
| 108 | 22 | ->requiresAtLeastOneElement() |
|
| 109 | 22 | ->prototype('array') |
|
| 110 | 22 | ->fixXmlConfig('header') |
|
| 111 | 22 | ->children() |
|
| 112 | 22 | ->scalarNode('url') |
|
| 113 | 22 | ->validate() |
|
| 114 | ->ifTrue(function ($url) { return $url && substr($url, -1) !== '/'; }) |
||
| 115 | ->then(function ($url) { return $url.'/'; }) |
||
| 116 | 22 | ->end() |
|
| 117 | 22 | ->end() |
|
| 118 | 22 | ->scalarNode('host')->end() |
|
| 119 | 22 | ->scalarNode('port')->end() |
|
| 120 | 22 | ->scalarNode('proxy')->end() |
|
| 121 | 22 | ->scalarNode('logger') |
|
| 122 | 22 | ->defaultValue($this->debug ? 'fos_elastica.logger' : false) |
|
| 123 | 22 | ->treatNullLike('fos_elastica.logger') |
|
| 124 | 22 | ->treatTrueLike('fos_elastica.logger') |
|
| 125 | 22 | ->end() |
|
| 126 | 22 | ->arrayNode('headers') |
|
| 127 | 22 | ->useAttributeAsKey('name') |
|
| 128 | 22 | ->prototype('scalar')->end() |
|
| 129 | 22 | ->end() |
|
| 130 | 22 | ->scalarNode('transport')->end() |
|
| 131 | 22 | ->scalarNode('timeout')->end() |
|
| 132 | 22 | ->end() |
|
| 133 | 22 | ->end() |
|
| 134 | 22 | ->end() |
|
| 135 | 22 | ->scalarNode('timeout')->end() |
|
| 136 | 22 | ->scalarNode('headers')->end() |
|
| 137 | 22 | ->scalarNode('connectionStrategy')->defaultValue('Simple')->end() |
|
| 138 | 22 | ->end() |
|
| 139 | 22 | ->end() |
|
| 140 | 22 | ->end() |
|
| 141 | 22 | ->end() |
|
| 142 | ; |
||
| 143 | 22 | } |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Adds the configuration for the "indexes" key. |
||
| 147 | */ |
||
| 148 | 22 | private function addIndexesSection(ArrayNodeDefinition $rootNode) |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Returns the array node used for "types". |
||
| 185 | */ |
||
| 186 | 22 | protected function getTypesNode() |
|
| 187 | { |
||
| 188 | 22 | $builder = new TreeBuilder(); |
|
| 189 | 22 | $node = $builder->root('types'); |
|
| 190 | |||
| 191 | $node |
||
| 192 | 22 | ->useAttributeAsKey('name') |
|
| 193 | 22 | ->prototype('array') |
|
| 194 | 22 | ->treatNullLike(array()) |
|
| 195 | 22 | ->beforeNormalization() |
|
| 196 | 22 | ->ifNull() |
|
| 197 | 22 | ->thenEmptyArray() |
|
| 198 | 22 | ->end() |
|
| 199 | // BC - Renaming 'mappings' node to 'properties' |
||
| 200 | 22 | ->beforeNormalization() |
|
| 201 | ->ifTrue(function ($v) { return array_key_exists('mappings', $v); }) |
||
| 202 | ->then(function ($v) { |
||
| 203 | 13 | $v['properties'] = $v['mappings']; |
|
| 204 | 13 | unset($v['mappings']); |
|
| 205 | |||
| 206 | 13 | return $v; |
|
| 207 | 22 | }) |
|
| 208 | 22 | ->end() |
|
| 209 | // BC - Support the old is_indexable_callback property |
||
| 210 | 22 | ->beforeNormalization() |
|
| 211 | ->ifTrue(function ($v) { |
||
| 212 | 17 | return isset($v['persistence']) && |
|
| 213 | 17 | isset($v['persistence']['listener']) && |
|
| 214 | 17 | isset($v['persistence']['listener']['is_indexable_callback']); |
|
| 215 | 22 | }) |
|
| 216 | ->then(function ($v) { |
||
| 217 | 5 | $callback = $v['persistence']['listener']['is_indexable_callback']; |
|
| 218 | |||
| 219 | 5 | if (is_array($callback)) { |
|
| 220 | 5 | list($class) = $callback + array(null); |
|
| 221 | |||
| 222 | 5 | if ($class[0] !== '@' && is_string($class) && !class_exists($class)) { |
|
| 223 | $callback[0] = '@'.$class; |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | 5 | $v['indexable_callback'] = $callback; |
|
| 228 | 5 | unset($v['persistence']['listener']['is_indexable_callback']); |
|
| 229 | |||
| 230 | 5 | return $v; |
|
| 231 | 22 | }) |
|
| 232 | 22 | ->end() |
|
| 233 | // Support multiple dynamic_template formats to match the old bundle style |
||
| 234 | // and the way ElasticSearch expects them |
||
| 235 | 22 | ->beforeNormalization() |
|
| 236 | ->ifTrue(function ($v) { return isset($v['dynamic_templates']); }) |
||
| 237 | ->then(function ($v) { |
||
| 238 | 4 | $dt = array(); |
|
| 239 | 4 | foreach ($v['dynamic_templates'] as $key => $type) { |
|
| 240 | 4 | if (is_int($key)) { |
|
| 241 | 4 | $dt[] = $type; |
|
| 242 | } else { |
||
| 243 | 4 | $dt[][$key] = $type; |
|
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | 4 | $v['dynamic_templates'] = $dt; |
|
| 248 | |||
| 249 | 4 | return $v; |
|
| 250 | 22 | }) |
|
| 251 | 22 | ->end() |
|
| 252 | 22 | ->children() |
|
| 253 | 22 | ->booleanNode('date_detection')->end() |
|
| 254 | 22 | ->arrayNode('dynamic_date_formats')->prototype('scalar')->end()->end() |
|
| 255 | 22 | ->scalarNode('index_analyzer')->end() |
|
| 256 | 22 | ->booleanNode('numeric_detection')->end() |
|
| 257 | 22 | ->scalarNode('search_analyzer')->end() |
|
| 258 | 22 | ->variableNode('indexable_callback')->end() |
|
| 259 | 22 | ->append($this->getPersistenceNode()) |
|
| 260 | 22 | ->append($this->getSerializerNode()) |
|
| 261 | 22 | ->end() |
|
| 262 | 22 | ->append($this->getIdNode()) |
|
| 263 | 22 | ->append($this->getPropertiesNode()) |
|
| 264 | 22 | ->append($this->getDynamicTemplateNode()) |
|
| 265 | 22 | ->append($this->getSourceNode()) |
|
| 266 | 22 | ->append($this->getBoostNode()) |
|
| 267 | 22 | ->append($this->getRoutingNode()) |
|
| 268 | 22 | ->append($this->getParentNode()) |
|
| 269 | 22 | ->append($this->getAllNode()) |
|
| 270 | 22 | ->append($this->getTimestampNode()) |
|
| 271 | 22 | ->append($this->getTtlNode()) |
|
| 272 | 22 | ->end() |
|
| 273 | ; |
||
| 274 | |||
| 275 | 22 | return $node; |
|
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Returns the array node used for "properties". |
||
| 280 | */ |
||
| 281 | 22 | protected function getPropertiesNode() |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Returns the array node used for "dynamic_templates". |
||
| 296 | */ |
||
| 297 | 22 | public function getDynamicTemplateNode() |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Returns the array node used for "_id". |
||
| 327 | */ |
||
| 328 | 22 | View Code Duplication | protected function getIdNode() |
| 341 | |||
| 342 | /** |
||
| 343 | * Returns the array node used for "_source". |
||
| 344 | */ |
||
| 345 | 22 | protected function getSourceNode() |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Returns the array node used for "_boost". |
||
| 371 | */ |
||
| 372 | 22 | protected function getBoostNode() |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Returns the array node used for "_routing". |
||
| 389 | */ |
||
| 390 | 22 | View Code Duplication | protected function getRoutingNode() |
| 404 | |||
| 405 | /** |
||
| 406 | * Returns the array node used for "_parent". |
||
| 407 | */ |
||
| 408 | 22 | protected function getParentNode() |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Returns the array node used for "_all". |
||
| 426 | */ |
||
| 427 | 22 | protected function getAllNode() |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Returns the array node used for "_timestamp". |
||
| 445 | */ |
||
| 446 | 22 | View Code Duplication | protected function getTimestampNode() |
| 463 | |||
| 464 | /** |
||
| 465 | * Returns the array node used for "_ttl". |
||
| 466 | */ |
||
| 467 | 22 | View Code Duplication | protected function getTtlNode() |
| 483 | |||
| 484 | /** |
||
| 485 | * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition |
||
| 486 | */ |
||
| 487 | 22 | protected function getPersistenceNode() |
|
| 569 | |||
| 570 | /** |
||
| 571 | * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition |
||
| 572 | */ |
||
| 573 | 22 | protected function getSerializerNode() |
|
| 590 | } |
||
| 591 |
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: