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) |
|
26 | { |
||
27 | 26 | $this->debug = $debug; |
|
28 | 26 | } |
|
29 | |||
30 | /** |
||
31 | * Generates the configuration tree. |
||
32 | * |
||
33 | * @return TreeBuilder |
||
34 | */ |
||
35 | 26 | public function getConfigTreeBuilder() |
|
36 | { |
||
37 | 26 | $treeBuilder = new TreeBuilder(); |
|
38 | 26 | $rootNode = $treeBuilder->root('fos_elastica', 'array'); |
|
39 | |||
40 | 26 | $this->addClientsSection($rootNode); |
|
41 | 26 | $this->addIndexesSection($rootNode); |
|
42 | |||
43 | $rootNode |
||
44 | 26 | ->children() |
|
45 | 26 | ->scalarNode('default_client') |
|
46 | 26 | ->info('Defaults to the first client defined') |
|
47 | 26 | ->end() |
|
48 | 26 | ->scalarNode('default_index') |
|
49 | 26 | ->info('Defaults to the first index defined') |
|
50 | 26 | ->end() |
|
51 | 26 | ->scalarNode('default_manager')->defaultValue('orm')->end() |
|
52 | 26 | ->arrayNode('serializer') |
|
53 | 26 | ->treatNullLike(array()) |
|
54 | 26 | ->children() |
|
55 | 26 | ->scalarNode('callback_class')->defaultValue('FOS\ElasticaBundle\Serializer\Callback')->end() |
|
56 | 26 | ->scalarNode('serializer')->defaultValue('serializer')->end() |
|
57 | 26 | ->end() |
|
58 | 26 | ->end() |
|
59 | 26 | ->end() |
|
60 | ; |
||
61 | |||
62 | 26 | return $treeBuilder; |
|
63 | } |
||
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) |
|
187 | 26 | ||
188 | /** |
||
189 | * Returns the array node used for "types". |
||
190 | */ |
||
191 | protected function getTypesNode() |
||
192 | 26 | { |
|
193 | $builder = new TreeBuilder(); |
||
194 | 26 | $node = $builder->root('types'); |
|
195 | 26 | ||
196 | $node |
||
197 | ->useAttributeAsKey('name') |
||
198 | 26 | ->prototype('array') |
|
199 | 26 | ->treatNullLike(array()) |
|
200 | 26 | ->beforeNormalization() |
|
201 | 26 | ->ifNull() |
|
202 | 26 | ->thenEmptyArray() |
|
203 | 26 | ->end() |
|
204 | 26 | // BC - Renaming 'mappings' node to 'properties' |
|
205 | ->beforeNormalization() |
||
206 | 26 | ->ifTrue(function ($v) { return array_key_exists('mappings', $v); }) |
|
207 | ->then(function ($v) { |
||
208 | $v['properties'] = $v['mappings']; |
||
209 | 13 | unset($v['mappings']); |
|
210 | 13 | ||
211 | return $v; |
||
212 | 13 | }) |
|
213 | 26 | ->end() |
|
214 | 26 | // BC - Support the old is_indexable_callback property |
|
215 | ->beforeNormalization() |
||
216 | 26 | ->ifTrue(function ($v) { |
|
217 | return isset($v['persistence']) && |
||
218 | 18 | isset($v['persistence']['listener']) && |
|
219 | 18 | isset($v['persistence']['listener']['is_indexable_callback']); |
|
220 | 18 | }) |
|
221 | 26 | ->then(function ($v) { |
|
222 | $callback = $v['persistence']['listener']['is_indexable_callback']; |
||
223 | 5 | ||
224 | if (is_array($callback)) { |
||
225 | 5 | list($class) = $callback + array(null); |
|
226 | 5 | ||
227 | if ($class[0] !== '@' && is_string($class) && !class_exists($class)) { |
||
228 | 5 | $callback[0] = '@'.$class; |
|
229 | } |
||
230 | } |
||
231 | 5 | ||
232 | $v['indexable_callback'] = $callback; |
||
233 | 5 | unset($v['persistence']['listener']['is_indexable_callback']); |
|
234 | 5 | ||
235 | return $v; |
||
236 | 5 | }) |
|
237 | 26 | ->end() |
|
238 | 26 | // Support multiple dynamic_template formats to match the old bundle style |
|
239 | // and the way ElasticSearch expects them |
||
240 | ->beforeNormalization() |
||
241 | 26 | ->ifTrue(function ($v) { return isset($v['dynamic_templates']); }) |
|
242 | ->then(function ($v) { |
||
243 | $dt = array(); |
||
244 | 4 | foreach ($v['dynamic_templates'] as $key => $type) { |
|
245 | 4 | if (is_int($key)) { |
|
246 | 4 | $dt[] = $type; |
|
247 | 4 | } else { |
|
248 | 4 | $dt[][$key] = $type; |
|
249 | 4 | } |
|
250 | } |
||
251 | 4 | ||
252 | $v['dynamic_templates'] = $dt; |
||
253 | 4 | ||
254 | return $v; |
||
255 | 4 | }) |
|
256 | 26 | ->end() |
|
257 | 26 | ->children() |
|
258 | 26 | ->booleanNode('date_detection')->end() |
|
259 | 26 | ->arrayNode('dynamic_date_formats')->prototype('scalar')->end()->end() |
|
260 | 26 | ->scalarNode('analyzer')->end() |
|
261 | 26 | ->booleanNode('numeric_detection')->end() |
|
262 | 26 | ->scalarNode('dynamic')->end() |
|
263 | 26 | ->variableNode('indexable_callback')->end() |
|
264 | 26 | ->append($this->getPersistenceNode()) |
|
265 | 26 | ->append($this->getSerializerNode()) |
|
266 | 26 | ->end() |
|
267 | 26 | ->append($this->getIdNode()) |
|
268 | 26 | ->append($this->getPropertiesNode()) |
|
269 | 26 | ->append($this->getDynamicTemplateNode()) |
|
270 | 26 | ->append($this->getSourceNode()) |
|
271 | 26 | ->append($this->getBoostNode()) |
|
272 | 26 | ->append($this->getRoutingNode()) |
|
273 | 26 | ->append($this->getParentNode()) |
|
274 | 26 | ->append($this->getAllNode()) |
|
275 | 26 | ->append($this->getTimestampNode()) |
|
276 | 26 | ->append($this->getTtlNode()) |
|
277 | 26 | ->end() |
|
278 | 26 | ; |
|
279 | 26 | ||
280 | return $node; |
||
281 | } |
||
282 | 26 | ||
283 | /** |
||
284 | * Returns the array node used for "properties". |
||
285 | */ |
||
286 | protected function getPropertiesNode() |
||
287 | { |
||
288 | 26 | $builder = new TreeBuilder(); |
|
289 | $node = $builder->root('properties'); |
||
290 | 26 | ||
291 | 26 | $node |
|
292 | ->useAttributeAsKey('name') |
||
293 | ->prototype('variable') |
||
294 | 26 | ->treatNullLike(array()); |
|
295 | 26 | ||
296 | 26 | return $node; |
|
297 | } |
||
298 | 26 | ||
299 | /** |
||
300 | * Returns the array node used for "dynamic_templates". |
||
301 | */ |
||
302 | public function getDynamicTemplateNode() |
||
303 | { |
||
304 | 26 | $builder = new TreeBuilder(); |
|
305 | $node = $builder->root('dynamic_templates'); |
||
306 | 26 | ||
307 | 26 | $node |
|
308 | ->prototype('array') |
||
309 | ->prototype('array') |
||
310 | 26 | ->children() |
|
311 | 26 | ->scalarNode('match')->end() |
|
312 | 26 | ->scalarNode('unmatch')->end() |
|
313 | 26 | ->scalarNode('match_mapping_type')->end() |
|
314 | 26 | ->scalarNode('path_match')->end() |
|
315 | 26 | ->scalarNode('path_unmatch')->end() |
|
316 | 26 | ->scalarNode('match_pattern')->end() |
|
317 | 26 | ->arrayNode('mapping') |
|
318 | 26 | ->prototype('variable') |
|
319 | 26 | ->treatNullLike(array()) |
|
320 | 26 | ->end() |
|
321 | 26 | ->end() |
|
322 | 26 | ->end() |
|
323 | 26 | ->end() |
|
324 | 26 | ->end() |
|
325 | 26 | ; |
|
326 | 26 | ||
327 | return $node; |
||
328 | } |
||
329 | 26 | ||
330 | /** |
||
331 | * Returns the array node used for "_id". |
||
332 | */ |
||
333 | View Code Duplication | protected function getIdNode() |
|
334 | { |
||
335 | 26 | $builder = new TreeBuilder(); |
|
336 | $node = $builder->root('_id'); |
||
337 | 26 | ||
338 | 26 | $node |
|
339 | ->children() |
||
340 | ->scalarNode('path')->end() |
||
341 | 26 | ->end() |
|
342 | 26 | ; |
|
343 | 26 | ||
344 | return $node; |
||
345 | } |
||
346 | 26 | ||
347 | /** |
||
348 | * Returns the array node used for "_source". |
||
349 | */ |
||
350 | protected function getSourceNode() |
||
351 | { |
||
352 | 26 | $builder = new TreeBuilder(); |
|
353 | $node = $builder->root('_source'); |
||
354 | 26 | ||
355 | 26 | $node |
|
356 | ->children() |
||
357 | ->arrayNode('excludes') |
||
358 | 26 | ->useAttributeAsKey('name') |
|
359 | 26 | ->prototype('scalar')->end() |
|
360 | 26 | ->end() |
|
361 | 26 | ->arrayNode('includes') |
|
362 | 26 | ->useAttributeAsKey('name') |
|
363 | 26 | ->prototype('scalar')->end() |
|
364 | 26 | ->end() |
|
365 | 26 | ->scalarNode('compress')->end() |
|
366 | 26 | ->scalarNode('compress_threshold')->end() |
|
367 | 26 | ->scalarNode('enabled')->defaultTrue()->end() |
|
368 | 26 | ->end() |
|
369 | 26 | ; |
|
370 | 26 | ||
371 | return $node; |
||
372 | } |
||
373 | 26 | ||
374 | /** |
||
375 | * Returns the array node used for "_boost". |
||
376 | */ |
||
377 | protected function getBoostNode() |
||
378 | { |
||
379 | 26 | $builder = new TreeBuilder(); |
|
380 | $node = $builder->root('_boost'); |
||
381 | 26 | ||
382 | 26 | $node |
|
383 | ->children() |
||
384 | ->scalarNode('name')->end() |
||
385 | 26 | ->scalarNode('null_value')->end() |
|
386 | 26 | ->end() |
|
387 | 26 | ; |
|
388 | 26 | ||
389 | return $node; |
||
390 | } |
||
391 | 26 | ||
392 | /** |
||
393 | * Returns the array node used for "_routing". |
||
394 | */ |
||
395 | View Code Duplication | protected function getRoutingNode() |
|
396 | { |
||
397 | 26 | $builder = new TreeBuilder(); |
|
398 | $node = $builder->root('_routing'); |
||
399 | 26 | ||
400 | 26 | $node |
|
401 | ->children() |
||
402 | ->scalarNode('required')->end() |
||
403 | 26 | ->scalarNode('path')->end() |
|
404 | 26 | ->end() |
|
405 | 26 | ; |
|
406 | 26 | ||
407 | return $node; |
||
408 | } |
||
409 | 26 | ||
410 | /** |
||
411 | * Returns the array node used for "_parent". |
||
412 | */ |
||
413 | protected function getParentNode() |
||
426 | |||
427 | /** |
||
428 | 26 | * Returns the array node used for "_all". |
|
429 | */ |
||
430 | protected function getAllNode() |
||
444 | 26 | ||
445 | /** |
||
446 | * Returns the array node used for "_timestamp". |
||
447 | 26 | */ |
|
448 | View Code Duplication | protected function getTimestampNode() |
|
449 | { |
||
450 | $builder = new TreeBuilder(); |
||
451 | $node = $builder->root('_timestamp'); |
||
452 | |||
453 | 26 | $node |
|
454 | ->children() |
||
465 | 26 | ||
466 | /** |
||
467 | * Returns the array node used for "_ttl". |
||
468 | 26 | */ |
|
469 | View Code Duplication | protected function getTtlNode() |
|
485 | 26 | ||
486 | /** |
||
487 | * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition |
||
488 | 26 | */ |
|
489 | protected function getPersistenceNode() |
||
584 | 26 | ||
585 | 26 | /** |
|
586 | * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition |
||
587 | 26 | */ |
|
588 | 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: