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