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) |
|
| 69 | { |
||
| 70 | $rootNode |
||
| 71 | 28 | ->fixXmlConfig('client') |
|
| 72 | 28 | ->children() |
|
| 73 | 28 | ->arrayNode('clients') |
|
| 74 | 28 | ->useAttributeAsKey('id') |
|
| 75 | 28 | ->prototype('array') |
|
| 76 | 28 | ->performNoDeepMerging() |
|
| 77 | // Elastica names its properties with camel case, support both |
||
| 78 | 28 | ->beforeNormalization() |
|
| 79 | ->ifTrue(function ($v) { return isset($v['connection_strategy']); }) |
||
| 80 | ->then(function ($v) { |
||
| 81 | 4 | $v['connectionStrategy'] = $v['connection_strategy']; |
|
| 82 | 4 | unset($v['connection_strategy']); |
|
| 83 | |||
| 84 | 4 | return $v; |
|
| 85 | 28 | }) |
|
| 86 | 28 | ->end() |
|
| 87 | // If there is no connections array key defined, assume a single connection. |
||
| 88 | 28 | ->beforeNormalization() |
|
| 89 | ->ifTrue(function ($v) { return is_array($v) && !array_key_exists('connections', $v); }) |
||
| 90 | ->then(function ($v) { |
||
| 91 | return array( |
||
| 92 | 27 | 'connections' => array($v), |
|
| 93 | 27 | ); |
|
| 94 | 28 | }) |
|
| 95 | 28 | ->end() |
|
| 96 | 28 | ->children() |
|
| 97 | 28 | ->arrayNode('connections') |
|
| 98 | 28 | ->requiresAtLeastOneElement() |
|
| 99 | 28 | ->prototype('array') |
|
| 100 | 28 | ->fixXmlConfig('header') |
|
| 101 | 28 | ->children() |
|
| 102 | 28 | ->scalarNode('url') |
|
| 103 | 28 | ->validate() |
|
| 104 | ->ifTrue(function ($url) { return $url && substr($url, -1) !== '/'; }) |
||
| 105 | ->then(function ($url) { return $url.'/'; }) |
||
| 106 | 28 | ->end() |
|
| 107 | 28 | ->end() |
|
| 108 | 28 | ->scalarNode('host')->end() |
|
| 109 | 28 | ->scalarNode('port')->end() |
|
| 110 | 28 | ->scalarNode('proxy')->end() |
|
| 111 | 28 | ->scalarNode('aws_access_key_id')->end() |
|
| 112 | 28 | ->scalarNode('aws_secret_access_key')->end() |
|
| 113 | 28 | ->scalarNode('aws_region')->end() |
|
| 114 | 28 | ->scalarNode('aws_session_token')->end() |
|
| 115 | 28 | ->scalarNode('logger') |
|
| 116 | 28 | ->defaultValue($this->debug ? 'fos_elastica.logger' : false) |
|
| 117 | 28 | ->treatNullLike('fos_elastica.logger') |
|
| 118 | 28 | ->treatTrueLike('fos_elastica.logger') |
|
| 119 | 28 | ->end() |
|
| 120 | 28 | ->booleanNode('compression')->defaultValue(false)->end() |
|
| 121 | 28 | ->arrayNode('headers') |
|
| 122 | 28 | ->useAttributeAsKey('name') |
|
| 123 | 28 | ->prototype('scalar')->end() |
|
| 124 | 28 | ->end() |
|
| 125 | 28 | ->arrayNode('curl') |
|
| 126 | 28 | ->useAttributeAsKey(CURLOPT_SSL_VERIFYPEER) |
|
| 127 | 28 | ->prototype('boolean')->end() |
|
| 128 | 28 | ->end() |
|
| 129 | 28 | ->scalarNode('transport')->end() |
|
| 130 | 28 | ->scalarNode('timeout')->end() |
|
| 131 | 28 | ->scalarNode('connectTimeout')->end() |
|
| 132 | 28 | ->scalarNode('retryOnConflict') |
|
| 133 | 28 | ->defaultValue(0) |
|
| 134 | 28 | ->end() |
|
| 135 | 28 | ->end() |
|
| 136 | 28 | ->end() |
|
| 137 | 28 | ->end() |
|
| 138 | 28 | ->scalarNode('timeout')->end() |
|
| 139 | 28 | ->scalarNode('connectTimeout')->end() |
|
| 140 | 28 | ->scalarNode('headers')->end() |
|
| 141 | 28 | ->scalarNode('connectionStrategy')->defaultValue('Simple')->end() |
|
| 142 | 28 | ->end() |
|
| 143 | 28 | ->end() |
|
| 144 | 28 | ->end() |
|
| 145 | 28 | ->end() |
|
| 146 | ; |
||
| 147 | 28 | } |
|
| 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 | 4 | } |
|
| 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() |
|
| 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: