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 | ); |
||
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) |
|
153 | { |
||
154 | $rootNode |
||
|
|||
155 | 28 | ->fixXmlConfig('index') |
|
156 | 28 | ->children() |
|
157 | 28 | ->arrayNode('indexes') |
|
158 | 28 | ->useAttributeAsKey('name') |
|
159 | 28 | ->prototype('array') |
|
160 | 28 | ->children() |
|
161 | 28 | ->scalarNode('index_name') |
|
162 | 28 | ->info('Defaults to the name of the index, but can be modified if the index name is different in ElasticSearch') |
|
163 | 28 | ->end() |
|
164 | 28 | ->booleanNode('use_alias')->defaultValue(false)->end() |
|
165 | 28 | ->scalarNode('client')->end() |
|
166 | 28 | ->scalarNode('finder') |
|
167 | 28 | ->treatNullLike(true) |
|
168 | 28 | ->defaultFalse() |
|
169 | 28 | ->end() |
|
170 | 28 | ->arrayNode('type_prototype') |
|
171 | 28 | ->children() |
|
172 | 28 | ->scalarNode('analyzer')->end() |
|
173 | 28 | ->append($this->getPersistenceNode()) |
|
174 | 28 | ->append($this->getSerializerNode()) |
|
175 | 28 | ->end() |
|
176 | 28 | ->end() |
|
177 | 28 | ->variableNode('settings')->defaultValue(array())->end() |
|
178 | 28 | ->end() |
|
179 | 28 | ->append($this->getTypesNode()) |
|
180 | 28 | ->end() |
|
181 | 28 | ->end() |
|
182 | 28 | ->end() |
|
183 | ; |
||
184 | 28 | } |
|
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 | } |
||
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->getBoostNode()) |
|
236 | 28 | ->append($this->getRoutingNode()) |
|
237 | 28 | ->append($this->getParentNode()) |
|
238 | 28 | ->append($this->getAllNode()) |
|
239 | 28 | ->end() |
|
240 | ; |
||
241 | |||
242 | 28 | return $node; |
|
243 | } |
||
244 | |||
245 | /** |
||
246 | * Returns the array node used for "properties". |
||
247 | */ |
||
248 | 28 | protected function getPropertiesNode() |
|
249 | { |
||
250 | 28 | $builder = new TreeBuilder(); |
|
251 | 28 | $node = $builder->root('properties'); |
|
252 | |||
253 | $node |
||
254 | 28 | ->useAttributeAsKey('name') |
|
255 | 28 | ->prototype('variable') |
|
256 | 28 | ->treatNullLike(array()); |
|
257 | |||
258 | 28 | return $node; |
|
259 | } |
||
260 | |||
261 | /** |
||
262 | * Returns the array node used for "dynamic_templates". |
||
263 | */ |
||
264 | 28 | public function getDynamicTemplateNode() |
|
291 | |||
292 | /** |
||
293 | * Returns the array node used for "_id". |
||
294 | */ |
||
295 | 28 | View Code Duplication | protected function getIdNode() |
308 | |||
309 | /** |
||
310 | * Returns the array node used for "_source". |
||
311 | */ |
||
312 | 28 | protected function getSourceNode() |
|
335 | |||
336 | /** |
||
337 | * Returns the array node used for "_boost". |
||
338 | */ |
||
339 | 28 | protected function getBoostNode() |
|
353 | |||
354 | /** |
||
355 | * Returns the array node used for "_routing". |
||
356 | */ |
||
357 | 28 | View Code Duplication | protected function getRoutingNode() |
371 | |||
372 | /** |
||
373 | * Returns the array node used for "_parent". |
||
374 | */ |
||
375 | 28 | protected function getParentNode() |
|
390 | |||
391 | /** |
||
392 | * Returns the array node used for "_all". |
||
393 | */ |
||
394 | 28 | protected function getAllNode() |
|
408 | |||
409 | /** |
||
410 | * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition |
||
411 | */ |
||
412 | 28 | protected function getPersistenceNode() |
|
516 | |||
517 | /** |
||
518 | * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition |
||
519 | */ |
||
520 | 28 | protected function getSerializerNode() |
|
540 | } |
||
541 |
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: