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 |
||
24 | class Configuration implements ConfigurationInterface |
||
25 | { |
||
26 | /** |
||
27 | * {@inheritdoc} |
||
28 | */ |
||
29 | 5 | public function getConfigTreeBuilder() |
|
30 | { |
||
31 | 5 | $treeBuilder = new TreeBuilder(); |
|
32 | |||
33 | 5 | $rootNode = $treeBuilder->root('run_open_code_exchange_rate'); |
|
34 | |||
35 | $rootNode |
||
36 | 5 | ->children() |
|
37 | 5 | ->scalarNode('base_currency') |
|
38 | 5 | ->isRequired() |
|
39 | 5 | ->info('Set base currency in which you are doing your business activities.') // TODO Validate!!! |
|
|
|||
40 | 5 | ->end() |
|
41 | 5 | ->scalarNode('repository') |
|
42 | 5 | ->defaultValue('file') |
|
43 | 5 | ->info('Service ID which is in charge for rates persistence.') |
|
44 | 5 | ->end() |
|
45 | 5 | ->append($this->getRatesDefinition()) |
|
46 | 5 | ->append($this->getFileRepositoryDefinition()) |
|
47 | 5 | ->append($this->getDoctrineDbalRepositoryDefinition()) |
|
48 | 5 | ->append($this->getSourcesDefinition()) |
|
49 | 5 | ->append($this->getAccessRolesDefinition()) |
|
50 | 5 | ->arrayNode('form_types') |
|
51 | 5 | ->addDefaultsIfNotSet() |
|
52 | 5 | ->children() |
|
53 | 5 | ->append($this->getSourceTypeDefinition()) |
|
54 | 5 | ->append($this->getRateTypeTypeDefinition()) |
|
55 | 5 | ->append($this->getCurrencyCodeTypeDefinition()) |
|
56 | 5 | ->append($this->getRateTypeDefinition()) |
|
57 | 5 | ->end() |
|
58 | 5 | ->end() |
|
59 | 5 | ->end() |
|
60 | 5 | ->end(); |
|
61 | |||
62 | 5 | return $treeBuilder; |
|
63 | } |
||
64 | |||
65 | /** |
||
66 | * Build configuration tree for rates. |
||
67 | * |
||
68 | * @return ArrayNodeDefinition |
||
69 | */ |
||
70 | 5 | protected function getRatesDefinition() |
|
88 | |||
89 | /** |
||
90 | * Build configuration tree for file repository. |
||
91 | * |
||
92 | * @return ArrayNodeDefinition |
||
93 | */ |
||
94 | 5 | protected function getFileRepositoryDefinition() |
|
111 | |||
112 | /** |
||
113 | * Build configuration tree for Doctrine Dbal repository. |
||
114 | * |
||
115 | * @return ArrayNodeDefinition |
||
116 | */ |
||
117 | 5 | protected function getDoctrineDbalRepositoryDefinition() |
|
138 | |||
139 | /** |
||
140 | * Build configuration tree for simple sources. |
||
141 | * |
||
142 | * @return ArrayNodeDefinition |
||
143 | */ |
||
144 | 5 | protected function getSourcesDefinition() |
|
156 | |||
157 | /** |
||
158 | * Build configuration tree for access roles. |
||
159 | * |
||
160 | * @return ArrayNodeDefinition |
||
161 | */ |
||
162 | 5 | protected function getAccessRolesDefinition() |
|
192 | |||
193 | /** |
||
194 | * Build configuration tree for "RunOpenCode\Bundle\ExchangeRate\Form\Type\SourceType" default settings. |
||
195 | * |
||
196 | * @return ArrayNodeDefinition |
||
197 | */ |
||
198 | 5 | View Code Duplication | protected function getSourceTypeDefinition() |
213 | |||
214 | /** |
||
215 | * Build configuration tree for "RunOpenCode\Bundle\ExchangeRate\Form\Type\RateTypeType" default settings. |
||
216 | * |
||
217 | * @return ArrayNodeDefinition |
||
218 | */ |
||
219 | 5 | View Code Duplication | protected function getRateTypeTypeDefinition() |
234 | |||
235 | /** |
||
236 | * Build configuration tree for "RunOpenCode\Bundle\ExchangeRate\Form\Type\CurrencyCodeType" default settings. |
||
237 | * |
||
238 | * @return ArrayNodeDefinition |
||
239 | */ |
||
240 | 5 | View Code Duplication | protected function getCurrencyCodeTypeDefinition() |
255 | |||
256 | /** |
||
257 | * Build configuration tree for "RunOpenCode\Bundle\ExchangeRate\Form\Type\RateType" default settings. |
||
258 | * |
||
259 | * @return ArrayNodeDefinition |
||
260 | */ |
||
261 | 5 | protected function getRateTypeDefinition() |
|
277 | } |
||
278 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.