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:
Complex classes like Configuration often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Configuration, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Configuration implements ConfigurationInterface |
||
11 | { |
||
12 | /** |
||
13 | * Generates the configuration tree. |
||
14 | * |
||
15 | * @return TreeBuilder |
||
16 | */ |
||
17 | 8 | public function getConfigTreeBuilder() |
|
54 | |||
55 | 8 | public function setDeprecatedNode($node, $type, $name, $deprecatedMessage) |
|
65 | |||
66 | 8 | protected function addTimings() |
|
87 | |||
88 | 8 | View Code Duplication | protected function addOrm() |
104 | |||
105 | 8 | View Code Duplication | protected function addOdm() |
121 | |||
122 | 8 | protected function addManager() |
|
140 | |||
141 | 8 | protected function addBeanstalkd() |
|
153 | |||
154 | 8 | protected function addRetry() |
|
199 | |||
200 | 8 | protected function addPriority() |
|
221 | |||
222 | 8 | protected function addClasses() |
|
242 | |||
243 | 8 | View Code Duplication | protected function addAdmin() |
258 | |||
259 | 8 | protected function addRedis() |
|
260 | { |
||
261 | 8 | $treeBuilder = new TreeBuilder(); |
|
262 | 8 | $rootNode = $treeBuilder->root('redis'); |
|
263 | $rootNode |
||
264 | 8 | ->addDefaultsIfNotSet() |
|
265 | 8 | ->children() |
|
266 | 8 | ->scalarNode('prefix')->defaultValue('dtc_queue_')->end() |
|
267 | 8 | ->arrayNode('snc_redis') |
|
268 | 8 | ->children() |
|
269 | 8 | ->enumNode('type') |
|
270 | 8 | ->values(['predis', 'phpredis']) |
|
271 | 8 | ->defaultNull()->end() |
|
272 | 8 | ->scalarNode('alias') |
|
273 | 8 | ->defaultNull()->end() |
|
274 | 8 | ->end() |
|
275 | 8 | View Code Duplication | ->validate()->ifTrue(function ($node) { |
276 | 1 | if (isset($node['type']) && !isset($node['alias'])) { |
|
277 | 1 | return true; |
|
278 | } |
||
279 | 1 | if (isset($node['alias']) && !isset($node['type'])) { |
|
280 | 1 | return true; |
|
281 | } |
||
282 | |||
283 | 1 | return false; |
|
284 | 8 | })->thenInvalid('if alias or type is set, then both must be set')->end() |
|
285 | 8 | ->end() |
|
286 | 8 | ->arrayNode('predis') |
|
287 | 8 | ->children() |
|
288 | 8 | ->scalarNode('dsn')->defaultNull()->end() |
|
289 | 8 | ->append($this->addPredisArgs()) |
|
290 | 8 | ->end() |
|
291 | 8 | ->validate()->ifTrue(function ($node) { |
|
292 | 1 | if (isset($node['dsn']) && (isset($node['connection_parameters']['host']) || isset($node['connection_parameters']['port']))) { |
|
293 | return true; |
||
294 | } |
||
295 | |||
296 | 1 | return false; |
|
297 | 8 | })->thenInvalid('if dsn is set, do not use connection_parameters for predis (and vice-versa)')->end() |
|
298 | 8 | ->end() |
|
299 | 8 | ->append($this->addPhpRedisArgs()) |
|
300 | 8 | ->end() |
|
301 | 8 | ->validate()->ifTrue(function ($node) { |
|
302 | 4 | View Code Duplication | if ((isset($node['predis']['dsn']) || isset($node['predis']['connection_parameters']['host'])) && |
303 | 4 | (isset($node['snc_redis']['type']) || isset($node['phpredis']['host']))) { |
|
304 | return true; |
||
305 | } |
||
306 | 4 | View Code Duplication | if (isset($node['snc_redis']['type']) && |
307 | 1 | (isset($node['predis']['dsn']) || isset($node['predis']['connection_parameters']['host']) || |
|
308 | 4 | isset($node['phpredis']['host']))) { |
|
309 | return true; |
||
310 | } |
||
311 | 4 | if (isset($node['phpredis']['host']) && |
|
312 | 2 | (isset($node['snc_redis']['type']) || isset($node['predis']['dsn']) || |
|
313 | 4 | isset($node['predis']['connection_parameters']['host']))) { |
|
314 | return true; |
||
315 | } |
||
316 | |||
317 | 4 | return false; |
|
318 | 8 | })->thenInvalid('only one of [snc_redis | predis | phpredis] should be set')->end(); |
|
319 | |||
320 | 8 | return $rootNode; |
|
321 | } |
||
322 | |||
323 | 8 | protected function addPhpRedisArgs() |
|
324 | { |
||
325 | 8 | $treeBuilder = new TreeBuilder(); |
|
326 | 8 | $rootNode = $treeBuilder->root('phpredis'); |
|
327 | $rootNode |
||
328 | 8 | ->addDefaultsIfNotSet() |
|
329 | 8 | ->children() |
|
330 | 8 | ->scalarNode('host')->end() |
|
331 | 8 | ->integerNode('port')->defaultValue(6379)->end() |
|
332 | 8 | ->floatNode('timeout')->defaultValue(0)->end() |
|
333 | 8 | ->integerNode('retry_interval')->defaultNull()->end() |
|
334 | 8 | ->floatNode('read_timeout')->defaultValue(0)->end() |
|
335 | 8 | ->scalarNode('auth')->end() |
|
336 | 8 | ->end() |
|
337 | 8 | ->validate()->ifTrue(function ($node) { |
|
338 | 2 | if (!empty($node) && !isset($node['host'])) { |
|
339 | return true; |
||
340 | } |
||
341 | |||
342 | 2 | return false; |
|
343 | 8 | })->thenInvalid('phpredis host should be set')->end(); |
|
344 | |||
345 | 8 | return $rootNode; |
|
346 | } |
||
347 | |||
348 | 8 | protected function addPredisArgs() |
|
349 | { |
||
350 | 8 | $treeBuilder = new TreeBuilder(); |
|
351 | 8 | $rootNode = $treeBuilder->root('connection_parameters'); |
|
352 | $rootNode |
||
353 | 8 | ->addDefaultsIfNotSet() |
|
354 | 8 | ->children() |
|
355 | 8 | ->scalarNode('scheme')->defaultValue('tcp')->end() |
|
356 | 8 | ->scalarNode('host')->defaultNull()->end() |
|
357 | 8 | ->integerNode('port')->defaultNull()->end() |
|
358 | 8 | ->scalarNode('path')->defaultNull()->end() |
|
359 | 8 | ->scalarNode('database')->defaultNull()->end() |
|
360 | 8 | ->scalarNode('password')->defaultNull()->end() |
|
361 | 8 | ->booleanNode('async')->defaultFalse()->end() |
|
362 | 8 | ->booleanNode('persistent')->defaultFalse()->end() |
|
363 | 8 | ->floatNode('timeout')->defaultValue(5.0)->end() |
|
364 | 8 | ->floatNode('read_write_timeout')->defaultNull()->end() |
|
365 | 8 | ->scalarNode('alias')->defaultNull()->end() |
|
366 | 8 | ->integerNode('weight')->defaultNull()->end() |
|
367 | 8 | ->booleanNode('iterable_multibulk')->defaultFalse()->end() |
|
368 | 8 | ->booleanNode('throw_errors')->defaultTrue()->end() |
|
369 | 8 | ->end() |
|
370 | 8 | View Code Duplication | ->validate()->ifTrue(function ($node) { |
371 | 1 | if (isset($node['host']) && !isset($node['port'])) { |
|
372 | return true; |
||
373 | } |
||
374 | 1 | if (isset($node['port']) && !isset($node['host'])) { |
|
375 | return true; |
||
376 | } |
||
377 | |||
378 | 1 | return false; |
|
379 | 8 | })->thenInvalid('preids connection_parameters host and port should both be set')->end(); |
|
380 | |||
381 | 8 | return $rootNode; |
|
382 | } |
||
383 | |||
384 | 8 | protected function addRabbitMqOptions() |
|
402 | |||
403 | 8 | protected function addRabbitMqSslOptions() |
|
404 | { |
||
405 | 8 | $treeBuilder = new TreeBuilder(); |
|
406 | 8 | $rootNode = $treeBuilder->root('ssl_options'); |
|
407 | $rootNode |
||
408 | 8 | ->prototype('variable')->end() |
|
409 | 8 | ->validate() |
|
410 | 8 | ->ifTrue(function ($node) { |
|
411 | 1 | if (!is_array($node)) { |
|
412 | return true; |
||
413 | } |
||
414 | 1 | foreach ($node as $key => $value) { |
|
415 | 1 | if (is_array($value)) { |
|
416 | 1 | if ('peer_fingerprint' !== $key) { |
|
417 | 1 | return true; |
|
418 | } else { |
||
419 | 1 | foreach ($value as $key1 => $value1) { |
|
420 | 1 | if (!is_string($key1) || !is_string($value1)) { |
|
421 | 1 | return true; |
|
422 | } |
||
423 | } |
||
424 | } |
||
425 | } |
||
426 | } |
||
427 | |||
428 | 1 | return false; |
|
429 | 8 | }) |
|
430 | 8 | ->thenInvalid('Must be key-value pairs') |
|
431 | 8 | ->end(); |
|
432 | |||
433 | 8 | return $rootNode; |
|
434 | } |
||
435 | |||
436 | 8 | View Code Duplication | protected function addRabbitMqExchange() |
452 | |||
453 | 8 | View Code Duplication | protected function addRabbitMqArgs() |
469 | |||
470 | 8 | protected function addRabbitMq() |
|
471 | { |
||
472 | 8 | $treeBuilder = new TreeBuilder(); |
|
473 | 8 | $rootNode = $treeBuilder->root('rabbit_mq'); |
|
508 | } |
||
509 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.