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 |
||
29 | final class Configuration implements ConfigurationInterface |
||
30 | { |
||
31 | /** |
||
32 | * Default debug mode value. |
||
33 | * |
||
34 | * @var bool |
||
35 | */ |
||
36 | private $debug; |
||
37 | |||
38 | /** |
||
39 | * @param bool $debug |
||
40 | */ |
||
41 | 58 | public function __construct($debug) |
|
42 | { |
||
43 | 58 | $this->debug = (bool) $debug; |
|
44 | 58 | } |
|
45 | |||
46 | /** |
||
47 | * Generates the configuration tree. |
||
48 | * |
||
49 | * @return TreeBuilder |
||
50 | */ |
||
51 | 57 | public function getConfigTreeBuilder() |
|
52 | { |
||
53 | 57 | $treeBuilder = new TreeBuilder(); |
|
54 | 57 | $rootNode = $treeBuilder->root('fos_rest', 'array'); |
|
55 | |||
56 | $rootNode |
||
57 | 57 | ->children() |
|
58 | 57 | ->scalarNode('disable_csrf_role')->defaultNull()->end() |
|
59 | 57 | ->arrayNode('access_denied_listener') |
|
60 | 57 | ->canBeEnabled() |
|
61 | 57 | ->beforeNormalization() |
|
62 | View Code Duplication | ->ifArray()->then(function ($v) { if (!empty($v) && empty($v['formats'])) { |
|
|
|||
63 | unset($v['enabled']); |
||
64 | $v = ['enabled' => true, 'formats' => $v]; |
||
65 | } |
||
66 | |||
67 | 57 | return $v; }) |
|
68 | 57 | ->end() |
|
69 | 57 | ->fixXmlConfig('format', 'formats') |
|
70 | 57 | ->children() |
|
71 | 57 | ->scalarNode('service')->defaultNull()->end() |
|
72 | 57 | ->arrayNode('formats') |
|
73 | 57 | ->useAttributeAsKey('name') |
|
74 | 57 | ->prototype('boolean')->end() |
|
75 | 57 | ->end() |
|
76 | 57 | ->end() |
|
77 | 57 | ->end() |
|
78 | 57 | ->scalarNode('unauthorized_challenge')->defaultNull()->end() |
|
79 | 57 | ->arrayNode('param_fetcher_listener') |
|
80 | 57 | ->beforeNormalization() |
|
81 | 57 | ->ifString() |
|
82 | View Code Duplication | ->then(function ($v) { return ['enabled' => in_array($v, ['force', 'true']), 'force' => 'force' === $v]; }) |
|
83 | 57 | ->end() |
|
84 | 57 | ->canBeEnabled() |
|
85 | 57 | ->children() |
|
86 | 57 | ->booleanNode('enabled')->defaultFalse()->end() |
|
87 | 57 | ->booleanNode('force')->defaultFalse()->end() |
|
88 | 57 | ->scalarNode('service')->defaultNull()->end() |
|
89 | 57 | ->end() |
|
90 | 57 | ->end() |
|
91 | 57 | ->scalarNode('cache_dir')->cannotBeEmpty()->defaultValue('%kernel.cache_dir%/fos_rest')->end() |
|
92 | 57 | ->arrayNode('allowed_methods_listener') |
|
93 | 57 | ->canBeEnabled() |
|
94 | 57 | ->children() |
|
95 | 57 | ->scalarNode('service')->defaultNull()->end() |
|
96 | 57 | ->end() |
|
97 | 57 | ->end() |
|
98 | 57 | ->arrayNode('routing_loader') |
|
99 | 57 | ->addDefaultsIfNotSet() |
|
100 | 57 | ->children() |
|
101 | 57 | ->scalarNode('default_format')->defaultNull()->end() |
|
102 | 57 | ->scalarNode('include_format')->defaultTrue()->end() |
|
103 | 57 | ->end() |
|
104 | 57 | ->end() |
|
105 | 57 | ->arrayNode('body_converter') |
|
106 | 57 | ->addDefaultsIfNotSet() |
|
107 | 57 | ->children() |
|
108 | 57 | ->scalarNode('enabled')->defaultFalse()->end() |
|
109 | 57 | ->scalarNode('validate')->defaultFalse()->end() |
|
110 | 57 | ->scalarNode('validation_errors_argument')->defaultValue('validationErrors')->end() |
|
111 | 57 | ->end() |
|
112 | 57 | ->end() |
|
113 | 57 | ->arrayNode('service') |
|
114 | 57 | ->addDefaultsIfNotSet() |
|
115 | 57 | ->children() |
|
116 | 57 | ->scalarNode('router')->defaultValue('router')->end() |
|
117 | 57 | ->scalarNode('templating')->defaultValue('templating')->end() |
|
118 | 57 | ->scalarNode('serializer')->defaultNull()->end() |
|
119 | 57 | ->scalarNode('view_handler')->defaultValue('fos_rest.view_handler.default')->end() |
|
120 | 57 | ->scalarNode('inflector')->defaultValue('fos_rest.inflector.doctrine')->end() |
|
121 | 57 | ->scalarNode('validator')->defaultValue('validator')->end() |
|
122 | 57 | ->end() |
|
123 | 57 | ->end() |
|
124 | 57 | ->arrayNode('serializer') |
|
125 | 57 | ->addDefaultsIfNotSet() |
|
126 | 57 | ->children() |
|
127 | 57 | ->scalarNode('version')->defaultNull()->end() |
|
128 | 57 | ->arrayNode('groups') |
|
129 | 57 | ->prototype('scalar')->end() |
|
130 | 57 | ->end() |
|
131 | 57 | ->booleanNode('serialize_null')->defaultFalse()->end() |
|
132 | 57 | ->end() |
|
133 | 57 | ->end() |
|
134 | 57 | ->arrayNode('zone') |
|
135 | 57 | ->cannotBeOverwritten() |
|
136 | 57 | ->prototype('array') |
|
137 | 57 | ->fixXmlConfig('ip') |
|
138 | 57 | ->children() |
|
139 | 57 | ->scalarNode('path') |
|
140 | 57 | ->defaultNull() |
|
141 | 57 | ->info('use the urldecoded format') |
|
142 | 57 | ->example('^/path to resource/') |
|
143 | 57 | ->end() |
|
144 | 57 | ->scalarNode('host')->defaultNull()->end() |
|
145 | 57 | ->arrayNode('methods') |
|
146 | ->beforeNormalization()->ifString()->then(function ($v) { return preg_split('/\s*,\s*/', $v); })->end() |
||
147 | 57 | ->prototype('scalar')->end() |
|
148 | 57 | ->end() |
|
149 | 57 | ->arrayNode('ips') |
|
150 | ->beforeNormalization()->ifString()->then(function ($v) { return array($v); })->end() |
||
151 | 57 | ->prototype('scalar')->end() |
|
152 | 57 | ->end() |
|
153 | 57 | ->end() |
|
154 | 57 | ->end() |
|
155 | 57 | ->end() |
|
156 | 57 | ->end(); |
|
157 | |||
158 | 57 | $this->addViewSection($rootNode); |
|
159 | 57 | $this->addExceptionSection($rootNode); |
|
160 | 57 | $this->addBodyListenerSection($rootNode); |
|
161 | 57 | $this->addFormatListenerSection($rootNode); |
|
162 | 57 | $this->addVersioningSection($rootNode); |
|
163 | |||
164 | 57 | return $treeBuilder; |
|
165 | } |
||
166 | |||
167 | 57 | private function addViewSection(ArrayNodeDefinition $rootNode) |
|
241 | |||
242 | 57 | private function addBodyListenerSection(ArrayNodeDefinition $rootNode) |
|
243 | { |
||
276 | |||
277 | 57 | private function addFormatListenerSection(ArrayNodeDefinition $rootNode) |
|
328 | |||
329 | 57 | private function addVersioningSection(ArrayNodeDefinition $rootNode) |
|
378 | |||
379 | 57 | private function addExceptionSection(ArrayNodeDefinition $rootNode) |
|
409 | } |
||
410 |
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.