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 | 21 | ); |
|
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 | ->scalarNode('retryOnConflict') |
|
133 | 22 | ->defaultValue(0) |
|
134 | 22 | ->end() |
|
135 | 22 | ->end() |
|
136 | 22 | ->end() |
|
137 | 22 | ->end() |
|
138 | 22 | ->scalarNode('timeout')->end() |
|
139 | 22 | ->scalarNode('headers')->end() |
|
140 | 22 | ->scalarNode('connectionStrategy')->defaultValue('Simple')->end() |
|
141 | 22 | ->end() |
|
142 | 22 | ->end() |
|
143 | 22 | ->end() |
|
144 | 22 | ->end() |
|
145 | ; |
||
146 | 22 | } |
|
147 | |||
148 | /** |
||
149 | * Adds the configuration for the "indexes" key. |
||
150 | */ |
||
151 | 22 | private function addIndexesSection(ArrayNodeDefinition $rootNode) |
|
185 | |||
186 | /** |
||
187 | * Returns the array node used for "types". |
||
188 | */ |
||
189 | 22 | protected function getTypesNode() |
|
190 | { |
||
191 | 22 | $builder = new TreeBuilder(); |
|
192 | 22 | $node = $builder->root('types'); |
|
193 | |||
194 | $node |
||
195 | 22 | ->useAttributeAsKey('name') |
|
196 | 22 | ->prototype('array') |
|
197 | 22 | ->treatNullLike(array()) |
|
198 | 22 | ->beforeNormalization() |
|
199 | 22 | ->ifNull() |
|
200 | 22 | ->thenEmptyArray() |
|
201 | 22 | ->end() |
|
202 | // BC - Renaming 'mappings' node to 'properties' |
||
203 | 22 | ->beforeNormalization() |
|
204 | ->ifTrue(function ($v) { return array_key_exists('mappings', $v); }) |
||
205 | ->then(function ($v) { |
||
206 | 13 | $v['properties'] = $v['mappings']; |
|
207 | 13 | unset($v['mappings']); |
|
208 | |||
209 | 13 | return $v; |
|
210 | 22 | }) |
|
211 | 22 | ->end() |
|
212 | // BC - Support the old is_indexable_callback property |
||
213 | 22 | ->beforeNormalization() |
|
214 | ->ifTrue(function ($v) { |
||
215 | 17 | return isset($v['persistence']) && |
|
216 | 17 | isset($v['persistence']['listener']) && |
|
217 | 17 | isset($v['persistence']['listener']['is_indexable_callback']); |
|
218 | 22 | }) |
|
219 | ->then(function ($v) { |
||
220 | 5 | $callback = $v['persistence']['listener']['is_indexable_callback']; |
|
221 | |||
222 | 5 | if (is_array($callback)) { |
|
223 | 5 | list($class) = $callback + array(null); |
|
224 | |||
225 | 5 | if ($class[0] !== '@' && is_string($class) && !class_exists($class)) { |
|
226 | $callback[0] = '@'.$class; |
||
227 | } |
||
228 | 5 | } |
|
229 | |||
230 | 5 | $v['indexable_callback'] = $callback; |
|
231 | 5 | unset($v['persistence']['listener']['is_indexable_callback']); |
|
232 | |||
233 | 5 | return $v; |
|
234 | 22 | }) |
|
235 | 22 | ->end() |
|
236 | // Support multiple dynamic_template formats to match the old bundle style |
||
237 | // and the way ElasticSearch expects them |
||
238 | 22 | ->beforeNormalization() |
|
239 | ->ifTrue(function ($v) { return isset($v['dynamic_templates']); }) |
||
240 | ->then(function ($v) { |
||
241 | 4 | $dt = array(); |
|
242 | 4 | foreach ($v['dynamic_templates'] as $key => $type) { |
|
243 | 4 | if (is_int($key)) { |
|
244 | 4 | $dt[] = $type; |
|
245 | 4 | } else { |
|
246 | 4 | $dt[][$key] = $type; |
|
247 | } |
||
248 | 4 | } |
|
249 | |||
250 | 4 | $v['dynamic_templates'] = $dt; |
|
251 | |||
252 | 4 | return $v; |
|
253 | 22 | }) |
|
254 | 22 | ->end() |
|
255 | 22 | ->children() |
|
256 | 22 | ->booleanNode('date_detection')->end() |
|
257 | 22 | ->arrayNode('dynamic_date_formats')->prototype('scalar')->end()->end() |
|
258 | 22 | ->scalarNode('index_analyzer')->end() |
|
259 | 22 | ->booleanNode('numeric_detection')->end() |
|
260 | 22 | ->scalarNode('search_analyzer')->end() |
|
261 | 22 | ->scalarNode('dynamic')->end() |
|
262 | 22 | ->variableNode('indexable_callback')->end() |
|
263 | 22 | ->append($this->getPersistenceNode()) |
|
264 | 22 | ->append($this->getSerializerNode()) |
|
265 | 22 | ->end() |
|
266 | 22 | ->append($this->getIdNode()) |
|
267 | 22 | ->append($this->getPropertiesNode()) |
|
268 | 22 | ->append($this->getDynamicTemplateNode()) |
|
269 | 22 | ->append($this->getSourceNode()) |
|
270 | 22 | ->append($this->getBoostNode()) |
|
271 | 22 | ->append($this->getRoutingNode()) |
|
272 | 22 | ->append($this->getParentNode()) |
|
273 | 22 | ->append($this->getAllNode()) |
|
274 | 22 | ->append($this->getTimestampNode()) |
|
275 | 22 | ->append($this->getTtlNode()) |
|
276 | 22 | ->end() |
|
277 | ; |
||
278 | |||
279 | 22 | return $node; |
|
280 | } |
||
281 | |||
282 | /** |
||
283 | * Returns the array node used for "properties". |
||
284 | */ |
||
285 | 22 | protected function getPropertiesNode() |
|
297 | |||
298 | /** |
||
299 | * Returns the array node used for "dynamic_templates". |
||
300 | */ |
||
301 | 22 | public function getDynamicTemplateNode() |
|
328 | |||
329 | /** |
||
330 | * Returns the array node used for "_id". |
||
331 | */ |
||
332 | 22 | View Code Duplication | protected function getIdNode() |
345 | |||
346 | /** |
||
347 | * Returns the array node used for "_source". |
||
348 | */ |
||
349 | 22 | protected function getSourceNode() |
|
372 | |||
373 | /** |
||
374 | * Returns the array node used for "_boost". |
||
375 | */ |
||
376 | 22 | protected function getBoostNode() |
|
390 | |||
391 | /** |
||
392 | * Returns the array node used for "_routing". |
||
393 | */ |
||
394 | 22 | View Code Duplication | protected function getRoutingNode() |
408 | |||
409 | /** |
||
410 | * Returns the array node used for "_parent". |
||
411 | */ |
||
412 | 22 | protected function getParentNode() |
|
427 | |||
428 | /** |
||
429 | * Returns the array node used for "_all". |
||
430 | */ |
||
431 | 22 | protected function getAllNode() |
|
446 | |||
447 | /** |
||
448 | * Returns the array node used for "_timestamp". |
||
449 | */ |
||
450 | 22 | View Code Duplication | protected function getTimestampNode() |
467 | |||
468 | /** |
||
469 | * Returns the array node used for "_ttl". |
||
470 | */ |
||
471 | 22 | View Code Duplication | protected function getTtlNode() |
487 | |||
488 | /** |
||
489 | * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition |
||
490 | */ |
||
491 | 22 | protected function getPersistenceNode() |
|
573 | |||
574 | /** |
||
575 | * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition |
||
576 | */ |
||
577 | 22 | protected function getSerializerNode() |
|
594 | } |
||
595 |
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: